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