summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/21_strings/basic_string
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2020-04-17 16:24:49 +0100
committerJonathan Wakely <jwakely@redhat.com>2020-04-17 16:40:11 +0100
commit875d6cb3b4919b58ae5e6313db715bc4dd3ddd6c (patch)
tree60ec809bcce439236e5778bc5b6a8c9d4d1fbcf0 /libstdc++-v3/testsuite/21_strings/basic_string
parent8b50d7a47624030d87645237c60bd8f7ac78b2ec (diff)
libstdc++: Add comparison operators for string and regex types
Some more C++20 changes from P1614R2, "The Mothership has Landed". This adds three-way comparison support to std::char_traits, std::basic_string, std::basic_string_view, and std::sub_match. * include/bits/basic_string.h (basic_string): Define operator<=> and remove redundant comparison operators for C++20. * include/bits/char_traits.h (__gnu_cxx::char_traits, char_traits): Add comparison_category members. (__detail::__char_traits_cmp_cat): New helper to get comparison category from char traits class. * include/bits/regex.h (regex_traits::_RegexMask::operator!=): Do not define for C++20. (sub_match): Define operator<=> and remove redundant comparison operators for C++20. (match_results): Remove redundant operator!= for C++20. * include/std/string_view (basic_string_view): Define operator<=> and remove redundant comparison operators for C++20. * testsuite/21_strings/basic_string/operators/char/cmp_c++20.cc: New test. * testsuite/21_strings/basic_string/operators/wchar_t/cmp_c++20.cc: New test. * testsuite/21_strings/basic_string_view/operations/copy/char/ constexpr.cc: Initialize variable. * testsuite/21_strings/basic_string_view/operations/copy/wchar_t/ constexpr.cc: Likewise. * testsuite/21_strings/basic_string_view/operators/char/2.cc: Add dg-do directive and remove comments showing incorrect signatures. * testsuite/21_strings/basic_string_view/operators/wchar_t/2.cc: Likewise. * testsuite/21_strings/basic_string_view/operators/char/cmp_c++20.cc: New test. * testsuite/21_strings/basic_string_view/operators/wchar_t/cmp_c++20.cc: New test. * testsuite/28_regex/sub_match/compare_c++20.cc: New test.
Diffstat (limited to 'libstdc++-v3/testsuite/21_strings/basic_string')
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string/operators/char/cmp_c++20.cc194
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string/operators/wchar_t/cmp_c++20.cc194
2 files changed, 388 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operators/char/cmp_c++20.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operators/char/cmp_c++20.cc
new file mode 100644
index 00000000000..b62d10bdb63
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/operators/char/cmp_c++20.cc
@@ -0,0 +1,194 @@
+// Copyright (C) 2020 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
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// 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 } }
+
+// C++20 21.3.3.2 Non-member comparison functions [string.cmp]
+
+// operator==
+/*
+template<class charT, class traits, class Allocator>
+ constexpr bool
+ operator==(const basic_string<charT, traits, Allocator>& lhs,
+ const basic_string<charT, traits, Allocator>& rhs);
+
+template<class charT, class traits, class Allocator>
+ constexpr bool
+ operator==(const basic_string<charT, traits, Allocator>& lhs,
+ const charT* rhs);
+*/
+
+// operator<=>
+/*
+template<class charT, class traits, class Allocator>
+ constexpr [see below]
+ operator<=>(const basic_string<charT, traits, Allocator>& lhs,
+ const basic_string<charT, traits, Allocator>& rhs);
+
+template<class charT, class traits, class Allocator>
+ constexpr [see below]
+ operator<=>(const basic_string<charT,traits,Allocator>& lhs,
+ const charT* rhs);
+*/
+
+#include <string>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ std::string str_0("costa rica");
+ std::string str_1("costa marbella");
+ std::string str_2("cost");
+ std::string str_3("costa ricans");
+ std::string str_4;
+
+ str_4 = str_0;
+ //comparisons between string objects
+ VERIFY( !(str_0 == str_1) );
+ VERIFY( !(str_0 == str_2) );
+ VERIFY( !(str_0 == str_3) );
+ VERIFY( !(str_1 == str_0) );
+ VERIFY( !(str_2 == str_0) );
+ VERIFY( !(str_3 == str_0) );
+ VERIFY( str_4 == str_0 );
+ VERIFY( str_0 == str_4 );
+
+ VERIFY( str_0 != str_1 );
+ VERIFY( str_0 != str_2 );
+ VERIFY( str_0 != str_3 );
+ VERIFY( str_1 != str_0 );
+ VERIFY( str_2 != str_0 );
+ VERIFY( str_3 != str_0 );
+ VERIFY( !(str_0 != str_4) );
+ VERIFY( !(str_4 != str_0) );
+
+ VERIFY( str_0 > str_1 ); //true cuz r>m
+ VERIFY( str_0 > str_2 );
+ VERIFY( !(str_0 > str_3) );
+ VERIFY( !(str_1 > str_0) ); //false cuz m<r
+ VERIFY( !(str_2 > str_0) );
+ VERIFY( str_3 > str_0 );
+ VERIFY( !(str_0 > str_4) );
+ VERIFY( !(str_4 > str_0) );
+
+ VERIFY( !(str_0 < str_1) ); //false cuz r>m
+ VERIFY( !(str_0 < str_2) );
+ VERIFY( str_0 < str_3 );
+ VERIFY( str_1 < str_0 ); //true cuz m<r
+ VERIFY( str_2 < str_0 );
+ VERIFY( !(str_3 < str_0) );
+ VERIFY( !(str_0 < str_4) );
+ VERIFY( !(str_4 < str_0) );
+
+ VERIFY( str_0 >= str_1 ); //true cuz r>m
+ VERIFY( str_0 >= str_2 );
+ VERIFY( !(str_0 >= str_3) );
+ VERIFY( !(str_1 >= str_0) );//false cuz m<r
+ VERIFY( !(str_2 >= str_0) );
+ VERIFY( str_3 >= str_0 );
+ VERIFY( str_0 >= str_4 );
+ VERIFY( str_4 >= str_0 );
+
+ VERIFY( !(str_0 <= str_1) );//false cuz r>m
+ VERIFY( !(str_0 <= str_2) );
+ VERIFY( str_0 <= str_3 );
+ VERIFY( str_1 <= str_0 );//true cuz m<r
+ VERIFY( str_2 <= str_0 );
+ VERIFY( !(str_3 <= str_0) );
+ VERIFY( str_0 <= str_4 );
+ VERIFY( str_4 <= str_0 );
+
+ VERIFY( std::is_gt(str_0 <=> str_1) );
+ VERIFY( std::is_gt(str_0 <=> str_2) );
+ VERIFY( std::is_lt(str_0 <=> str_3) );
+ VERIFY( std::is_eq(str_0 <=> str_4) );
+ VERIFY( std::is_lt(str_1 <=> str_0) );
+ VERIFY( std::is_lt(str_2 <=> str_0) );
+ VERIFY( std::is_gt(str_3 <=> str_0) );
+ VERIFY( std::is_eq(str_4 <=> str_0) );
+
+ //comparisons between string object and string literal
+ VERIFY( !(str_0 == "costa marbella") );
+ VERIFY( !(str_0 == "cost") );
+ VERIFY( !(str_0 == "costa ricans") );
+ VERIFY( !("costa marbella" == str_0) );
+ VERIFY( !("cost" == str_0) );
+ VERIFY( !("costa ricans" == str_0) );
+ VERIFY( "costa rica" == str_0 );
+ VERIFY( str_0 == "costa rica" );
+
+ VERIFY( str_0 != "costa marbella" );
+ VERIFY( str_0 != "cost" );
+ VERIFY( str_0 != "costa ricans" );
+ VERIFY( "costa marbella" != str_0 );
+ VERIFY( "cost" != str_0 );
+ VERIFY( "costa ricans" != str_0 );
+ VERIFY( !("costa rica" != str_0) );
+ VERIFY( !(str_0 != "costa rica") );
+
+ VERIFY( str_0 > "costa marbella" ); //true cuz r>m
+ VERIFY( str_0 > "cost" );
+ VERIFY( !(str_0 > "costa ricans") );
+ VERIFY( !("costa marbella" > str_0) );//false cuz m<r
+ VERIFY( !("cost" > str_0) );
+ VERIFY( "costa ricans" > str_0 );
+ VERIFY( !("costa rica" > str_0) );
+ VERIFY( !(str_0 > "costa rica") );
+
+ VERIFY( !(str_0 < "costa marbella") );//false cuz r>m
+ VERIFY( !(str_0 < "cost") );
+ VERIFY( str_0 < "costa ricans" );
+ VERIFY( "costa marbella" < str_0 );//true cuz m<r
+ VERIFY( "cost" < str_0 );
+ VERIFY( !("costa ricans" < str_0) );
+ VERIFY( !("costa rica" < str_0) );
+ VERIFY( !(str_0 < "costa rica") );
+
+ VERIFY( str_0 >= "costa marbella" );//true cuz r>m
+ VERIFY( str_0 >= "cost" );
+ VERIFY( !(str_0 >= "costa ricans") );
+ VERIFY( !("costa marbella" >= str_0) );//false cuz m<r
+ VERIFY( !("cost" >= str_0) );
+ VERIFY( "costa ricans" >= str_0 );
+ VERIFY( "costa rica" >= str_0 );
+ VERIFY( str_0 >= "costa rica" );
+
+ VERIFY( !(str_0 <= "costa marbella") );//false cuz r>m
+ VERIFY( !(str_0 <= "cost") );
+ VERIFY( str_0 <= "costa ricans" );
+ VERIFY( "costa marbella" <= str_0 );//true cuz m<r
+ VERIFY( "cost" <= str_0 );
+ VERIFY( !("costa ricans" <= str_0) );
+ VERIFY( "costa rica" <= str_0 );
+ VERIFY( str_0 <= "costa rica" );
+
+ VERIFY( std::is_gt(str_0 <=> "costa marbella") );
+ VERIFY( std::is_gt(str_0 <=> "cost") );
+ VERIFY( std::is_lt(str_0 <=> "costa ricans") );
+ VERIFY( std::is_eq(str_0 <=> "costa rica") );
+ VERIFY( std::is_lt("costa marbella" <=> str_0) );
+ VERIFY( std::is_lt("cost" <=> str_0) );
+ VERIFY( std::is_gt("costa ricans" <=> str_0) );
+ VERIFY( std::is_eq("costa rica" <=> str_0) );
+}
+
+int main()
+{
+ test01();
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operators/wchar_t/cmp_c++20.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operators/wchar_t/cmp_c++20.cc
new file mode 100644
index 00000000000..4dda0b5c0f5
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/operators/wchar_t/cmp_c++20.cc
@@ -0,0 +1,194 @@
+// Copyright (C) 2020 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
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// 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 } }
+
+// C++20 21.3.3.2 Non-member comparison functions [string.cmp]
+
+// operator==
+/*
+template<class charT, class traits, class Allocator>
+ constexpr bool
+ operator==(const basic_string<charT, traits, Allocator>& lhs,
+ const basic_string<charT, traits, Allocator>& rhs);
+
+template<class charT, class traits, class Allocator>
+ constexpr bool
+ operator==(const basic_string<charT, traits, Allocator>& lhs,
+ const charT* rhs);
+*/
+
+// operator<=>
+/*
+template<class charT, class traits, class Allocator>
+ constexpr [see below]
+ operator<=>(const basic_string<charT, traits, Allocator>& lhs,
+ const basic_string<charT, traits, Allocator>& rhs);
+
+template<class charT, class traits, class Allocator>
+ constexpr [see below]
+ operator<=>(const basic_string<charT,traits,Allocator>& lhs,
+ const charT* rhs);
+*/
+
+#include <string>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ std::wstring str_0(L"costa rica");
+ std::wstring str_1(L"costa marbella");
+ std::wstring str_2(L"cost");
+ std::wstring str_3(L"costa ricans");
+ std::wstring str_4;
+
+ str_4 = str_0;
+ //comparisons between string objects
+ VERIFY( !(str_0 == str_1) );
+ VERIFY( !(str_0 == str_2) );
+ VERIFY( !(str_0 == str_3) );
+ VERIFY( !(str_1 == str_0) );
+ VERIFY( !(str_2 == str_0) );
+ VERIFY( !(str_3 == str_0) );
+ VERIFY( str_4 == str_0 );
+ VERIFY( str_0 == str_4 );
+
+ VERIFY( str_0 != str_1 );
+ VERIFY( str_0 != str_2 );
+ VERIFY( str_0 != str_3 );
+ VERIFY( str_1 != str_0 );
+ VERIFY( str_2 != str_0 );
+ VERIFY( str_3 != str_0 );
+ VERIFY( !(str_0 != str_4) );
+ VERIFY( !(str_4 != str_0) );
+
+ VERIFY( str_0 > str_1 ); //true cuz r>m
+ VERIFY( str_0 > str_2 );
+ VERIFY( !(str_0 > str_3) );
+ VERIFY( !(str_1 > str_0) ); //false cuz m<r
+ VERIFY( !(str_2 > str_0) );
+ VERIFY( str_3 > str_0 );
+ VERIFY( !(str_0 > str_4) );
+ VERIFY( !(str_4 > str_0) );
+
+ VERIFY( !(str_0 < str_1) ); //false cuz r>m
+ VERIFY( !(str_0 < str_2) );
+ VERIFY( str_0 < str_3 );
+ VERIFY( str_1 < str_0 ); //true cuz m<r
+ VERIFY( str_2 < str_0 );
+ VERIFY( !(str_3 < str_0) );
+ VERIFY( !(str_0 < str_4) );
+ VERIFY( !(str_4 < str_0) );
+
+ VERIFY( str_0 >= str_1 ); //true cuz r>m
+ VERIFY( str_0 >= str_2 );
+ VERIFY( !(str_0 >= str_3) );
+ VERIFY( !(str_1 >= str_0) );//false cuz m<r
+ VERIFY( !(str_2 >= str_0) );
+ VERIFY( str_3 >= str_0 );
+ VERIFY( str_0 >= str_4 );
+ VERIFY( str_4 >= str_0 );
+
+ VERIFY( !(str_0 <= str_1) );//false cuz r>m
+ VERIFY( !(str_0 <= str_2) );
+ VERIFY( str_0 <= str_3 );
+ VERIFY( str_1 <= str_0 );//true cuz m<r
+ VERIFY( str_2 <= str_0 );
+ VERIFY( !(str_3 <= str_0) );
+ VERIFY( str_0 <= str_4 );
+ VERIFY( str_4 <= str_0 );
+
+ VERIFY( std::is_gt(str_0 <=> str_1) );
+ VERIFY( std::is_gt(str_0 <=> str_2) );
+ VERIFY( std::is_lt(str_0 <=> str_3) );
+ VERIFY( std::is_eq(str_0 <=> str_4) );
+ VERIFY( std::is_lt(str_1 <=> str_0) );
+ VERIFY( std::is_lt(str_2 <=> str_0) );
+ VERIFY( std::is_gt(str_3 <=> str_0) );
+ VERIFY( std::is_eq(str_4 <=> str_0) );
+
+ //comparisons between string object and string literal
+ VERIFY( !(str_0 == L"costa marbella") );
+ VERIFY( !(str_0 == L"cost") );
+ VERIFY( !(str_0 == L"costa ricans") );
+ VERIFY( !(L"costa marbella" == str_0) );
+ VERIFY( !(L"cost" == str_0) );
+ VERIFY( !(L"costa ricans" == str_0) );
+ VERIFY( L"costa rica" == str_0 );
+ VERIFY( str_0 == L"costa rica" );
+
+ VERIFY( str_0 != L"costa marbella" );
+ VERIFY( str_0 != L"cost" );
+ VERIFY( str_0 != L"costa ricans" );
+ VERIFY( L"costa marbella" != str_0 );
+ VERIFY( L"cost" != str_0 );
+ VERIFY( L"costa ricans" != str_0 );
+ VERIFY( !(L"costa rica" != str_0) );
+ VERIFY( !(str_0 != L"costa rica") );
+
+ VERIFY( str_0 > L"costa marbella" ); //true cuz r>m
+ VERIFY( str_0 > L"cost" );
+ VERIFY( !(str_0 > L"costa ricans") );
+ VERIFY( !(L"costa marbella" > str_0) );//false cuz m<r
+ VERIFY( !(L"cost" > str_0) );
+ VERIFY( L"costa ricans" > str_0 );
+ VERIFY( !(L"costa rica" > str_0) );
+ VERIFY( !(str_0 > L"costa rica") );
+
+ VERIFY( !(str_0 < L"costa marbella") );//false cuz r>m
+ VERIFY( !(str_0 < L"cost") );
+ VERIFY( str_0 < L"costa ricans" );
+ VERIFY( L"costa marbella" < str_0 );//true cuz m<r
+ VERIFY( L"cost" < str_0 );
+ VERIFY( !(L"costa ricans" < str_0) );
+ VERIFY( !(L"costa rica" < str_0) );
+ VERIFY( !(str_0 < L"costa rica") );
+
+ VERIFY( str_0 >= L"costa marbella" );//true cuz r>m
+ VERIFY( str_0 >= L"cost" );
+ VERIFY( !(str_0 >= L"costa ricans") );
+ VERIFY( !(L"costa marbella" >= str_0) );//false cuz m<r
+ VERIFY( !(L"cost" >= str_0) );
+ VERIFY( L"costa ricans" >= str_0 );
+ VERIFY( L"costa rica" >= str_0 );
+ VERIFY( str_0 >= L"costa rica" );
+
+ VERIFY( !(str_0 <= L"costa marbella") );//false cuz r>m
+ VERIFY( !(str_0 <= L"cost") );
+ VERIFY( str_0 <= L"costa ricans" );
+ VERIFY( L"costa marbella" <= str_0 );//true cuz m<r
+ VERIFY( L"cost" <= str_0 );
+ VERIFY( !(L"costa ricans" <= str_0) );
+ VERIFY( L"costa rica" <= str_0 );
+ VERIFY( str_0 <= L"costa rica" );
+
+ VERIFY( std::is_gt(str_0 <=> L"costa marbella") );
+ VERIFY( std::is_gt(str_0 <=> L"cost") );
+ VERIFY( std::is_lt(str_0 <=> L"costa ricans") );
+ VERIFY( std::is_eq(str_0 <=> L"costa rica") );
+ VERIFY( std::is_lt(L"costa marbella" <=> str_0) );
+ VERIFY( std::is_lt(L"cost" <=> str_0) );
+ VERIFY( std::is_gt(L"costa ricans" <=> str_0) );
+ VERIFY( std::is_eq(L"costa rica" <=> str_0) );
+}
+
+int main()
+{
+ test01();
+}