]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/test/test_constexpr.cpp
1 // Copyright (c) 2014 Robert Ramey
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)
7 // Test constexpr operations on literals
11 #include <boost/safe_numerics/safe_integer_literal.hpp>
12 #include <boost/safe_numerics/native.hpp>
13 #include <boost/safe_numerics/exception.hpp>
15 using namespace boost::safe_numerics
;
17 template<std::uintmax_t N
>
18 using compile_time_value
= safe_unsigned_literal
<N
, native
, loose_trap_policy
>;
21 constexpr const compile_time_value
<1000> x
;
22 constexpr const compile_time_value
<1> y
;
24 // should compile and execute without problem
25 std::cout
<< x
<< '\n';
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");
31 constexpr auto x_minus_y
= x
- y
;
32 static_assert(999 == x_minus_y
, "999 == x - y");
34 constexpr auto x_times_y
= x
* y
;
35 static_assert(1000 == x_times_y
, "1000 == x * y");
37 constexpr auto x_and_y
= x
& y
;
38 static_assert(0 == x_and_y
, "0 == x & y");
40 constexpr auto x_or_y
= x
| y
;
41 static_assert(1001 == x_or_y
, "1001 == x | y");
43 constexpr auto x_xor_y
= x
^ y
;
44 static_assert(1001 == x_xor_y
, "1001 == x ^ y");
46 constexpr auto x_divided_by_y
= x
/ y
;
47 static_assert(1000 == x_divided_by_y
, "1000 == x / y");
49 constexpr auto x_mod_y
= x
% y
;
50 static_assert(0 == x_mod_y
, "0 == x % y");
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
;
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");
61 constexpr auto not_x
= ~x
;