1// Copyright (c) 2017 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 construction assignments
8
9#include <iostream>
10
11#include <boost/safe_numerics/safe_integer.hpp>
12
13template <class T>
14using safe_t = boost::safe_numerics::safe<
15 T,
16 boost::safe_numerics::native
17>;
18
19#include <boost/mp11/list.hpp>
20#include <boost/mp11/algorithm.hpp>
21#include "test_values.hpp"
22
23// note: same test matrix as used in test_checked. Here we test all combinations
24// safe and unsafe integers. in test_checked we test all combinations of
25// integer primitives
26
27const char *test_assignment_result[boost::mp11::mp_size<test_values>::value] = {
28// 0 0 0 0
29// 012345670123456701234567012345670
30// 012345678901234567890123456789012
31/* 0*/ ".....xx..xx..xx...xx.xxx.xxx.xxx.",
32/* 1*/ ".....xx..xx..xx...xx.xxx.xxx.xxx.",
33/* 2*/ ".....xx..xx..xx...xx.xxx.xxx.xxx.",
34/* 3*/ ".....xx..xx..xx...xx.xxx.xxx.xxx.",
35/* 4*/ ".........xx..xx.......xx.xxx.xxx.",
36/* 5*/ ".........xx..xx.......xx.xxx.xxx.",
37/* 6*/ ".........xx..xx.......xx.xxx.xxx.",
38/* 7*/ ".........xx..xx.......xx.xxx.xxx.",
39
40/* 8*/ ".............xx...........xx.xxx.",
41/* 9*/ ".............xx...........xx.xxx.",
42/*10*/ ".............xx...........xx.xxx.",
43/*11*/ ".............xx...........xx.xxx.",
44/*12*/ "..............................xx.",
45/*13*/ "..............................xx.",
46/*14*/ "..............................xx.",
47/*15*/ "..............................xx.",
48
49// 0 0 0 0
50// 012345670123456701234567012345670
51// 012345678901234567890123456789012
52/*16*/ "..xx.xxx.xxx.xxx.....xxx.xxx.xxx.",
53/*17*/ "..xx.xxx.xxx.xxx.....xxx.xxx.xxx.",
54/*18*/ "..xx.xxx.xxx.xxx.....xxx.xxx.xxx.",
55/*19*/ "..xx.xxx.xxx.xxx.....xxx.xxx.xxx.",
56/*20*/ "..xx..xx.xxx.xxx.........xxx.xxx.",
57/*21*/ "..xx..xx.xxx.xxx.........xxx.xxx.",
58/*22*/ "..xx..xx.xxx.xxx.........xxx.xxx.",
59/*23*/ "..xx..xx.xxx.xxx.........xxx.xxx.",
60
61/*24*/ "..xx..xx..xx.xxx.............xxx.",
62/*25*/ "..xx..xx..xx.xxx.............xxx.",
63/*26*/ "..xx..xx..xx.xxx.............xxx.",
64/*27*/ "..xx..xx..xx.xxx.............xxx.",
65/*28*/ "..xx..xx..xx..xx.................",
66/*29*/ "..xx..xx..xx..xx.................",
67/*30*/ "..xx..xx..xx..xx.................",
68/*31*/ "..xx..xx..xx..xx.................",
69// 012345678901234567890123456789012
70/*32*/ ".....xx..xx..xx...xx.xxx.xxx.xxx."
71};
72
73#include <boost/mp11/algorithm.hpp>
74#include <boost/core/demangle.hpp>
75
76template <class T>
77using safe_t = boost::safe_numerics::safe<
78 T,
79 boost::safe_numerics::native
80>;
81#include "test_assignment.hpp"
82
83using namespace boost::mp11;
84
85template<typename L>
86struct test {
87 static_assert(mp_is_list<L>(), "must be a list of integral constants");
88 bool m_error;
89 test(bool b = true) : m_error(b) {}
90 operator bool(){
91 return m_error;
92 }
93 template<typename T>
94 void operator()(const T &){
95 static_assert(mp_is_list<T>(), "must be a list of two integral constants");
96 constexpr size_t i1 = mp_first<T>(); // index of first argument
97 constexpr size_t i2 = mp_second<T>();// index of second argument
98 std::cout << i1 << ',' << i2 << ',';
99 using T1 = typename boost::mp11::mp_at_c<L, i1>::value_type;
100 using T2 = typename boost::mp11::mp_at_c<L, i2>::value_type;
101 m_error &= test_assignment<T1, T2>(
102 boost::mp11::mp_at_c<L, i1>(), // value of first argument
103 boost::mp11::mp_at_c<L, i2>(), // value of second argument
104 boost::core::demangle(name: typeid(T1).name()).c_str(),
105 boost::core::demangle(name: typeid(T2).name()).c_str(),
106 test_assignment_result[i1][i2]
107 );
108 }
109};
110
111int main(int, char *[]){
112
113 // TEST_EACH_VALUE_PAIR
114 test<test_values> rval(true);
115
116 using value_indices = mp_iota_c<mp_size<test_values>::value>;
117 mp_for_each<
118 mp_product<mp_list, value_indices, value_indices>
119 >(f&: rval);
120
121 std::cout << (rval ? "success!" : "failure") << std::endl;
122 return ! rval ;
123}
124

source code of boost/libs/safe_numerics/test/test_assignment.cpp