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 | |
| 17 | #include "base/logging.h" |
| 18 | #include "common_test.h" |
| 19 | #include "file_output_stream.h" |
| 20 | #include "vector_output_stream.h" |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | class OutputStreamTest : public CommonTest { |
| 25 | protected: |
| 26 | void CheckOffset(off_t expected) { |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 27 | off_t actual = output_stream_->Seek(0, kSeekCurrent); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 28 | CHECK_EQ(expected, actual); |
| 29 | } |
| 30 | |
| 31 | void SetOutputStream(OutputStream& output_stream) { |
| 32 | output_stream_ = &output_stream; |
| 33 | } |
| 34 | |
| 35 | void GenerateTestOutput() { |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 36 | CHECK_EQ(3, output_stream_->Seek(3, kSeekCurrent)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 37 | CheckOffset(3); |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 38 | CHECK_EQ(2, output_stream_->Seek(2, kSeekSet)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 39 | CheckOffset(2); |
| 40 | uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 41 | CHECK(output_stream_->WriteFully(buf, 2)); |
| 42 | CheckOffset(4); |
Brian Carlstrom | 49a0f15 | 2013-01-22 17:17:36 -0800 | [diff] [blame] | 43 | CHECK_EQ(6, output_stream_->Seek(2, kSeekEnd)); |
Brian Carlstrom | cd60ac7 | 2013-01-20 17:09:51 -0800 | [diff] [blame] | 44 | CheckOffset(6); |
| 45 | CHECK(output_stream_->WriteFully(buf, 4)); |
| 46 | CheckOffset(10); |
| 47 | } |
| 48 | |
| 49 | void CheckTestOutput(const std::vector<uint8_t>& actual) { |
| 50 | uint8_t expected[] = { |
| 51 | 0, 0, 1, 2, 0, 0, 1, 2, 3, 4 |
| 52 | }; |
| 53 | CHECK_EQ(sizeof(expected), actual.size()); |
| 54 | CHECK_EQ(0, memcmp(expected, &actual[0], actual.size())); |
| 55 | } |
| 56 | |
| 57 | OutputStream* output_stream_; |
| 58 | }; |
| 59 | |
| 60 | TEST_F(OutputStreamTest, File) { |
| 61 | ScratchFile tmp; |
| 62 | FileOutputStream output_stream(tmp.GetFile()); |
| 63 | SetOutputStream(output_stream); |
| 64 | GenerateTestOutput(); |
| 65 | UniquePtr<File> in(OS::OpenFile(tmp.GetFilename().c_str(), false)); |
| 66 | CHECK(in.get() != NULL); |
| 67 | std::vector<uint8_t> actual(in->GetLength()); |
| 68 | bool readSuccess = in->ReadFully(&actual[0], actual.size()); |
| 69 | CHECK(readSuccess); |
| 70 | CheckTestOutput(actual); |
| 71 | } |
| 72 | |
| 73 | TEST_F(OutputStreamTest, Vector) { |
| 74 | std::vector<uint8_t> output; |
| 75 | VectorOutputStream output_stream("test vector output", output); |
| 76 | SetOutputStream(output_stream); |
| 77 | GenerateTestOutput(); |
| 78 | CheckTestOutput(output); |
| 79 | } |
| 80 | |
| 81 | } // namespace std |