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

Function AddVerifyInt8Int16

Test/AddVerify.cpp:6688–6744  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6686};
6687
6688void AddVerifyInt8Int16()
6689{
6690 size_t i;
6691
6692 for( i = 0; i < COUNTOF(int8_int16); ++i )
6693 {
6694 std::int8_t ret;
6695 if( SafeAdd(int8_int16[i].x, int8_int16[i].y, ret) != int8_int16[i].fExpected )
6696 {
6697 cerr << "Error in case int8_int16: ";
6698 cerr << HEX(2) << (0xFF & (int)int8_int16[i].x) << ", ";
6699 cerr << HEX(4) << int8_int16[i].y << ", ";
6700 cerr << "expected = " << int8_int16[i].fExpected << endl;
6701 }
6702
6703 // Now test throwing version
6704 bool fSuccess = true;
6705 try
6706 {
6707 SafeInt<std::int8_t> si(int8_int16[i].x);
6708 si += int8_int16[i].y;
6709 }
6710 catch(...)
6711 {
6712 fSuccess = false;
6713 }
6714
6715 if( fSuccess != int8_int16[i].fExpected )
6716 {
6717 cerr << "Error in case int8_int16 throw (1): ";
6718 cerr << HEX(2) << (0xFF & (int)int8_int16[i].x) << ", ";
6719 cerr << HEX(4) << int8_int16[i].y << ", ";
6720 cerr << "expected = " << int8_int16[i].fExpected << endl;
6721 }
6722
6723 // Also need to test the version that assigns back out
6724 // to a plain int, as it has different logic
6725 fSuccess = true;
6726 try
6727 {
6728 std::int8_t x(int8_int16[i].x);
6729 x += SafeInt<std::int16_t>(int8_int16[i].y);
6730 }
6731 catch(...)
6732 {
6733 fSuccess = false;
6734 }
6735
6736 if( fSuccess != int8_int16[i].fExpected )
6737 {
6738 cerr << "Error in case int8_int16 throw (2): ";
6739 cerr << HEX(2) << (0xFF & (int)int8_int16[i].x) << ", ";
6740 cerr << HEX(4) << int8_int16[i].y << ", ";
6741 cerr << "expected = " << int8_int16[i].fExpected << endl;
6742 }
6743 }
6744}
6745

Callers 1

AddVerifyFunction · 0.70

Calls 1

SafeAddFunction · 0.50

Tested by

no test coverage detected