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 <unistd.h>
22
23 #include <android/log.h>
24 #include <gtest/gtest.h>
25 #include <utils/Vector.h>
26
27 namespace android {
28
29 class VectorTest : public testing::Test {
30 protected:
SetUp()31 virtual void SetUp() {
32 }
33
TearDown()34 virtual void TearDown() {
35 }
36
37 public:
38 };
39
40
TEST_F(VectorTest,CopyOnWrite_CopyAndAddElements)41 TEST_F(VectorTest, CopyOnWrite_CopyAndAddElements) {
42
43 Vector<int> vector;
44 Vector<int> other;
45 vector.setCapacity(8);
46
47 vector.add(1);
48 vector.add(2);
49 vector.add(3);
50
51 EXPECT_EQ(3U, vector.size());
52
53 // copy the vector
54 other = vector;
55
56 EXPECT_EQ(3U, other.size());
57
58 // add an element to the first vector
59 vector.add(4);
60
61 // make sure the sizes are correct
62 EXPECT_EQ(4U, vector.size());
63 EXPECT_EQ(3U, other.size());
64
65 // add an element to the copy
66 other.add(5);
67
68 // make sure the sizes are correct
69 EXPECT_EQ(4U, vector.size());
70 EXPECT_EQ(4U, other.size());
71
72 // make sure the content of both vectors are correct
73 EXPECT_EQ(vector[3], 4);
74 EXPECT_EQ(other[3], 5);
75 }
76
TEST_F(VectorTest,SetCapacity_Overflow)77 TEST_F(VectorTest, SetCapacity_Overflow) {
78 Vector<int> vector;
79 EXPECT_DEATH(vector.setCapacity(SIZE_MAX / sizeof(int) + 1), "Assertion failed");
80 }
81
TEST_F(VectorTest,SetCapacity_ShrinkBelowSize)82 TEST_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(8U, vector.capacity());
91 vector.setCapacity(2);
92 ASSERT_EQ(8U, vector.capacity());
93 }
94
TEST_F(VectorTest,_grow_OverflowSize)95 TEST_F(VectorTest, _grow_OverflowSize) {
96 Vector<int> vector;
97 vector.add(1);
98
99 // Checks that the size calculation (not the capacity calculation) doesn't
100 // overflow : the size here will be (1 + SIZE_MAX).
101 EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, SIZE_MAX), "new_size overflow");
102 }
103
TEST_F(VectorTest,_grow_OverflowCapacityDoubling)104 TEST_F(VectorTest, _grow_OverflowCapacityDoubling) {
105 Vector<int> vector;
106
107 // This should fail because the calculated capacity will overflow even though
108 // the size of the vector doesn't.
109 EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, (SIZE_MAX - 1)), "new_capacity overflow");
110 }
111
TEST_F(VectorTest,_grow_OverflowBufferAlloc)112 TEST_F(VectorTest, _grow_OverflowBufferAlloc) {
113 Vector<int> vector;
114 // This should fail because the capacity * sizeof(int) overflows, even
115 // though the capacity itself doesn't.
116 EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, (SIZE_MAX / 2)), "new_alloc_size overflow");
117 }
118
TEST_F(VectorTest,editArray_Shared)119 TEST_F(VectorTest, editArray_Shared) {
120 Vector<int> vector1;
121 vector1.add(1);
122 vector1.add(2);
123 vector1.add(3);
124 vector1.add(4);
125
126 Vector<int> vector2 = vector1;
127 ASSERT_EQ(vector1.array(), vector2.array());
128 // We must make a copy here, since we're not the exclusive owners
129 // of this array.
130 ASSERT_NE(vector1.editArray(), vector2.editArray());
131
132 // Vector doesn't implement operator ==.
133 ASSERT_EQ(vector1.size(), vector2.size());
134 for (size_t i = 0; i < vector1.size(); ++i) {
135 EXPECT_EQ(vector1[i], vector2[i]);
136 }
137 }
138
139 } // namespace android
140