Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: base/stl_util_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698