blob: 8320d973041fe79dba5f6fb3edbc54b184e33c53 [file] [log] [blame]
Ben Tynera8126fd2019-08-01 19:40:07 -05001#include <hei_user_defines.hpp>
2#include <util/hei_bit_string.hpp>
3#include "gtest/gtest.h"
4
5using namespace libhei;
6
7static constexpr uint32_t UINT8_BIT_LEN = (sizeof(uint8_t) * 8);
8static constexpr uint32_t UINT64_BIT_LEN = (sizeof(uint64_t) * 8);
9
10// setBit()
11// clearBit()
12// setAll()
13// clearAll()
14// isBitSet()
15// getSetCount()
16// isZero()
17TEST( BitStringTest, TestSet1 )
18{
19 BitStringBuffer bs(UINT64_BIT_LEN);
20 uint32_t i;
21
22 // set all bits in ascending order
23 for(i = 0; i < UINT64_BIT_LEN; i++)
24 {
25 // Make sure bit gets set and set count
26 // is increasing
27 bs.setBit(i);
28 ASSERT_TRUE(bs.isBitSet(i));
29 ASSERT_EQ(bs.getSetCount(), i+1);
30 }
31 // all bits should be set at this point
32 ASSERT_EQ(bs.getFieldRight(0,64), UINT64_MAX);
33 ASSERT_EQ(bs.getFieldLeft(0,64), UINT64_MAX);
34
35 // test clearAll(), setAll()
36 bs.clearAll();
37 ASSERT_TRUE(bs.isZero());
38 bs.setAll();
39 ASSERT_EQ(bs.getSetCount(), UINT64_BIT_LEN);
40
41 // clear all bits in descending order
42 for(i = UINT64_BIT_LEN; 0 != i; i--)
43 {
44 // make sure bit gets cleared and set count
45 // is decreasing
46 ASSERT_EQ(bs.getSetCount(), i);
47 bs.clearBit(i-1);
48 ASSERT_FALSE(bs.isBitSet(i-1));
49 }
50 // all bits should be clear at this point
51 ASSERT_EQ(bs.getSetCount(), 0u);
52 ASSERT_EQ(bs.getFieldRight(0,64), 0u);
53 ASSERT_EQ(bs.getFieldLeft(0,64), 0u);
54 ASSERT_TRUE(bs.isZero());
55}
56
57// setPattern()
58TEST( BitStringTest, TestSet2 )
59{
60 BitStringBuffer bs(UINT64_BIT_LEN);
61 uint64_t field = 0xaaaaaaaaaaaaaaaa;
62
63 bs.setPattern(field);
64 ASSERT_EQ(field, bs.getFieldRight(0, 64));
65 ASSERT_EQ(field, bs.getFieldLeft(0, 64));
66
67 bs.clearAll();
68 ASSERT_TRUE(bs.isZero());
69
70 bs.setPattern(0xaa, 8);
71 ASSERT_EQ(field, bs.getFieldRight(0, 64));
72 ASSERT_EQ(field, bs.getFieldLeft(0, 64));
73
74 bs.clearAll();
75 ASSERT_TRUE(bs.isZero());
76
77 bs.setPattern(0, 64, 0xaaaa, 16);
78 ASSERT_EQ(field, bs.getFieldRight(0, 64));
79 ASSERT_EQ(field, bs.getFieldLeft(0, 64));
80}
81
82// setString()
83TEST( BitStringTest, TestSet3 )
84{
85 BitStringBuffer bsb_dest(64);
86 BitStringBuffer bsb_src(64);
87
88 bsb_dest.clearAll();
89 ASSERT_TRUE(bsb_dest.isZero());
90
91 bsb_src.setAll();
92 ASSERT_EQ(64u, bsb_src.getSetCount());
93
94 bsb_dest.setString(bsb_src);
95 ASSERT_FALSE(bsb_dest.isZero());
96 ASSERT_EQ(bsb_dest.getFieldRight(0, 64), bsb_src.getFieldRight(0,64));
97 ASSERT_EQ(bsb_dest.getFieldLeft(0, 64), bsb_src.getFieldLeft(0,64));
98}
99
100// maskString()
101TEST( BitStringTest, TestSet4 )
102{
103 BitStringBuffer bsb(64);
104 bsb.setAll();
105 ASSERT_EQ(64u, bsb.getSetCount());
106
107 BitStringBuffer bsb_mask(64);
108 bsb_mask.setFieldRight(0, 64, 0xaaaaaaaaaaaaaaaa);
109
110 bsb.maskString(bsb_mask);
111 ASSERT_EQ(bsb.getFieldRight(0, 64), 0x5555555555555555u);
112}
113
114// setFieldRight()
115// setFieldLeft()
116// getFielRight()
117// getFieldLeft()
118TEST( BitStringTest, TestSet5 )
119{
120 uint64_t field = 0x1234567890abcdef;
121 BitStringBuffer bsb(64);
122
123 // set bitstring to low end of field
124 bsb.setFieldRight(0, 32, field);
125 ASSERT_EQ(field << 32, bsb.getFieldLeft(0,32));
126
127 // set bitstring to high end of field
128 bsb.setFieldLeft(0, 32, field);
129 ASSERT_EQ(field >> 32, bsb.getFieldRight(0,32));
130
131 // increasing offset
132 for(uint32_t i = 0; i < UINT64_BIT_LEN; i++)
133 {
134 // increasing length
135 for(uint32_t j = 1; j <= UINT64_BIT_LEN - i; j++)
136 {
137 bsb.clearAll();
138 bsb.setFieldRight(i, j, UINT64_MAX);
139
140 // verify
141 ASSERT_EQ(bsb.getFieldRight(i, j), \
142 UINT64_MAX >> (UINT64_BIT_LEN - j));
143
144 }
145 }
146 for(uint32_t i = 0; i < UINT64_BIT_LEN; i++)
147 {
148 // set 1 bit at offset i
149 bsb.clearAll();
150 bsb.setFieldRight(i, 1, 1);
151
152 // verify bit is set
153 ASSERT_EQ(bsb.getFieldRight(0, 64), (uint64_t)1 << \
154 (UINT64_BIT_LEN - i - 1));
155 }
156}
157
158// operator >>
159// operator <<
160TEST( BitStringTest, TestSet6 )
161{
162 uint64_t field = 0x1234567890abcdef;
163 BitStringBuffer bsb(64);
164
165 bsb.setFieldRight(0, 64, field);
166 ASSERT_EQ(field, bsb.getFieldRight(0, 64));
167 ASSERT_EQ(field >> 32, (bsb >> 32).getFieldRight(0, 64));
168 ASSERT_EQ(field << 32, (bsb << 32).getFieldRight(0, 64));
169}