//===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // UNSUPPORTED: c++98, c++03, c++11, c++14 // // template // constexpr common_type_t<_M,_N> gcd(_M __m, _N __n) #include #include #include #include #include // for rand() #include constexpr struct { int x; int y; int expect; } Cases[] = { {0, 0, 0}, {1, 0, 1}, {0, 1, 1}, {1, 1, 1}, {2, 3, 1}, {2, 4, 2}, {36, 17, 1}, {36, 18, 18} }; template constexpr bool test0(int in1, int in2, int out) { auto value1 = static_cast(in1); auto value2 = static_cast(in2); static_assert(std::is_same_v, ""); static_assert(std::is_same_v, ""); assert(static_cast(out) == std::gcd(value1, value2)); return true; } template constexpr bool do_test(int = 0) { using S1 = std::make_signed_t; using S2 = std::make_signed_t; using U1 = std::make_unsigned_t; using U2 = std::make_unsigned_t; bool accumulate = true; for (auto TC : Cases) { { // Test with two signed types using Output = std::common_type_t; accumulate &= test0(TC.x, TC.y, TC.expect); accumulate &= test0(-TC.x, TC.y, TC.expect); accumulate &= test0(TC.x, -TC.y, TC.expect); accumulate &= test0(-TC.x, -TC.y, TC.expect); accumulate &= test0(TC.x, TC.y, TC.expect); accumulate &= test0(-TC.x, TC.y, TC.expect); accumulate &= test0(TC.x, -TC.y, TC.expect); accumulate &= test0(-TC.x, -TC.y, TC.expect); } { // test with two unsigned types using Output = std::common_type_t; accumulate &= test0(TC.x, TC.y, TC.expect); accumulate &= test0(TC.x, TC.y, TC.expect); } { // Test with mixed signs using Output = std::common_type_t; accumulate &= test0(TC.x, TC.y, TC.expect); accumulate &= test0(TC.x, TC.y, TC.expect); accumulate &= test0(-TC.x, TC.y, TC.expect); accumulate &= test0(TC.x, -TC.y, TC.expect); } { // Test with mixed signs using Output = std::common_type_t; accumulate &= test0(TC.x, TC.y, TC.expect); accumulate &= test0(TC.x, TC.y, TC.expect); accumulate &= test0(-TC.x, TC.y, TC.expect); accumulate &= test0(TC.x, -TC.y, TC.expect); } } return accumulate; } int main() { auto non_cce = std::rand(); // a value that can't possibly be constexpr static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); assert(do_test(non_cce)); assert(do_test(non_cce)); assert(do_test(non_cce)); assert(do_test(non_cce)); assert(do_test(non_cce)); static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); assert(do_test(non_cce)); assert(do_test(non_cce)); assert(do_test(non_cce)); assert(do_test(non_cce)); static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); static_assert(do_test(), ""); assert((do_test(non_cce))); assert((do_test(non_cce))); assert((do_test(non_cce))); assert((do_test(non_cce))); assert((do_test(non_cce))); assert((do_test(non_cce))); assert((do_test(non_cce))); assert((do_test(non_cce))); // LWG#2837 { auto res = std::gcd(static_cast(1234), INT32_MIN); static_assert(std::is_same_v, ""); assert(res == 2); } }