1/*
2 * Copyright (C) 2012 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#define LOG_TAG "Vector_test"
18
19#define __STDC_LIMIT_MACROS
20#include <stdint.h>
21#include <utils/Vector.h>
22#include <cutils/log.h>
23#include <gtest/gtest.h>
24#include <unistd.h>
25
26namespace android {
27
28class VectorTest : public testing::Test {
29protected:
30    virtual void SetUp() {
31    }
32
33    virtual void TearDown() {
34    }
35
36public:
37};
38
39
40TEST_F(VectorTest, CopyOnWrite_CopyAndAddElements) {
41
42    Vector<int> vector;
43    Vector<int> other;
44    vector.setCapacity(8);
45
46    vector.add(1);
47    vector.add(2);
48    vector.add(3);
49
50    EXPECT_EQ(3U, vector.size());
51
52    // copy the vector
53    other = vector;
54
55    EXPECT_EQ(3U, other.size());
56
57    // add an element to the first vector
58    vector.add(4);
59
60    // make sure the sizes are correct
61    EXPECT_EQ(4U, vector.size());
62    EXPECT_EQ(3U, other.size());
63
64    // add an element to the copy
65    other.add(5);
66
67    // make sure the sizes are correct
68    EXPECT_EQ(4U, vector.size());
69    EXPECT_EQ(4U, other.size());
70
71    // make sure the content of both vectors are correct
72    EXPECT_EQ(vector[3], 4);
73    EXPECT_EQ(other[3], 5);
74}
75
76// TODO: gtest isn't capable of parsing Abort messages formatted by
77// Android (fails differently on host and target), so we always need to
78// use an empty error message for death tests.
79TEST_F(VectorTest, SetCapacity_Overflow) {
80  Vector<int> vector;
81  EXPECT_DEATH(vector.setCapacity(SIZE_MAX / sizeof(int) + 1), "");
82}
83
84TEST_F(VectorTest, SetCapacity_ShrinkBelowSize) {
85  Vector<int> vector;
86  vector.add(1);
87  vector.add(2);
88  vector.add(3);
89  vector.add(4);
90
91  vector.setCapacity(8);
92  ASSERT_EQ(8, vector.capacity());
93  vector.setCapacity(2);
94  ASSERT_EQ(8, vector.capacity());
95}
96
97// NOTE: All of the tests below are useless because of the "TODO" above.
98// We have no way of knowing *why* the process crashed. Given that we're
99// inserting a NULL array, we'll fail with a SIGSEGV eventually. We need
100// the ability to make assertions on the abort message to make sure we're
101// failing for the right reasons.
102TEST_F(VectorTest, _grow_OverflowSize) {
103  Vector<int> vector;
104  vector.add(1);
105
106  // Checks that the size calculation (not the capacity calculation) doesn't
107  // overflow : the size here will be (1 + SIZE_MAX).
108  //
109  // EXPECT_DEATH(vector.insertArrayAt(NULL, 0, SIZE_MAX), "new_size_overflow");
110  EXPECT_DEATH(vector.insertArrayAt(NULL, 0, SIZE_MAX), "");
111}
112
113TEST_F(VectorTest, _grow_OverflowCapacityDoubling) {
114  Vector<int> vector;
115
116  // This should fail because the calculated capacity will overflow even though
117  // the size of the vector doesn't.
118  //
119  // EXPECT_DEATH(vector.insertArrayAt(NULL, 0, (SIZE_MAX - 1)), "new_capacity_overflow");
120  EXPECT_DEATH(vector.insertArrayAt(NULL, 0, (SIZE_MAX - 1)), "");
121}
122
123TEST_F(VectorTest, _grow_OverflowBufferAlloc) {
124  Vector<int> vector;
125  // This should fail because the capacity * sizeof(int) overflows, even
126  // though the capacity itself doesn't.
127  //
128  // EXPECT_DEATH(vector.insertArrayAt(NULL, 0, (SIZE_MAX / 2)), "new_alloc_size overflow");
129  EXPECT_DEATH(vector.insertArrayAt(NULL, 0, (SIZE_MAX / 2)), "");
130}
131
132TEST_F(VectorTest, editArray_Shared) {
133  Vector<int> vector1;
134  vector1.add(1);
135  vector1.add(2);
136  vector1.add(3);
137  vector1.add(4);
138
139  Vector<int> vector2 = vector1;
140  ASSERT_EQ(vector1.array(), vector2.array());
141  // We must make a copy here, since we're not the exclusive owners
142  // of this array.
143  ASSERT_NE(vector1.editArray(), vector2.editArray());
144
145  // Vector doesn't implement operator ==.
146  ASSERT_EQ(vector1.size(), vector2.size());
147  for (size_t i = 0; i < vector1.size(); ++i) {
148    EXPECT_EQ(vector1[i], vector2[i]);
149  }
150}
151
152} // namespace android
153