comparison libstdc++-v3/testsuite/23_containers/map/operators/cmp_c++20.cc @ 19:2b5abeee2509 default tip

update gcc11
author anatofuz
date Mon, 25 May 2020 07:50:57 +0900
parents
children
comparison
equal deleted inserted replaced
18:1830386684a0 19:2b5abeee2509
1 // Copyright (C) 2020 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20
21 #include <map>
22 #include <testsuite_hooks.h>
23
24 void
25 test01()
26 {
27 std::map<int, int> c1{ {1,1}, {2,1}, {3,1} };
28 std::map<int, int> c2{ {1,1}, {2,1}, {3,1}, {4,1} };
29 std::map<int, int> c3{ {1,1}, {2,1}, {3,2} };
30 VERIFY( c1 == c1 );
31 VERIFY( std::is_eq(c1 <=> c1) );
32 VERIFY( c1 < c2 );
33 VERIFY( std::is_lt(c1 <=> c2) );
34 VERIFY( c1 < c3 );
35 VERIFY( std::is_lt(c1 <=> c3) );
36 VERIFY( c2 < c3 );
37 VERIFY( std::is_lt(c2 <=> c3) );
38
39 static_assert( std::totally_ordered<std::map<int, int>> );
40
41 static_assert( std::three_way_comparable<std::map<int, int>,
42 std::strong_ordering> );
43 static_assert( ! std::three_way_comparable<std::map<int, float>,
44 std::strong_ordering> );
45 static_assert( ! std::three_way_comparable<std::map<int, float>,
46 std::weak_ordering> );
47 static_assert( std::three_way_comparable<std::map<int, float>,
48 std::partial_ordering> );
49
50 struct E
51 {
52 bool operator==(E) { return true; }
53 };
54 static_assert( ! std::totally_ordered<std::map<int, E>> );
55 static_assert( ! std::three_way_comparable<E> );
56 static_assert( ! std::three_way_comparable<std::map<int, E>> );
57 }
58
59 void
60 test02()
61 {
62 struct W
63 {
64 int value = 0;
65
66 bool operator==(W rhs) const noexcept
67 { return (value | 1) == (rhs.value | 1); }
68
69 std::weak_ordering
70 operator<=>(W rhs) const noexcept
71 { return (value | 1) <=> (rhs.value | 1); }
72 };
73
74 static_assert( std::totally_ordered<std::map<int, W>> );
75
76 using P = std::pair<const W, W>;
77 std::map<W, W> c1{ P{1,1}, P{2,2}, P{3,3} }, c2{ P{1,0}, P{3,2}, P{3,3} };
78 static_assert( std::same_as<decltype(c1 <=> c1), std::weak_ordering> );
79 VERIFY( c1 == c2 );
80 VERIFY( std::is_eq(c1 <=> c2) );
81 }
82
83 void
84 test04()
85 {
86 struct L
87 {
88 int value = 0;
89
90 bool operator<(L rhs) const noexcept { return value < rhs.value; }
91 };
92
93 static_assert( std::totally_ordered<std::map<int, L>> );
94
95 using P = std::pair<const L, L>;
96 std::map<L, L> c{ P{1,1}, P{2,2}, P{3,3} }, d{ P{1,1}, P{2,2}, P{3,4} };
97 static_assert( std::same_as<decltype(c <=> c), std::weak_ordering> );
98 VERIFY( std::is_lt(c <=> d) );
99 }
100
101 // Associative container iterators are not random access
102 static_assert( ! std::totally_ordered<std::map<int, int>::iterator> );
103 static_assert( ! std::three_way_comparable<std::map<int, int>::iterator> );
104
105 int
106 main()
107 {
108 test01();
109 test02();
110 test04();
111 }