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 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 20 | #include "base/logging.h" |
| 21 | #include "buffered_output_stream.h" |
| 22 | #include "common_runtime_test.h" |
| 23 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 26 | class OutputStreamTest : public CommonRuntimeTest { |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 27 | protected: |
| 28 | void CheckOffset(off_t expected) { |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 29 | off_t actual = output_stream_->Seek(0, kSeekCurrent); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 30 | EXPECT_EQ(expected, actual); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void SetOutputStream(OutputStream& output_stream) { |
| 34 | output_stream_ = &output_stream; |
| 35 | } |
| 36 | |
| 37 | void GenerateTestOutput() { |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 38 | EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 39 | CheckOffset(3); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 40 | EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 41 | CheckOffset(2); |
| 42 | uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 43 | EXPECT_TRUE(output_stream_->WriteFully(buf, 2)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 44 | CheckOffset(4); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 45 | EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 46 | CheckOffset(6); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 47 | EXPECT_TRUE(output_stream_->WriteFully(buf, 4)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 48 | CheckOffset(10); |
| 49 | } |
| 50 | |
| 51 | void CheckTestOutput(const std::vector<uint8_t>& actual) { |
| 52 | uint8_t expected[] = { |
| 53 | 0, 0, 1, 2, 0, 0, 1, 2, 3, 4 |
| 54 | }; |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 55 | EXPECT_EQ(sizeof(expected), actual.size()); |
| 56 | EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size())); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | OutputStream* output_stream_; |
| 60 | }; |
| 61 | |
| 62 | TEST_F(OutputStreamTest, File) { |
| 63 | ScratchFile tmp; |
| 64 | FileOutputStream output_stream(tmp.GetFile()); |
| 65 | SetOutputStream(output_stream); |
| 66 | GenerateTestOutput(); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 67 | std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str())); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 68 | EXPECT_TRUE(in.get() != NULL); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 69 | std::vector<uint8_t> actual(in->GetLength()); |
| 70 | bool readSuccess = in->ReadFully(&actual[0], actual.size()); |
Ian Rogers | 8d3a117 | 2013-06-04 01:13:28 -0700 | [diff] [blame] | 71 | EXPECT_TRUE(readSuccess); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 72 | CheckTestOutput(actual); |
| 73 | } |
| 74 | |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 75 | TEST_F(OutputStreamTest, Buffered) { |
| 76 | ScratchFile tmp; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 77 | std::unique_ptr<FileOutputStream> file_output_stream(new FileOutputStream(tmp.GetFile())); |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 78 | CHECK(file_output_stream.get() != NULL); |
| 79 | BufferedOutputStream buffered_output_stream(file_output_stream.release()); |
| 80 | SetOutputStream(buffered_output_stream); |
| 81 | GenerateTestOutput(); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 82 | std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str())); |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 83 | EXPECT_TRUE(in.get() != NULL); |
| 84 | std::vector<uint8_t> actual(in->GetLength()); |
| 85 | bool readSuccess = in->ReadFully(&actual[0], actual.size()); |
| 86 | EXPECT_TRUE(readSuccess); |
| 87 | CheckTestOutput(actual); |
| 88 | } |
| 89 | |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 90 | TEST_F(OutputStreamTest, Vector) { |
| 91 | std::vector<uint8_t> output; |
| 92 | VectorOutputStream output_stream("test vector output", output); |
| 93 | SetOutputStream(output_stream); |
| 94 | GenerateTestOutput(); |
| 95 | CheckTestOutput(output); |
| 96 | } |
| 97 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 98 | } // namespace art |