Validates encoding of values by encoding and decoding them. If expected_encoding != NULL, validates that the encoded buffer is exactly 'expected_encoding'. if expected_len is not -1, validates that is is the same as the encoded size (in bytes).
| 102 | // if expected_len is not -1, validates that is is the same as the encoded size (in |
| 103 | // bytes). |
| 104 | int ValidateRle(const vector<int>& values, int bit_width, uint8_t* expected_encoding, |
| 105 | int expected_len) { |
| 106 | stringstream ss; |
| 107 | ss << "bit_width=" << bit_width; |
| 108 | const string& description = ss.str(); |
| 109 | const int len = 64 * 1024; |
| 110 | uint8_t buffer[len]; |
| 111 | EXPECT_LE(expected_len, len); |
| 112 | |
| 113 | RleEncoder encoder(buffer, len, bit_width); |
| 114 | |
| 115 | int encoded_len = 0; |
| 116 | for (int clear_count = 0; clear_count < 2; clear_count++) { |
| 117 | if (clear_count >= 1) { |
| 118 | // Check that we can reuse the encoder after calling Clear(). |
| 119 | encoder.Clear(); |
| 120 | } |
| 121 | for (int i = 0; i < values.size(); ++i) { |
| 122 | bool result = encoder.Put(values[i]); |
| 123 | EXPECT_TRUE(result); |
| 124 | } |
| 125 | encoded_len = encoder.Flush(); |
| 126 | |
| 127 | if (expected_len != -1) { |
| 128 | EXPECT_EQ(encoded_len, expected_len); |
| 129 | } |
| 130 | if (expected_encoding != NULL) { |
| 131 | EXPECT_TRUE(memcmp(buffer, expected_encoding, expected_len) == 0); |
| 132 | } |
| 133 | |
| 134 | // Verify read. |
| 135 | RleBatchDecoder<uint64_t> per_value_decoder(buffer, len, bit_width); |
| 136 | RleBatchDecoder<uint64_t> per_run_decoder(buffer, len, bit_width); |
| 137 | RleBatchDecoder<uint64_t> batch_decoder(buffer, len, bit_width); |
| 138 | // Ensure it returns the same results after Reset(). |
| 139 | for (int trial = 0; trial < 2; ++trial) { |
| 140 | for (int i = 0; i < values.size(); ++i) { |
| 141 | uint64_t val; |
| 142 | EXPECT_TRUE(per_value_decoder.GetSingleValue(&val)) << description; |
| 143 | EXPECT_EQ(values[i], val) << description << " i=" << i << " trial=" << trial; |
| 144 | } |
| 145 | // Unpack everything at once from the other decoders. |
| 146 | vector<uint64_t> decoded_values1(values.size()); |
| 147 | vector<uint64_t> decoded_values2(values.size()); |
| 148 | EXPECT_TRUE(GetRleValues( |
| 149 | &per_run_decoder, decoded_values1.size(), decoded_values1.data())); |
| 150 | EXPECT_TRUE(GetRleValuesBatched( |
| 151 | &batch_decoder, decoded_values2.size(), decoded_values2.data())); |
| 152 | for (int i = 0; i < values.size(); ++i) { |
| 153 | EXPECT_EQ(values[i], decoded_values1[i]) << description << " i=" << i; |
| 154 | EXPECT_EQ(values[i], decoded_values2[i]) << description << " i=" << i; |
| 155 | } |
| 156 | per_value_decoder.Reset(buffer, len, bit_width); |
| 157 | per_run_decoder.Reset(buffer, len, bit_width); |
| 158 | batch_decoder.Reset(buffer, len, bit_width); |
| 159 | } |
| 160 | } |
| 161 | return encoded_len; |