Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 17 | #include "file_output_stream.h" |
| 18 | #include "vector_output_stream.h" |
| 19 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 20 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 21 | #include "base/logging.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 23 | #include "buffered_output_stream.h" |
| 24 | #include "common_runtime_test.h" |
| 25 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 26 | namespace art { |
| 27 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | class OutputStreamTest : public CommonRuntimeTest { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 29 | protected: |
| 30 | void CheckOffset(off_t expected) { |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 31 | off_t actual = output_stream_->Seek(0, kSeekCurrent); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 32 | EXPECT_EQ(expected, actual); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void SetOutputStream(OutputStream& output_stream) { |
| 36 | output_stream_ = &output_stream; |
| 37 | } |
| 38 | |
| 39 | void GenerateTestOutput() { |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 40 | EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 41 | CheckOffset(3); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 42 | EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 43 | CheckOffset(2); |
| 44 | uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 45 | EXPECT_TRUE(output_stream_->WriteFully(buf, 2)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 46 | CheckOffset(4); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 47 | EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 48 | CheckOffset(6); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 49 | EXPECT_TRUE(output_stream_->WriteFully(buf, 4)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 50 | CheckOffset(10); |
Zheng Xu | 98088c4 | 2015-06-19 14:12:45 +0800 | [diff] [blame] | 51 | EXPECT_TRUE(output_stream_->WriteFully(buf, 6)); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 52 | EXPECT_TRUE(output_stream_->Flush()); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void CheckTestOutput(const std::vector<uint8_t>& actual) { |
| 56 | uint8_t expected[] = { |
Zheng Xu | 98088c4 | 2015-06-19 14:12:45 +0800 | [diff] [blame] | 57 | 0, 0, 1, 2, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6 |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 58 | }; |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 59 | EXPECT_EQ(sizeof(expected), actual.size()); |
| 60 | EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size())); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | OutputStream* output_stream_; |
| 64 | }; |
| 65 | |
| 66 | TEST_F(OutputStreamTest, File) { |
| 67 | ScratchFile tmp; |
| 68 | FileOutputStream output_stream(tmp.GetFile()); |
| 69 | SetOutputStream(output_stream); |
| 70 | GenerateTestOutput(); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 71 | std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str())); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 72 | EXPECT_TRUE(in.get() != nullptr); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 73 | std::vector<uint8_t> actual(in->GetLength()); |
| 74 | bool readSuccess = in->ReadFully(&actual[0], actual.size()); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 75 | EXPECT_TRUE(readSuccess); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 76 | CheckTestOutput(actual); |
| 77 | } |
| 78 | |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 79 | TEST_F(OutputStreamTest, Buffered) { |
| 80 | ScratchFile tmp; |
Zheng Xu | 98088c4 | 2015-06-19 14:12:45 +0800 | [diff] [blame] | 81 | { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 82 | BufferedOutputStream buffered_output_stream(MakeUnique<FileOutputStream>(tmp.GetFile())); |
Zheng Xu | 98088c4 | 2015-06-19 14:12:45 +0800 | [diff] [blame] | 83 | SetOutputStream(buffered_output_stream); |
| 84 | GenerateTestOutput(); |
| 85 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 86 | std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str())); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 87 | EXPECT_TRUE(in.get() != nullptr); |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 88 | std::vector<uint8_t> actual(in->GetLength()); |
| 89 | bool readSuccess = in->ReadFully(&actual[0], actual.size()); |
| 90 | EXPECT_TRUE(readSuccess); |
| 91 | CheckTestOutput(actual); |
| 92 | } |
| 93 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 94 | TEST_F(OutputStreamTest, Vector) { |
| 95 | std::vector<uint8_t> output; |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 96 | VectorOutputStream output_stream("test vector output", &output); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 97 | SetOutputStream(output_stream); |
| 98 | GenerateTestOutput(); |
| 99 | CheckTestOutput(output); |
| 100 | } |
| 101 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 102 | TEST_F(OutputStreamTest, BufferedFlush) { |
| 103 | struct CheckingOutputStream : OutputStream { |
| 104 | CheckingOutputStream() |
| 105 | : OutputStream("dummy"), |
| 106 | flush_called(false) { } |
| 107 | ~CheckingOutputStream() OVERRIDE {} |
| 108 | |
| 109 | bool WriteFully(const void* buffer ATTRIBUTE_UNUSED, |
| 110 | size_t byte_count ATTRIBUTE_UNUSED) OVERRIDE { |
| 111 | LOG(FATAL) << "UNREACHABLE"; |
| 112 | UNREACHABLE(); |
| 113 | } |
| 114 | |
| 115 | off_t Seek(off_t offset ATTRIBUTE_UNUSED, Whence whence ATTRIBUTE_UNUSED) OVERRIDE { |
| 116 | LOG(FATAL) << "UNREACHABLE"; |
| 117 | UNREACHABLE(); |
| 118 | } |
| 119 | |
| 120 | bool Flush() OVERRIDE { |
| 121 | flush_called = true; |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | bool flush_called; |
| 126 | }; |
| 127 | |
| 128 | std::unique_ptr<CheckingOutputStream> cos = MakeUnique<CheckingOutputStream>(); |
| 129 | CheckingOutputStream* checking_output_stream = cos.get(); |
| 130 | BufferedOutputStream buffered(std::move(cos)); |
| 131 | ASSERT_FALSE(checking_output_stream->flush_called); |
| 132 | bool flush_result = buffered.Flush(); |
| 133 | ASSERT_TRUE(flush_result); |
| 134 | ASSERT_TRUE(checking_output_stream->flush_called); |
| 135 | } |
| 136 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 137 | } // namespace art |