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/paged_array.h" | 5 #include "courgette/third_party/paged_array.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
8 | 10 |
9 class PagedArrayTest : public testing::Test { | 11 class PagedArrayTest : public testing::Test { |
10 public: | 12 public: |
11 // Total allocation of 4GB will fail in 32 bit programs if allocations are | 13 // Total allocation of 4GB will fail in 32 bit programs if allocations are |
12 // leaked. | 14 // leaked. |
13 static const int kIterations = 20; | 15 static const int kIterations = 20; |
14 static const int kSize = 200 * 1024 * 1024 / sizeof(int); // 200MB | 16 static const int kSize = 200 * 1024 * 1024 / sizeof(int); // 200MB |
15 }; | 17 }; |
16 | 18 |
(...skipping 20 matching lines...) Expand all Loading... |
37 const int kAccessSize = 3 * 1024 * 1024; | 39 const int kAccessSize = 3 * 1024 * 1024; |
38 courgette::PagedArray<int> a; | 40 courgette::PagedArray<int> a; |
39 a.Allocate(kAccessSize); | 41 a.Allocate(kAccessSize); |
40 for (int i = 0; i < kAccessSize; ++i) { | 42 for (int i = 0; i < kAccessSize; ++i) { |
41 a[i] = i; | 43 a[i] = i; |
42 } | 44 } |
43 for (int i = 0; i < kAccessSize; ++i) { | 45 for (int i = 0; i < kAccessSize; ++i) { |
44 EXPECT_EQ(i, a[i]); | 46 EXPECT_EQ(i, a[i]); |
45 } | 47 } |
46 } | 48 } |
| 49 |
| 50 TEST_F(PagedArrayTest, TestIterator) { |
| 51 const int kIterSize = 1024 * 1024; |
| 52 courgette::PagedArray<int> a; |
| 53 EXPECT_TRUE(a.begin() == a.end()); |
| 54 EXPECT_FALSE(a.begin() != a.end()); |
| 55 EXPECT_FALSE(a.begin() < a.end()); |
| 56 EXPECT_FALSE(a.end() < a.begin()); |
| 57 |
| 58 a.Allocate(kIterSize); |
| 59 EXPECT_FALSE(a.begin() == a.end()); |
| 60 EXPECT_TRUE(a.begin() != a.end()); |
| 61 EXPECT_TRUE(a.begin() < a.end()); |
| 62 EXPECT_FALSE(a.end() < a.begin()); |
| 63 EXPECT_EQ(kIterSize, a.end() - a.begin()); |
| 64 EXPECT_TRUE(a.begin() + kIterSize == a.end()); |
| 65 |
| 66 for (int i = 0; i < kIterSize; ++i) { |
| 67 a[i] = i; |
| 68 } |
| 69 std::reverse(a.begin(), a.end()); |
| 70 for (auto it = a.begin(); it != a.end(); ++it) { |
| 71 EXPECT_EQ(a.end() - it - 1, *it); |
| 72 } |
| 73 } |
| 74 |
| 75 TEST_F(PagedArrayTest, TestSort) { |
| 76 const int kPrimeSize = 300007; |
| 77 courgette::PagedArray<int> a; |
| 78 a.Allocate(kPrimeSize); |
| 79 EXPECT_EQ(kPrimeSize, a.end() - a.begin()); |
| 80 for (int i = 0; i < kPrimeSize; ++i) { |
| 81 a[i] = (i + 11) * 1031 % kPrimeSize; // Produces distinct elements. |
| 82 } |
| 83 std::sort(a.begin(), a.end()); |
| 84 for (int i = 0; i < kPrimeSize; i += 1000) { |
| 85 EXPECT_EQ(i, *(a.begin() + i)); |
| 86 } |
| 87 } |
OLD | NEW |