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