| 22 | namespace { |
| 23 | |
| 24 | TEST(OverflowTest, Nonnegative) { |
| 25 | // Various interesting values |
| 26 | std::vector<int64> interesting = {0, std::numeric_limits<int64>::max()}; |
| 27 | for (int i = 0; i < 63; i++) { |
| 28 | int64 bit = static_cast<int64>(1) << i; |
| 29 | interesting.push_back(bit); |
| 30 | interesting.push_back(bit + 1); |
| 31 | interesting.push_back(bit - 1); |
| 32 | } |
| 33 | for (const int64 mid : {static_cast<int64>(1) << 32, |
| 34 | static_cast<int64>(std::pow(2, 63.0 / 2))}) { |
| 35 | for (int i = -5; i < 5; i++) { |
| 36 | interesting.push_back(mid + i); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Check all pairs |
| 41 | for (auto x : interesting) { |
| 42 | for (auto y : interesting) { |
| 43 | int64 xy = MultiplyWithoutOverflow(x, y); |
| 44 | long double dxy = static_cast<long double>(x) * y; |
| 45 | if (dxy > std::numeric_limits<int64>::max()) { |
| 46 | EXPECT_LT(xy, 0); |
| 47 | } else { |
| 48 | EXPECT_EQ(dxy, xy); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | TEST(OverflowTest, Negative) { |
| 55 | const int64 negatives[] = {-1, std::numeric_limits<int64>::min()}; |
nothing calls this directly
no test coverage detected