| 5318 | }; |
| 5319 | |
| 5320 | void AddVerifyUint8Int16() |
| 5321 | { |
| 5322 | size_t i; |
| 5323 | |
| 5324 | for( i = 0; i < COUNTOF(uint8_int16); ++i ) |
| 5325 | { |
| 5326 | std::uint8_t ret; |
| 5327 | if( SafeAdd(uint8_int16[i].x, uint8_int16[i].y, ret) != uint8_int16[i].fExpected ) |
| 5328 | { |
| 5329 | cerr << "Error in case uint8_int16: "; |
| 5330 | cerr << HEX(2) << (0xFF & (unsigned int)uint8_int16[i].x) << ", "; |
| 5331 | cerr << HEX(4) << uint8_int16[i].y << ", "; |
| 5332 | cerr << "expected = " << uint8_int16[i].fExpected << endl; |
| 5333 | } |
| 5334 | |
| 5335 | // Now test throwing version |
| 5336 | bool fSuccess = true; |
| 5337 | try |
| 5338 | { |
| 5339 | SafeInt<std::uint8_t> si(uint8_int16[i].x); |
| 5340 | si += uint8_int16[i].y; |
| 5341 | } |
| 5342 | catch(...) |
| 5343 | { |
| 5344 | fSuccess = false; |
| 5345 | } |
| 5346 | |
| 5347 | if( fSuccess != uint8_int16[i].fExpected ) |
| 5348 | { |
| 5349 | cerr << "Error in case uint8_int16 throw (1): "; |
| 5350 | cerr << HEX(2) << (0xFF & (unsigned int)uint8_int16[i].x) << ", "; |
| 5351 | cerr << HEX(4) << uint8_int16[i].y << ", "; |
| 5352 | cerr << "expected = " << uint8_int16[i].fExpected << endl; |
| 5353 | } |
| 5354 | |
| 5355 | // Also need to test the version that assigns back out |
| 5356 | // to a plain int, as it has different logic |
| 5357 | fSuccess = true; |
| 5358 | try |
| 5359 | { |
| 5360 | std::uint8_t x(uint8_int16[i].x); |
| 5361 | x += SafeInt<std::int16_t>(uint8_int16[i].y); |
| 5362 | } |
| 5363 | catch(...) |
| 5364 | { |
| 5365 | fSuccess = false; |
| 5366 | } |
| 5367 | |
| 5368 | if( fSuccess != uint8_int16[i].fExpected ) |
| 5369 | { |
| 5370 | cerr << "Error in case uint8_int16 throw (2): "; |
| 5371 | cerr << HEX(2) << (0xFF & (unsigned int)uint8_int16[i].x) << ", "; |
| 5372 | cerr << HEX(4) << uint8_int16[i].y << ", "; |
| 5373 | cerr << "expected = " << uint8_int16[i].fExpected << endl; |
| 5374 | } |
| 5375 | } |
| 5376 | } |
| 5377 | |