From c59eb627ad1a41795ca492b3db36af96af9f16b2 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 28 Apr 2019 00:58:51 +0300 Subject: [PATCH] Add endian_arithmetic_test --- test/Jamfile.v2 | 3 + test/endian_arithmetic_test.cpp | 144 ++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 test/endian_arithmetic_test.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 9e015b7..f1eea02 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -65,3 +65,6 @@ run endian_store_test.cpp : : : BOOST_ENDIAN_NO_INTRINSICS : endian_stor run endian_ld_st_roundtrip_test.cpp ; run endian_ld_st_roundtrip_test.cpp : : : BOOST_ENDIAN_NO_INTRINSICS : endian_ld_st_roundtrip_test_ni ; + +run endian_arithmetic_test.cpp ; +run endian_arithmetic_test.cpp : : : BOOST_ENDIAN_NO_INTRINSICS : endian_arithmetic_test_ni ; diff --git a/test/endian_arithmetic_test.cpp b/test/endian_arithmetic_test.cpp new file mode 100644 index 0000000..d127b5b --- /dev/null +++ b/test/endian_arithmetic_test.cpp @@ -0,0 +1,144 @@ +// Copyright 2019 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +template void test_( T const& x ) +{ + boost::endian::endian_arithmetic y( x ); + + BOOST_TEST_EQ( +x, +y ); + + BOOST_TEST_EQ( x + x, y + y ); + BOOST_TEST_EQ( x - x, y - y ); + + BOOST_TEST_EQ( x * x, y * y ); + BOOST_TEST_EQ( x / x, y / y ); + BOOST_TEST_EQ( x % x, y % y ); + + BOOST_TEST_EQ( x & x, y & y ); + BOOST_TEST_EQ( x | x, y | y ); + BOOST_TEST_EQ( x ^ x, y ^ y ); + + BOOST_TEST_EQ( x << 1, y << 1 ); + BOOST_TEST_EQ( x >> 1, y >> 1 ); + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2 += x, y2 += y ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2 -= x, y2 -= y ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2 *= x, y2 *= y ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2 /= x, y2 /= y ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2 %= x, y2 %= y ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2 &= x, y2 &= y ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2 |= x, y2 |= y ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2 ^= x, y2 ^= y ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2 <<= 1, y2 <<= 1 ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2 >>= 1, y2 >>= 1 ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( ++x2, ++y2 ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( --x2, --y2 ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2++, y2++ ); + } + + { + T x2( x ); + boost::endian::endian_arithmetic y2( y ); + + BOOST_TEST_EQ( x2--, y2-- ); + } +} + +template void test( T const& x ) +{ + test_( x ); + test_( x ); + test_( x ); + test_( x ); +} + +int main() +{ + test( 0x01020304 ); + + return boost::report_errors(); +}