| 5167 | }; |
| 5168 | |
| 5169 | void AddVerifyUint8Int32() |
| 5170 | { |
| 5171 | size_t i; |
| 5172 | |
| 5173 | for( i = 0; i < COUNTOF(uint8_int32); ++i ) |
| 5174 | { |
| 5175 | std::uint8_t ret; |
| 5176 | if( SafeAdd(uint8_int32[i].x, uint8_int32[i].y, ret) != uint8_int32[i].fExpected ) |
| 5177 | { |
| 5178 | cerr << "Error in case uint8_int32: "; |
| 5179 | cerr << HEX(2) << (0xFF & (unsigned int)uint8_int32[i].x) << ", "; |
| 5180 | cerr << HEX(8) << uint8_int32[i].y << ", "; |
| 5181 | cerr << "expected = " << uint8_int32[i].fExpected << endl; |
| 5182 | } |
| 5183 | |
| 5184 | // Now test throwing version |
| 5185 | bool fSuccess = true; |
| 5186 | try |
| 5187 | { |
| 5188 | SafeInt<std::uint8_t> si(uint8_int32[i].x); |
| 5189 | si += uint8_int32[i].y; |
| 5190 | } |
| 5191 | catch(...) |
| 5192 | { |
| 5193 | fSuccess = false; |
| 5194 | } |
| 5195 | |
| 5196 | if( fSuccess != uint8_int32[i].fExpected ) |
| 5197 | { |
| 5198 | cerr << "Error in case uint8_int32 throw (1): "; |
| 5199 | cerr << HEX(2) << (0xFF & (unsigned int)uint8_int32[i].x) << ", "; |
| 5200 | cerr << HEX(8) << uint8_int32[i].y << ", "; |
| 5201 | cerr << "expected = " << uint8_int32[i].fExpected << endl; |
| 5202 | } |
| 5203 | |
| 5204 | // Also need to test the version that assigns back out |
| 5205 | // to a plain int, as it has different logic |
| 5206 | fSuccess = true; |
| 5207 | try |
| 5208 | { |
| 5209 | std::uint8_t x(uint8_int32[i].x); |
| 5210 | x += SafeInt<std::int32_t>(uint8_int32[i].y); |
| 5211 | } |
| 5212 | catch(...) |
| 5213 | { |
| 5214 | fSuccess = false; |
| 5215 | } |
| 5216 | |
| 5217 | if( fSuccess != uint8_int32[i].fExpected ) |
| 5218 | { |
| 5219 | cerr << "Error in case uint8_int32 throw (2): "; |
| 5220 | cerr << HEX(2) << (0xFF & (unsigned int)uint8_int32[i].x) << ", "; |
| 5221 | cerr << HEX(8) << uint8_int32[i].y << ", "; |
| 5222 | cerr << "expected = " << uint8_int32[i].fExpected << endl; |
| 5223 | } |
| 5224 | } |
| 5225 | } |
| 5226 | |