blob: 09fef29d481e888130da5a9d2fb33679196fe6b2 [file] [log] [blame]
Brian Carlstromcd60ac72013-01-20 17:09:51 -08001/*
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 Carlstromcd60ac72013-01-20 17:09:51 -080017#include "file_output_stream.h"
18#include "vector_output_stream.h"
19
Ian Rogerse63db272014-07-15 15:36:11 -070020#include "base/unix_file/fd_file.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080021#include "base/logging.h"
22#include "buffered_output_stream.h"
23#include "common_runtime_test.h"
24
Brian Carlstromcd60ac72013-01-20 17:09:51 -080025namespace art {
26
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080027class OutputStreamTest : public CommonRuntimeTest {
Brian Carlstromcd60ac72013-01-20 17:09:51 -080028 protected:
29 void CheckOffset(off_t expected) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -080030 off_t actual = output_stream_->Seek(0, kSeekCurrent);
Ian Rogers8d3a1172013-06-04 01:13:28 -070031 EXPECT_EQ(expected, actual);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080032 }
33
34 void SetOutputStream(OutputStream& output_stream) {
35 output_stream_ = &output_stream;
36 }
37
38 void GenerateTestOutput() {
Ian Rogers8d3a1172013-06-04 01:13:28 -070039 EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080040 CheckOffset(3);
Ian Rogers8d3a1172013-06-04 01:13:28 -070041 EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080042 CheckOffset(2);
43 uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Ian Rogers8d3a1172013-06-04 01:13:28 -070044 EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080045 CheckOffset(4);
Ian Rogers8d3a1172013-06-04 01:13:28 -070046 EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080047 CheckOffset(6);
Ian Rogers8d3a1172013-06-04 01:13:28 -070048 EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080049 CheckOffset(10);
Zheng Xu98088c42015-06-19 14:12:45 +080050 EXPECT_TRUE(output_stream_->WriteFully(buf, 6));
Vladimir Marko10c13562015-11-25 14:33:36 +000051 EXPECT_TRUE(output_stream_->Flush());
Brian Carlstromcd60ac72013-01-20 17:09:51 -080052 }
53
54 void CheckTestOutput(const std::vector<uint8_t>& actual) {
55 uint8_t expected[] = {
Zheng Xu98088c42015-06-19 14:12:45 +080056 0, 0, 1, 2, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6
Brian Carlstromcd60ac72013-01-20 17:09:51 -080057 };
Ian Rogers8d3a1172013-06-04 01:13:28 -070058 EXPECT_EQ(sizeof(expected), actual.size());
59 EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080060 }
61
62 OutputStream* output_stream_;
63};
64
65TEST_F(OutputStreamTest, File) {
66 ScratchFile tmp;
67 FileOutputStream output_stream(tmp.GetFile());
68 SetOutputStream(output_stream);
69 GenerateTestOutput();
Ian Rogers700a4022014-05-19 16:49:03 -070070 std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070071 EXPECT_TRUE(in.get() != nullptr);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080072 std::vector<uint8_t> actual(in->GetLength());
73 bool readSuccess = in->ReadFully(&actual[0], actual.size());
Ian Rogers8d3a1172013-06-04 01:13:28 -070074 EXPECT_TRUE(readSuccess);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080075 CheckTestOutput(actual);
76}
77
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070078TEST_F(OutputStreamTest, Buffered) {
79 ScratchFile tmp;
Zheng Xu98088c42015-06-19 14:12:45 +080080 {
Andreas Gampe8bdda5a2017-06-08 15:30:36 -070081 BufferedOutputStream buffered_output_stream(std::make_unique<FileOutputStream>(tmp.GetFile()));
Zheng Xu98088c42015-06-19 14:12:45 +080082 SetOutputStream(buffered_output_stream);
83 GenerateTestOutput();
84 }
Ian Rogers700a4022014-05-19 16:49:03 -070085 std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070086 EXPECT_TRUE(in.get() != nullptr);
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070087 std::vector<uint8_t> actual(in->GetLength());
88 bool readSuccess = in->ReadFully(&actual[0], actual.size());
89 EXPECT_TRUE(readSuccess);
90 CheckTestOutput(actual);
91}
92
Brian Carlstromcd60ac72013-01-20 17:09:51 -080093TEST_F(OutputStreamTest, Vector) {
94 std::vector<uint8_t> output;
Ian Rogers0279ebb2014-10-08 17:27:48 -070095 VectorOutputStream output_stream("test vector output", &output);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080096 SetOutputStream(output_stream);
97 GenerateTestOutput();
98 CheckTestOutput(output);
99}
100
Vladimir Marko10c13562015-11-25 14:33:36 +0000101TEST_F(OutputStreamTest, BufferedFlush) {
102 struct CheckingOutputStream : OutputStream {
103 CheckingOutputStream()
104 : OutputStream("dummy"),
105 flush_called(false) { }
106 ~CheckingOutputStream() OVERRIDE {}
107
108 bool WriteFully(const void* buffer ATTRIBUTE_UNUSED,
109 size_t byte_count ATTRIBUTE_UNUSED) OVERRIDE {
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113
114 off_t Seek(off_t offset ATTRIBUTE_UNUSED, Whence whence ATTRIBUTE_UNUSED) OVERRIDE {
115 LOG(FATAL) << "UNREACHABLE";
116 UNREACHABLE();
117 }
118
119 bool Flush() OVERRIDE {
120 flush_called = true;
121 return true;
122 }
123
124 bool flush_called;
125 };
126
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700127 std::unique_ptr<CheckingOutputStream> cos = std::make_unique<CheckingOutputStream>();
Vladimir Marko10c13562015-11-25 14:33:36 +0000128 CheckingOutputStream* checking_output_stream = cos.get();
129 BufferedOutputStream buffered(std::move(cos));
130 ASSERT_FALSE(checking_output_stream->flush_called);
131 bool flush_result = buffered.Flush();
132 ASSERT_TRUE(flush_result);
133 ASSERT_TRUE(checking_output_stream->flush_called);
134}
135
Mathieu Chartier02e25112013-08-14 16:14:24 -0700136} // namespace art