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

Side by Side Diff: base/stl_util_unittest.cc

Issue 2723853002: Implementing erase/erase_if functions from library fundamentals ts: (Closed)
Patch Set: 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 RunStlEraseTest() {
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::STLErase(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 RunStlEraseIfTest() {
58 auto PairFromInt = [](int num) { return std::make_pair(num, num); };
59 struct {
60 Container input;
61 Container erase_even;
62 Container erase_odd;
63 } test_data[] = {
64 {{}, {}, {}},
65 {{PairFromInt(1), PairFromInt(2), PairFromInt(3)},
Peter Kasting 2017/03/01 03:20:11 Nit: This all seems longer than just writing {1, 1
66 {PairFromInt(1), PairFromInt(3)},
67 {PairFromInt(2)}},
68 {{PairFromInt(1), PairFromInt(2), PairFromInt(3), PairFromInt(4)},
69 {PairFromInt(1), PairFromInt(3)},
70 {PairFromInt(2), PairFromInt(4)}},
71 };
72
73 for (auto test_case : test_data) {
74 base::STLEraseIf(test_case.input, [](const std::pair<int, int>& elem) {
75 return !(elem.first & 1);
76 });
77 EXPECT_EQ(test_case.erase_even, test_case.input);
78 }
79
80 for (auto test_case : test_data) {
81 base::STLEraseIf(test_case.input, [](const std::pair<int, int>& elem) {
82 return elem.first & 1;
83 });
84 EXPECT_EQ(test_case.erase_odd, test_case.input);
85 }
86 }
87
88 struct HashByFirst {
89 size_t operator()(const std::pair<int, int>& elem) const {
90 return std::hash<int>()(elem.first);
91 }
92 };
93
31 } // namespace 94 } // namespace
32 95
33 namespace base { 96 namespace base {
34 namespace { 97 namespace {
35 98
36 TEST(STLUtilTest, STLIsSorted) { 99 TEST(STLUtilTest, STLIsSorted) {
37 { 100 {
38 std::set<int> set; 101 std::set<int> set;
39 set.insert(24); 102 set.insert(24);
40 set.insert(1); 103 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 319 // With a COW implementation, this test will fail if
257 // string_as_array(&str) is implemented as 320 // string_as_array(&str) is implemented as
258 // const_cast<char*>(str->data()). 321 // const_cast<char*>(str->data()).
259 std::string s1("abc"); 322 std::string s1("abc");
260 const std::string s2(s1); 323 const std::string s2(s1);
261 string_as_array(&s1)[1] = 'x'; 324 string_as_array(&s1)[1] = 'x';
262 EXPECT_EQ("axc", s1); 325 EXPECT_EQ("axc", s1);
263 EXPECT_EQ("abc", s2); 326 EXPECT_EQ("abc", s2);
264 } 327 }
265 328
329 TEST(STLErase, String) {
330 const std::pair<std::string, std::string> test_data[] = {
331 {"", ""}, {"abc", "bc"}, {"abca", "bc"},
332 };
333
334 for (auto test_case : test_data) {
335 STLErase(test_case.first, 'a');
336 EXPECT_EQ(test_case.second, test_case.first);
337 }
338
339 for (auto test_case : test_data) {
340 STLEraseIf(test_case.first, [](char elem) { return elem < 'b'; });
341 EXPECT_EQ(test_case.second, test_case.first);
342 }
343 }
344
345 TEST(STLErase, String16) {
346 std::pair<base::string16, base::string16> test_data[] = {
347 {base::string16(), base::string16()},
348 {UTF8ToUTF16("abc"), UTF8ToUTF16("bc")},
349 {UTF8ToUTF16("abca"), UTF8ToUTF16("bc")},
350 };
351
352 const base::string16 letters = UTF8ToUTF16("ab");
353 for (auto test_case : test_data) {
354 STLErase(test_case.first, letters[0]);
355 EXPECT_EQ(test_case.second, test_case.first);
356 }
357
358 for (auto test_case : test_data) {
359 STLEraseIf(test_case.first, [&](short elem) { return elem < letters[1]; });
360 EXPECT_EQ(test_case.second, test_case.first);
361 }
362 }
363
364 TEST(STLErase, Deque) {
365 RunStlEraseTest<std::deque<int>>();
366 RunStlEraseIfTest<std::deque<std::pair<int, int>>>();
367 }
368
369 TEST(STLErase, Vector) {
370 RunStlEraseTest<std::vector<int>>();
371 RunStlEraseIfTest<std::vector<std::pair<int, int>>>();
372 }
373
374 TEST(STLErase, ForwardList) {
375 RunStlEraseTest<std::forward_list<int>>();
376 RunStlEraseIfTest<std::forward_list<std::pair<int, int>>>();
377 }
378
379 TEST(STLErase, List) {
380 RunStlEraseTest<std::list<int>>();
381 RunStlEraseIfTest<std::list<std::pair<int, int>>>();
382 }
383
384 TEST(STLErase, Map) {
385 RunStlEraseIfTest<std::map<int, int>>();
386 }
387
388 TEST(STLErase, Multimap) {
389 RunStlEraseIfTest<std::multimap<int, int>>();
390 }
391
392 TEST(STLErase, Set) {
393 RunStlEraseIfTest<std::set<std::pair<int, int>>>();
394 }
395
396 TEST(STLErase, Multiset) {
397 RunStlEraseIfTest<std::multiset<std::pair<int, int>>>();
398 }
399
400 TEST(STLErase, UnorderedMap) {
401 RunStlEraseIfTest<std::unordered_map<int, int>>();
402 }
403
404 TEST(STLErase, UnorderedMultimap) {
405 RunStlEraseIfTest<std::unordered_multimap<int, int>>();
406 }
407
408 TEST(STLErase, UnorderedSet) {
409 RunStlEraseIfTest<std::unordered_set<std::pair<int, int>, HashByFirst>>();
410 }
411
412 TEST(STLErase, UnorderedMultiset) {
413 RunStlEraseIfTest<
414 std::unordered_multiset<std::pair<int, int>, HashByFirst>>();
415 }
416
417 TEST(STLErase, FlatSet) {
418 RunStlEraseIfTest<base::flat_set<std::pair<int, int>>>();
419 }
420
266 } // namespace 421 } // namespace
267 } // namespace base 422 } // 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