| 1 | // { dg-do run { target c++26 } } |
| 2 | |
| 3 | #include <stdckdint.h> |
| 4 | |
| 5 | #if __STDC_VERSION_STDCKDINT_H__ != 202311L |
| 6 | # error "__STDC_VERSION_STDCKDINT_H__ not defined correctly in <stdckdint.h>" |
| 7 | #endif |
| 8 | |
| 9 | #include <limits.h> |
| 10 | #include <testsuite_hooks.h> |
| 11 | |
| 12 | void |
| 13 | test_add() |
| 14 | { |
| 15 | int result; |
| 16 | bool overflow; |
| 17 | |
| 18 | overflow = ::ckd_add(&result, (unsigned)INT_MAX, 1LL); |
| 19 | VERIFY( overflow ); |
| 20 | VERIFY( result == INT_MIN ); |
| 21 | |
| 22 | overflow = ::ckd_add(&result, (long long)INT_MIN, -1); |
| 23 | VERIFY( overflow ); |
| 24 | VERIFY( result == INT_MAX ); |
| 25 | |
| 26 | overflow = ::ckd_add(&result, 99u, 100ll); |
| 27 | VERIFY( ! overflow ); |
| 28 | VERIFY( result == 199 ); |
| 29 | } |
| 30 | |
| 31 | void |
| 32 | test_sub() |
| 33 | { |
| 34 | int result; |
| 35 | bool overflow; |
| 36 | |
| 37 | overflow = ::ckd_sub(&result, -1, -5); |
| 38 | VERIFY( ! overflow ); |
| 39 | VERIFY( result == 4 ); |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | test_mul() |
| 44 | { |
| 45 | long long result; |
| 46 | bool overflow; |
| 47 | |
| 48 | overflow = ::ckd_mul(&result, INT_MIN, -1); |
| 49 | VERIFY( ! overflow ); |
| 50 | VERIFY( result == -(long long)INT_MIN ); |
| 51 | |
| 52 | unsigned uresult; |
| 53 | overflow = ::ckd_mul(&uresult, INT_MIN, -1); |
| 54 | VERIFY( ! overflow ); |
| 55 | VERIFY( result == (unsigned)INT_MAX + 1u ); |
| 56 | } |
| 57 | |
| 58 | int main() |
| 59 | { |
| 60 | test_add(); |
| 61 | test_sub(); |
| 62 | test_mul(); |
| 63 | } |