diff --git a/tests/safe_tests.cpp b/tests/safe_tests.cpp index d37d2170..a1968b32 100644 --- a/tests/safe_tests.cpp +++ b/tests/safe_tests.cpp @@ -21,11 +21,15 @@ BOOST_AUTO_TEST_CASE(safe_test) BOOST_CHECK_THROW( safe( -129 ), fc::underflow_exception ); BOOST_CHECK_THROW( safe( int16_t(-129) ), fc::underflow_exception ); + BOOST_CHECK_EQUAL( safe( uint16_t(127) ).value, 127 ); + BOOST_CHECK_THROW( safe( uint16_t(128) ), fc::overflow_exception ); BOOST_CHECK_THROW( safe( -1 ), fc::underflow_exception ); BOOST_CHECK_THROW( safe( int16_t(-1) ), fc::underflow_exception ); + BOOST_CHECK_EQUAL( safe( uint16_t(255) ).value, 255U ); + BOOST_CHECK_THROW( safe( int16_t(256) ), fc::overflow_exception ); BOOST_CHECK_THROW( safe( uint16_t(256) ), fc::overflow_exception ); @@ -64,6 +68,12 @@ BOOST_AUTO_TEST_CASE(safe_test) BOOST_CHECK( safe( -2 ) * safe( 64 ) == -128 ); BOOST_CHECK( safe( 2 ) * safe( -64 ) == -128 ); + BOOST_CHECK( safe( 0 ) * safe( 0 ) == 0 ); + BOOST_CHECK( safe( 0 ) * safe( 127 ) == 0 ); + BOOST_CHECK( safe( 0 ) * safe( -128 ) == 0 ); + BOOST_CHECK( safe( 127 ) * safe( 0 ) == 0 ); + BOOST_CHECK( safe( -128 ) * safe( 0 ) == 0 ); + BOOST_CHECK_THROW( safe( 100 ) * 2, fc::overflow_exception ); BOOST_CHECK_THROW( 2 * safe( 100 ), fc::overflow_exception ); BOOST_CHECK_THROW( safe( 2 ) * safe( 100 ), fc::overflow_exception ); @@ -89,6 +99,8 @@ BOOST_AUTO_TEST_CASE(safe_test) BOOST_CHECK( safe( 100 ) / safe( -11 ) == 100 / (-11) ); BOOST_CHECK( safe( -100 ) / safe( 11 ) == (-100) / 11 ); + BOOST_CHECK( safe( -127 ) / safe( -1 ) == (-127) / (-1) ); + BOOST_CHECK_THROW( safe( -128 ) / safe( 0 ), fc::divide_by_zero_exception ); BOOST_CHECK_THROW( safe( -128 ) / safe( -1 ), fc::overflow_exception ); @@ -101,6 +113,8 @@ BOOST_AUTO_TEST_CASE(safe_test) BOOST_CHECK( safe( 100 ) % safe( -11 ) == 100 % (-11) ); BOOST_CHECK( safe( -100 ) % safe( 11 ) == (-100) % 11 ); + BOOST_CHECK( safe( -127 ) % safe( -1 ) == (-127) % (-1) ); + BOOST_CHECK_THROW( safe( -128 ) % safe( 0 ), fc::divide_by_zero_exception ); BOOST_CHECK_THROW( safe( -128 ) % safe( -1 ), fc::overflow_exception ); @@ -148,6 +162,7 @@ BOOST_AUTO_TEST_CASE(safe_test) BOOST_CHECK_THROW( ++si, fc::overflow_exception ); BOOST_CHECK_THROW( si*=2, fc::overflow_exception ); BOOST_CHECK_THROW( si*=(-2), fc::underflow_exception ); + BOOST_CHECK_EQUAL( si.value, 127 ); si = -128; @@ -159,6 +174,48 @@ BOOST_AUTO_TEST_CASE(safe_test) BOOST_CHECK_THROW( si%=(-1), fc::overflow_exception ); BOOST_CHECK_THROW( si/=(0), fc::divide_by_zero_exception ); BOOST_CHECK_THROW( si%=(0), fc::divide_by_zero_exception ); + BOOST_CHECK_EQUAL( si.value, -128 ); + + safe su = 0; + + BOOST_CHECK( (su+=1) == 1U ); + BOOST_CHECK_EQUAL( su.value, 1U ); + BOOST_CHECK( (++su) == 2U ); + BOOST_CHECK_EQUAL( su.value, 2U ); + BOOST_CHECK( (su++) == 2U ); + BOOST_CHECK_EQUAL( su.value, 3U ); + + BOOST_CHECK( (su*=5) == 15U ); + BOOST_CHECK_EQUAL( su.value, 15U ); + + BOOST_CHECK( (su/=2) == 7U ); + BOOST_CHECK_EQUAL( su.value, 7U ); + + BOOST_CHECK( (su%=4) == 3U ); + BOOST_CHECK_EQUAL( su.value, 3U ); + + BOOST_CHECK( (su-=1) == 2U ); + BOOST_CHECK_EQUAL( su.value, 2U ); + BOOST_CHECK( (--su) == 1U ); + BOOST_CHECK_EQUAL( su.value, 1 ); + BOOST_CHECK( (su--) == 1U ); + BOOST_CHECK_EQUAL( su.value, 0U ); + + BOOST_CHECK_THROW( su-=1, fc::underflow_exception ); + BOOST_CHECK_THROW( su--, fc::underflow_exception ); + BOOST_CHECK_THROW( --su, fc::underflow_exception ); + BOOST_CHECK_EQUAL( su.value, 0U ); + + su = 255; + + BOOST_CHECK_THROW( su+=1, fc::overflow_exception ); + BOOST_CHECK_THROW( su++, fc::overflow_exception ); + BOOST_CHECK_THROW( ++su, fc::overflow_exception ); + BOOST_CHECK_THROW( su*=2, fc::overflow_exception ); + BOOST_CHECK_THROW( su*=(-2), fc::underflow_exception ); + BOOST_CHECK_THROW( su/=(0), fc::divide_by_zero_exception ); + BOOST_CHECK_THROW( su%=(0), fc::divide_by_zero_exception ); + BOOST_CHECK_EQUAL( su.value, 255U ); BOOST_CHECK( safe( 1 ) != safe( 2 ) ); BOOST_CHECK( 1 != safe( 2 ) );