| Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 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 | |
| Narayan Kamath | 419e6c3 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 19 | #define __STDC_LIMIT_MACROS |
| 20 | #include <stdint.h> |
| Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 23 | #include <android/log.h> |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <utils/Vector.h> |
| 26 | |
| Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | |
| 29 | class VectorTest : public testing::Test { |
| 30 | protected: |
| 31 | virtual void SetUp() { |
| 32 | } |
| 33 | |
| 34 | virtual void TearDown() { |
| 35 | } |
| 36 | |
| 37 | public: |
| 38 | }; |
| 39 | |
| 40 | |
| 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 | |
| Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 51 | EXPECT_EQ(3U, vector.size()); |
| Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 52 | |
| 53 | // copy the vector |
| 54 | other = vector; |
| 55 | |
| Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 56 | EXPECT_EQ(3U, other.size()); |
| Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 57 | |
| 58 | // add an element to the first vector |
| 59 | vector.add(4); |
| 60 | |
| 61 | // make sure the sizes are correct |
| Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 62 | EXPECT_EQ(4U, vector.size()); |
| 63 | EXPECT_EQ(3U, other.size()); |
| Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 64 | |
| 65 | // add an element to the copy |
| 66 | other.add(5); |
| 67 | |
| 68 | // make sure the sizes are correct |
| Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 69 | EXPECT_EQ(4U, vector.size()); |
| 70 | EXPECT_EQ(4U, other.size()); |
| Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 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 | |
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 77 | TEST_F(VectorTest, SetCapacity_Overflow) { |
| 78 | Vector<int> vector; |
| Elliott Hughes | 5bae1b8 | 2017-02-06 10:54:00 -0800 | [diff] [blame] | 79 | EXPECT_DEATH(vector.setCapacity(SIZE_MAX / sizeof(int) + 1), "Assertion failed"); |
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 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); |
| James Hawkins | b898075 | 2016-03-01 11:21:53 -0800 | [diff] [blame] | 90 | ASSERT_EQ(8U, vector.capacity()); |
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 91 | vector.setCapacity(2); |
| James Hawkins | b898075 | 2016-03-01 11:21:53 -0800 | [diff] [blame] | 92 | ASSERT_EQ(8U, vector.capacity()); |
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 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). |
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 101 | EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, SIZE_MAX), "new_size overflow"); |
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 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. |
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 109 | EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, (SIZE_MAX - 1)), "new_capacity overflow"); |
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 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. |
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 116 | EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, (SIZE_MAX / 2)), "new_alloc_size overflow"); |
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 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 | } |
| Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 138 | |
| 139 | } // namespace android |