MCPcopy Create free account
hub / github.com/dcleblanc/SafeInt / SubVerifyInt8Int8

Function SubVerifyInt8Int8

Test/SubVerify.cpp:3400–3456  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3398};
3399
3400void SubVerifyInt8Int8()
3401{
3402 size_t i;
3403
3404 for( i = 0; i < COUNTOF(int8_int8); ++i )
3405 {
3406 std::int8_t ret;
3407 if( SafeSubtract(int8_int8[i].x, int8_int8[i].y, ret) != int8_int8[i].fExpected )
3408 {
3409 cerr << "Error in case int8_int8: ";
3410 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int8[i].x) << ", ";
3411 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int8[i].y) << ", ";
3412 cerr << "expected = " << int8_int8[i].fExpected << endl;
3413 }
3414
3415 // Now test throwing version
3416 bool fSuccess = true;
3417 try
3418 {
3419 SafeInt<std::int8_t> si(int8_int8[i].x);
3420 si -= int8_int8[i].y;
3421 }
3422 catch(...)
3423 {
3424 fSuccess = false;
3425 }
3426
3427 if( fSuccess != int8_int8[i].fExpected )
3428 {
3429 cerr << "Error in case int8_int8 throw (1): ";
3430 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int8[i].x) << ", ";
3431 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int8[i].y) << ", ";
3432 cerr << "expected = " << int8_int8[i].fExpected << endl;
3433 }
3434
3435 // Also need to test the version that assigns back out
3436 // to a plain int, as it has different logic
3437 fSuccess = true;
3438 try
3439 {
3440 std::int8_t x(int8_int8[i].x);
3441 x -= SafeInt<std::int64_t>(int8_int8[i].y);
3442 }
3443 catch(...)
3444 {
3445 fSuccess = false;
3446 }
3447
3448 if( fSuccess != int8_int8[i].fExpected )
3449 {
3450 cerr << "Error in case int8_int8 throw (2): ";
3451 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int8[i].x) << ", ";
3452 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int8[i].y) << ", ";
3453 cerr << "expected = " << int8_int8[i].fExpected << endl;
3454 }
3455 }
3456}
3457

Callers 1

SubVerifyFunction · 0.70

Calls 1

SafeSubtractFunction · 0.50

Tested by

no test coverage detected