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

Function SubVerifyUint8Int8

Test/SubVerify.cpp:5120–5176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5118};
5119
5120void SubVerifyUint8Int8()
5121{
5122 size_t i;
5123
5124 for( i = 0; i < COUNTOF(uint8_int8); ++i )
5125 {
5126 std::uint8_t ret;
5127 if( SafeSubtract(uint8_int8[i].x, uint8_int8[i].y, ret) != uint8_int8[i].fExpected )
5128 {
5129 cerr << "Error in case uint8_int8: ";
5130 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)uint8_int8[i].x) << ", ";
5131 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)uint8_int8[i].y) << ", ";
5132 cerr << "expected = " << uint8_int8[i].fExpected << endl;
5133 }
5134
5135 // Now test throwing version
5136 bool fSuccess = true;
5137 try
5138 {
5139 SafeInt<std::uint8_t> si(uint8_int8[i].x);
5140 si -= uint8_int8[i].y;
5141 }
5142 catch(...)
5143 {
5144 fSuccess = false;
5145 }
5146
5147 if( fSuccess != uint8_int8[i].fExpected )
5148 {
5149 cerr << "Error in case uint8_int8 throw (1): ";
5150 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)uint8_int8[i].x) << ", ";
5151 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)uint8_int8[i].y) << ", ";
5152 cerr << "expected = " << uint8_int8[i].fExpected << endl;
5153 }
5154
5155 // Also need to test the version that assigns back out
5156 // to a plain int, as it has different logic
5157 fSuccess = true;
5158 try
5159 {
5160 std::uint8_t x(uint8_int8[i].x);
5161 x -= SafeInt<std::int8_t>(uint8_int8[i].y);
5162 }
5163 catch(...)
5164 {
5165 fSuccess = false;
5166 }
5167
5168 if( fSuccess != uint8_int8[i].fExpected )
5169 {
5170 cerr << "Error in case uint8_int8 throw (2): ";
5171 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)uint8_int8[i].x) << ", ";
5172 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)uint8_int8[i].y) << ", ";
5173 cerr << "expected = " << uint8_int8[i].fExpected << endl;
5174 }
5175 }
5176}
5177

Callers 1

SubVerifyFunction · 0.70

Calls 1

SafeSubtractFunction · 0.50

Tested by

no test coverage detected