]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2_neg.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / bit / bit.pow.two / ceil2_neg.cc
1 // Copyright (C) 2019-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 -D_GLIBCXX_ASSERTIONS" }
19 // { dg-do run { target c++2a } }
20 // { dg-xfail-run-if "__glibcxx_assert in ceil2 should fail" { *-*-* } }
21
22 #include <bit>
23
24 // P1355R2: not a constant expression if the result is not representable
25
26 template<auto N, typename = void>
27 struct ceil2_valid
28 : std::false_type { };
29
30 template<auto N>
31 struct ceil2_valid<N, std::void_t<char[(std::ceil2(N), 1)]>>
32 : std::true_type { };
33
34 template<typename T>
35 constexpr T max = std::numeric_limits<T>::max();
36 template<typename T>
37 constexpr T maxpow2 = T(1) << (std::numeric_limits<T>::digits - 1);
38
39 static_assert( ceil2_valid<maxpow2<unsigned char>>() );
40 static_assert( !ceil2_valid<maxpow2<unsigned char> + (unsigned char)1>() );
41
42 static_assert( !ceil2_valid<max<unsigned char>>() );
43 static_assert( !ceil2_valid<max<unsigned char> - (unsigned char)1>() );
44
45 static_assert( ceil2_valid<maxpow2<unsigned short>>() );
46 static_assert( !ceil2_valid<maxpow2<unsigned short> + (unsigned short)1>() );
47 static_assert( !ceil2_valid<max<unsigned short>>() );
48 static_assert( !ceil2_valid<max<unsigned short> - (unsigned short)1>() );
49
50 static_assert( ceil2_valid<maxpow2<unsigned int>>() );
51 static_assert( !ceil2_valid<maxpow2<unsigned int> + 1u>() );
52 static_assert( !ceil2_valid<max<unsigned int>>() );
53 static_assert( !ceil2_valid<max<unsigned int> - 1u>() );
54
55 static_assert( ceil2_valid<maxpow2<unsigned long>>() );
56 static_assert( !ceil2_valid<maxpow2<unsigned long> + 1ul>() );
57 static_assert( !ceil2_valid<max<unsigned long>>() );
58 static_assert( !ceil2_valid<max<unsigned long> - 1ul>() );
59
60 static_assert( ceil2_valid<maxpow2<unsigned long long>>() );
61 static_assert( !ceil2_valid<maxpow2<unsigned long long> + 1ull>() );
62 static_assert( !ceil2_valid<max<unsigned long long>>() );
63 static_assert( !ceil2_valid<max<unsigned long long> - 1ull>() );
64
65 void
66 test01()
67 {
68 std::ceil2( maxpow2<unsigned> + 1u ); // should fail __glibcxx_assert
69 }
70
71 int main()
72 {
73 test01();
74 }