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/random_inputstream.h" 17 18 #include "tensorflow/core/lib/core/status_test_util.h" 19 #include "tensorflow/core/platform/env.h" 20 #include "tensorflow/core/platform/test.h" 21 22 namespace tensorflow { 23 namespace io { 24 namespace { 25 26 TEST(RandomInputStream, ReadNBytes) { 27 Env* env = Env::Default(); 28 string fname = testing::TmpDir() + "/random_inputbuffer_test"; 29 TF_ASSERT_OK(WriteStringToFile(env, fname, "0123456789")); 30 31 std::unique_ptr<RandomAccessFile> file; 32 TF_ASSERT_OK(env->NewRandomAccessFile(fname, &file)); 33 string read; 34 RandomAccessInputStream in(file.get()); 35 TF_ASSERT_OK(in.ReadNBytes(3, &read)); 36 EXPECT_EQ(read, "012"); 37 EXPECT_EQ(3, in.Tell()); 38 TF_ASSERT_OK(in.ReadNBytes(0, &read)); 39 EXPECT_EQ(read, ""); 40 EXPECT_EQ(3, in.Tell()); 41 TF_ASSERT_OK(in.ReadNBytes(5, &read)); 42 EXPECT_EQ(read, "34567"); 43 EXPECT_EQ(8, in.Tell()); 44 TF_ASSERT_OK(in.ReadNBytes(0, &read)); 45 EXPECT_EQ(read, ""); 46 EXPECT_EQ(8, in.Tell()); 47 EXPECT_TRUE(errors::IsOutOfRange(in.ReadNBytes(20, &read))); 48 EXPECT_EQ(read, "89"); 49 EXPECT_EQ(10, in.Tell()); 50 TF_ASSERT_OK(in.ReadNBytes(0, &read)); 51 EXPECT_EQ(read, ""); 52 EXPECT_EQ(10, in.Tell()); 53 } 54 55 TEST(RandomInputStream, SkipNBytes) { 56 Env* env = Env::Default(); 57 string fname = testing::TmpDir() + "/random_inputbuffer_test"; 58 TF_ASSERT_OK(WriteStringToFile(env, fname, "0123456789")); 59 60 std::unique_ptr<RandomAccessFile> file; 61 TF_ASSERT_OK(env->NewRandomAccessFile(fname, &file)); 62 string read; 63 RandomAccessInputStream in(file.get()); 64 TF_ASSERT_OK(in.SkipNBytes(3)); 65 EXPECT_EQ(3, in.Tell()); 66 TF_ASSERT_OK(in.ReadNBytes(0, &read)); 67 EXPECT_EQ(read, ""); 68 EXPECT_EQ(3, in.Tell()); 69 TF_ASSERT_OK(in.ReadNBytes(4, &read)); 70 EXPECT_EQ(read, "3456"); 71 EXPECT_EQ(7, in.Tell()); 72 TF_ASSERT_OK(in.SkipNBytes(0)); 73 EXPECT_EQ(7, in.Tell()); 74 TF_ASSERT_OK(in.ReadNBytes(2, &read)); 75 EXPECT_EQ(read, "78"); 76 EXPECT_EQ(9, in.Tell()); 77 EXPECT_TRUE(errors::IsOutOfRange(in.SkipNBytes(20))); 78 EXPECT_EQ(10, in.Tell()); 79 // Making sure that if we read after we've skipped beyond end of file, we get 80 // nothing. 81 EXPECT_TRUE(errors::IsOutOfRange(in.ReadNBytes(5, &read))); 82 EXPECT_EQ(read, ""); 83 EXPECT_EQ(10, in.Tell()); 84 } 85 86 TEST(RandomInputStream, Seek) { 87 Env* env = Env::Default(); 88 string fname = testing::TmpDir() + "/random_inputbuffer_seek_test"; 89 TF_ASSERT_OK(WriteStringToFile(env, fname, "0123456789")); 90 91 std::unique_ptr<RandomAccessFile> file; 92 TF_ASSERT_OK(env->NewRandomAccessFile(fname, &file)); 93 string read; 94 RandomAccessInputStream in(file.get()); 95 96 // Seek forward 97 TF_ASSERT_OK(in.Seek(3)); 98 EXPECT_EQ(3, in.Tell()); 99 100 // Read 4 bytes 101 TF_ASSERT_OK(in.ReadNBytes(4, &read)); 102 EXPECT_EQ(read, "3456"); 103 EXPECT_EQ(7, in.Tell()); 104 105 // Seek backwards 106 TF_ASSERT_OK(in.Seek(1)); 107 TF_ASSERT_OK(in.ReadNBytes(4, &read)); 108 EXPECT_EQ(read, "1234"); 109 EXPECT_EQ(5, in.Tell()); 110 } 111 112 } // anonymous namespace 113 } // namespace io 114 } // namespace tensorflow 115