blob: 5336c40c368b5b321e412795d0893a71bbe4ca3e [file] [log] [blame]
Mathias Agopian03b168a2012-05-17 16:52:21 -07001/*
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
Narayan Kamath419e6c32015-08-28 12:59:48 +010019#define __STDC_LIMIT_MACROS
20#include <stdint.h>
Mathias Agopian03b168a2012-05-17 16:52:21 -070021#include <unistd.h>
22
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070023#include <android/log.h>
24#include <gtest/gtest.h>
25#include <utils/Vector.h>
26
Mathias Agopian03b168a2012-05-17 16:52:21 -070027namespace android {
28
29class VectorTest : public testing::Test {
30protected:
31 virtual void SetUp() {
32 }
33
34 virtual void TearDown() {
35 }
36
37public:
38};
39
40
41TEST_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
Nick Kralevich3e6c4512015-08-17 18:26:13 -070051 EXPECT_EQ(3U, vector.size());
Mathias Agopian03b168a2012-05-17 16:52:21 -070052
53 // copy the vector
54 other = vector;
55
Nick Kralevich3e6c4512015-08-17 18:26:13 -070056 EXPECT_EQ(3U, other.size());
Mathias Agopian03b168a2012-05-17 16:52:21 -070057
58 // add an element to the first vector
59 vector.add(4);
60
61 // make sure the sizes are correct
Nick Kralevich3e6c4512015-08-17 18:26:13 -070062 EXPECT_EQ(4U, vector.size());
63 EXPECT_EQ(3U, other.size());
Mathias Agopian03b168a2012-05-17 16:52:21 -070064
65 // add an element to the copy
66 other.add(5);
67
68 // make sure the sizes are correct
Nick Kralevich3e6c4512015-08-17 18:26:13 -070069 EXPECT_EQ(4U, vector.size());
70 EXPECT_EQ(4U, other.size());
Mathias Agopian03b168a2012-05-17 16:52:21 -070071
72 // make sure the content of both vectors are correct
73 EXPECT_EQ(vector[3], 4);
74 EXPECT_EQ(other[3], 5);
75}
76
Narayan Kamathc609c312015-08-28 12:59:48 +010077TEST_F(VectorTest, SetCapacity_Overflow) {
78 Vector<int> vector;
Elliott Hughes5bae1b82017-02-06 10:54:00 -080079 EXPECT_DEATH(vector.setCapacity(SIZE_MAX / sizeof(int) + 1), "Assertion failed");
Narayan Kamathc609c312015-08-28 12:59:48 +010080}
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);
James Hawkinsb8980752016-03-01 11:21:53 -080090 ASSERT_EQ(8U, vector.capacity());
Narayan Kamathc609c312015-08-28 12:59:48 +010091 vector.setCapacity(2);
James Hawkinsb8980752016-03-01 11:21:53 -080092 ASSERT_EQ(8U, vector.capacity());
Narayan Kamathc609c312015-08-28 12:59:48 +010093}
94
Narayan Kamathc609c312015-08-28 12:59:48 +010095TEST_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).
Yi Konge1731a42018-07-16 18:11:34 -0700101 EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, SIZE_MAX), "new_size overflow");
Narayan Kamathc609c312015-08-28 12:59:48 +0100102}
103
104TEST_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.
Yi Konge1731a42018-07-16 18:11:34 -0700109 EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, (SIZE_MAX - 1)), "new_capacity overflow");
Narayan Kamathc609c312015-08-28 12:59:48 +0100110}
111
112TEST_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.
Yi Konge1731a42018-07-16 18:11:34 -0700116 EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, (SIZE_MAX / 2)), "new_alloc_size overflow");
Narayan Kamathc609c312015-08-28 12:59:48 +0100117}
118
119TEST_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}
Mathias Agopian03b168a2012-05-17 16:52:21 -0700138
139} // namespace android