blob: a957ee375a2db2b71af9d3dbc1e7fa5bc0bbe206 [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
17#include "base/logging.h"
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070018#include "buffered_output_stream.h"
Brian Carlstromcd60ac72013-01-20 17:09:51 -080019#include "common_test.h"
20#include "file_output_stream.h"
21#include "vector_output_stream.h"
22
23namespace art {
24
25class OutputStreamTest : public CommonTest {
26 protected:
27 void CheckOffset(off_t expected) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -080028 off_t actual = output_stream_->Seek(0, kSeekCurrent);
Ian Rogers8d3a1172013-06-04 01:13:28 -070029 EXPECT_EQ(expected, actual);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080030 }
31
32 void SetOutputStream(OutputStream& output_stream) {
33 output_stream_ = &output_stream;
34 }
35
36 void GenerateTestOutput() {
Ian Rogers8d3a1172013-06-04 01:13:28 -070037 EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080038 CheckOffset(3);
Ian Rogers8d3a1172013-06-04 01:13:28 -070039 EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080040 CheckOffset(2);
41 uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Ian Rogers8d3a1172013-06-04 01:13:28 -070042 EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080043 CheckOffset(4);
Ian Rogers8d3a1172013-06-04 01:13:28 -070044 EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080045 CheckOffset(6);
Ian Rogers8d3a1172013-06-04 01:13:28 -070046 EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080047 CheckOffset(10);
48 }
49
50 void CheckTestOutput(const std::vector<uint8_t>& actual) {
51 uint8_t expected[] = {
52 0, 0, 1, 2, 0, 0, 1, 2, 3, 4
53 };
Ian Rogers8d3a1172013-06-04 01:13:28 -070054 EXPECT_EQ(sizeof(expected), actual.size());
55 EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080056 }
57
58 OutputStream* output_stream_;
59};
60
61TEST_F(OutputStreamTest, File) {
62 ScratchFile tmp;
63 FileOutputStream output_stream(tmp.GetFile());
64 SetOutputStream(output_stream);
65 GenerateTestOutput();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070066 UniquePtr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
Ian Rogers8d3a1172013-06-04 01:13:28 -070067 EXPECT_TRUE(in.get() != NULL);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080068 std::vector<uint8_t> actual(in->GetLength());
69 bool readSuccess = in->ReadFully(&actual[0], actual.size());
Ian Rogers8d3a1172013-06-04 01:13:28 -070070 EXPECT_TRUE(readSuccess);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080071 CheckTestOutput(actual);
72}
73
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070074TEST_F(OutputStreamTest, Buffered) {
75 ScratchFile tmp;
76 UniquePtr<FileOutputStream> file_output_stream(new FileOutputStream(tmp.GetFile()));
77 CHECK(file_output_stream.get() != NULL);
78 BufferedOutputStream buffered_output_stream(file_output_stream.release());
79 SetOutputStream(buffered_output_stream);
80 GenerateTestOutput();
81 UniquePtr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
82 EXPECT_TRUE(in.get() != NULL);
83 std::vector<uint8_t> actual(in->GetLength());
84 bool readSuccess = in->ReadFully(&actual[0], actual.size());
85 EXPECT_TRUE(readSuccess);
86 CheckTestOutput(actual);
87}
88
Brian Carlstromcd60ac72013-01-20 17:09:51 -080089TEST_F(OutputStreamTest, Vector) {
90 std::vector<uint8_t> output;
91 VectorOutputStream output_stream("test vector output", output);
92 SetOutputStream(output_stream);
93 GenerateTestOutput();
94 CheckTestOutput(output);
95}
96
Mathieu Chartier02e25112013-08-14 16:14:24 -070097} // namespace art