Add additional VECTOR2 tests

Check overflow, addition using alternate value representations and
clamping
This commit is contained in:
Seth Hillbrand 2024-06-10 15:18:51 -07:00
parent 37191720ca
commit 8dd93501cf
1 changed files with 63 additions and 32 deletions

View File

@ -75,30 +75,30 @@ BOOST_AUTO_TEST_CASE( test_casting )
VECTOR2<float> vfloat( 4.0f, 3.0f );
VECTOR2<unsigned> vunsigned( 4, 3 );
BOOST_CHECK( vint == VECTOR2I( vdouble ) );
BOOST_CHECK( vint == VECTOR2I( vlong ) );
BOOST_CHECK( vint == VECTOR2I( vfloat ) );
BOOST_CHECK( vint == VECTOR2I( vunsigned ) );
BOOST_CHECK_EQUAL( vint, VECTOR2I( vdouble ) );
BOOST_CHECK_EQUAL( vint, VECTOR2I( vlong ) );
BOOST_CHECK_EQUAL( vint, VECTOR2I( vfloat ) );
BOOST_CHECK_EQUAL( vint, VECTOR2I( vunsigned ) );
BOOST_CHECK( vdouble == VECTOR2D( vint ) );
BOOST_CHECK( vdouble == VECTOR2D( vlong ) );
BOOST_CHECK( vdouble == VECTOR2D( vfloat ) );
BOOST_CHECK( vdouble == VECTOR2D( vunsigned ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vint ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vlong ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vfloat ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( vunsigned ) );
BOOST_CHECK( vlong == VECTOR2L( vint ) );
BOOST_CHECK( vlong == VECTOR2L( vdouble ) );
BOOST_CHECK( vlong == VECTOR2L( vfloat ) );
BOOST_CHECK( vlong == VECTOR2L( vunsigned ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( vint ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( vdouble ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( vfloat ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( vunsigned ) );
BOOST_CHECK( vfloat == VECTOR2<float>( vint ) );
BOOST_CHECK( vfloat == VECTOR2<float>( vdouble ) );
BOOST_CHECK( vfloat == VECTOR2<float>( vlong ) );
BOOST_CHECK( vfloat == VECTOR2<float>( vunsigned ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vint ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vdouble ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vlong ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( vunsigned ) );
BOOST_CHECK( vunsigned == VECTOR2<unsigned>( vint ) );
BOOST_CHECK( vunsigned == VECTOR2<unsigned>( vdouble ) );
BOOST_CHECK( vunsigned == VECTOR2<unsigned>( vlong ) );
BOOST_CHECK( vunsigned == VECTOR2<unsigned>( vfloat ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vint ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vdouble ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vlong ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( vfloat ) );
// Check that negative values are handled correctly
vint = vint - 1;
@ -107,12 +107,36 @@ BOOST_AUTO_TEST_CASE( test_casting )
vfloat = vfloat - 1;
vunsigned = vunsigned - 1;
BOOST_CHECK( vint == VECTOR2I( 3, 2 ) );
BOOST_CHECK( vdouble == VECTOR2D( 3.0, 2.0 ) );
BOOST_CHECK( vlong == VECTOR2L( 3, 2 ) );
BOOST_CHECK( vfloat == VECTOR2<float>( 3.0f, 2.0f ) );
BOOST_CHECK( vunsigned == VECTOR2<unsigned>( 3, 2 ) );
BOOST_CHECK_EQUAL( vint, VECTOR2I( 3, 2 ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( 3.0, 2.0 ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( 3, 2 ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( 3.0f, 2.0f ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 3, 2 ) );
// Check that subtracting unsigned values works correctly
vint = vint - (unsigned)1;
vdouble = vdouble - (unsigned)1;
vlong = vlong - (unsigned)1;
vfloat = vfloat - (unsigned)1;
vunsigned = vunsigned - (unsigned)1;
BOOST_CHECK_EQUAL( vint, VECTOR2I( 2, 1 ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( 2.0, 1.0 ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( 2, 1 ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( 2.0f, 1.0f ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 2, 1 ) );
vint = vint - 5.0;
vdouble = vdouble - 5.0;
vlong = vlong - 5.0;
vfloat = vfloat - 5.0;
vunsigned = vunsigned - 5.0;
BOOST_CHECK_EQUAL( vint, VECTOR2I( -3, -4 ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( -3.0, -4.0 ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( -3, -4 ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( -3.0f, -4.0f ) );
BOOST_CHECK_EQUAL( vunsigned, VECTOR2<unsigned>( 0, 0 ) ); // unsigned can't be negative, so we clamp
// Check that negative initial values are handled correctly
vint = VECTOR2I( -4, -3 );
@ -125,17 +149,24 @@ BOOST_AUTO_TEST_CASE( test_casting )
vlong = vlong - 1;
vfloat = vfloat - 1;
BOOST_CHECK( vint == VECTOR2I( -5, -4 ) );
BOOST_CHECK( vdouble == VECTOR2D( -5.0, -4.0 ) );
BOOST_CHECK( vlong == VECTOR2L( -5, -4 ) );
BOOST_CHECK( vfloat == VECTOR2<float>( -5.0f, -4.0f ) );
BOOST_CHECK_EQUAL( vint, VECTOR2I( -5, -4 ) );
BOOST_CHECK_EQUAL( vdouble, VECTOR2D( -5.0, -4.0 ) );
BOOST_CHECK_EQUAL( vlong, VECTOR2L( -5, -4 ) );
BOOST_CHECK_EQUAL( vfloat, VECTOR2<float>( -5.0f, -4.0f ) );
auto add = vint + vdouble;
BOOST_CHECK( add == VECTOR2D( -10.0, -8.0 ) );
BOOST_CHECK_EQUAL( add, VECTOR2D( -10.0, -8.0 ) );
auto sub = vint - 2 * vlong;
BOOST_CHECK( sub.x == 5 );
BOOST_CHECK( sub.y == 4 );
BOOST_CHECK_EQUAL( sub.x, 5 );
BOOST_CHECK_EQUAL( sub.y, 4 );
vunsigned = VECTOR2<unsigned>( std::numeric_limits<unsigned>::max(), std::numeric_limits<unsigned>::max() );
vint = VECTOR2I( vunsigned );
BOOST_CHECK_EQUAL( vint.x, std::numeric_limits<int>::max() );
vunsigned += 1;
BOOST_CHECK_EQUAL( vunsigned.x, 0 );
}
BOOST_AUTO_TEST_SUITE_END()