]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/lexicographical_compare_three_way/constexpr.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / lexicographical_compare_three_way / constexpr.cc
1 // Copyright (C) 2019-2024 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 compile { target c++20 } }
19
20 #include <algorithm>
21
22 constexpr bool
23 test01(int i)
24 {
25 int a1[] = { 0, 1, 2, 3, 4, 5, 6, i, 8 };
26 long a2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
27
28 return std::lexicographical_compare_three_way(a1, a1+0, a2, a2+0) == 0
29 && std::lexicographical_compare_three_way(a1, a1+9, a2, a2+9) == (i <=> 7)
30 && std::lexicographical_compare_three_way(a1, a1+7, a2, a2+7) == 0
31 && std::lexicographical_compare_three_way(a1, a1+8, a2, a2+7) > 0
32 && std::lexicographical_compare_three_way(a1, a1+7, a2, a2+8) < 0;
33 }
34
35 static_assert( test01(~7) );
36 static_assert( test01(7) );
37 static_assert( test01(8) );
38
39 constexpr bool
40 test02(unsigned char i)
41 {
42 unsigned char a1[] = { 0, 1, 2, 3, 4, 5, 6, i, 8 };
43 unsigned char a2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
44
45 return std::lexicographical_compare_three_way(a1, a1+0, a2, a2+0) == 0
46 && std::lexicographical_compare_three_way(a1, a1+9, a2, a2+9) == (i <=> 7)
47 && std::lexicographical_compare_three_way(a1, a1+7, a2, a2+7) == 0
48 && std::lexicographical_compare_three_way(a1, a1+8, a2, a2+7) > 0
49 && std::lexicographical_compare_three_way(a1, a1+7, a2, a2+8) < 0;
50 }
51
52 static_assert( test02(248) );
53 static_assert( test02(7) );
54 static_assert( test02(8) );
55
56 constexpr bool
57 test03(unsigned char* p)
58 {
59 unsigned char a[2] = { 1, 2 };
60 return std::lexicographical_compare_three_way(p, p, a, a+2) < 0
61 && std::lexicographical_compare_three_way(a, a+2, p, p) > 0;
62 }
63
64 static_assert( test03(nullptr) ); // ensure memcmp not called with nullptr