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

Unified Diff: base/stl_util_unittest.cc

Issue 2723853002: Implementing erase/erase_if functions from library fundamentals ts: (Closed)
Patch Set: Compilation on linux. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « 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..34c5af81cf749065bb612cf504b39e542108dbd7 100644
--- a/base/stl_util_unittest.cc
+++ b/base/stl_util_unittest.cc
@@ -4,8 +4,20 @@
#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/strings/string16.h"
+#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
@@ -28,6 +40,51 @@ class ComparableValue {
int value_;
};
+template <typename Container>
+void RunEraseTest() {
+ const std::pair<Container, Container> test_data[] = {
+ {Container(), Container()}, {{1, 2, 3}, {1, 3}}, {{1, 2, 3, 2}, {1, 3}}};
+
+ for (auto test_case : test_data) {
+ base::Erase(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 RunEraseIfTest() {
+ struct {
+ Container input;
+ Container erase_even;
+ Container erase_odd;
+ } test_data[] = {
+ {Container(), Container(), Container()},
+ {{{1, 1}, {2, 2}, {3, 3}}, {{1, 1}, {3, 3}}, {{2, 2}}},
+ {{{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {3, 3}}, {{2, 2}, {4, 4}}},
+ };
+
+ for (auto test_case : test_data) {
+ base::EraseIf(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::EraseIf(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 +320,92 @@ TEST(StringAsArrayTest, WriteCopy) {
EXPECT_EQ("abc", s2);
}
+TEST(Erase, String) {
+ const std::pair<std::string, std::string> test_data[] = {
+ {"", ""}, {"abc", "bc"}, {"abca", "bc"},
+ };
+
+ for (auto test_case : test_data) {
+ Erase(test_case.first, 'a');
+ EXPECT_EQ(test_case.second, test_case.first);
+ }
+
+ for (auto test_case : test_data) {
+ EraseIf(test_case.first, [](char elem) { return elem < 'b'; });
+ EXPECT_EQ(test_case.second, test_case.first);
+ }
+}
+
+TEST(Erase, 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) {
+ Erase(test_case.first, letters[0]);
+ EXPECT_EQ(test_case.second, test_case.first);
+ }
+
+ for (auto test_case : test_data) {
+ EraseIf(test_case.first, [&](short elem) { return elem < letters[1]; });
+ EXPECT_EQ(test_case.second, test_case.first);
+ }
+}
+
+TEST(Erase, Deque) {
+ RunEraseTest<std::deque<int>>();
+ RunEraseIfTest<std::deque<std::pair<int, int>>>();
+}
+
+TEST(Erase, Vector) {
+ RunEraseTest<std::vector<int>>();
+ RunEraseIfTest<std::vector<std::pair<int, int>>>();
+}
+
+TEST(Erase, ForwardList) {
+ RunEraseTest<std::forward_list<int>>();
+ RunEraseIfTest<std::forward_list<std::pair<int, int>>>();
+}
+
+TEST(Erase, List) {
+ RunEraseTest<std::list<int>>();
+ RunEraseIfTest<std::list<std::pair<int, int>>>();
+}
+
+TEST(Erase, Map) {
+ RunEraseIfTest<std::map<int, int>>();
+}
+
+TEST(Erase, Multimap) {
+ RunEraseIfTest<std::multimap<int, int>>();
+}
+
+TEST(Erase, Set) {
+ RunEraseIfTest<std::set<std::pair<int, int>>>();
+}
+
+TEST(Erase, Multiset) {
+ RunEraseIfTest<std::multiset<std::pair<int, int>>>();
+}
+
+TEST(Erase, UnorderedMap) {
+ RunEraseIfTest<std::unordered_map<int, int>>();
+}
+
+TEST(Erase, UnorderedMultimap) {
+ RunEraseIfTest<std::unordered_multimap<int, int>>();
+}
+
+TEST(Erase, UnorderedSet) {
+ RunEraseIfTest<std::unordered_set<std::pair<int, int>, HashByFirst>>();
+}
+
+TEST(Erase, UnorderedMultiset) {
+ RunEraseIfTest<std::unordered_multiset<std::pair<int, int>, HashByFirst>>();
+}
+
} // namespace
} // namespace base
« no previous file with comments | « base/stl_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698