output_stream_test.cc revision 02e25119b15a6f619f17db99f5d05124a5807ff3
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"
18cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom#include "common_test.h"
19cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom#include "file_output_stream.h"
20cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom#include "vector_output_stream.h"
21cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
22cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstromnamespace art {
23cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
24cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstromclass OutputStreamTest : public CommonTest {
25cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom protected:
26cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  void CheckOffset(off_t expected) {
2749a0f158ed11974fa2cc12014c9f55a31dabd8dfBrian Carlstrom    off_t actual = output_stream_->Seek(0, kSeekCurrent);
288d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(expected, actual);
29cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  }
30cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
31cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  void SetOutputStream(OutputStream& output_stream) {
32cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    output_stream_ = &output_stream;
33cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  }
34cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
35cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  void GenerateTestOutput() {
368d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
37cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    CheckOffset(3);
388d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
39cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    CheckOffset(2);
40cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
418d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
42cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    CheckOffset(4);
438d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
44cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    CheckOffset(6);
458d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
46cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    CheckOffset(10);
47cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  }
48cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
49cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  void CheckTestOutput(const std::vector<uint8_t>& actual) {
50cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    uint8_t expected[] = {
51cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom        0, 0, 1, 2, 0, 0, 1, 2, 3, 4
52cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom    };
538d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(sizeof(expected), actual.size());
548d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers    EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
55cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  }
56cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
57cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  OutputStream* output_stream_;
58cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom};
59cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
60cd60ac736bc7104785dc67671660d70fb434466fBrian CarlstromTEST_F(OutputStreamTest, File) {
61cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  ScratchFile tmp;
62cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  FileOutputStream output_stream(tmp.GetFile());
63cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  SetOutputStream(output_stream);
64cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  GenerateTestOutput();
657571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom  UniquePtr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
668d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers  EXPECT_TRUE(in.get() != NULL);
67cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  std::vector<uint8_t> actual(in->GetLength());
68cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  bool readSuccess = in->ReadFully(&actual[0], actual.size());
698d3a117b374352a1853fae9b7306afeaaa9e3b91Ian Rogers  EXPECT_TRUE(readSuccess);
70cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  CheckTestOutput(actual);
71cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom}
72cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
73cd60ac736bc7104785dc67671660d70fb434466fBrian CarlstromTEST_F(OutputStreamTest, Vector) {
74cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  std::vector<uint8_t> output;
75cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  VectorOutputStream output_stream("test vector output", output);
76cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  SetOutputStream(output_stream);
77cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  GenerateTestOutput();
78cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom  CheckTestOutput(output);
79cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom}
80cd60ac736bc7104785dc67671660d70fb434466fBrian Carlstrom
8102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier}  // namespace art
82