| 107 | } |
| 108 | |
| 109 | void TestMultipleWrites(uint8 input_buf_size, uint8 output_buf_size, |
| 110 | int num_writes, bool with_flush = false) { |
| 111 | Env* env = Env::Default(); |
| 112 | CompressionOptions input_options = CompressionOptions::DEFAULT(); |
| 113 | CompressionOptions output_options = CompressionOptions::DEFAULT(); |
| 114 | |
| 115 | string fname = testing::TmpDir() + "/zlib_buffers_test"; |
| 116 | string data = GenTestString(); |
| 117 | std::unique_ptr<WritableFile> file_writer; |
| 118 | string actual_result; |
| 119 | string expected_result; |
| 120 | |
| 121 | TF_ASSERT_OK(env->NewWritableFile(fname, &file_writer)); |
| 122 | ZlibOutputBuffer out(file_writer.get(), input_buf_size, output_buf_size, |
| 123 | output_options); |
| 124 | TF_ASSERT_OK(out.Init()); |
| 125 | |
| 126 | for (int i = 0; i < num_writes; i++) { |
| 127 | TF_ASSERT_OK(out.Append(StringPiece(data))); |
| 128 | if (with_flush) { |
| 129 | TF_ASSERT_OK(out.Flush()); |
| 130 | } |
| 131 | strings::StrAppend(&expected_result, data); |
| 132 | } |
| 133 | TF_ASSERT_OK(out.Close()); |
| 134 | TF_ASSERT_OK(file_writer->Flush()); |
| 135 | TF_ASSERT_OK(file_writer->Close()); |
| 136 | |
| 137 | std::unique_ptr<RandomAccessFile> file_reader; |
| 138 | TF_ASSERT_OK(env->NewRandomAccessFile(fname, &file_reader)); |
| 139 | std::unique_ptr<RandomAccessInputStream> input_stream( |
| 140 | new RandomAccessInputStream(file_reader.get())); |
| 141 | ZlibInputStream in(input_stream.get(), input_buf_size, output_buf_size, |
| 142 | input_options); |
| 143 | |
| 144 | for (int i = 0; i < num_writes; i++) { |
| 145 | string decompressed_output; |
| 146 | TF_ASSERT_OK(in.ReadNBytes(data.size(), &decompressed_output)); |
| 147 | strings::StrAppend(&actual_result, decompressed_output); |
| 148 | } |
| 149 | |
| 150 | EXPECT_EQ(actual_result, expected_result); |
| 151 | } |
| 152 | |
| 153 | TEST(ZlibBuffers, MultipleWritesWithoutFlush) { |
| 154 | TestMultipleWrites(200, 200, 10); |
no test coverage detected