Vector_test.cpp 3.5 KB

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