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

Function SubVerifyInt8Int16

Test/SubVerify.cpp:3249–3305  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3247};
3248
3249void SubVerifyInt8Int16()
3250{
3251 size_t i;
3252
3253 for( i = 0; i < COUNTOF(int8_int16); ++i )
3254 {
3255 std::int8_t ret;
3256 if( SafeSubtract(int8_int16[i].x, int8_int16[i].y, ret) != int8_int16[i].fExpected )
3257 {
3258 cerr << "Error in case int8_int16: ";
3259 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int16[i].x) << ", ";
3260 cerr << hex << setw(4) << setfill('0') << int8_int16[i].y << ", ";
3261 cerr << "expected = " << int8_int16[i].fExpected << endl;
3262 }
3263
3264 // Now test throwing version
3265 bool fSuccess = true;
3266 try
3267 {
3268 SafeInt<std::int8_t> si(int8_int16[i].x);
3269 si -= int8_int16[i].y;
3270 }
3271 catch(...)
3272 {
3273 fSuccess = false;
3274 }
3275
3276 if( fSuccess != int8_int16[i].fExpected )
3277 {
3278 cerr << "Error in case int8_int16 throw (1): ";
3279 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int16[i].x) << ", ";
3280 cerr << hex << setw(4) << setfill('0') << int8_int16[i].y << ", ";
3281 cerr << "expected = " << int8_int16[i].fExpected << endl;
3282 }
3283
3284 // Also need to test the version that assigns back out
3285 // to a plain int, as it has different logic
3286 fSuccess = true;
3287 try
3288 {
3289 std::int8_t x(int8_int16[i].x);
3290 x -= SafeInt<std::int64_t>(int8_int16[i].y);
3291 }
3292 catch(...)
3293 {
3294 fSuccess = false;
3295 }
3296
3297 if( fSuccess != int8_int16[i].fExpected )
3298 {
3299 cerr << "Error in case int8_int16 throw (2): ";
3300 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int16[i].x) << ", ";
3301 cerr << hex << setw(4) << setfill('0') << int8_int16[i].y << ", ";
3302 cerr << "expected = " << int8_int16[i].fExpected << endl;
3303 }
3304 }
3305}
3306

Callers 1

SubVerifyFunction · 0.70

Calls 1

SafeSubtractFunction · 0.50

Tested by

no test coverage detected