OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "courgette/third_party/bsdiff/paged_array.h" | 5 #include "courgette/third_party/bsdiff/paged_array.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <algorithm> |
| 10 #include <iterator> |
| 11 #include <random> |
| 12 #include <vector> |
| 13 |
7 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
8 | 15 |
| 16 namespace { |
| 17 |
| 18 // Total allocation of 4GB will fail in 32 bit programs if allocations are |
| 19 // leaked. |
| 20 const int kIterations = 20; |
| 21 const int kSizeBig = 200 * 1024 * 1024 / sizeof(int); // 200MB |
| 22 |
| 23 const size_t kLogBlockSizeSmall = 10; |
| 24 const size_t kBlockSizeSmall = 1 << kLogBlockSizeSmall; |
| 25 const size_t kSizeList[] = { |
| 26 1, |
| 27 16, |
| 28 123, |
| 29 kBlockSizeSmall, |
| 30 kBlockSizeSmall + 1, |
| 31 123 * kBlockSizeSmall + 567 |
| 32 }; |
| 33 |
| 34 const size_t kIteratorTestDataSize = 7; |
| 35 const int32_t kIteratorTestData[kIteratorTestDataSize] = { |
| 36 2, 3, 5, 7, 11, 13, 17}; |
| 37 |
| 38 } // namespace |
| 39 |
9 class PagedArrayTest : public testing::Test { | 40 class PagedArrayTest : public testing::Test { |
10 public: | 41 public: |
11 // Total allocation of 4GB will fail in 32 bit programs if allocations are | 42 |
12 // leaked. | 43 template <typename Iterator> |
13 static const int kIterations = 20; | 44 void TestIteratorBasic(Iterator begin, Iterator end) { |
14 static const int kSize = 200 * 1024 * 1024 / sizeof(int); // 200MB | 45 EXPECT_FALSE(begin == nullptr); |
| 46 EXPECT_FALSE(end == nullptr); |
| 47 |
| 48 // Test TestPagedArray::iterator data read. |
| 49 Iterator it = begin; |
| 50 EXPECT_EQ(2, *it); |
| 51 EXPECT_EQ(3, *(it + 1)); |
| 52 EXPECT_EQ(5, it[2]); // Pseudo-pointer. |
| 53 EXPECT_EQ(7, (it + 1)[2]); |
| 54 EXPECT_EQ(3, *(++it)); |
| 55 EXPECT_EQ(3, *it); |
| 56 EXPECT_EQ(3, *(it++)); |
| 57 EXPECT_EQ(5, *it); |
| 58 EXPECT_EQ(11, *(it += 2)); |
| 59 EXPECT_EQ(11, *it); |
| 60 EXPECT_FALSE(it == begin); |
| 61 EXPECT_TRUE(it != begin); |
| 62 EXPECT_TRUE(it == (begin + 4)); |
| 63 EXPECT_FALSE(it != (begin + 4)); |
| 64 EXPECT_EQ(static_cast<ptrdiff_t>(kIteratorTestDataSize), end - begin); |
| 65 it = begin + 5; |
| 66 EXPECT_EQ(begin, it - 5); |
| 67 EXPECT_EQ(13, *it); |
| 68 EXPECT_EQ(11, *(--it)); |
| 69 EXPECT_EQ(11, *it); |
| 70 EXPECT_EQ(11, *(it--)); |
| 71 EXPECT_EQ(7, *it); |
| 72 EXPECT_EQ(3, *(it -= 2)); |
| 73 EXPECT_EQ(3, *it); |
| 74 |
| 75 // Test binary operators for every pair of iterator. |
| 76 EXPECT_TRUE(begin <= end); |
| 77 EXPECT_TRUE(begin < end); |
| 78 for (size_t i = 0; i < kIteratorTestDataSize; ++i) { |
| 79 Iterator it1 = begin + i; |
| 80 EXPECT_TRUE(it1 == it1); |
| 81 EXPECT_FALSE(it1 != it1); |
| 82 EXPECT_TRUE(begin <= it1); |
| 83 EXPECT_EQ(begin, it1 - i); |
| 84 EXPECT_EQ(end, it1 + (kIteratorTestDataSize - i)); |
| 85 EXPECT_EQ(kIteratorTestData[i], *it1); |
| 86 EXPECT_EQ(kIteratorTestData[i], begin[i]); |
| 87 for (size_t j = 0; i + j < kIteratorTestDataSize; ++j) { |
| 88 Iterator it2 = it1 + j; |
| 89 EXPECT_TRUE(it1 <= it2); |
| 90 EXPECT_FALSE(it2 < it1); |
| 91 if (j > 0) { |
| 92 EXPECT_TRUE(it1 < it2); |
| 93 EXPECT_FALSE(it2 <= it1); |
| 94 EXPECT_TRUE(it1 != it2); |
| 95 } |
| 96 EXPECT_EQ(kIteratorTestData[i + j], it1[j]); // Pseudo-pointer. |
| 97 EXPECT_EQ(kIteratorTestData[i + j], *it2); |
| 98 EXPECT_EQ(static_cast<ptrdiff_t>(j), it2 - it1); |
| 99 EXPECT_EQ(it1, it2 - j); |
| 100 } |
| 101 EXPECT_TRUE(it1 < end); |
| 102 EXPECT_TRUE(it1 <= end); |
| 103 EXPECT_TRUE(it1 != end); |
| 104 } |
| 105 |
| 106 // Test initialize with null. |
| 107 Iterator it_dummy; |
| 108 it_dummy = nullptr; |
| 109 EXPECT_TRUE(it_dummy == nullptr); |
| 110 |
| 111 // Test copy constructor. |
| 112 Iterator begin_copy(begin); |
| 113 EXPECT_TRUE(begin_copy == begin); |
| 114 |
| 115 // Test STL read-only usage. |
| 116 std::vector<typename std::iterator_traits<Iterator>::value_type> v(begin, en
d); |
| 117 EXPECT_TRUE(std::equal(begin, end, v.begin())); |
| 118 EXPECT_TRUE(std::equal(v.begin(), v.end(), begin)); |
| 119 } |
15 }; | 120 }; |
16 | 121 |
17 // AddressSanitizer on Windows adds additional memory overhead, which | 122 // AddressSanitizer on Windows adds additional memory overhead, which |
18 // causes these tests to go OOM and fail. | 123 // causes these tests to go OOM and fail. |
19 #if !defined(ADDRESS_SANITIZER) || !defined(OS_WIN) | 124 #if !defined(ADDRESS_SANITIZER) || !defined(OS_WIN) |
20 TEST_F(PagedArrayTest, TestManyAllocationsDestructorFree) { | 125 TEST_F(PagedArrayTest, TestManyAllocationsDestructorFree) { |
21 for (int i = 0; i < kIterations; ++i) { | 126 for (int i = 0; i < kIterations; ++i) { |
22 courgette::PagedArray<int> a; | 127 courgette::PagedArray<int> a; |
23 EXPECT_TRUE(a.Allocate(kSize)); | 128 EXPECT_TRUE(a.Allocate(kSizeBig)); |
24 } | 129 } |
25 } | 130 } |
26 | 131 |
27 TEST_F(PagedArrayTest, TestManyAllocationsManualFree) { | 132 TEST_F(PagedArrayTest, TestManyAllocationsManualFree) { |
28 courgette::PagedArray<int> a; | 133 courgette::PagedArray<int> a; |
29 for (int i = 0; i < kIterations; ++i) { | 134 for (int i = 0; i < kIterations; ++i) { |
30 EXPECT_TRUE(a.Allocate(kSize)); | 135 EXPECT_TRUE(a.Allocate(kSizeBig)); |
31 a.clear(); | 136 a.clear(); |
32 } | 137 } |
33 } | 138 } |
34 #endif | 139 #endif |
35 | 140 |
36 TEST_F(PagedArrayTest, TestAccess) { | 141 TEST_F(PagedArrayTest, TestAccess) { |
37 const int kAccessSize = 3 * 1024 * 1024; | 142 for (size_t size : kSizeList) { |
38 courgette::PagedArray<int> a; | 143 courgette::PagedArray<int32_t, kLogBlockSizeSmall> a; |
39 a.Allocate(kAccessSize); | 144 EXPECT_TRUE(a.Allocate(size)); |
40 for (int i = 0; i < kAccessSize; ++i) { | 145 for (size_t i = 0; i < size; ++i) |
41 a[i] = i; | 146 a[i] = i; |
42 } | 147 for (size_t i = 0; i < size; ++i) |
43 for (int i = 0; i < kAccessSize; ++i) { | 148 EXPECT_EQ(static_cast<int32_t>(i), a[i]); |
44 EXPECT_EQ(i, a[i]); | |
45 } | 149 } |
46 } | 150 } |
| 151 |
| 152 TEST_F(PagedArrayTest, TestIterator) { |
| 153 using TestPagedArray = courgette::PagedArray<int32_t, 1>; // Page size = 2. |
| 154 |
| 155 TestPagedArray::const_iterator uninit_const_it; |
| 156 EXPECT_TRUE(uninit_const_it == nullptr); |
| 157 |
| 158 TestPagedArray::iterator uninit_it; |
| 159 EXPECT_TRUE(uninit_it == nullptr); |
| 160 |
| 161 TestPagedArray a; |
| 162 EXPECT_TRUE(a.Allocate(kIteratorTestDataSize)); |
| 163 std::copy(kIteratorTestData, kIteratorTestData + kIteratorTestDataSize, |
| 164 a.begin()); |
| 165 const TestPagedArray& a_const = a; |
| 166 |
| 167 // Test TestPagedArray::const_iterator. |
| 168 TestIteratorBasic(a_const.begin(), a_const.end()); |
| 169 |
| 170 // Test TestPagedArray::iterator. |
| 171 TestIteratorBasic(a.begin(), a.end()); |
| 172 |
| 173 // Test equality of const and non-const. |
| 174 EXPECT_TRUE(a.begin() == a_const.begin()); |
| 175 EXPECT_TRUE(a_const.begin() == a.begin()); |
| 176 |
| 177 // Test casting from non-const to const. |
| 178 TestPagedArray::iterator non_const_it = a.begin(); |
| 179 EXPECT_TRUE(non_const_it == a.begin()); |
| 180 TestPagedArray::const_iterator const_it = non_const_it; |
| 181 EXPECT_TRUE(const_it == non_const_it); |
| 182 // The following should and will emit compile error: |
| 183 // non_const_it = a_const.begin(); |
| 184 |
| 185 // Test copy constructor from non-const to const. |
| 186 TestPagedArray::iterator const_it2(a.begin()); |
| 187 EXPECT_TRUE(const_it2 == a.begin()); |
| 188 |
| 189 // Test pointer distance from non-const to const. |
| 190 EXPECT_EQ(static_cast<ptrdiff_t>(kIteratorTestDataSize), |
| 191 a.end() - a_const.begin()); |
| 192 EXPECT_EQ(static_cast<ptrdiff_t>(kIteratorTestDataSize), |
| 193 a_const.end() - a.begin()); |
| 194 |
| 195 // Test operator->(). |
| 196 struct TestStruct { |
| 197 int32_t value = 0; |
| 198 }; |
| 199 using TestStructPagedArray = courgette::PagedArray<TestStruct, 1>; |
| 200 TestStructPagedArray b; |
| 201 b.Allocate(3); |
| 202 b[0].value = 100; |
| 203 b[1].value = 200; |
| 204 b[2].value = 300; |
| 205 const TestStructPagedArray& b_const = b; |
| 206 |
| 207 EXPECT_EQ(100, b.begin()->value); |
| 208 EXPECT_EQ(100, b_const.begin()->value); |
| 209 EXPECT_EQ(200, (b.begin() + 1)->value); |
| 210 EXPECT_EQ(200, (b_const.begin() + 1)->value); |
| 211 (b.begin() + 2)->value *= -1; |
| 212 EXPECT_EQ(-300, (b.begin() + 2)->value); |
| 213 EXPECT_EQ(-300, (b_const.begin() + 2)->value); |
| 214 } |
| 215 |
| 216 // Test generic read-write of itrators by sorting pseudo-random numbers. |
| 217 TEST_F(PagedArrayTest, TestSort) { |
| 218 courgette::PagedArray<int> a; |
| 219 std::minstd_rand pseudo_rand_gen; // Deterministic, using defaults. |
| 220 for (size_t size : kSizeList) { |
| 221 std::vector<int32_t> v(size); |
| 222 courgette::PagedArray<int32_t, kLogBlockSizeSmall> a; |
| 223 EXPECT_TRUE(a.Allocate(size)); |
| 224 for (size_t i = 0; i < size; ++i) { |
| 225 v[i] = pseudo_rand_gen(); |
| 226 a[i] = v[i]; |
| 227 } |
| 228 std::sort(v.begin(), v.end()); |
| 229 std::sort(a.begin(), a.end()); |
| 230 for (size_t i = 0; i < size; ++i) |
| 231 EXPECT_EQ(v[i], a[i]); |
| 232 } |
| 233 } |
OLD | NEW |