Skip to content

Commit

Permalink
Add more tests for safe<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed Mar 29, 2023
1 parent 1ff29a8 commit 2661364
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/safe_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ BOOST_AUTO_TEST_CASE(safe_test)
BOOST_CHECK_THROW( safe<int8_t>( -129 ), fc::underflow_exception );
BOOST_CHECK_THROW( safe<int8_t>( int16_t(-129) ), fc::underflow_exception );

BOOST_CHECK_EQUAL( safe<int8_t>( uint16_t(127) ).value, 127 );

BOOST_CHECK_THROW( safe<int8_t>( uint16_t(128) ), fc::overflow_exception );

BOOST_CHECK_THROW( safe<uint16_t>( -1 ), fc::underflow_exception );
BOOST_CHECK_THROW( safe<uint16_t>( int16_t(-1) ), fc::underflow_exception );

BOOST_CHECK_EQUAL( safe<uint8_t>( uint16_t(255) ).value, 255U );

BOOST_CHECK_THROW( safe<uint8_t>( int16_t(256) ), fc::overflow_exception );
BOOST_CHECK_THROW( safe<uint8_t>( uint16_t(256) ), fc::overflow_exception );

Expand Down Expand Up @@ -64,6 +68,12 @@ BOOST_AUTO_TEST_CASE(safe_test)
BOOST_CHECK( safe<int8_t>( -2 ) * safe<int8_t>( 64 ) == -128 );
BOOST_CHECK( safe<int8_t>( 2 ) * safe<int8_t>( -64 ) == -128 );

BOOST_CHECK( safe<int8_t>( 0 ) * safe<int8_t>( 0 ) == 0 );
BOOST_CHECK( safe<int8_t>( 0 ) * safe<int8_t>( 127 ) == 0 );
BOOST_CHECK( safe<int8_t>( 0 ) * safe<int8_t>( -128 ) == 0 );
BOOST_CHECK( safe<int8_t>( 127 ) * safe<int8_t>( 0 ) == 0 );
BOOST_CHECK( safe<int8_t>( -128 ) * safe<int8_t>( 0 ) == 0 );

BOOST_CHECK_THROW( safe<int8_t>( 100 ) * 2, fc::overflow_exception );
BOOST_CHECK_THROW( 2 * safe<int8_t>( 100 ), fc::overflow_exception );
BOOST_CHECK_THROW( safe<int8_t>( 2 ) * safe<int8_t>( 100 ), fc::overflow_exception );
Expand All @@ -89,6 +99,8 @@ BOOST_AUTO_TEST_CASE(safe_test)
BOOST_CHECK( safe<int8_t>( 100 ) / safe<int8_t>( -11 ) == 100 / (-11) );
BOOST_CHECK( safe<int8_t>( -100 ) / safe<int8_t>( 11 ) == (-100) / 11 );

BOOST_CHECK( safe<int8_t>( -127 ) / safe<int8_t>( -1 ) == (-127) / (-1) );

BOOST_CHECK_THROW( safe<int8_t>( -128 ) / safe<int8_t>( 0 ), fc::divide_by_zero_exception );
BOOST_CHECK_THROW( safe<int8_t>( -128 ) / safe<int8_t>( -1 ), fc::overflow_exception );

Expand All @@ -101,6 +113,8 @@ BOOST_AUTO_TEST_CASE(safe_test)
BOOST_CHECK( safe<int8_t>( 100 ) % safe<int8_t>( -11 ) == 100 % (-11) );
BOOST_CHECK( safe<int8_t>( -100 ) % safe<int8_t>( 11 ) == (-100) % 11 );

BOOST_CHECK( safe<int8_t>( -127 ) % safe<int8_t>( -1 ) == (-127) % (-1) );

BOOST_CHECK_THROW( safe<int8_t>( -128 ) % safe<int8_t>( 0 ), fc::divide_by_zero_exception );
BOOST_CHECK_THROW( safe<int8_t>( -128 ) % safe<int8_t>( -1 ), fc::overflow_exception );

Expand Down Expand Up @@ -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;

Expand All @@ -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<uint8_t> 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<int8_t>( 1 ) != safe<int8_t>( 2 ) );
BOOST_CHECK( 1 != safe<int8_t>( 2 ) );
Expand Down

0 comments on commit 2661364

Please sign in to comment.