summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/strings
diff options
context:
space:
mode:
authorMarshall Clow <mclow.lists@gmail.com>2018-12-14 18:49:35 +0000
committerMarshall Clow <mclow.lists@gmail.com>2018-12-14 18:49:35 +0000
commitf60c63c090114a6b5f0ba0a7aacd67e6c92920e7 (patch)
tree17e10b3f0bbd3a373498bbe621f9036bdd176e32 /libcxx/test/std/strings
parentb7e2d6e4931b88fc5c84358a1fa7a9d3dffbecbc (diff)
downloadbcm5719-llvm-f60c63c090114a6b5f0ba0a7aacd67e6c92920e7.tar.gz
bcm5719-llvm-f60c63c090114a6b5f0ba0a7aacd67e6c92920e7.zip
Implement P1209 - Adopt Consistent Container Erasure from Library Fundamentals 2 for C++20. Reviewed as https://reviews.llvm.org/D55532
llvm-svn: 349178
Diffstat (limited to 'libcxx/test/std/strings')
-rw-r--r--libcxx/test/std/strings/strings.erasure/erase.pass.cpp76
-rw-r--r--libcxx/test/std/strings/strings.erasure/erase_if.pass.cpp76
2 files changed, 152 insertions, 0 deletions
diff --git a/libcxx/test/std/strings/strings.erasure/erase.pass.cpp b/libcxx/test/std/strings/strings.erasure/erase.pass.cpp
new file mode 100644
index 00000000000..657a56c7315
--- /dev/null
+++ b/libcxx/test/std/strings/strings.erasure/erase.pass.cpp
@@ -0,0 +1,76 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
+
+// <string>
+
+// template <class charT, class traits, class Allocator, class U>
+// void erase(basic_string<charT, traits, Allocator>& c, const U& value);
+
+
+#include <string>
+#include <optional>
+
+#include "test_macros.h"
+#include "test_allocator.h"
+#include "min_allocator.h"
+
+template <class S, class U>
+void
+test0(S s, U val, S expected)
+{
+ ASSERT_SAME_TYPE(void, decltype(std::erase(s, val)));
+ std::erase(s, val);
+ LIBCPP_ASSERT(s.__invariants());
+ assert(s == expected);
+}
+
+template <class S>
+void test()
+{
+
+ test0(S(""), 'a', S(""));
+
+ test0(S("a"), 'a', S(""));
+ test0(S("a"), 'b', S("a"));
+
+ test0(S("ab"), 'a', S("b"));
+ test0(S("ab"), 'b', S("a"));
+ test0(S("ab"), 'c', S("ab"));
+ test0(S("aa"), 'a', S(""));
+ test0(S("aa"), 'c', S("aa"));
+
+ test0(S("abc"), 'a', S("bc"));
+ test0(S("abc"), 'b', S("ac"));
+ test0(S("abc"), 'c', S("ab"));
+ test0(S("abc"), 'd', S("abc"));
+
+ test0(S("aab"), 'a', S("b"));
+ test0(S("aab"), 'b', S("aa"));
+ test0(S("aab"), 'c', S("aab"));
+ test0(S("abb"), 'a', S("bb"));
+ test0(S("abb"), 'b', S("a"));
+ test0(S("abb"), 'c', S("abb"));
+ test0(S("aaa"), 'a', S(""));
+ test0(S("aaa"), 'b', S("aaa"));
+
+// Test cross-type erasure
+ using opt = std::optional<typename S::value_type>;
+ test0(S("aba"), opt(), S("aba"));
+ test0(S("aba"), opt('a'), S("b"));
+ test0(S("aba"), opt('b'), S("aa"));
+ test0(S("aba"), opt('c'), S("aba"));
+}
+
+int main()
+{
+ test<std::string>();
+ test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>> ();
+ test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>> ();
+}
diff --git a/libcxx/test/std/strings/strings.erasure/erase_if.pass.cpp b/libcxx/test/std/strings/strings.erasure/erase_if.pass.cpp
new file mode 100644
index 00000000000..d7014868fd3
--- /dev/null
+++ b/libcxx/test/std/strings/strings.erasure/erase_if.pass.cpp
@@ -0,0 +1,76 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
+
+// <string>
+
+// template <class charT, class traits, class Allocator, class Predicate>
+// void erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred);
+
+#include <string>
+
+#include "test_macros.h"
+#include "test_allocator.h"
+#include "min_allocator.h"
+
+template <class S, class Pred>
+void
+test0(S s, Pred p, S expected)
+{
+ ASSERT_SAME_TYPE(void, decltype(std::erase_if(s, p)));
+ std::erase_if(s, p);
+ LIBCPP_ASSERT(s.__invariants());
+ assert(s == expected);
+}
+
+template <typename S>
+void test()
+{
+ auto isA = [](auto ch) { return ch == 'a';};
+ auto isB = [](auto ch) { return ch == 'b';};
+ auto isC = [](auto ch) { return ch == 'c';};
+ auto isD = [](auto ch) { return ch == 'd';};
+ auto True = [](auto) { return true; };
+ auto False = [](auto) { return false; };
+
+ test0(S(""), isA, S(""));
+
+ test0(S("a"), isA, S(""));
+ test0(S("a"), isB, S("a"));
+
+ test0(S("ab"), isA, S("b"));
+ test0(S("ab"), isB, S("a"));
+ test0(S("ab"), isC, S("ab"));
+ test0(S("aa"), isA, S(""));
+ test0(S("aa"), isC, S("aa"));
+
+ test0(S("abc"), isA, S("bc"));
+ test0(S("abc"), isB, S("ac"));
+ test0(S("abc"), isC, S("ab"));
+ test0(S("abc"), isD, S("abc"));
+
+ test0(S("aab"), isA, S("b"));
+ test0(S("aab"), isB, S("aa"));
+ test0(S("aab"), isC, S("aab"));
+ test0(S("abb"), isA, S("bb"));
+ test0(S("abb"), isB, S("a"));
+ test0(S("abb"), isC, S("abb"));
+ test0(S("aaa"), isA, S(""));
+ test0(S("aaa"), isB, S("aaa"));
+
+ test0(S("aba"), False, S("aba"));
+ test0(S("aba"), True, S(""));
+}
+
+int main()
+{
+ test<std::string>();
+ test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>> ();
+ test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>> ();
+}
OpenPOWER on IntegriCloud