| 225 | // Check the streaming compressor and decompressor together |
| 226 | |
| 227 | void CheckStreamingRoundtrip(std::shared_ptr<Compressor> compressor, |
| 228 | std::shared_ptr<Decompressor> decompressor, |
| 229 | const std::vector<uint8_t>& data) { |
| 230 | std::default_random_engine engine(42); |
| 231 | std::uniform_int_distribution<int> buf_size_distribution(10, 40); |
| 232 | |
| 233 | auto make_buf_size = [&]() -> int64_t { return buf_size_distribution(engine); }; |
| 234 | |
| 235 | // Compress... |
| 236 | |
| 237 | std::vector<uint8_t> compressed(1); |
| 238 | int64_t compressed_size = 0; |
| 239 | { |
| 240 | const uint8_t* input = data.data(); |
| 241 | int64_t remaining = data.size(); |
| 242 | |
| 243 | while (remaining > 0) { |
| 244 | // Feed a varying amount each time |
| 245 | int64_t input_len = std::min(remaining, make_buf_size()); |
| 246 | int64_t output_len = compressed.size() - compressed_size; |
| 247 | uint8_t* output = compressed.data() + compressed_size; |
| 248 | ASSERT_OK_AND_ASSIGN(auto result, |
| 249 | compressor->Compress(input_len, input, output_len, output)); |
| 250 | ASSERT_LE(result.bytes_read, input_len); |
| 251 | ASSERT_LE(result.bytes_written, output_len); |
| 252 | compressed_size += result.bytes_written; |
| 253 | input += result.bytes_read; |
| 254 | remaining -= result.bytes_read; |
| 255 | if (result.bytes_read == 0) { |
| 256 | compressed.resize(compressed.capacity() * 2); |
| 257 | } |
| 258 | } |
| 259 | // End the compressed stream |
| 260 | Compressor::EndResult result; |
| 261 | do { |
| 262 | int64_t output_len = compressed.size() - compressed_size; |
| 263 | uint8_t* output = compressed.data() + compressed_size; |
| 264 | ASSERT_OK_AND_ASSIGN(result, compressor->End(output_len, output)); |
| 265 | ASSERT_LE(result.bytes_written, output_len); |
| 266 | compressed_size += result.bytes_written; |
| 267 | if (result.should_retry) { |
| 268 | compressed.resize(compressed.capacity() * 2); |
| 269 | } |
| 270 | } while (result.should_retry); |
| 271 | |
| 272 | compressed.resize(compressed_size); |
| 273 | } |
| 274 | |
| 275 | // Then decompress... |
| 276 | |
| 277 | std::vector<uint8_t> decompressed(2); |
| 278 | int64_t decompressed_size = 0; |
| 279 | { |
| 280 | const uint8_t* input = compressed.data(); |
| 281 | int64_t remaining = compressed.size(); |
| 282 | |
| 283 | while (!decompressor->IsFinished()) { |
| 284 | // Feed a varying amount each time |
no test coverage detected