blob: aaf5b904ace60ea518795f7645969ab4abe898e4 [file] [log] [blame]
Ben Tynera8126fd2019-08-01 19:40:07 -05001#include <hei_user_defines.hpp>
2#include <util/hei_bit_string.hpp>
Zane Shelleyca9f6252019-10-25 21:17:30 -05003
Ben Tynera8126fd2019-08-01 19:40:07 -05004#include "gtest/gtest.h"
5
6using namespace libhei;
7
Zane Shelley7c8faa12019-10-28 22:26:28 -05008static constexpr uint32_t UINT8_BIT_LEN = (sizeof(uint8_t) * 8);
Ben Tynera8126fd2019-08-01 19:40:07 -05009static constexpr uint32_t UINT64_BIT_LEN = (sizeof(uint64_t) * 8);
10
11// setBit()
12// clearBit()
13// setAll()
14// clearAll()
15// isBitSet()
16// getSetCount()
17// isZero()
Zane Shelley83da2452019-10-25 15:45:34 -050018TEST(BitStringTest, TestSet1)
Ben Tynera8126fd2019-08-01 19:40:07 -050019{
20 BitStringBuffer bs(UINT64_BIT_LEN);
21 uint32_t i;
22
23 // set all bits in ascending order
Zane Shelley83da2452019-10-25 15:45:34 -050024 for (i = 0; i < UINT64_BIT_LEN; i++)
Ben Tynera8126fd2019-08-01 19:40:07 -050025 {
26 // Make sure bit gets set and set count
27 // is increasing
28 bs.setBit(i);
29 ASSERT_TRUE(bs.isBitSet(i));
Zane Shelley83da2452019-10-25 15:45:34 -050030 ASSERT_EQ(bs.getSetCount(), i + 1);
Ben Tynera8126fd2019-08-01 19:40:07 -050031 }
32 // all bits should be set at this point
Zane Shelley83da2452019-10-25 15:45:34 -050033 ASSERT_EQ(bs.getFieldRight(0, 64), UINT64_MAX);
34 ASSERT_EQ(bs.getFieldLeft(0, 64), UINT64_MAX);
Ben Tynera8126fd2019-08-01 19:40:07 -050035
36 // test clearAll(), setAll()
37 bs.clearAll();
38 ASSERT_TRUE(bs.isZero());
39 bs.setAll();
40 ASSERT_EQ(bs.getSetCount(), UINT64_BIT_LEN);
41
42 // clear all bits in descending order
Zane Shelley83da2452019-10-25 15:45:34 -050043 for (i = UINT64_BIT_LEN; 0 != i; i--)
Ben Tynera8126fd2019-08-01 19:40:07 -050044 {
45 // make sure bit gets cleared and set count
46 // is decreasing
47 ASSERT_EQ(bs.getSetCount(), i);
Zane Shelley83da2452019-10-25 15:45:34 -050048 bs.clearBit(i - 1);
49 ASSERT_FALSE(bs.isBitSet(i - 1));
Ben Tynera8126fd2019-08-01 19:40:07 -050050 }
51 // all bits should be clear at this point
52 ASSERT_EQ(bs.getSetCount(), 0u);
Zane Shelley83da2452019-10-25 15:45:34 -050053 ASSERT_EQ(bs.getFieldRight(0, 64), 0u);
54 ASSERT_EQ(bs.getFieldLeft(0, 64), 0u);
Ben Tynera8126fd2019-08-01 19:40:07 -050055 ASSERT_TRUE(bs.isZero());
56}
57
58// setPattern()
Zane Shelley83da2452019-10-25 15:45:34 -050059TEST(BitStringTest, TestSet2)
Ben Tynera8126fd2019-08-01 19:40:07 -050060{
61 BitStringBuffer bs(UINT64_BIT_LEN);
62 uint64_t field = 0xaaaaaaaaaaaaaaaa;
63
64 bs.setPattern(field);
65 ASSERT_EQ(field, bs.getFieldRight(0, 64));
66 ASSERT_EQ(field, bs.getFieldLeft(0, 64));
67
68 bs.clearAll();
69 ASSERT_TRUE(bs.isZero());
70
71 bs.setPattern(0xaa, 8);
72 ASSERT_EQ(field, bs.getFieldRight(0, 64));
73 ASSERT_EQ(field, bs.getFieldLeft(0, 64));
74
75 bs.clearAll();
76 ASSERT_TRUE(bs.isZero());
77
78 bs.setPattern(0, 64, 0xaaaa, 16);
79 ASSERT_EQ(field, bs.getFieldRight(0, 64));
80 ASSERT_EQ(field, bs.getFieldLeft(0, 64));
81}
82
83// setString()
Zane Shelley83da2452019-10-25 15:45:34 -050084TEST(BitStringTest, TestSet3)
Ben Tynera8126fd2019-08-01 19:40:07 -050085{
86 BitStringBuffer bsb_dest(64);
87 BitStringBuffer bsb_src(64);
88
89 bsb_dest.clearAll();
90 ASSERT_TRUE(bsb_dest.isZero());
91
92 bsb_src.setAll();
93 ASSERT_EQ(64u, bsb_src.getSetCount());
94
95 bsb_dest.setString(bsb_src);
96 ASSERT_FALSE(bsb_dest.isZero());
Zane Shelley83da2452019-10-25 15:45:34 -050097 ASSERT_EQ(bsb_dest.getFieldRight(0, 64), bsb_src.getFieldRight(0, 64));
98 ASSERT_EQ(bsb_dest.getFieldLeft(0, 64), bsb_src.getFieldLeft(0, 64));
Ben Tynera8126fd2019-08-01 19:40:07 -050099}
100
101// maskString()
Zane Shelley83da2452019-10-25 15:45:34 -0500102TEST(BitStringTest, TestSet4)
Ben Tynera8126fd2019-08-01 19:40:07 -0500103{
104 BitStringBuffer bsb(64);
105 bsb.setAll();
106 ASSERT_EQ(64u, bsb.getSetCount());
107
108 BitStringBuffer bsb_mask(64);
109 bsb_mask.setFieldRight(0, 64, 0xaaaaaaaaaaaaaaaa);
110
111 bsb.maskString(bsb_mask);
112 ASSERT_EQ(bsb.getFieldRight(0, 64), 0x5555555555555555u);
113}
114
115// setFieldRight()
116// setFieldLeft()
117// getFielRight()
118// getFieldLeft()
Zane Shelley83da2452019-10-25 15:45:34 -0500119TEST(BitStringTest, TestSet5)
Ben Tynera8126fd2019-08-01 19:40:07 -0500120{
121 uint64_t field = 0x1234567890abcdef;
122 BitStringBuffer bsb(64);
123
124 // set bitstring to low end of field
125 bsb.setFieldRight(0, 32, field);
Zane Shelley83da2452019-10-25 15:45:34 -0500126 ASSERT_EQ(field << 32, bsb.getFieldLeft(0, 32));
Ben Tynera8126fd2019-08-01 19:40:07 -0500127
128 // set bitstring to high end of field
129 bsb.setFieldLeft(0, 32, field);
Zane Shelley83da2452019-10-25 15:45:34 -0500130 ASSERT_EQ(field >> 32, bsb.getFieldRight(0, 32));
Ben Tynera8126fd2019-08-01 19:40:07 -0500131
132 // increasing offset
Zane Shelley83da2452019-10-25 15:45:34 -0500133 for (uint32_t i = 0; i < UINT64_BIT_LEN; i++)
Ben Tynera8126fd2019-08-01 19:40:07 -0500134 {
135 // increasing length
Zane Shelley83da2452019-10-25 15:45:34 -0500136 for (uint32_t j = 1; j <= UINT64_BIT_LEN - i; j++)
Ben Tynera8126fd2019-08-01 19:40:07 -0500137 {
138 bsb.clearAll();
139 bsb.setFieldRight(i, j, UINT64_MAX);
140
141 // verify
Zane Shelley83da2452019-10-25 15:45:34 -0500142 ASSERT_EQ(bsb.getFieldRight(i, j),
143 UINT64_MAX >> (UINT64_BIT_LEN - j));
Ben Tynera8126fd2019-08-01 19:40:07 -0500144 }
145 }
Zane Shelley83da2452019-10-25 15:45:34 -0500146 for (uint32_t i = 0; i < UINT64_BIT_LEN; i++)
Ben Tynera8126fd2019-08-01 19:40:07 -0500147 {
148 // set 1 bit at offset i
149 bsb.clearAll();
150 bsb.setFieldRight(i, 1, 1);
151
152 // verify bit is set
Zane Shelley83da2452019-10-25 15:45:34 -0500153 ASSERT_EQ(bsb.getFieldRight(0, 64),
154 (uint64_t)1 << (UINT64_BIT_LEN - i - 1));
Ben Tynera8126fd2019-08-01 19:40:07 -0500155 }
156}
157
158// operator >>
159// operator <<
Zane Shelley83da2452019-10-25 15:45:34 -0500160TEST(BitStringTest, TestSet6)
Ben Tynera8126fd2019-08-01 19:40:07 -0500161{
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}