• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright (c) 2012 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 #include <iostream>
8 
9 #include <boost/safe_numerics/checked_integer.hpp>
10 
11 // test conversion to T2 from different literal types
12 template<class T2, class T1>
test_cast_constexpr(const T1 & v1,char result)13 constexpr bool test_cast_constexpr(
14     const T1 & v1,
15     char result
16 ){
17     const boost::safe_numerics::checked_result<T2> r2 =
18         boost::safe_numerics::checked::cast<T2>(v1);
19     return ('x' == result) ?
20         r2.exception() :
21         ! r2.exception();
22 }
23 
24 #include "test_checked_cast.hpp"
25 
26 #include <boost/mp11/algorithm.hpp> // mp_iota
27 #include <boost/mp11/list.hpp> // mp_first, mp_second, mp_size, mp_is_list
28 
29 using namespace boost::mp11;
30 
31 // given a list of integral constants I, return a list of values
32 template<typename I>
33 struct get_values {
34     static_assert(mp_is_list<I>(), "must be a list of two types");
35     static_assert(2 == mp_size<I>::value, "must be a list of two types");
36     static constexpr const size_t first = mp_first<I>(); // index of first argument
37     static constexpr const size_t second = mp_second<I>();// index of second argument
38 };
39 
40 template<typename I>
41 struct test_pair_constexpr {
42     using pair = get_values<I>;
43     using TResult = mp_at<test_types, mp_first<I>>;
44     using TArg = typename mp_at<test_values, mp_second<I>>::value_type;
45     static constexpr TArg v = mp_at<test_values, mp_second<I>>()();
46     static constexpr bool value = test_cast_constexpr<TResult>(
47         v,
48         test_result_cast[pair::first][pair::second]
49     );
50 };
51 
main()52 int main(){
53     // list of indices for values (integral constants)
54     using value_indices = mp_iota_c<mp_size<test_values>::value>;
55     // list of indices for types (integral constants)
56     using type_indices = mp_iota_c<mp_size<test_types>::value>;
57     // all combinations of type index, value index
58     using index_pairs = mp_product<mp_list, type_indices, value_indices>;
59     // test all type/value pairs to verify that cast can be done.
60     static_assert(
61         mp_all_of<index_pairs, test_pair_constexpr>::value,
62         "all type/value pairs correctly cast"
63     );
64     return 0;
65 }
66