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

Function SubVerifyInt8Int32

Test/SubVerify.cpp:3098–3154  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3096};
3097
3098void SubVerifyInt8Int32()
3099{
3100 size_t i;
3101
3102 for( i = 0; i < COUNTOF(int8_int32); ++i )
3103 {
3104 std::int8_t ret;
3105 if( SafeSubtract(int8_int32[i].x, int8_int32[i].y, ret) != int8_int32[i].fExpected )
3106 {
3107 cerr << "Error in case int8_int32: ";
3108 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int32[i].x) << ", ";
3109 cerr << hex << setw(8) << setfill('0') << int8_int32[i].y << ", ";
3110 cerr << "expected = " << int8_int32[i].fExpected << endl;
3111 }
3112
3113 // Now test throwing version
3114 bool fSuccess = true;
3115 try
3116 {
3117 SafeInt<std::int8_t> si(int8_int32[i].x);
3118 si -= int8_int32[i].y;
3119 }
3120 catch(...)
3121 {
3122 fSuccess = false;
3123 }
3124
3125 if( fSuccess != int8_int32[i].fExpected )
3126 {
3127 cerr << "Error in case int8_int32 throw (1): ";
3128 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int32[i].x) << ", ";
3129 cerr << hex << setw(8) << setfill('0') << int8_int32[i].y << ", ";
3130 cerr << "expected = " << int8_int32[i].fExpected << endl;
3131 }
3132
3133 // Also need to test the version that assigns back out
3134 // to a plain int, as it has different logic
3135 fSuccess = true;
3136 try
3137 {
3138 std::int8_t x(int8_int32[i].x);
3139 x -= SafeInt<std::int64_t>(int8_int32[i].y);
3140 }
3141 catch(...)
3142 {
3143 fSuccess = false;
3144 }
3145
3146 if( fSuccess != int8_int32[i].fExpected )
3147 {
3148 cerr << "Error in case int8_int32 throw (2): ";
3149 cerr << hex << setw(2) << setfill('0') << (0xFF & (int)int8_int32[i].x) << ", ";
3150 cerr << hex << setw(8) << setfill('0') << int8_int32[i].y << ", ";
3151 cerr << "expected = " << int8_int32[i].fExpected << endl;
3152 }
3153 }
3154}
3155

Callers 1

SubVerifyFunction · 0.70

Calls 1

SafeSubtractFunction · 0.50

Tested by

no test coverage detected