| 28 | const int MAX_WIDTH = BatchedBitReader::MAX_BITWIDTH; |
| 29 | |
| 30 | TEST(BitArray, TestBool) { |
| 31 | const int len = 8; |
| 32 | uint8_t buffer[len]; |
| 33 | |
| 34 | BitWriter writer(buffer, len); |
| 35 | |
| 36 | // Write alternating 0's and 1's |
| 37 | for (int i = 0; i < 8; ++i) { |
| 38 | bool result = writer.PutValue(static_cast<uint64_t>(i % 2), 1); |
| 39 | EXPECT_TRUE(result); |
| 40 | } |
| 41 | writer.Flush(); |
| 42 | EXPECT_EQ((int)buffer[0], BOOST_BINARY(1 0 1 0 1 0 1 0)); |
| 43 | |
| 44 | // Write 00110011 |
| 45 | for (int i = 0; i < 8; ++i) { |
| 46 | bool result; |
| 47 | switch (i) { |
| 48 | case 0: |
| 49 | case 1: |
| 50 | case 4: |
| 51 | case 5: |
| 52 | result = writer.PutValue(0, 1); |
| 53 | break; |
| 54 | default: |
| 55 | result = writer.PutValue(1, 1); |
| 56 | break; |
| 57 | } |
| 58 | EXPECT_TRUE(result); |
| 59 | } |
| 60 | writer.Flush(); |
| 61 | |
| 62 | // Validate the exact bit value |
| 63 | EXPECT_EQ((int)buffer[0], BOOST_BINARY(1 0 1 0 1 0 1 0)); |
| 64 | EXPECT_EQ((int)buffer[1], BOOST_BINARY(1 1 0 0 1 1 0 0)); |
| 65 | |
| 66 | // Use the reader and validate |
| 67 | BatchedBitReader reader(buffer, len); |
| 68 | |
| 69 | // Ensure it returns the same results after Reset(). |
| 70 | for (int trial = 0; trial < 2; ++trial) { |
| 71 | // We use uint8_t instead of bool because unpacking is only supported for unsigned |
| 72 | // integers. |
| 73 | uint8_t batch_vals[16]; |
| 74 | EXPECT_EQ(16, reader.UnpackBatch(1, 16, batch_vals)); |
| 75 | for (int i = 0; i < 8; ++i) EXPECT_EQ(batch_vals[i], i % 2); |
| 76 | |
| 77 | for (int i = 0; i < 8; ++i) { |
| 78 | switch (i) { |
| 79 | case 0: |
| 80 | case 1: |
| 81 | case 4: |
| 82 | case 5: |
| 83 | EXPECT_EQ(batch_vals[8 + i], false); |
| 84 | break; |
| 85 | default: |
| 86 | EXPECT_EQ(batch_vals[8 + i], true); |
| 87 | break; |
nothing calls this directly
no test coverage detected