gcd.pass.cpp 5.0 KB

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