Zane Shelley | c888dff | 2021-01-14 12:45:02 -0600 | [diff] [blame] | 1 | #include <hei_bit_string.hpp> |
Zane Shelley | ca9f625 | 2019-10-25 21:17:30 -0500 | [diff] [blame] | 2 | |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | using namespace libhei; |
| 6 | |
Zane Shelley | 7c8faa1 | 2019-10-28 22:26:28 -0500 | [diff] [blame] | 7 | static constexpr uint32_t UINT8_BIT_LEN = (sizeof(uint8_t) * 8); |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 8 | static 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 Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 17 | TEST(BitStringTest, TestSet1) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 18 | { |
| 19 | BitStringBuffer bs(UINT64_BIT_LEN); |
| 20 | uint32_t i; |
| 21 | |
| 22 | // set all bits in ascending order |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 23 | for (i = 0; i < UINT64_BIT_LEN; i++) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 24 | { |
| 25 | // Make sure bit gets set and set count |
| 26 | // is increasing |
| 27 | bs.setBit(i); |
| 28 | ASSERT_TRUE(bs.isBitSet(i)); |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 29 | ASSERT_EQ(bs.getSetCount(), i + 1); |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 30 | } |
| 31 | // all bits should be set at this point |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 32 | ASSERT_EQ(bs.getFieldRight(0, 64), UINT64_MAX); |
| 33 | ASSERT_EQ(bs.getFieldLeft(0, 64), UINT64_MAX); |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 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 |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 42 | for (i = UINT64_BIT_LEN; 0 != i; i--) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 43 | { |
| 44 | // make sure bit gets cleared and set count |
| 45 | // is decreasing |
| 46 | ASSERT_EQ(bs.getSetCount(), i); |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 47 | bs.clearBit(i - 1); |
| 48 | ASSERT_FALSE(bs.isBitSet(i - 1)); |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 49 | } |
| 50 | // all bits should be clear at this point |
| 51 | ASSERT_EQ(bs.getSetCount(), 0u); |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 52 | ASSERT_EQ(bs.getFieldRight(0, 64), 0u); |
| 53 | ASSERT_EQ(bs.getFieldLeft(0, 64), 0u); |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 54 | ASSERT_TRUE(bs.isZero()); |
| 55 | } |
| 56 | |
| 57 | // setPattern() |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 58 | TEST(BitStringTest, TestSet2) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 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() |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 83 | TEST(BitStringTest, TestSet3) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 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()); |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 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)); |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // maskString() |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 101 | TEST(BitStringTest, TestSet4) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 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() |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 118 | TEST(BitStringTest, TestSet5) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 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); |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 125 | ASSERT_EQ(field << 32, bsb.getFieldLeft(0, 32)); |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 126 | |
| 127 | // set bitstring to high end of field |
| 128 | bsb.setFieldLeft(0, 32, field); |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 129 | ASSERT_EQ(field >> 32, bsb.getFieldRight(0, 32)); |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 130 | |
| 131 | // increasing offset |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 132 | for (uint32_t i = 0; i < UINT64_BIT_LEN; i++) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 133 | { |
| 134 | // increasing length |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 135 | for (uint32_t j = 1; j <= UINT64_BIT_LEN - i; j++) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 136 | { |
| 137 | bsb.clearAll(); |
| 138 | bsb.setFieldRight(i, j, UINT64_MAX); |
| 139 | |
| 140 | // verify |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 141 | ASSERT_EQ(bsb.getFieldRight(i, j), |
| 142 | UINT64_MAX >> (UINT64_BIT_LEN - j)); |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 143 | } |
| 144 | } |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 145 | for (uint32_t i = 0; i < UINT64_BIT_LEN; i++) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 146 | { |
| 147 | // set 1 bit at offset i |
| 148 | bsb.clearAll(); |
| 149 | bsb.setFieldRight(i, 1, 1); |
| 150 | |
| 151 | // verify bit is set |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 152 | ASSERT_EQ(bsb.getFieldRight(0, 64), |
| 153 | (uint64_t)1 << (UINT64_BIT_LEN - i - 1)); |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | // operator >> |
| 158 | // operator << |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 159 | TEST(BitStringTest, TestSet6) |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 160 | { |
| 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 | } |