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

Function AddVerifyUint8Int64

Test/AddVerify.cpp:5018–5074  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5016};
5017
5018void AddVerifyUint8Int64()
5019{
5020 size_t i;
5021
5022 for( i = 0; i < COUNTOF(uint8_int64); ++i )
5023 {
5024 std::uint8_t ret;
5025 if( SafeAdd(uint8_int64[i].x, uint8_int64[i].y, ret) != uint8_int64[i].fExpected )
5026 {
5027 cerr << "Error in case uint8_int64: ";
5028 cerr << HEX(2) << (0xFF & (unsigned int)uint8_int64[i].x) << ", ";
5029 cerr << HEX(16) << uint8_int64[i].y << ", ";
5030 cerr << "expected = " << uint8_int64[i].fExpected << endl;
5031 }
5032
5033 // Now test throwing version
5034 bool fSuccess = true;
5035 try
5036 {
5037 SafeInt<std::uint8_t> si(uint8_int64[i].x);
5038 si += uint8_int64[i].y;
5039 }
5040 catch(...)
5041 {
5042 fSuccess = false;
5043 }
5044
5045 if( fSuccess != uint8_int64[i].fExpected )
5046 {
5047 cerr << "Error in case uint8_int64 throw (1): ";
5048 cerr << HEX(2) << (0xFF & (unsigned int)uint8_int64[i].x) << ", ";
5049 cerr << HEX(16) << uint8_int64[i].y << ", ";
5050 cerr << "expected = " << uint8_int64[i].fExpected << endl;
5051 }
5052
5053 // Also need to test the version that assigns back out
5054 // to a plain int, as it has different logic
5055 fSuccess = true;
5056 try
5057 {
5058 std::uint8_t x(uint8_int64[i].x);
5059 x += SafeInt<std::int64_t>(uint8_int64[i].y);
5060 }
5061 catch(...)
5062 {
5063 fSuccess = false;
5064 }
5065
5066 if( fSuccess != uint8_int64[i].fExpected )
5067 {
5068 cerr << "Error in case uint8_int64 throw (2): ";
5069 cerr << HEX(2) << (0xFF & (unsigned int)uint8_int64[i].x) << ", ";
5070 cerr << HEX(16) << uint8_int64[i].y << ", ";
5071 cerr << "expected = " << uint8_int64[i].fExpected << endl;
5072 }
5073 }
5074}
5075

Callers 1

AddVerifyFunction · 0.70

Calls 1

SafeAddFunction · 0.50

Tested by

no test coverage detected