2 * Copyright 2014 Google, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "base/bitunion.hh"
34 #include "base/cprintf.hh"
35 #include "unittest/unittest.hh"
38 using UnitTest::setCase
;
43 Bitfield
<39, 32> byte5
;
45 BitfieldRO
<39, 32> byte5RO
;
46 BitfieldWO
<39, 32> byte5WO
;
47 SubBitUnion(byte6
, 47, 40)
48 Bitfield
<43, 42> bits43To42
;
50 SignedBitfield
<41> bit41Signed
;
52 SignedBitfield
<47, 40> byte6Signed
;
53 SignedBitfieldRO
<47, 40> byte6SignedRO
;
54 SignedBitfieldWO
<47, 40> byte6SignedWO
;
55 EndBitUnion(SixtyFour
)
57 BitUnion64(EmptySixtyFour
)
58 EndBitUnion(EmptySixtyFour
)
60 BitUnion32(EmptyThirtyTwo
)
61 EndBitUnion(EmptyThirtyTwo
)
63 BitUnion16(EmptySixteen
)
64 EndBitUnion(EmptySixteen
)
67 EndBitUnion(EmptyEight
)
69 struct ContainingStruct
72 Bitfield
<63, 60> topNibble
;
73 EndBitUnion(Contained
)
79 containingFunc(uint64_t init_val
, uint64_t fieldVal
)
82 Bitfield
<16, 15> field
;
83 EndBitUnion(Contained
)
85 Contained contained
= init_val
;
86 contained
.field
= fieldVal
;
90 } // anonymous namespace
92 // Declare these as global so g++ doesn't ignore them. Initialize them in
94 EmptySixtyFour emptySixtyFour
= 0;
95 EmptyThirtyTwo emptyThirtyTwo
;
96 EmptySixteen emptySixteen
;
97 EmptyEight
emptyEight(0);
102 SixtyFour sixtyFour
= 0;
104 setCase("normal bitfield");
105 EXPECT_EQ(sixtyFour
.byte5
, 0);
106 sixtyFour
.byte5
= 0xff;
107 EXPECT_EQ(sixtyFour
, 0xff00000000);
108 sixtyFour
.byte5
= 0xfff;
109 EXPECT_EQ(sixtyFour
, 0xff00000000);
110 EXPECT_EQ(sixtyFour
.byte5
, 0xff);
113 setCase("single bitfield");
114 EXPECT_EQ(sixtyFour
.bit2
, 0);
115 sixtyFour
.bit2
= 0x1;
116 EXPECT_EQ(sixtyFour
, 0x4);
117 EXPECT_EQ(sixtyFour
.bit2
, 0x1);
120 setCase("read only bitfield");
121 EXPECT_EQ(sixtyFour
.byte5RO
, 0);
122 sixtyFour
.byte5
= 0xff;
123 EXPECT_EQ(sixtyFour
.byte5RO
, 0xff);
126 setCase("write only bitfield");
127 sixtyFour
.byte5WO
= 0xff;
128 EXPECT_EQ(sixtyFour
, 0xff00000000);
131 setCase("sub bitunions and their bitfields");
132 EXPECT_EQ(sixtyFour
.byte6
.bit41
, 0);
133 sixtyFour
.byte6
= 0x2;
134 EXPECT_EQ(sixtyFour
.byte6
.bit41
, 1);
135 sixtyFour
.byte6
.bits43To42
= 0x3;
136 EXPECT_EQ(sixtyFour
.byte6
, 0xe);
137 sixtyFour
.byte6
= 0xff;
138 sixtyFour
.byte6
.bit41
= 0;
139 EXPECT_EQ(sixtyFour
, 0xfd0000000000);
142 setCase("normal, read only, and write only signed bitfields");
143 sixtyFour
.byte6
= 0xff;
144 EXPECT_EQ(sixtyFour
.byte6Signed
, -1);
145 EXPECT_EQ(sixtyFour
.byte6SignedRO
, -1);
146 sixtyFour
.byte6SignedWO
= 0;
147 EXPECT_EQ(sixtyFour
.byte6Signed
, 0);
148 EXPECT_EQ(sixtyFour
.byte6SignedRO
, 0);
149 EXPECT_EQ(sixtyFour
.byte6
, 0);
152 setCase("bitunion declared inside a struct");
153 ContainingStruct containing
;
154 containing
.contained
= 0;
155 containing
.contained
.topNibble
= 0xd;
156 EXPECT_EQ(containing
.contained
, 0xd000000000000000);
158 setCase("bitunion declared inside a function");
159 EXPECT_EQ(containingFunc(0xfffff, 0), 0xe7fff);
161 setCase("assigning bitfields to other bitfields");
162 SixtyFour otherSixtyFour
= 0;
164 otherSixtyFour
.byte6
.bit41
= sixtyFour
.bit2
;
165 EXPECT_EQ(otherSixtyFour
, 0x20000000000);
166 otherSixtyFour
.bit2
= sixtyFour
.bit2
;
167 EXPECT_EQ(otherSixtyFour
, 0x20000000004);
169 setCase("bitunion operators");
171 otherSixtyFour
= 0x4;
172 sixtyFour
= otherSixtyFour
;
173 EXPECT_EQ(sixtyFour
, 0x4);
175 EXPECT_TRUE(sixtyFour
< otherSixtyFour
);
176 EXPECT_TRUE(otherSixtyFour
> sixtyFour
);
177 EXPECT_TRUE(sixtyFour
!= otherSixtyFour
);
178 sixtyFour
= otherSixtyFour
;
179 EXPECT_TRUE(sixtyFour
== otherSixtyFour
);
181 return UnitTest::printResults();