OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 <set> | 5 #include <set> |
6 | 6 |
7 #include "cc/base/scoped_ptr_vector.h" | 7 #include "cc/base/scoped_ptr_vector.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
9 | 9 |
10 namespace cc { | 10 namespace cc { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 EXPECT_EQ(3u, odd_numbers.size()); | 94 EXPECT_EQ(3u, odd_numbers.size()); |
95 | 95 |
96 std::set<int> even_numbers; | 96 std::set<int> even_numbers; |
97 for (; it != v.end(); ++it) { | 97 for (; it != v.end(); ++it) { |
98 EXPECT_EQ(0, (*it)->data() % 2); | 98 EXPECT_EQ(0, (*it)->data() % 2); |
99 even_numbers.insert((*it)->data()); | 99 even_numbers.insert((*it)->data()); |
100 } | 100 } |
101 EXPECT_EQ(2u, even_numbers.size()); | 101 EXPECT_EQ(2u, even_numbers.size()); |
102 } | 102 } |
103 | 103 |
| 104 class DataWithDestruction { |
| 105 public: |
| 106 static scoped_ptr<DataWithDestruction> Create(int i, int* destroy_count) { |
| 107 return make_scoped_ptr(new DataWithDestruction(i, destroy_count)); |
| 108 } |
| 109 int data() const { return data_; } |
| 110 ~DataWithDestruction() { ++(*destroy_count_); } |
| 111 |
| 112 private: |
| 113 explicit DataWithDestruction(int i, int* destroy_count) |
| 114 : data_(i), destroy_count_(destroy_count) {} |
| 115 int data_; |
| 116 int* destroy_count_; |
| 117 }; |
| 118 |
| 119 TEST(ScopedPtrVectorTest, RemoveIf) { |
| 120 ScopedPtrVector<DataWithDestruction> v; |
| 121 int destroyed[6] = {0}; |
| 122 v.push_back(DataWithDestruction::Create(1, &destroyed[0])); |
| 123 v.push_back(DataWithDestruction::Create(2, &destroyed[1])); |
| 124 v.push_back(DataWithDestruction::Create(3, &destroyed[2])); |
| 125 v.push_back(DataWithDestruction::Create(3, &destroyed[3])); |
| 126 v.push_back(DataWithDestruction::Create(4, &destroyed[4])); |
| 127 v.push_back(DataWithDestruction::Create(5, &destroyed[5])); |
| 128 |
| 129 int expect_destroyed[6] = {0}; |
| 130 |
| 131 // Removing more than one thing that matches. |
| 132 auto is_three = [](DataWithDestruction* d) { return d->data() == 3; }; |
| 133 v.erase(v.remove_if(is_three), v.end()); |
| 134 EXPECT_EQ(4u, v.size()); |
| 135 expect_destroyed[2]++; |
| 136 expect_destroyed[3]++; |
| 137 for (size_t i = 0; i < arraysize(destroyed); ++i) |
| 138 EXPECT_EQ(expect_destroyed[i], destroyed[i]) << i; |
| 139 { |
| 140 int expect_data[4] = {1, 2, 4, 5}; |
| 141 for (size_t i = 0; i < arraysize(expect_data); ++i) |
| 142 EXPECT_EQ(expect_data[i], v[i]->data()) << i; |
| 143 } |
| 144 |
| 145 // Removing from the back of the vector. |
| 146 auto is_five = [](DataWithDestruction* d) { return d->data() == 5; }; |
| 147 v.erase(v.remove_if(is_five), v.end()); |
| 148 EXPECT_EQ(3u, v.size()); |
| 149 expect_destroyed[5]++; |
| 150 for (size_t i = 0; i < arraysize(destroyed); ++i) |
| 151 EXPECT_EQ(expect_destroyed[i], destroyed[i]) << i; |
| 152 { |
| 153 int expect_data[3] = {1, 2, 4}; |
| 154 for (size_t i = 0; i < arraysize(expect_data); ++i) |
| 155 EXPECT_EQ(expect_data[i], v[i]->data()) << i; |
| 156 } |
| 157 |
| 158 // Removing from the front of the vector. |
| 159 auto is_one = [](DataWithDestruction* d) { return d->data() == 1; }; |
| 160 v.erase(v.remove_if(is_one), v.end()); |
| 161 EXPECT_EQ(2u, v.size()); |
| 162 expect_destroyed[0]++; |
| 163 for (size_t i = 0; i < arraysize(destroyed); ++i) |
| 164 EXPECT_EQ(expect_destroyed[i], destroyed[i]) << i; |
| 165 { |
| 166 int expect_data[2] = {2, 4}; |
| 167 for (size_t i = 0; i < arraysize(expect_data); ++i) |
| 168 EXPECT_EQ(expect_data[i], v[i]->data()) << i; |
| 169 } |
| 170 |
| 171 // Removing things that aren't in the vector does nothing. |
| 172 v.erase(v.remove_if(is_one), v.end()); |
| 173 EXPECT_EQ(2u, v.size()); |
| 174 for (size_t i = 0; i < arraysize(destroyed); ++i) |
| 175 EXPECT_EQ(expect_destroyed[i], destroyed[i]) << i; |
| 176 { |
| 177 int expect_data[2] = {2, 4}; |
| 178 for (size_t i = 0; i < arraysize(expect_data); ++i) |
| 179 EXPECT_EQ(expect_data[i], v[i]->data()) << i; |
| 180 } |
| 181 } |
| 182 |
104 } // namespace | 183 } // namespace |
105 } // namespace cc | 184 } // namespace cc |
OLD | NEW |