1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #include "tensorflow/core/lib/io/record_reader.h" 17 #include "tensorflow/core/lib/io/record_writer.h" 18 19 #include <vector> 20 #include "tensorflow/core/platform/env.h" 21 22 #include "tensorflow/core/lib/core/errors.h" 23 #include "tensorflow/core/lib/core/status.h" 24 #include "tensorflow/core/lib/core/status_test_util.h" 25 #include "tensorflow/core/lib/strings/strcat.h" 26 #include "tensorflow/core/platform/logging.h" 27 #include "tensorflow/core/platform/test.h" 28 29 namespace tensorflow { 30 31 static std::vector<int> BufferSizes() { 32 return {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 33 12, 13, 14, 15, 16, 17, 18, 19, 20, 65536}; 34 } 35 36 TEST(RecordReaderWriterTest, TestBasics) { 37 Env* env = Env::Default(); 38 string fname = testing::TmpDir() + "/record_reader_writer_test"; 39 40 for (auto buf_size : BufferSizes()) { 41 { 42 std::unique_ptr<WritableFile> file; 43 TF_CHECK_OK(env->NewWritableFile(fname, &file)); 44 45 io::RecordWriterOptions options; 46 options.zlib_options.output_buffer_size = buf_size; 47 io::RecordWriter writer(file.get(), options); 48 TF_EXPECT_OK(writer.WriteRecord("abc")); 49 TF_EXPECT_OK(writer.WriteRecord("defg")); 50 TF_CHECK_OK(writer.Flush()); 51 } 52 53 { 54 std::unique_ptr<RandomAccessFile> read_file; 55 // Read it back with the RecordReader. 56 TF_CHECK_OK(env->NewRandomAccessFile(fname, &read_file)); 57 io::RecordReaderOptions options; 58 options.zlib_options.input_buffer_size = buf_size; 59 io::RecordReader reader(read_file.get(), options); 60 uint64 offset = 0; 61 string record; 62 TF_CHECK_OK(reader.ReadRecord(&offset, &record)); 63 EXPECT_EQ("abc", record); 64 TF_CHECK_OK(reader.ReadRecord(&offset, &record)); 65 EXPECT_EQ("defg", record); 66 } 67 } 68 } 69 70 TEST(RecordReaderWriterTest, TestZlib) { 71 Env* env = Env::Default(); 72 string fname = testing::TmpDir() + "/record_reader_writer_zlib_test"; 73 74 for (auto buf_size : BufferSizes()) { 75 // Zlib compression needs output buffer size > 1. 76 if (buf_size == 1) continue; 77 { 78 std::unique_ptr<WritableFile> file; 79 TF_CHECK_OK(env->NewWritableFile(fname, &file)); 80 81 io::RecordWriterOptions options; 82 options.compression_type = io::RecordWriterOptions::ZLIB_COMPRESSION; 83 options.zlib_options.output_buffer_size = buf_size; 84 io::RecordWriter writer(file.get(), options); 85 TF_EXPECT_OK(writer.WriteRecord("abc")); 86 TF_EXPECT_OK(writer.WriteRecord("defg")); 87 TF_CHECK_OK(writer.Flush()); 88 } 89 90 { 91 std::unique_ptr<RandomAccessFile> read_file; 92 // Read it back with the RecordReader. 93 TF_CHECK_OK(env->NewRandomAccessFile(fname, &read_file)); 94 io::RecordReaderOptions options; 95 options.compression_type = io::RecordReaderOptions::ZLIB_COMPRESSION; 96 options.zlib_options.input_buffer_size = buf_size; 97 io::RecordReader reader(read_file.get(), options); 98 uint64 offset = 0; 99 string record; 100 TF_CHECK_OK(reader.ReadRecord(&offset, &record)); 101 EXPECT_EQ("abc", record); 102 TF_CHECK_OK(reader.ReadRecord(&offset, &record)); 103 EXPECT_EQ("defg", record); 104 } 105 } 106 } 107 108 } // namespace tensorflow 109