Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/lexicographical_compare_three_way/1.cc

raw
1 // Copyright (C) 2019-2025 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-do run { target c++20 } }
19
20 #include <algorithm>
21 #include <array>
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
24
25 // Lambda that calls lexicographical_compare_three_way on two ranges.
26 // Arguments are passed by value intentionally, so that a copy of the range
27 // is traversed and the original is not modified. Otherwise when the range
28 // has input iterators the range will be consumed after the first comparison.
29 auto lexicomp3 = [](auto r1, auto r2) {
30 return std::lexicographical_compare_three_way(r1.begin(), r1.end(),
31 r2.begin(), r2.end());
32 };
33
34 void
35 test01()
36 {
37 using __gnu_test::test_container;
38 using __gnu_test::input_iterator_wrapper;
39 using __gnu_test::forward_iterator_wrapper;
40 int arr1[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
41 int arr2[] = { 0, 1, 2, 3, 4, 5, 6, 777 };
42
43 {
44 test_container<int, input_iterator_wrapper> c1(arr1);
45 test_container<int, input_iterator_wrapper> c2(arr2);
46 VERIFY( lexicomp3(c1, c1) == 0 );
47 VERIFY( lexicomp3(c1, c2) < 0 );
48 VERIFY( lexicomp3(c2, c1) > 0 );
49 }
50
51 {
52 test_container<int, input_iterator_wrapper> c1(arr1, arr1+6);
53 test_container<int, input_iterator_wrapper> c2(arr2, arr2+6);
54 VERIFY( lexicomp3(c1, c1) == 0 );
55 VERIFY( lexicomp3(c1, c2) == 0 );
56 VERIFY( lexicomp3(c2, c1) == 0 );
57 }
58
59 {
60 test_container<int, input_iterator_wrapper> c1(arr1);
61 test_container<int, input_iterator_wrapper> c2(arr2, arr2+7);
62 VERIFY( lexicomp3(c1, c1) == 0 );
63 VERIFY( lexicomp3(c1, c2) > 0 );
64 VERIFY( lexicomp3(c2, c1) < 0 );
65 }
66
67 {
68 test_container<int, input_iterator_wrapper> c1(arr1);
69 test_container<int, forward_iterator_wrapper> c2(arr2);
70 VERIFY( lexicomp3(c1, c1) == 0 );
71 VERIFY( lexicomp3(c1, c2) < 0 );
72 VERIFY( lexicomp3(c2, c1) > 0 );
73 }
74
75 {
76 test_container<int, input_iterator_wrapper> c1(arr1);
77 test_container<int, forward_iterator_wrapper> c2(arr2, arr2+7);
78 VERIFY( lexicomp3(c1, c1) == 0 );
79 VERIFY( lexicomp3(c2, c2) == 0 );
80 VERIFY( lexicomp3(c1, c2) > 0 );
81 VERIFY( lexicomp3(c2, c1) < 0 );
82 }
83
84 {
85 test_container<int, forward_iterator_wrapper> c1(arr1, arr1+7);
86 test_container<int, input_iterator_wrapper> c2(arr2);
87 VERIFY( lexicomp3(c1, c1) == 0 );
88 VERIFY( lexicomp3(c2, c2) == 0 );
89 VERIFY( lexicomp3(c1, c2) < 0 );
90 VERIFY( lexicomp3(c2, c1) > 0 );
91 }
92 }
93
94 void
95 test02()
96 {
97 using __gnu_test::test_container;
98 using __gnu_test::input_iterator_wrapper;
99 using __gnu_test::forward_iterator_wrapper;
100 std::array<unsigned char, 8> c1 = { 0, 1, 2, 3, 4, 5, 6, 7 };
101 std::array<unsigned char, 8> c2 = { 0, 1, 2, 3, 4, 5, 6, 77 };
102
103 VERIFY( lexicomp3(c1, c1) == 0 );
104 VERIFY( lexicomp3(c1, c2) < 0 );
105 VERIFY( lexicomp3(c2, c1) > 0 );
106
107 std::array<unsigned char, 7> c3 = { 0, 1, 2, 3, 4, 5, 6 };
108 VERIFY( lexicomp3(c3, c3) == 0 );
109 VERIFY( lexicomp3(c1, c3) > 0 );
110 VERIFY( lexicomp3(c3, c1) < 0 );
111 }
112
113 void
114 test03()
115 {
116 unsigned char a[2] = { 1, 2 };
117 unsigned char* p = nullptr;
118 // ensure memcmp not called with nullptr
119 VERIFY( std::lexicographical_compare_three_way(p, p, a, a+2) < 0 );
120 VERIFY( std::lexicographical_compare_three_way(a, a+2, p, p) > 0 );
121 }
122
123 int
124 main()
125 {
126 test01();
127 test02();
128 test03();
129 }