gcd.pass.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14
  11. // XFAIL: ubsan
  12. // <numeric>
  13. // template<class _M, class _N>
  14. // constexpr common_type_t<_M,_N> gcd(_M __m, _N __n)
  15. #include <numeric>
  16. #include <cassert>
  17. #include <cstdlib> // for rand()
  18. #include <iostream>
  19. constexpr struct {
  20. int x;
  21. int y;
  22. int expect;
  23. } Cases[] = {
  24. {0, 0, 0},
  25. {1, 0, 1},
  26. {0, 1, 1},
  27. {1, 1, 1},
  28. {2, 3, 1},
  29. {2, 4, 2},
  30. {36, 17, 1},
  31. {36, 18, 18}
  32. };
  33. template <typename Input1, typename Input2, typename Output>
  34. constexpr bool test0(Input1 in1, Input2 in2, Output out)
  35. {
  36. static_assert((std::is_same<Output, decltype(std::gcd(in1, in2))>::value), "" );
  37. static_assert((std::is_same<Output, decltype(std::gcd(in2, in1))>::value), "" );
  38. return out == std::gcd(in1, in2) ? true : (std::abort(), false);
  39. }
  40. template <typename Input1, typename Input2 = Input1>
  41. constexpr bool do_test(int = 0)
  42. {
  43. using S1 = typename std::make_signed<Input1>::type;
  44. using S2 = typename std::make_signed<Input2>::type;
  45. using U1 = typename std::make_unsigned<Input1>::type;
  46. using U2 = typename std::make_unsigned<Input2>::type;
  47. bool accumulate = true;
  48. for (auto TC : Cases) {
  49. { // Test with two signed types
  50. using Output = std::common_type_t<S1, S2>;
  51. accumulate &= test0<S1, S2, Output>(TC.x, TC.y, TC.expect);
  52. accumulate &= test0<S1, S2, Output>(-TC.x, TC.y, TC.expect);
  53. accumulate &= test0<S1, S2, Output>(TC.x, -TC.y, TC.expect);
  54. accumulate &= test0<S1, S2, Output>(-TC.x, -TC.y, TC.expect);
  55. accumulate &= test0<S2, S1, Output>(TC.x, TC.y, TC.expect);
  56. accumulate &= test0<S2, S1, Output>(-TC.x, TC.y, TC.expect);
  57. accumulate &= test0<S2, S1, Output>(TC.x, -TC.y, TC.expect);
  58. accumulate &= test0<S2, S1, Output>(-TC.x, -TC.y, TC.expect);
  59. }
  60. { // test with two unsigned types
  61. using Output = std::common_type_t<U1, U2>;
  62. accumulate &= test0<U1, U2, Output>(TC.x, TC.y, TC.expect);
  63. accumulate &= test0<U2, U1, Output>(TC.x, TC.y, TC.expect);
  64. }
  65. { // Test with mixed signs
  66. using Output = std::common_type_t<S1, U2>;
  67. accumulate &= test0<S1, U2, Output>(TC.x, TC.y, TC.expect);
  68. accumulate &= test0<U2, S1, Output>(TC.x, TC.y, TC.expect);
  69. accumulate &= test0<S1, U2, Output>(-TC.x, TC.y, TC.expect);
  70. accumulate &= test0<U2, S1, Output>(TC.x, -TC.y, TC.expect);
  71. }
  72. { // Test with mixed signs
  73. using Output = std::common_type_t<S2, U1>;
  74. accumulate &= test0<S2, U1, Output>(TC.x, TC.y, TC.expect);
  75. accumulate &= test0<U1, S2, Output>(TC.x, TC.y, TC.expect);
  76. accumulate &= test0<S2, U1, Output>(-TC.x, TC.y, TC.expect);
  77. accumulate &= test0<U1, S2, Output>(TC.x, -TC.y, TC.expect);
  78. }
  79. }
  80. return accumulate;
  81. }
  82. int main()
  83. {
  84. auto non_cce = std::rand(); // a value that can't possibly be constexpr
  85. static_assert(do_test<signed char>(), "");
  86. static_assert(do_test<short>(), "");
  87. static_assert(do_test<int>(), "");
  88. static_assert(do_test<long>(), "");
  89. static_assert(do_test<long long>(), "");
  90. assert(do_test<signed char>(non_cce));
  91. assert(do_test<short>(non_cce));
  92. assert(do_test<int>(non_cce));
  93. assert(do_test<long>(non_cce));
  94. assert(do_test<long long>(non_cce));
  95. static_assert(do_test< int8_t>(), "");
  96. static_assert(do_test<int16_t>(), "");
  97. static_assert(do_test<int32_t>(), "");
  98. static_assert(do_test<int64_t>(), "");
  99. assert(do_test< int8_t>(non_cce));
  100. assert(do_test<int16_t>(non_cce));
  101. assert(do_test<int32_t>(non_cce));
  102. assert(do_test<int64_t>(non_cce));
  103. static_assert(do_test<signed char, int>(), "");
  104. static_assert(do_test<int, signed char>(), "");
  105. static_assert(do_test<short, int>(), "");
  106. static_assert(do_test<int, short>(), "");
  107. static_assert(do_test<int, long>(), "");
  108. static_assert(do_test<long, int>(), "");
  109. static_assert(do_test<int, long long>(), "");
  110. static_assert(do_test<long long, int>(), "");
  111. assert((do_test<signed char, int>(non_cce)));
  112. assert((do_test<int, signed char>(non_cce)));
  113. assert((do_test<short, int>(non_cce)));
  114. assert((do_test<int, short>(non_cce)));
  115. assert((do_test<int, long>(non_cce)));
  116. assert((do_test<long, int>(non_cce)));
  117. assert((do_test<int, long long>(non_cce)));
  118. assert((do_test<long long, int>(non_cce)));
  119. // LWG#2837
  120. {
  121. auto res = std::gcd((int64_t)1234, (int32_t)-2147483648);
  122. static_assert( std::is_same<decltype(res), std::common_type<int64_t, int32_t>::type>::value, "");
  123. assert(res == 2);
  124. }
  125. }