blob: 923d5032d7635d13e112f978ef06f7af42497d43 [file] [log] [blame]
Ben Tynera8126fd2019-08-01 19:40:07 -05001#include <util/hei_bit_string.hpp>
Zane Shelleyca9f6252019-10-25 21:17:30 -05002
Ben Tynera8126fd2019-08-01 19:40:07 -05003#include "gtest/gtest.h"
4
5using namespace libhei;
6
Zane Shelley7c8faa12019-10-28 22:26:28 -05007static constexpr uint32_t UINT8_BIT_LEN = (sizeof(uint8_t) * 8);
Ben Tynera8126fd2019-08-01 19:40:07 -05008static 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()
Zane Shelley83da2452019-10-25 15:45:34 -050017TEST(BitStringTest, TestSet1)
Ben Tynera8126fd2019-08-01 19:40:07 -050018{
19 BitStringBuffer bs(UINT64_BIT_LEN);
20 uint32_t i;
21
22 // set all bits in ascending order
Zane Shelley83da2452019-10-25 15:45:34 -050023 for (i = 0; i < UINT64_BIT_LEN; i++)
Ben Tynera8126fd2019-08-01 19:40:07 -050024 {
25 // Make sure bit gets set and set count
26 // is increasing
27 bs.setBit(i);
28 ASSERT_TRUE(bs.isBitSet(i));
Zane Shelley83da2452019-10-25 15:45:34 -050029 ASSERT_EQ(bs.getSetCount(), i + 1);
Ben Tynera8126fd2019-08-01 19:40:07 -050030 }
31 // all bits should be set at this point
Zane Shelley83da2452019-10-25 15:45:34 -050032 ASSERT_EQ(bs.getFieldRight(0, 64), UINT64_MAX);
33 ASSERT_EQ(bs.getFieldLeft(0, 64), UINT64_MAX);
Ben Tynera8126fd2019-08-01 19:40:07 -050034
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
Zane Shelley83da2452019-10-25 15:45:34 -050042 for (i = UINT64_BIT_LEN; 0 != i; i--)
Ben Tynera8126fd2019-08-01 19:40:07 -050043 {
44 // make sure bit gets cleared and set count
45 // is decreasing
46 ASSERT_EQ(bs.getSetCount(), i);
Zane Shelley83da2452019-10-25 15:45:34 -050047 bs.clearBit(i - 1);
48 ASSERT_FALSE(bs.isBitSet(i - 1));
Ben Tynera8126fd2019-08-01 19:40:07 -050049 }
50 // all bits should be clear at this point
51 ASSERT_EQ(bs.getSetCount(), 0u);
Zane Shelley83da2452019-10-25 15:45:34 -050052 ASSERT_EQ(bs.getFieldRight(0, 64), 0u);
53 ASSERT_EQ(bs.getFieldLeft(0, 64), 0u);
Ben Tynera8126fd2019-08-01 19:40:07 -050054 ASSERT_TRUE(bs.isZero());
55}
56
57// setPattern()
Zane Shelley83da2452019-10-25 15:45:34 -050058TEST(BitStringTest, TestSet2)
Ben Tynera8126fd2019-08-01 19:40:07 -050059{
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()
Zane Shelley83da2452019-10-25 15:45:34 -050083TEST(BitStringTest, TestSet3)
Ben Tynera8126fd2019-08-01 19:40:07 -050084{
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());
Zane Shelley83da2452019-10-25 15:45:34 -050096 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));
Ben Tynera8126fd2019-08-01 19:40:07 -050098}
99
100// maskString()
Zane Shelley83da2452019-10-25 15:45:34 -0500101TEST(BitStringTest, TestSet4)
Ben Tynera8126fd2019-08-01 19:40:07 -0500102{
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()
Zane Shelley83da2452019-10-25 15:45:34 -0500118TEST(BitStringTest, TestSet5)
Ben Tynera8126fd2019-08-01 19:40:07 -0500119{
120 uint64_t field = 0x1234567890abcdef;
121 BitStringBuffer bsb(64);
122
123 // set bitstring to low end of field
124 bsb.setFieldRight(0, 32, field);
Zane Shelley83da2452019-10-25 15:45:34 -0500125 ASSERT_EQ(field << 32, bsb.getFieldLeft(0, 32));
Ben Tynera8126fd2019-08-01 19:40:07 -0500126
127 // set bitstring to high end of field
128 bsb.setFieldLeft(0, 32, field);
Zane Shelley83da2452019-10-25 15:45:34 -0500129 ASSERT_EQ(field >> 32, bsb.getFieldRight(0, 32));
Ben Tynera8126fd2019-08-01 19:40:07 -0500130
131 // increasing offset
Zane Shelley83da2452019-10-25 15:45:34 -0500132 for (uint32_t i = 0; i < UINT64_BIT_LEN; i++)
Ben Tynera8126fd2019-08-01 19:40:07 -0500133 {
134 // increasing length
Zane Shelley83da2452019-10-25 15:45:34 -0500135 for (uint32_t j = 1; j <= UINT64_BIT_LEN - i; j++)
Ben Tynera8126fd2019-08-01 19:40:07 -0500136 {
137 bsb.clearAll();
138 bsb.setFieldRight(i, j, UINT64_MAX);
139
140 // verify
Zane Shelley83da2452019-10-25 15:45:34 -0500141 ASSERT_EQ(bsb.getFieldRight(i, j),
142 UINT64_MAX >> (UINT64_BIT_LEN - j));
Ben Tynera8126fd2019-08-01 19:40:07 -0500143 }
144 }
Zane Shelley83da2452019-10-25 15:45:34 -0500145 for (uint32_t i = 0; i < UINT64_BIT_LEN; i++)
Ben Tynera8126fd2019-08-01 19:40:07 -0500146 {
147 // set 1 bit at offset i
148 bsb.clearAll();
149 bsb.setFieldRight(i, 1, 1);
150
151 // verify bit is set
Zane Shelley83da2452019-10-25 15:45:34 -0500152 ASSERT_EQ(bsb.getFieldRight(0, 64),
153 (uint64_t)1 << (UINT64_BIT_LEN - i - 1));
Ben Tynera8126fd2019-08-01 19:40:07 -0500154 }
155}
156
157// operator >>
158// operator <<
Zane Shelley83da2452019-10-25 15:45:34 -0500159TEST(BitStringTest, TestSet6)
Ben Tynera8126fd2019-08-01 19:40:07 -0500160{
161 uint64_t field = 0x1234567890abcdef;
162 BitStringBuffer bsb(64);
163
164 bsb.setFieldRight(0, 64, field);
165 ASSERT_EQ(field, bsb.getFieldRight(0, 64));
166 ASSERT_EQ(field >> 32, (bsb >> 32).getFieldRight(0, 64));
167 ASSERT_EQ(field << 32, (bsb << 32).getFieldRight(0, 64));
168}