blob: c9e0edefcd71c2c75f54802b25a51dcaf5ac67d1 [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"
18#include "common_test.h"
19#include "file_output_stream.h"
20#include "vector_output_stream.h"
21
22namespace art {
23
24class OutputStreamTest : public CommonTest {
25 protected:
26 void CheckOffset(off_t expected) {
Brian Carlstrom49a0f152013-01-22 17:17:36 -080027 off_t actual = output_stream_->Seek(0, kSeekCurrent);
Ian Rogers8d3a1172013-06-04 01:13:28 -070028 EXPECT_EQ(expected, actual);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080029 }
30
31 void SetOutputStream(OutputStream& output_stream) {
32 output_stream_ = &output_stream;
33 }
34
35 void GenerateTestOutput() {
Ian Rogers8d3a1172013-06-04 01:13:28 -070036 EXPECT_EQ(3, output_stream_->Seek(3, kSeekCurrent));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080037 CheckOffset(3);
Ian Rogers8d3a1172013-06-04 01:13:28 -070038 EXPECT_EQ(2, output_stream_->Seek(2, kSeekSet));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080039 CheckOffset(2);
40 uint8_t buf[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Ian Rogers8d3a1172013-06-04 01:13:28 -070041 EXPECT_TRUE(output_stream_->WriteFully(buf, 2));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080042 CheckOffset(4);
Ian Rogers8d3a1172013-06-04 01:13:28 -070043 EXPECT_EQ(6, output_stream_->Seek(2, kSeekEnd));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080044 CheckOffset(6);
Ian Rogers8d3a1172013-06-04 01:13:28 -070045 EXPECT_TRUE(output_stream_->WriteFully(buf, 4));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080046 CheckOffset(10);
47 }
48
49 void CheckTestOutput(const std::vector<uint8_t>& actual) {
50 uint8_t expected[] = {
51 0, 0, 1, 2, 0, 0, 1, 2, 3, 4
52 };
Ian Rogers8d3a1172013-06-04 01:13:28 -070053 EXPECT_EQ(sizeof(expected), actual.size());
54 EXPECT_EQ(0, memcmp(expected, &actual[0], actual.size()));
Brian Carlstromcd60ac72013-01-20 17:09:51 -080055 }
56
57 OutputStream* output_stream_;
58};
59
60TEST_F(OutputStreamTest, File) {
61 ScratchFile tmp;
62 FileOutputStream output_stream(tmp.GetFile());
63 SetOutputStream(output_stream);
64 GenerateTestOutput();
65 UniquePtr<File> in(OS::OpenFile(tmp.GetFilename().c_str(), false));
Ian Rogers8d3a1172013-06-04 01:13:28 -070066 EXPECT_TRUE(in.get() != NULL);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080067 std::vector<uint8_t> actual(in->GetLength());
68 bool readSuccess = in->ReadFully(&actual[0], actual.size());
Ian Rogers8d3a1172013-06-04 01:13:28 -070069 EXPECT_TRUE(readSuccess);
Brian Carlstromcd60ac72013-01-20 17:09:51 -080070 CheckTestOutput(actual);
71}
72
73TEST_F(OutputStreamTest, Vector) {
74 std::vector<uint8_t> output;
75 VectorOutputStream output_stream("test vector output", output);
76 SetOutputStream(output_stream);
77 GenerateTestOutput();
78 CheckTestOutput(output);
79}
80
81} // namespace std