Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Stephan T. Lavavej | d72ece6 | 2016-11-23 22:03:28 +0000 | [diff] [blame] | 8 | // |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 9 | // UNSUPPORTED: c++98, c++03, c++11, c++14 |
Marshall Clow | a2e54b6 | 2017-02-08 07:40:59 +0000 | [diff] [blame] | 10 | |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 11 | // <numeric> |
| 12 | |
| 13 | // template<class _M, class _N> |
| 14 | // constexpr common_type_t<_M,_N> gcd(_M __m, _N __n) |
| 15 | |
| 16 | #include <numeric> |
| 17 | #include <cassert> |
Billy Robert O'Neal III | 8c40117 | 2017-05-08 21:52:05 +0000 | [diff] [blame] | 18 | #include <climits> |
| 19 | #include <cstdint> |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 20 | #include <cstdlib> // for rand() |
Billy Robert O'Neal III | 8c40117 | 2017-05-08 21:52:05 +0000 | [diff] [blame] | 21 | #include <type_traits> |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 22 | |
| 23 | constexpr struct { |
| 24 | int x; |
| 25 | int y; |
| 26 | int expect; |
| 27 | } Cases[] = { |
| 28 | {0, 0, 0}, |
| 29 | {1, 0, 1}, |
| 30 | {0, 1, 1}, |
| 31 | {1, 1, 1}, |
| 32 | {2, 3, 1}, |
| 33 | {2, 4, 2}, |
| 34 | {36, 17, 1}, |
| 35 | {36, 18, 18} |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | template <typename Input1, typename Input2, typename Output> |
Billy Robert O'Neal III | 8c40117 | 2017-05-08 21:52:05 +0000 | [diff] [blame] | 40 | constexpr bool test0(int in1, int in2, int out) |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 41 | { |
Billy Robert O'Neal III | 8c40117 | 2017-05-08 21:52:05 +0000 | [diff] [blame] | 42 | auto value1 = static_cast<Input1>(in1); |
| 43 | auto value2 = static_cast<Input2>(in2); |
| 44 | static_assert(std::is_same_v<Output, decltype(std::gcd(value1, value2))>, ""); |
| 45 | static_assert(std::is_same_v<Output, decltype(std::gcd(value2, value1))>, ""); |
| 46 | assert(static_cast<Output>(out) == std::gcd(value1, value2)); |
| 47 | return true; |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
| 51 | template <typename Input1, typename Input2 = Input1> |
Eric Fiselier | fd83822 | 2016-12-23 23:37:52 +0000 | [diff] [blame] | 52 | constexpr bool do_test(int = 0) |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 53 | { |
Billy Robert O'Neal III | 8c40117 | 2017-05-08 21:52:05 +0000 | [diff] [blame] | 54 | using S1 = std::make_signed_t<Input1>; |
| 55 | using S2 = std::make_signed_t<Input2>; |
| 56 | using U1 = std::make_unsigned_t<Input1>; |
| 57 | using U2 = std::make_unsigned_t<Input2>; |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 58 | bool accumulate = true; |
| 59 | for (auto TC : Cases) { |
| 60 | { // Test with two signed types |
| 61 | using Output = std::common_type_t<S1, S2>; |
| 62 | accumulate &= test0<S1, S2, Output>(TC.x, TC.y, TC.expect); |
| 63 | accumulate &= test0<S1, S2, Output>(-TC.x, TC.y, TC.expect); |
| 64 | accumulate &= test0<S1, S2, Output>(TC.x, -TC.y, TC.expect); |
| 65 | accumulate &= test0<S1, S2, Output>(-TC.x, -TC.y, TC.expect); |
| 66 | accumulate &= test0<S2, S1, Output>(TC.x, TC.y, TC.expect); |
| 67 | accumulate &= test0<S2, S1, Output>(-TC.x, TC.y, TC.expect); |
| 68 | accumulate &= test0<S2, S1, Output>(TC.x, -TC.y, TC.expect); |
| 69 | accumulate &= test0<S2, S1, Output>(-TC.x, -TC.y, TC.expect); |
| 70 | } |
| 71 | { // test with two unsigned types |
| 72 | using Output = std::common_type_t<U1, U2>; |
| 73 | accumulate &= test0<U1, U2, Output>(TC.x, TC.y, TC.expect); |
| 74 | accumulate &= test0<U2, U1, Output>(TC.x, TC.y, TC.expect); |
| 75 | } |
| 76 | { // Test with mixed signs |
| 77 | using Output = std::common_type_t<S1, U2>; |
| 78 | accumulate &= test0<S1, U2, Output>(TC.x, TC.y, TC.expect); |
| 79 | accumulate &= test0<U2, S1, Output>(TC.x, TC.y, TC.expect); |
| 80 | accumulate &= test0<S1, U2, Output>(-TC.x, TC.y, TC.expect); |
| 81 | accumulate &= test0<U2, S1, Output>(TC.x, -TC.y, TC.expect); |
| 82 | } |
| 83 | { // Test with mixed signs |
| 84 | using Output = std::common_type_t<S2, U1>; |
| 85 | accumulate &= test0<S2, U1, Output>(TC.x, TC.y, TC.expect); |
| 86 | accumulate &= test0<U1, S2, Output>(TC.x, TC.y, TC.expect); |
| 87 | accumulate &= test0<S2, U1, Output>(-TC.x, TC.y, TC.expect); |
| 88 | accumulate &= test0<U1, S2, Output>(TC.x, -TC.y, TC.expect); |
| 89 | } |
| 90 | } |
| 91 | return accumulate; |
| 92 | } |
| 93 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame^] | 94 | int main(int, char**) |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 95 | { |
| 96 | auto non_cce = std::rand(); // a value that can't possibly be constexpr |
| 97 | |
| 98 | static_assert(do_test<signed char>(), ""); |
| 99 | static_assert(do_test<short>(), ""); |
| 100 | static_assert(do_test<int>(), ""); |
| 101 | static_assert(do_test<long>(), ""); |
| 102 | static_assert(do_test<long long>(), ""); |
| 103 | |
| 104 | assert(do_test<signed char>(non_cce)); |
| 105 | assert(do_test<short>(non_cce)); |
| 106 | assert(do_test<int>(non_cce)); |
| 107 | assert(do_test<long>(non_cce)); |
| 108 | assert(do_test<long long>(non_cce)); |
| 109 | |
Billy Robert O'Neal III | 8c40117 | 2017-05-08 21:52:05 +0000 | [diff] [blame] | 110 | static_assert(do_test<std::int8_t>(), ""); |
| 111 | static_assert(do_test<std::int16_t>(), ""); |
| 112 | static_assert(do_test<std::int32_t>(), ""); |
| 113 | static_assert(do_test<std::int64_t>(), ""); |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 114 | |
Billy Robert O'Neal III | 8c40117 | 2017-05-08 21:52:05 +0000 | [diff] [blame] | 115 | assert(do_test<std::int8_t>(non_cce)); |
| 116 | assert(do_test<std::int16_t>(non_cce)); |
| 117 | assert(do_test<std::int32_t>(non_cce)); |
| 118 | assert(do_test<std::int64_t>(non_cce)); |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 119 | |
| 120 | static_assert(do_test<signed char, int>(), ""); |
| 121 | static_assert(do_test<int, signed char>(), ""); |
| 122 | static_assert(do_test<short, int>(), ""); |
| 123 | static_assert(do_test<int, short>(), ""); |
| 124 | static_assert(do_test<int, long>(), ""); |
| 125 | static_assert(do_test<long, int>(), ""); |
| 126 | static_assert(do_test<int, long long>(), ""); |
| 127 | static_assert(do_test<long long, int>(), ""); |
| 128 | |
| 129 | assert((do_test<signed char, int>(non_cce))); |
| 130 | assert((do_test<int, signed char>(non_cce))); |
| 131 | assert((do_test<short, int>(non_cce))); |
| 132 | assert((do_test<int, short>(non_cce))); |
| 133 | assert((do_test<int, long>(non_cce))); |
| 134 | assert((do_test<long, int>(non_cce))); |
| 135 | assert((do_test<int, long long>(non_cce))); |
| 136 | assert((do_test<long long, int>(non_cce))); |
Marshall Clow | 7a1c0ef | 2017-02-06 16:03:23 +0000 | [diff] [blame] | 137 | |
| 138 | // LWG#2837 |
| 139 | { |
Billy Robert O'Neal III | 8c40117 | 2017-05-08 21:52:05 +0000 | [diff] [blame] | 140 | auto res = std::gcd(static_cast<std::int64_t>(1234), INT32_MIN); |
| 141 | static_assert(std::is_same_v<decltype(res), std::int64_t>, ""); |
| 142 | assert(res == 2); |
Marshall Clow | 7a1c0ef | 2017-02-06 16:03:23 +0000 | [diff] [blame] | 143 | } |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame^] | 144 | |
| 145 | return 0; |
Marshall Clow | 19b4035 | 2016-07-26 14:29:45 +0000 | [diff] [blame] | 146 | } |