1cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom/*
2cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * Copyright (C) 2013 The Android Open Source Project
3cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom *
4cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License");
5cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * you may not use this file except in compliance with the License.
6cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * You may obtain a copy of the License at
7cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom *
8cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom *      http://www.apache.org/licenses/LICENSE-2.0
9cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom *
10cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * Unless required by applicable law or agreed to in writing, software
11cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS,
12cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * See the License for the specific language governing permissions and
14cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom * limitations under the License.
15cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom */
16cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
17cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom#include "base/logging.h"
18c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom#include "buffered_output_stream.h"
19cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom#include "common_test.h"
20cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom#include "file_output_stream.h"
21cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom#include "vector_output_stream.h"
22cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
23cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstromnamespace art {
24cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
25cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstromclass OutputStreamTest : public CommonTest {
26cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom protected:
27cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  void CheckOffset(off_t expected) {
2849a0f158ed11974fa2cc12014c9f55a31dabd8dfBrian Carlstrom    off_t actual = output_stream_->Seek(0, kSeekCurrent);
298d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(expected, actual);
30cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  }
31cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
32cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  void SetOutputStream(OutputStream& output_stream) {
33cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    output_stream_ = &output_stream;
34cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  }
35cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
36cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  void GenerateTestOutput() {
378d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
38cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    CheckOffset(3);
398d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
40cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    CheckOffset(2);
41cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
428d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
43cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    CheckOffset(4);
448d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
45cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    CheckOffset(6);
468d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
47cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    CheckOffset(10);
48cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  }
49cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
50cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  void CheckTestOutput(const std::vector<uint8_t>& actual) {
51cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    uint8_t expected[] = {
52cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom        0, 0, 1, 2, 0, 0, 1, 2, 3, 4
53cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    };
548d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(sizeof(expected), actual.size());
558d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
56cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  }
57cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
58cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  OutputStream* output_stream_;
59cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom};
60cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
61cd60ac736bc7104785dc67671660d70fb434466fBrian CarlstromTEST_F(OutputStreamTest, File) {
62cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  ScratchFile tmp;
63cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  FileOutputStream output_stream(tmp.GetFile());
64cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  SetOutputStream(output_stream);
65cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  GenerateTestOutput();
667571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom  UniquePtr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
678d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers  EXPECT_TRUE(in.get() != NULL);
68cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  std::vector<uint8_t> actual(in->GetLength());
69cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  bool readSuccess = in->ReadFully(&actual[0], actual.size());
708d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers  EXPECT_TRUE(readSuccess);
71cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  CheckTestOutput(actual);
72cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom}
73cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
74c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian CarlstromTEST_F(OutputStreamTest, Buffered) {
75c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  ScratchFile tmp;
76c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  UniquePtr<FileOutputStream> file_output_stream(new FileOutputStream(tmp.GetFile()));
77c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  CHECK(file_output_stream.get() != NULL);
78c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  BufferedOutputStream buffered_output_stream(file_output_stream.release());
79c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  SetOutputStream(buffered_output_stream);
80c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  GenerateTestOutput();
81c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  UniquePtr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
82c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  EXPECT_TRUE(in.get() != NULL);
83c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  std::vector<uint8_t> actual(in->GetLength());
84c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  bool readSuccess = in->ReadFully(&actual[0], actual.size());
85c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  EXPECT_TRUE(readSuccess);
86c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom  CheckTestOutput(actual);
87c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom}
88c6dfdacea2fd9e268f70328805b0366cdd6b7b9eBrian Carlstrom
89cd60ac736bc7104785dc67671660d70fb434466fBrian CarlstromTEST_F(OutputStreamTest, Vector) {
90cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  std::vector<uint8_t> output;
91cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  VectorOutputStream output_stream("test vector output", output);
92cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  SetOutputStream(output_stream);
93cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  GenerateTestOutput();
94cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  CheckTestOutput(output);
95cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom}
96cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
9702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier}  // namespace art
98