1/*
2 * Copyright 2011 Google Inc. All Rights Reserved.
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 <stdio.h>
18
19#include <algorithm>
20
21#include "gtest/gtest.h"
22#include "sfntly/port/memory_input_stream.h"
23#include "sfntly/port/memory_output_stream.h"
24#include "sfntly/port/type.h"
25
26namespace {
27  const char* kTestData =
28"01234567890123456789012345678901234567890123456789"  // 50
29"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx"  // 100
30"yz";                                                 // 102
31  const size_t kTestBufferLen = 102;
32}
33
34namespace sfntly {
35
36bool TestMemoryInputStream() {
37  ByteVector test_buffer;
38  test_buffer.resize(kTestBufferLen);
39  std::copy(kTestData, kTestData + kTestBufferLen, test_buffer.begin());
40
41  MemoryInputStream is;
42  is.Attach(&(test_buffer[0]), kTestBufferLen);
43  EXPECT_EQ(is.Available(), (int32_t)kTestBufferLen);
44
45  // Read one byte
46  EXPECT_EQ(is.Read(), '0');  // position 1
47  EXPECT_EQ(is.Read(), '1');  // position 2
48  EXPECT_EQ(is.Read(), '2');  // position 3
49
50  // Read byte vector
51  ByteVector b;
52  b.resize(7);
53  EXPECT_EQ(is.Read(&b), 7);  // position 10
54  EXPECT_EQ(memcmp(&(b[0]), &(test_buffer[0]) + 3, 7), 0);
55
56  b.resize(17);
57  EXPECT_EQ(is.Read(&b, 7, 10), 10);  // position 20
58  EXPECT_EQ(memcmp(&(b[0]), &(test_buffer[0]) + 3, 17), 0);
59
60  // Test skip
61  b.clear();
62  b.resize(10);
63  EXPECT_EQ(is.Skip(30), 30);  // position 50
64  EXPECT_EQ(is.Read(&b), 10);  // position 60
65  EXPECT_EQ(memcmp(&(b[0]), &(test_buffer[0]) + 50, 10), 0);
66  b.clear();
67  b.resize(10);
68  EXPECT_EQ(is.Skip(-20), -20);  // position 40
69  EXPECT_EQ(is.Read(&b), 10);  // position 50
70  EXPECT_EQ(memcmp(&(b[0]), &(test_buffer[0]) + 40, 10), 0);
71
72  EXPECT_EQ(is.Available(), (int32_t)kTestBufferLen - 50);
73  EXPECT_EQ(is.Skip(-60), -50);  // Out of bound, position 0
74  EXPECT_EQ(is.Skip(kTestBufferLen + 10), (int32_t)kTestBufferLen);
75
76  b.clear();
77  b.resize(10);
78  is.Unread(&b);
79  EXPECT_EQ(memcmp(&(b[0]), &(test_buffer[0]) + kTestBufferLen - 10, 10), 0);
80
81  return true;
82}
83
84bool TestMemoryOutputStream() {
85  ByteVector test_buffer;
86  test_buffer.resize(kTestBufferLen);
87  std::copy(kTestData, kTestData + kTestBufferLen, test_buffer.begin());
88
89  MemoryOutputStream os;
90  os.Write(&(test_buffer[0]), (int32_t)50, (int32_t)(kTestBufferLen - 50));
91  EXPECT_EQ(os.Size(), kTestBufferLen - 50);
92  EXPECT_EQ(memcmp(os.Get(), &(test_buffer[0]) + 50, kTestBufferLen - 50), 0);
93
94  return true;
95}
96
97}  // namespace sfntly
98
99TEST(MemoryIO, All) {
100  ASSERT_TRUE(sfntly::TestMemoryInputStream());
101  ASSERT_TRUE(sfntly::TestMemoryOutputStream());
102}
103