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 "file_output_stream.h"
18#include "vector_output_stream.h"
19
20#include "base/unix_file/fd_file.h"
21#include "base/logging.h"
22#include "buffered_output_stream.h"
23#include "common_runtime_test.h"
24
25namespace art {
26
27class OutputStreamTest : public CommonRuntimeTest {
28 protected:
29  void CheckOffset(off_t expected) {
30    off_t actual = output_stream_->Seek(0, kSeekCurrent);
31    EXPECT_EQ(expected, actual);
32  }
33
34  void SetOutputStream(OutputStream& output_stream) {
35    output_stream_ = &output_stream;
36  }
37
38  void GenerateTestOutput() {
39    EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
40    CheckOffset(3);
41    EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
42    CheckOffset(2);
43    uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
44    EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
45    CheckOffset(4);
46    EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
47    CheckOffset(6);
48    EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
49    CheckOffset(10);
50  }
51
52  void CheckTestOutput(const std::vector<uint8_t>& actual) {
53    uint8_t expected[] = {
54        0, 0, 1, 2, 0, 0, 1, 2, 3, 4
55    };
56    EXPECT_EQ(sizeof(expected), actual.size());
57    EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
58  }
59
60  OutputStream* output_stream_;
61};
62
63TEST_F(OutputStreamTest, File) {
64  ScratchFile tmp;
65  FileOutputStream output_stream(tmp.GetFile());
66  SetOutputStream(output_stream);
67  GenerateTestOutput();
68  std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
69  EXPECT_TRUE(in.get() != NULL);
70  std::vector<uint8_t> actual(in->GetLength());
71  bool readSuccess = in->ReadFully(&actual[0], actual.size());
72  EXPECT_TRUE(readSuccess);
73  CheckTestOutput(actual);
74}
75
76TEST_F(OutputStreamTest, Buffered) {
77  ScratchFile tmp;
78  std::unique_ptr<FileOutputStream> file_output_stream(new FileOutputStream(tmp.GetFile()));
79  CHECK(file_output_stream.get() != NULL);
80  BufferedOutputStream buffered_output_stream(file_output_stream.release());
81  SetOutputStream(buffered_output_stream);
82  GenerateTestOutput();
83  std::unique_ptr<File> in(OS::OpenFileForReading(tmp.GetFilename().c_str()));
84  EXPECT_TRUE(in.get() != NULL);
85  std::vector<uint8_t> actual(in->GetLength());
86  bool readSuccess = in->ReadFully(&actual[0], actual.size());
87  EXPECT_TRUE(readSuccess);
88  CheckTestOutput(actual);
89}
90
91TEST_F(OutputStreamTest, Vector) {
92  std::vector<uint8_t> output;
93  VectorOutputStream output_stream("test vector output", output);
94  SetOutputStream(output_stream);
95  GenerateTestOutput();
96  CheckTestOutput(output);
97}
98
99}  // namespace art
100