Writes 'num_vals' values with width 'bit_width' starting from 'start' and increasing and reads them back.
| 161 | // Writes 'num_vals' values with width 'bit_width' starting from 'start' and increasing |
| 162 | // and reads them back. |
| 163 | void TestBitArrayValues(int bit_width, uint64_t start, uint64_t num_vals) { |
| 164 | const int len = BitUtil::Ceil(bit_width * num_vals, 8); |
| 165 | const uint64_t mask = bit_width == 64 ? ~0UL : (1UL << bit_width) - 1; |
| 166 | |
| 167 | uint8_t buffer[(len > 0) ? len : 1]; |
| 168 | BitWriter writer(buffer, len); |
| 169 | for (uint64_t i = 0; i < num_vals; ++i) { |
| 170 | bool result = writer.PutValue((start + i) & mask, bit_width); |
| 171 | EXPECT_TRUE(result); |
| 172 | } |
| 173 | writer.Flush(); |
| 174 | EXPECT_EQ(writer.bytes_written(), len); |
| 175 | |
| 176 | BatchedBitReader reader(buffer, len); |
| 177 | BatchedBitReader reader2(reader); // Test copy constructor. |
| 178 | // Ensure it returns the same results after Reset(). |
| 179 | for (int trial = 0; trial < 2; ++trial) { |
| 180 | // Unpack all values at once with one batched reader and in small batches with the |
| 181 | // other batched reader. |
| 182 | vector<uint64_t> batch_vals(num_vals); |
| 183 | const uint64_t BATCH_SIZE = 32; |
| 184 | vector<uint64_t> batch_vals2(BATCH_SIZE); |
| 185 | EXPECT_EQ(num_vals, |
| 186 | reader.UnpackBatch(bit_width, num_vals, batch_vals.data())); |
| 187 | for (uint64_t i = 0; i < num_vals; ++i) { |
| 188 | if (i % BATCH_SIZE == 0) { |
| 189 | int num_to_unpack = min(BATCH_SIZE, num_vals - i); |
| 190 | EXPECT_EQ(num_to_unpack, |
| 191 | reader2.UnpackBatch(bit_width, num_to_unpack, batch_vals2.data())); |
| 192 | } |
| 193 | EXPECT_EQ((start + i) & mask, batch_vals[i]); |
| 194 | EXPECT_EQ((start + i) & mask, batch_vals2[i % BATCH_SIZE]); |
| 195 | } |
| 196 | |
| 197 | EXPECT_EQ(reader.bytes_left(), 0); |
| 198 | EXPECT_EQ(reader2.bytes_left(), 0); |
| 199 | reader.Reset(buffer, len); |
| 200 | reader2.Reset(buffer, len); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | TEST(BitArray, TestValues) { |
| 205 | for (int width = 0; width <= MAX_WIDTH; ++width) { |
no test coverage detected