]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/test/test_constexpr.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / safe_numerics / test / test_constexpr.cpp
1 // Copyright (c) 2014 Robert Ramey
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Test constexpr operations on literals
8
9 #include <iostream>
10
11 #include <boost/safe_numerics/safe_integer_literal.hpp>
12 #include <boost/safe_numerics/native.hpp>
13 #include <boost/safe_numerics/exception.hpp>
14
15 using namespace boost::safe_numerics;
16
17 template<std::uintmax_t N>
18 using compile_time_value = safe_unsigned_literal<N, native, loose_trap_policy>;
19
20 int main(){
21 constexpr const compile_time_value<1000> x;
22 constexpr const compile_time_value<1> y;
23
24 // should compile and execute without problem
25 std::cout << x << '\n';
26
27 // all the following statements should compile
28 constexpr auto x_plus_y = x + y;
29 static_assert(1001 == x_plus_y, "1001 == x + y");
30
31 constexpr auto x_minus_y = x - y;
32 static_assert(999 == x_minus_y, "999 == x - y");
33
34 constexpr auto x_times_y = x * y;
35 static_assert(1000 == x_times_y, "1000 == x * y");
36
37 constexpr auto x_and_y = x & y;
38 static_assert(0 == x_and_y, "0 == x & y");
39
40 constexpr auto x_or_y = x | y;
41 static_assert(1001 == x_or_y, "1001 == x | y");
42
43 constexpr auto x_xor_y = x ^ y;
44 static_assert(1001 == x_xor_y, "1001 == x ^ y");
45
46 constexpr auto x_divided_by_y = x / y;
47 static_assert(1000 == x_divided_by_y, "1000 == x / y");
48
49 constexpr auto x_mod_y = x % y;
50 static_assert(0 == x_mod_y, "0 == x % y");
51
52 // this should fail compilation since a positive unsigned number
53 // can't be converted to a negative value of the same type
54 constexpr auto minus_x = -x;
55
56 // should compile OK since the negative inverse of a zero is still zero
57 constexpr const compile_time_value<0> x0;
58 constexpr auto minus_x0 = -x0; // should compile OK
59 static_assert(0 == minus_x0, "0 == -x where x == 0");
60
61 constexpr auto not_x = ~x;
62
63 return 0;
64 }