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.
28 #include <gtest/gtest.h>
32 #include <type_traits>
34 #include "base/bitunion.hh"
35 #include "base/cprintf.hh"
42 Bitfield
<39, 32> byte5
;
44 BitfieldRO
<39, 32> byte5RO
;
45 BitfieldWO
<39, 32> byte5WO
;
46 SubBitUnion(byte6
, 47, 40)
47 Bitfield
<43, 42> bits43To42
;
49 SignedBitfield
<41> bit41Signed
;
51 SignedBitfield
<47, 40> byte6Signed
;
52 SignedBitfieldRO
<47, 40> byte6SignedRO
;
53 SignedBitfieldWO
<47, 40> byte6SignedWO
;
54 EndBitUnion(SixtyFour
)
56 BitUnion64(EmptySixtyFour
)
57 EndBitUnion(EmptySixtyFour
)
59 BitUnion32(EmptyThirtyTwo
)
60 EndBitUnion(EmptyThirtyTwo
)
62 BitUnion16(EmptySixteen
)
63 EndBitUnion(EmptySixteen
)
66 EndBitUnion(EmptyEight
)
72 Bitfield
<15, 12> high
;
82 getter(const uint64_t &storage
) const
92 setter(uint64_t &storage
, uint64_t val
)
103 BitfieldType
<SplitField
> split
;
106 struct ContainingStruct
108 BitUnion64(Contained
)
109 Bitfield
<63, 60> topNibble
;
110 EndBitUnion(Contained
)
116 containingFunc(uint64_t init_val
, uint64_t fieldVal
)
118 BitUnion32(Contained
)
119 Bitfield
<16, 15> field
;
120 EndBitUnion(Contained
)
122 Contained contained
= init_val
;
123 contained
.field
= fieldVal
;
127 } // anonymous namespace
129 // Declare these as global so g++ doesn't ignore them. Initialize them in
131 EmptySixtyFour emptySixtyFour
= 0;
132 EmptyThirtyTwo emptyThirtyTwo
;
133 EmptySixteen emptySixteen
;
134 EmptyEight
emptyEight(0);
136 class BitUnionData
: public testing::Test
{
141 void SetUp() override
{ sixtyFour
= 0; split
= 0; }
143 template <typename T
>
144 uint64_t templatedFunction(T
) { return 0; }
146 template <typename T
>
148 templatedFunction(BitUnionType
<T
> u
)
150 BitUnionBaseType
<T
> b
= u
;
155 TEST_F(BitUnionData
, NormalBitfield
)
157 EXPECT_EQ(sixtyFour
.byte5
, 0);
158 sixtyFour
.byte5
= 0xff;
159 EXPECT_EQ(sixtyFour
, 0xff00000000);
160 sixtyFour
.byte5
= 0xfff;
161 EXPECT_EQ(sixtyFour
, 0xff00000000);
162 EXPECT_EQ(sixtyFour
.byte5
, 0xff);
165 TEST_F(BitUnionData
, SingleBitfield
)
167 EXPECT_EQ(sixtyFour
.bit2
, 0);
168 sixtyFour
.bit2
= 0x1;
169 EXPECT_EQ(sixtyFour
, 0x4);
170 EXPECT_EQ(sixtyFour
.bit2
, 0x1);
173 TEST_F(BitUnionData
, ReadOnlyBitfield
)
175 EXPECT_EQ(sixtyFour
.byte5RO
, 0);
176 sixtyFour
.byte5
= 0xff;
177 EXPECT_EQ(sixtyFour
.byte5RO
, 0xff);
180 TEST_F(BitUnionData
, WriteOnlyBitfield
)
182 sixtyFour
.byte5WO
= 0xff;
183 EXPECT_EQ(sixtyFour
, 0xff00000000);
186 TEST_F(BitUnionData
, SubBitUnions
)
188 EXPECT_EQ(sixtyFour
.byte6
.bit41
, 0);
189 sixtyFour
.byte6
= 0x2;
190 EXPECT_EQ(sixtyFour
.byte6
.bit41
, 1);
191 sixtyFour
.byte6
.bits43To42
= 0x3;
192 EXPECT_EQ(sixtyFour
.byte6
, 0xe);
193 sixtyFour
.byte6
= 0xff;
194 sixtyFour
.byte6
.bit41
= 0;
195 EXPECT_EQ(sixtyFour
, 0xfd0000000000);
198 TEST_F(BitUnionData
, SignedBitfields
)
200 sixtyFour
.byte6
= 0xff;
201 EXPECT_EQ(sixtyFour
.byte6Signed
, -1);
202 EXPECT_EQ(sixtyFour
.byte6SignedRO
, -1);
203 sixtyFour
.byte6SignedWO
= 0;
204 EXPECT_EQ(sixtyFour
.byte6Signed
, 0);
205 EXPECT_EQ(sixtyFour
.byte6SignedRO
, 0);
206 EXPECT_EQ(sixtyFour
.byte6
, 0);
209 TEST_F(BitUnionData
, InsideStruct
)
211 ContainingStruct containing
;
212 containing
.contained
= 0;
213 containing
.contained
.topNibble
= 0xd;
214 EXPECT_EQ(containing
.contained
, 0xd000000000000000);
217 TEST_F(BitUnionData
, InsideFunction
)
219 EXPECT_EQ(containingFunc(0xfffff, 0), 0xe7fff);
222 TEST_F(BitUnionData
, BitfieldToBitfieldAssignment
)
224 SixtyFour otherSixtyFour
= 0;
226 otherSixtyFour
.byte6
.bit41
= sixtyFour
.bit2
;
227 EXPECT_EQ(otherSixtyFour
, 0x20000000000);
228 otherSixtyFour
.bit2
= sixtyFour
.bit2
;
229 EXPECT_EQ(otherSixtyFour
, 0x20000000004);
232 TEST_F(BitUnionData
, Operators
)
234 SixtyFour otherSixtyFour
= 0x4;
235 sixtyFour
= otherSixtyFour
;
236 EXPECT_EQ(sixtyFour
, 0x4);
238 EXPECT_TRUE(sixtyFour
< otherSixtyFour
);
239 EXPECT_TRUE(otherSixtyFour
> sixtyFour
);
240 EXPECT_TRUE(sixtyFour
!= otherSixtyFour
);
241 sixtyFour
= otherSixtyFour
;
242 EXPECT_TRUE(sixtyFour
== otherSixtyFour
);
245 TEST_F(BitUnionData
, Custom
)
249 EXPECT_EQ(split
, 0xf0f0);
250 EXPECT_EQ((uint64_t)split
.split
, 0xff);
253 TEST_F(BitUnionData
, Templating
)
256 EXPECT_EQ(templatedFunction(sixtyFour
), 0xff);
257 EXPECT_EQ(templatedFunction((uint64_t)sixtyFour
), 0);
259 BitUnion(uint64_t, Dummy64
)
260 EndBitUnion(Dummy64
);
262 BitUnion(uint32_t, Dummy32
)
263 EndBitUnion(Dummy32
);
266 is64
= std::is_same
<BitUnionBaseType
<Dummy64
>, uint64_t>::value
;
268 is64
= std::is_same
<BitUnionBaseType
<Dummy32
>, uint64_t>::value
;
272 TEST_F(BitUnionData
, Output
)
274 sixtyFour
= 1234567812345678;
275 std::stringstream ss
;
277 EXPECT_EQ(ss
.str(), "1234567812345678");
280 EmptyEight eight
= 65;
282 EXPECT_EQ(ss
.str(), "65");