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