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

Unified Diff: base/stl_util_unittest.cc

Issue 2723853002: Implementing erase/erase_if functions from library fundamentals ts: (Closed)
Patch Set: Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
« base/stl_util.h ('K') | « base/stl_util.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/stl_util_unittest.cc
diff --git a/base/stl_util_unittest.cc b/base/stl_util_unittest.cc
index 42004eb869b0424583817f9980b2aa156922c13d..e6fe016c84039836632232772884b96cb77b27a1 100644
--- a/base/stl_util_unittest.cc
+++ b/base/stl_util_unittest.cc
@@ -4,8 +4,21 @@
#include "base/stl_util.h"
+#include <deque>
+#include <forward_list>
+#include <functional>
+#include <iterator>
+#include <list>
+#include <map>
#include <set>
-
+#include <string>
+#include <unordered_map>
+#include <unordered_set>
+#include <vector>
+
+#include "base/containers/flat_set.h"
+#include "base/strings/string16.h"
+#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
@@ -28,6 +41,56 @@ class ComparableValue {
int value_;
};
+template <typename Container>
+void RunStlEraseTest() {
+ const std::pair<Container, Container> test_data[] = {
+ {{}, {}}, {{1, 2, 3}, {1, 3}}, {{1, 2, 3, 2}, {1, 3}}};
+
+ for (auto test_case : test_data) {
+ base::STLErase(test_case.first, 2);
+ EXPECT_EQ(test_case.second, test_case.first);
+ }
+}
+
+// This test is written for containers of std::pair<int, int> to support maps.
+template <typename Container>
+void RunStlEraseIfTest() {
+ auto PairFromInt = [](int num) { return std::make_pair(num, num); };
+ struct {
+ Container input;
+ Container erase_even;
+ Container erase_odd;
+ } test_data[] = {
+ {{}, {}, {}},
+ {{PairFromInt(1), PairFromInt(2), PairFromInt(3)},
Peter Kasting 2017/03/01 03:20:11 Nit: This all seems longer than just writing {1, 1
+ {PairFromInt(1), PairFromInt(3)},
+ {PairFromInt(2)}},
+ {{PairFromInt(1), PairFromInt(2), PairFromInt(3), PairFromInt(4)},
+ {PairFromInt(1), PairFromInt(3)},
+ {PairFromInt(2), PairFromInt(4)}},
+ };
+
+ for (auto test_case : test_data) {
+ base::STLEraseIf(test_case.input, [](const std::pair<int, int>& elem) {
+ return !(elem.first & 1);
+ });
+ EXPECT_EQ(test_case.erase_even, test_case.input);
+ }
+
+ for (auto test_case : test_data) {
+ base::STLEraseIf(test_case.input, [](const std::pair<int, int>& elem) {
+ return elem.first & 1;
+ });
+ EXPECT_EQ(test_case.erase_odd, test_case.input);
+ }
+}
+
+struct HashByFirst {
+ size_t operator()(const std::pair<int, int>& elem) const {
+ return std::hash<int>()(elem.first);
+ }
+};
+
} // namespace
namespace base {
@@ -263,5 +326,97 @@ TEST(StringAsArrayTest, WriteCopy) {
EXPECT_EQ("abc", s2);
}
+TEST(STLErase, String) {
+ const std::pair<std::string, std::string> test_data[] = {
+ {"", ""}, {"abc", "bc"}, {"abca", "bc"},
+ };
+
+ for (auto test_case : test_data) {
+ STLErase(test_case.first, 'a');
+ EXPECT_EQ(test_case.second, test_case.first);
+ }
+
+ for (auto test_case : test_data) {
+ STLEraseIf(test_case.first, [](char elem) { return elem < 'b'; });
+ EXPECT_EQ(test_case.second, test_case.first);
+ }
+}
+
+TEST(STLErase, String16) {
+ std::pair<base::string16, base::string16> test_data[] = {
+ {base::string16(), base::string16()},
+ {UTF8ToUTF16("abc"), UTF8ToUTF16("bc")},
+ {UTF8ToUTF16("abca"), UTF8ToUTF16("bc")},
+ };
+
+ const base::string16 letters = UTF8ToUTF16("ab");
+ for (auto test_case : test_data) {
+ STLErase(test_case.first, letters[0]);
+ EXPECT_EQ(test_case.second, test_case.first);
+ }
+
+ for (auto test_case : test_data) {
+ STLEraseIf(test_case.first, [&](short elem) { return elem < letters[1]; });
+ EXPECT_EQ(test_case.second, test_case.first);
+ }
+}
+
+TEST(STLErase, Deque) {
+ RunStlEraseTest<std::deque<int>>();
+ RunStlEraseIfTest<std::deque<std::pair<int, int>>>();
+}
+
+TEST(STLErase, Vector) {
+ RunStlEraseTest<std::vector<int>>();
+ RunStlEraseIfTest<std::vector<std::pair<int, int>>>();
+}
+
+TEST(STLErase, ForwardList) {
+ RunStlEraseTest<std::forward_list<int>>();
+ RunStlEraseIfTest<std::forward_list<std::pair<int, int>>>();
+}
+
+TEST(STLErase, List) {
+ RunStlEraseTest<std::list<int>>();
+ RunStlEraseIfTest<std::list<std::pair<int, int>>>();
+}
+
+TEST(STLErase, Map) {
+ RunStlEraseIfTest<std::map<int, int>>();
+}
+
+TEST(STLErase, Multimap) {
+ RunStlEraseIfTest<std::multimap<int, int>>();
+}
+
+TEST(STLErase, Set) {
+ RunStlEraseIfTest<std::set<std::pair<int, int>>>();
+}
+
+TEST(STLErase, Multiset) {
+ RunStlEraseIfTest<std::multiset<std::pair<int, int>>>();
+}
+
+TEST(STLErase, UnorderedMap) {
+ RunStlEraseIfTest<std::unordered_map<int, int>>();
+}
+
+TEST(STLErase, UnorderedMultimap) {
+ RunStlEraseIfTest<std::unordered_multimap<int, int>>();
+}
+
+TEST(STLErase, UnorderedSet) {
+ RunStlEraseIfTest<std::unordered_set<std::pair<int, int>, HashByFirst>>();
+}
+
+TEST(STLErase, UnorderedMultiset) {
+ RunStlEraseIfTest<
+ std::unordered_multiset<std::pair<int, int>, HashByFirst>>();
+}
+
+TEST(STLErase, FlatSet) {
+ RunStlEraseIfTest<base::flat_set<std::pair<int, int>>>();
+}
+
} // namespace
} // namespace base
« 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