diff options
Diffstat (limited to '')
-rw-r--r-- | src/boost/libs/safe_numerics/test/test_checked_and_constexpr.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/boost/libs/safe_numerics/test/test_checked_and_constexpr.cpp b/src/boost/libs/safe_numerics/test/test_checked_and_constexpr.cpp new file mode 100644 index 000000000..45f8e42da --- /dev/null +++ b/src/boost/libs/safe_numerics/test/test_checked_and_constexpr.cpp @@ -0,0 +1,98 @@ +// Copyright (c) 2018 Robert Ramey +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/safe_numerics/checked_result.hpp> +#include <boost/safe_numerics/checked_result_operations.hpp> +#include <boost/safe_numerics/checked_integer.hpp> + +// note: T should be of tyme checked_result<R> for some integer type R +template<class T> +constexpr bool test_checked_and( + const T & v1, + const T & v2, + char expected_result +){ + using namespace boost::safe_numerics; + const T result = v1 & v2; + switch(expected_result){ + case '.': + return ! result.exception(); + case '-': + return safe_numerics_error::negative_overflow_error == result.m_e; + case '+': + return safe_numerics_error::positive_overflow_error == result.m_e; + case '!': + return safe_numerics_error::range_error == result.m_e; + } + return false; +} + +#include "test_checked_and.hpp" + +template<typename T, typename First, typename Second> +struct test_signed_pair { + static const std::size_t i = First(); + static const std::size_t j = Second(); + // note: is constexpr really required here? compilers disagree! + constexpr static const bool value = test_checked_and( + signed_values<T>[i], + signed_values<T>[j], + signed_and_results[i][j] + ); +}; + +template<typename T, typename First, typename Second> +struct test_unsigned_pair { + static const std::size_t i = First(); + static const std::size_t j = Second(); + // note: is constexpr really required here? compilers disagree! + constexpr static const bool value = test_checked_and( + unsigned_values<T>[i], + unsigned_values<T>[j], + unsigned_and_results[i][j] + ); +}; + +#include "check_symmetry.hpp" +#include <boost/mp11/algorithm.hpp> + +int main(){ + static_assert( + check_symmetry(signed_and_results), + "sanity check on test matrix - should be symmetrical" + ); + static_assert( + check_symmetry(unsigned_and_results), + "sanity check on test matrix - should be symmetrical" + ); + + using namespace boost::mp11; + + static_assert( + mp_all_of< + mp_product< + test_signed_pair, + signed_test_types, + signed_value_indices, signed_value_indices + >, + mp_to_bool + >(), + "all values for all signed types correctly anded" + ); + + static_assert( + mp_all_of< + mp_product< + test_unsigned_pair, + unsigned_test_types, + unsigned_value_indices, unsigned_value_indices + >, + mp_to_bool + >(), + "all values for all unsigned types correctly anded" + ); + return 0; +} |