]> git.ipfire.org Git - thirdparty/gcc.git/blame - 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
CommitLineData
8d9254fc 1// Copyright (C) 2019-2020 Free Software Foundation, Inc.
281ab2fb
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 -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
26template<auto N, typename = void>
27 struct ceil2_valid
28 : std::false_type { };
29
30template<auto N>
31 struct ceil2_valid<N, std::void_t<char[(std::ceil2(N), 1)]>>
32 : std::true_type { };
33
34template<typename T>
35 constexpr T max = std::numeric_limits<T>::max();
36template<typename T>
37 constexpr T maxpow2 = T(1) << (std::numeric_limits<T>::digits - 1);
38
39static_assert( ceil2_valid<maxpow2<unsigned char>>() );
40static_assert( !ceil2_valid<maxpow2<unsigned char> + (unsigned char)1>() );
41
42static_assert( !ceil2_valid<max<unsigned char>>() );
43static_assert( !ceil2_valid<max<unsigned char> - (unsigned char)1>() );
44
45static_assert( ceil2_valid<maxpow2<unsigned short>>() );
46static_assert( !ceil2_valid<maxpow2<unsigned short> + (unsigned short)1>() );
47static_assert( !ceil2_valid<max<unsigned short>>() );
48static_assert( !ceil2_valid<max<unsigned short> - (unsigned short)1>() );
49
50static_assert( ceil2_valid<maxpow2<unsigned int>>() );
51static_assert( !ceil2_valid<maxpow2<unsigned int> + 1u>() );
52static_assert( !ceil2_valid<max<unsigned int>>() );
53static_assert( !ceil2_valid<max<unsigned int> - 1u>() );
54
55static_assert( ceil2_valid<maxpow2<unsigned long>>() );
56static_assert( !ceil2_valid<maxpow2<unsigned long> + 1ul>() );
57static_assert( !ceil2_valid<max<unsigned long>>() );
58static_assert( !ceil2_valid<max<unsigned long> - 1ul>() );
59
60static_assert( ceil2_valid<maxpow2<unsigned long long>>() );
61static_assert( !ceil2_valid<maxpow2<unsigned long long> + 1ull>() );
62static_assert( !ceil2_valid<max<unsigned long long>>() );
63static_assert( !ceil2_valid<max<unsigned long long> - 1ull>() );
64
65void
66test01()
67{
68 std::ceil2( maxpow2<unsigned> + 1u ); // should fail __glibcxx_assert
69}
70
71int main()
72{
73 test01();
74}