| 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 "base/stl_util.h" | 5 #include "base/stl_util.h" |
| 6 | 6 |
| 7 #include <deque> |
| 8 #include <forward_list> |
| 9 #include <functional> |
| 10 #include <iterator> |
| 11 #include <list> |
| 12 #include <map> |
| 7 #include <set> | 13 #include <set> |
| 14 #include <string> |
| 15 #include <unordered_map> |
| 16 #include <unordered_set> |
| 17 #include <vector> |
| 8 | 18 |
| 19 #include "base/strings/string16.h" |
| 20 #include "base/strings/utf_string_conversions.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 22 |
| 11 namespace { | 23 namespace { |
| 12 | 24 |
| 13 // Used as test case to ensure the various base::STLXxx functions don't require | 25 // Used as test case to ensure the various base::STLXxx functions don't require |
| 14 // more than operators "<" and "==" on values stored in containers. | 26 // more than operators "<" and "==" on values stored in containers. |
| 15 class ComparableValue { | 27 class ComparableValue { |
| 16 public: | 28 public: |
| 17 explicit ComparableValue(int value) : value_(value) {} | 29 explicit ComparableValue(int value) : value_(value) {} |
| 18 | 30 |
| 19 bool operator==(const ComparableValue& rhs) const { | 31 bool operator==(const ComparableValue& rhs) const { |
| 20 return value_ == rhs.value_; | 32 return value_ == rhs.value_; |
| 21 } | 33 } |
| 22 | 34 |
| 23 bool operator<(const ComparableValue& rhs) const { | 35 bool operator<(const ComparableValue& rhs) const { |
| 24 return value_ < rhs.value_; | 36 return value_ < rhs.value_; |
| 25 } | 37 } |
| 26 | 38 |
| 27 private: | 39 private: |
| 28 int value_; | 40 int value_; |
| 29 }; | 41 }; |
| 30 | 42 |
| 43 template <typename Container> |
| 44 void RunEraseTest() { |
| 45 const std::pair<Container, Container> test_data[] = { |
| 46 {Container(), Container()}, {{1, 2, 3}, {1, 3}}, {{1, 2, 3, 2}, {1, 3}}}; |
| 47 |
| 48 for (auto test_case : test_data) { |
| 49 base::Erase(test_case.first, 2); |
| 50 EXPECT_EQ(test_case.second, test_case.first); |
| 51 } |
| 52 } |
| 53 |
| 54 // This test is written for containers of std::pair<int, int> to support maps. |
| 55 template <typename Container> |
| 56 void RunEraseIfTest() { |
| 57 struct { |
| 58 Container input; |
| 59 Container erase_even; |
| 60 Container erase_odd; |
| 61 } test_data[] = { |
| 62 {Container(), Container(), Container()}, |
| 63 {{{1, 1}, {2, 2}, {3, 3}}, {{1, 1}, {3, 3}}, {{2, 2}}}, |
| 64 {{{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {3, 3}}, {{2, 2}, {4, 4}}}, |
| 65 }; |
| 66 |
| 67 for (auto test_case : test_data) { |
| 68 base::EraseIf(test_case.input, [](const std::pair<int, int>& elem) { |
| 69 return !(elem.first & 1); |
| 70 }); |
| 71 EXPECT_EQ(test_case.erase_even, test_case.input); |
| 72 } |
| 73 |
| 74 for (auto test_case : test_data) { |
| 75 base::EraseIf(test_case.input, [](const std::pair<int, int>& elem) { |
| 76 return elem.first & 1; |
| 77 }); |
| 78 EXPECT_EQ(test_case.erase_odd, test_case.input); |
| 79 } |
| 80 } |
| 81 |
| 82 struct HashByFirst { |
| 83 size_t operator()(const std::pair<int, int>& elem) const { |
| 84 return std::hash<int>()(elem.first); |
| 85 } |
| 86 }; |
| 87 |
| 31 } // namespace | 88 } // namespace |
| 32 | 89 |
| 33 namespace base { | 90 namespace base { |
| 34 namespace { | 91 namespace { |
| 35 | 92 |
| 36 TEST(STLUtilTest, STLIsSorted) { | 93 TEST(STLUtilTest, STLIsSorted) { |
| 37 { | 94 { |
| 38 std::set<int> set; | 95 std::set<int> set; |
| 39 set.insert(24); | 96 set.insert(24); |
| 40 set.insert(1); | 97 set.insert(1); |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 // With a COW implementation, this test will fail if | 313 // With a COW implementation, this test will fail if |
| 257 // string_as_array(&str) is implemented as | 314 // string_as_array(&str) is implemented as |
| 258 // const_cast<char*>(str->data()). | 315 // const_cast<char*>(str->data()). |
| 259 std::string s1("abc"); | 316 std::string s1("abc"); |
| 260 const std::string s2(s1); | 317 const std::string s2(s1); |
| 261 string_as_array(&s1)[1] = 'x'; | 318 string_as_array(&s1)[1] = 'x'; |
| 262 EXPECT_EQ("axc", s1); | 319 EXPECT_EQ("axc", s1); |
| 263 EXPECT_EQ("abc", s2); | 320 EXPECT_EQ("abc", s2); |
| 264 } | 321 } |
| 265 | 322 |
| 323 TEST(Erase, String) { |
| 324 const std::pair<std::string, std::string> test_data[] = { |
| 325 {"", ""}, {"abc", "bc"}, {"abca", "bc"}, |
| 326 }; |
| 327 |
| 328 for (auto test_case : test_data) { |
| 329 Erase(test_case.first, 'a'); |
| 330 EXPECT_EQ(test_case.second, test_case.first); |
| 331 } |
| 332 |
| 333 for (auto test_case : test_data) { |
| 334 EraseIf(test_case.first, [](char elem) { return elem < 'b'; }); |
| 335 EXPECT_EQ(test_case.second, test_case.first); |
| 336 } |
| 337 } |
| 338 |
| 339 TEST(Erase, String16) { |
| 340 std::pair<base::string16, base::string16> test_data[] = { |
| 341 {base::string16(), base::string16()}, |
| 342 {UTF8ToUTF16("abc"), UTF8ToUTF16("bc")}, |
| 343 {UTF8ToUTF16("abca"), UTF8ToUTF16("bc")}, |
| 344 }; |
| 345 |
| 346 const base::string16 letters = UTF8ToUTF16("ab"); |
| 347 for (auto test_case : test_data) { |
| 348 Erase(test_case.first, letters[0]); |
| 349 EXPECT_EQ(test_case.second, test_case.first); |
| 350 } |
| 351 |
| 352 for (auto test_case : test_data) { |
| 353 EraseIf(test_case.first, [&](short elem) { return elem < letters[1]; }); |
| 354 EXPECT_EQ(test_case.second, test_case.first); |
| 355 } |
| 356 } |
| 357 |
| 358 TEST(Erase, Deque) { |
| 359 RunEraseTest<std::deque<int>>(); |
| 360 RunEraseIfTest<std::deque<std::pair<int, int>>>(); |
| 361 } |
| 362 |
| 363 TEST(Erase, Vector) { |
| 364 RunEraseTest<std::vector<int>>(); |
| 365 RunEraseIfTest<std::vector<std::pair<int, int>>>(); |
| 366 } |
| 367 |
| 368 TEST(Erase, ForwardList) { |
| 369 RunEraseTest<std::forward_list<int>>(); |
| 370 RunEraseIfTest<std::forward_list<std::pair<int, int>>>(); |
| 371 } |
| 372 |
| 373 TEST(Erase, List) { |
| 374 RunEraseTest<std::list<int>>(); |
| 375 RunEraseIfTest<std::list<std::pair<int, int>>>(); |
| 376 } |
| 377 |
| 378 TEST(Erase, Map) { |
| 379 RunEraseIfTest<std::map<int, int>>(); |
| 380 } |
| 381 |
| 382 TEST(Erase, Multimap) { |
| 383 RunEraseIfTest<std::multimap<int, int>>(); |
| 384 } |
| 385 |
| 386 TEST(Erase, Set) { |
| 387 RunEraseIfTest<std::set<std::pair<int, int>>>(); |
| 388 } |
| 389 |
| 390 TEST(Erase, Multiset) { |
| 391 RunEraseIfTest<std::multiset<std::pair<int, int>>>(); |
| 392 } |
| 393 |
| 394 TEST(Erase, UnorderedMap) { |
| 395 RunEraseIfTest<std::unordered_map<int, int>>(); |
| 396 } |
| 397 |
| 398 TEST(Erase, UnorderedMultimap) { |
| 399 RunEraseIfTest<std::unordered_multimap<int, int>>(); |
| 400 } |
| 401 |
| 402 TEST(Erase, UnorderedSet) { |
| 403 RunEraseIfTest<std::unordered_set<std::pair<int, int>, HashByFirst>>(); |
| 404 } |
| 405 |
| 406 TEST(Erase, UnorderedMultiset) { |
| 407 RunEraseIfTest<std::unordered_multiset<std::pair<int, int>, HashByFirst>>(); |
| 408 } |
| 409 |
| 266 } // namespace | 410 } // namespace |
| 267 } // namespace base | 411 } // namespace base |
| OLD | NEW |