Debian Codegraph @

libstdc++-v3/testsuite/20_util/integer_comparisons/in_range.cc

raw
1 // Copyright (C) 2020-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 <utility>
21 #include <limits>
22 #include <testsuite_hooks.h>
23
24 void
25 test01()
26 {
27 unsigned int u = std::numeric_limits<unsigned int>::max();
28 VERIFY( std::in_range<unsigned int>(u) );
29 VERIFY( !std::in_range<int>(u) );
30 int s = -1;
31 VERIFY( !std::in_range<unsigned int>(s) );
32 s = std::numeric_limits<int>::max();
33 VERIFY( std::in_range<unsigned int>(s) );
34 }
35
36 constexpr bool
37 test02()
38 {
39 unsigned int u = std::numeric_limits<unsigned int>::max();
40 if (std::in_range<int>(u))
41 throw 1;
42 int s = -1;
43 if (std::in_range<unsigned>(s))
44 throw 2;
45 s = std::numeric_limits<int>::max();
46 if (!std::in_range<unsigned>(s))
47 throw 3;
48 return true;
49 }
50
51 void
52 test03()
53 {
54 short ss = -1;
55 VERIFY( std::in_range<int>(ss) );
56 VERIFY( !std::in_range<unsigned>(ss) );
57 int s = -1;
58 VERIFY( std::in_range<short>(s) );
59 VERIFY( !std::in_range<unsigned>(s) );
60 VERIFY( !std::in_range<unsigned long>(s) );
61 VERIFY( std::in_range<long>(s) );
62 s = std::numeric_limits<short>::min() - 1;
63 VERIFY( !std::in_range<short>(s) );
64
65 unsigned int u = (unsigned int) -1;
66 VERIFY( !std::in_range<int>(u) );
67 VERIFY( !std::in_range<unsigned short>(u) );
68
69 unsigned long ul = (unsigned long) -1;
70 VERIFY( !std::in_range<unsigned short>(ul) );
71 VERIFY( std::in_range<unsigned long long>(ul) );
72 }
73
74 int
75 main()
76 {
77 test01();
78 static_assert( test02() );
79 test03();
80 }