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 <stdint.h>
20 #include <unistd.h>
21 
22 #include <android/log.h>
23 #include <gtest/gtest.h>
24 #include <utils/Vector.h>
25 
26 namespace android {
27 
28 class VectorTest : public testing::Test {
29 protected:
SetUp()30     virtual void SetUp() {
31     }
32 
TearDown()33     virtual void TearDown() {
34     }
35 
36 public:
37 };
38 
39 
TEST_F(VectorTest,CopyOnWrite_CopyAndAddElements)40 TEST_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 
TEST_F(VectorTest,SetCapacity_Overflow)76 TEST_F(VectorTest, SetCapacity_Overflow) {
77   Vector<int> vector;
78   EXPECT_DEATH(vector.setCapacity(SIZE_MAX / sizeof(int) + 1), "Assertion failed");
79 }
80 
TEST_F(VectorTest,SetCapacity_ShrinkBelowSize)81 TEST_F(VectorTest, SetCapacity_ShrinkBelowSize) {
82   Vector<int> vector;
83   vector.add(1);
84   vector.add(2);
85   vector.add(3);
86   vector.add(4);
87 
88   vector.setCapacity(8);
89   ASSERT_EQ(8U, vector.capacity());
90   vector.setCapacity(2);
91   ASSERT_EQ(8U, vector.capacity());
92 }
93 
TEST_F(VectorTest,_grow_OverflowSize)94 TEST_F(VectorTest, _grow_OverflowSize) {
95   Vector<int> vector;
96   vector.add(1);
97 
98   // Checks that the size calculation (not the capacity calculation) doesn't
99   // overflow : the size here will be (1 + SIZE_MAX).
100   EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, SIZE_MAX), "new_size overflow");
101 }
102 
TEST_F(VectorTest,_grow_OverflowCapacityDoubling)103 TEST_F(VectorTest, _grow_OverflowCapacityDoubling) {
104   Vector<int> vector;
105 
106   // This should fail because the calculated capacity will overflow even though
107   // the size of the vector doesn't.
108   EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, (SIZE_MAX - 1)), "new_capacity overflow");
109 }
110 
TEST_F(VectorTest,_grow_OverflowBufferAlloc)111 TEST_F(VectorTest, _grow_OverflowBufferAlloc) {
112   Vector<int> vector;
113   // This should fail because the capacity * sizeof(int) overflows, even
114   // though the capacity itself doesn't.
115   EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, (SIZE_MAX / 2)), "new_alloc_size overflow");
116 }
117 
TEST_F(VectorTest,editArray_Shared)118 TEST_F(VectorTest, editArray_Shared) {
119   Vector<int> vector1;
120   vector1.add(1);
121   vector1.add(2);
122   vector1.add(3);
123   vector1.add(4);
124 
125   Vector<int> vector2 = vector1;
126   ASSERT_EQ(vector1.array(), vector2.array());
127   // We must make a copy here, since we're not the exclusive owners
128   // of this array.
129   ASSERT_NE(vector1.editArray(), vector2.editArray());
130 
131   // Vector doesn't implement operator ==.
132   ASSERT_EQ(vector1.size(), vector2.size());
133   for (size_t i = 0; i < vector1.size(); ++i) {
134     EXPECT_EQ(vector1[i], vector2[i]);
135   }
136 }
137 
TEST_F(VectorTest,removeItemsAt_overflow)138 TEST_F(VectorTest, removeItemsAt_overflow) {
139     android::Vector<int> v;
140     for (int i = 0; i < 666; i++) v.add(i);
141 
142     ASSERT_DEATH(v.removeItemsAt(SIZE_MAX, 666), "overflow");
143     ASSERT_DEATH(v.removeItemsAt(666, SIZE_MAX), "overflow");
144     ASSERT_DEATH(v.removeItemsAt(SIZE_MAX, SIZE_MAX), "overflow");
145 }
146 
147 } // namespace android
148