]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/testsuite/std/ranges/adaptors/split.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / adaptors / split.cc
index b9fb3728708adb2ff89f41775819d91bdfa7070c..0e91482e7b7207963f87d032228e8b25d421be2d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2021 Free Software Foundation, Inc.
+// Copyright (C) 2020-2024 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-options "-std=gnu++2a" }
-// { dg-do run { target c++2a } }
+// { dg-do run { target c++20 } }
 
 #include <algorithm>
 #include <ranges>
 #include <string>
 #include <string_view>
+#include <vector>
 #include <testsuite_hooks.h>
 #include <testsuite_iterators.h>
 
@@ -34,6 +34,31 @@ namespace views = std::ranges::views;
 
 using namespace std::literals;
 
+void
+test01()
+{
+  auto from_chars = [] (auto v) {
+    return std::stoi(std::string(v.data(), v.data() + v.size()));
+  };
+  auto ints = "1.2.3.4"sv
+    | views::split('.')
+    | views::transform(from_chars);
+  VERIFY( ranges::equal(ints, (int[]){1,2,3,4}) );
+}
+
+void
+test02()
+{
+  // PR libstdc++/101214
+  auto v = views::iota(0) | views::take(5) | views::split(0);
+  static_assert(!ranges::common_range<decltype(v)>);
+  static_assert(std::default_initializable<decltype(v.end())>);
+  static_assert(std::sentinel_for<decltype(v.end()), decltype(v.begin())>);
+}
+
+// The following testcases are adapted from lazy_split.cc.
+namespace from_lazy_split_cc
+{
 void
 test01()
 {
@@ -109,52 +134,9 @@ test05()
                        str | views::filter(not_space_p)) );
 }
 
-void
-test06()
-{
-  std::string str = "hello world";
-  auto v = str | views::transform(std::identity{}) | views::split(' ');
-
-  // Verify that _Iterator<false> is implicitly convertible to _Iterator<true>.
-  static_assert(!std::same_as<decltype(ranges::begin(v)),
-                             decltype(ranges::cbegin(v))>);
-  auto b = ranges::cbegin(v);
-  b = ranges::begin(v);
-}
-
-void
-test07()
-{
-  char str[] = "banana split";
-  auto split = str | views::split(' ');
-  auto val = *split.begin();
-  auto b = val.begin();
-  auto b2 = b++;
-  static_assert( noexcept(iter_move(b)) );
-  static_assert( noexcept(iter_swap(b, b2)) );
-}
-
-void
-test08()
-{
-  char x[] = "the quick brown fox";
-  test_range<char, input_iterator_wrapper> rx(x, x+sizeof(x)-1);
-  auto v = rx | views::split(' ');
-  auto i = v.begin();
-  VERIFY( ranges::equal(*i, "the"sv) );
-  ++i;
-  VERIFY( ranges::equal(*i, "quick"sv) );
-  ++i;
-  VERIFY( ranges::equal(*i, "brown"sv) );
-  ++i;
-  VERIFY( ranges::equal(*i, "fox"sv) );
-  ++i;
-  VERIFY( i == v.end() );
-}
-
 template<auto split = views::split>
 void
-test09()
+test06()
 {
   // Verify SFINAE behavior.
   std::string s, p;
@@ -163,10 +145,15 @@ test09()
   static_assert(!requires { split(p)(); });
   static_assert(!requires { s | split; });
 
-  static_assert(!requires { s | split(p); });
-  static_assert(!requires { split(p)(s); });
-  static_assert(!requires { s | (split(p) | views::all); });
-  static_assert(!requires { (split(p) | views::all)(s); });
+  // Test the case where the closure object is used as an rvalue and therefore
+  // the copy of p is forwarded as an rvalue.
+  // This used to be invalid, but is now well-formed after P2415R2 relaxed
+  // the requirements of viewable_range to admit rvalue non-view non-borrowed
+  // ranges such as std::string&&.
+  static_assert(requires { s | split(p); });
+  static_assert(requires { split(p)(s); });
+  static_assert(requires { s | (split(p) | views::all); });
+  static_assert(requires { (split(p) | views::all)(s); });
 
   static_assert(requires { s | split(views::all(p)); });
   static_assert(requires { split(views::all(p))(s); });
@@ -182,16 +169,49 @@ test09()
   static_assert(requires { adapt2(s); });
 }
 
+void
+test10()
+{
+  // LWG 3505
+  auto to_string = [] (auto r) {
+    return std::string(r.begin(), ranges::next(r.begin(), r.end()));
+  };
+  auto v = "xxyx"sv | views::split("xy"sv) | views::transform(to_string);
+  VERIFY( ranges::equal(v, (std::string_view[]){"x", "x"}) );
+}
+
+void
+test11()
+{
+  // LWG 3478
+  auto v = views::split("text"sv, "text"sv);
+  auto i = v.begin();
+  VERIFY( ranges::empty(*i++) );
+  VERIFY( ranges::empty(*i++) );
+  VERIFY( i == v.end() );
+
+  static_assert(ranges::distance(views::split(" text "sv, ' ')) == 3);
+  static_assert(ranges::distance(views::split(" t e x t "sv, ' ')) == 6);
+  static_assert(ranges::distance(views::split("  text  "sv, "  "sv)) == 3);
+  static_assert(ranges::distance(views::split("  text    "sv, "  "sv)) == 4);
+  static_assert(ranges::distance(views::split("  text     "sv, "  "sv)) == 4);
+  static_assert(ranges::distance(views::split("t"sv, 't')) == 2);
+  static_assert(ranges::distance(views::split("text"sv, ""sv)) == 4);
+}
+} // namespace from_lazy_split_cc
+
 int
 main()
 {
   test01();
   test02();
-  test03();
-  test04();
-  test05();
-  test06();
-  test07();
-  test08();
-  test09();
+
+  from_lazy_split_cc::test01();
+  from_lazy_split_cc::test02();
+  from_lazy_split_cc::test03();
+  from_lazy_split_cc::test04();
+  from_lazy_split_cc::test05();
+  from_lazy_split_cc::test06();
+  from_lazy_split_cc::test10();
+  from_lazy_split_cc::test11();
 }