lcm.pass.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // <numeric>
  12. // template<class _M, class _N>
  13. // constexpr common_type_t<_M,_N> lcm(_M __m, _N __n)
  14. #include <numeric>
  15. #include <cassert>
  16. #include <climits>
  17. #include <cstdint>
  18. #include <cstdlib>
  19. #include <type_traits>
  20. constexpr struct {
  21. int x;
  22. int y;
  23. int expect;
  24. } Cases[] = {
  25. {0, 0, 0},
  26. {1, 0, 0},
  27. {0, 1, 0},
  28. {1, 1, 1},
  29. {2, 3, 6},
  30. {2, 4, 4},
  31. {3, 17, 51},
  32. {36, 18, 36}
  33. };
  34. template <typename Input1, typename Input2, typename Output>
  35. constexpr bool test0(int in1, int in2, int out)
  36. {
  37. auto value1 = static_cast<Input1>(in1);
  38. auto value2 = static_cast<Input2>(in2);
  39. static_assert(std::is_same_v<Output, decltype(std::lcm(value1, value2))>, "");
  40. static_assert(std::is_same_v<Output, decltype(std::lcm(value2, value1))>, "");
  41. assert(static_cast<Output>(out) == std::lcm(value1, value2));
  42. return true;
  43. }
  44. template <typename Input1, typename Input2 = Input1>
  45. constexpr bool do_test(int = 0)
  46. {
  47. using S1 = std::make_signed_t<Input1>;
  48. using S2 = std::make_signed_t<Input2>;
  49. using U1 = std::make_unsigned_t<Input1>;
  50. using U2 = std::make_unsigned_t<Input2>;
  51. bool accumulate = true;
  52. for (auto TC : Cases) {
  53. { // Test with two signed types
  54. using Output = std::common_type_t<S1, S2>;
  55. accumulate &= test0<S1, S2, Output>(TC.x, TC.y, TC.expect);
  56. accumulate &= test0<S1, S2, Output>(-TC.x, TC.y, TC.expect);
  57. accumulate &= test0<S1, S2, Output>(TC.x, -TC.y, TC.expect);
  58. accumulate &= test0<S1, S2, Output>(-TC.x, -TC.y, TC.expect);
  59. accumulate &= test0<S2, S1, Output>(TC.x, TC.y, TC.expect);
  60. accumulate &= test0<S2, S1, Output>(-TC.x, TC.y, TC.expect);
  61. accumulate &= test0<S2, S1, Output>(TC.x, -TC.y, TC.expect);
  62. accumulate &= test0<S2, S1, Output>(-TC.x, -TC.y, TC.expect);
  63. }
  64. { // test with two unsigned types
  65. using Output = std::common_type_t<U1, U2>;
  66. accumulate &= test0<U1, U2, Output>(TC.x, TC.y, TC.expect);
  67. accumulate &= test0<U2, U1, Output>(TC.x, TC.y, TC.expect);
  68. }
  69. { // Test with mixed signs
  70. using Output = std::common_type_t<S1, U2>;
  71. accumulate &= test0<S1, U2, Output>(TC.x, TC.y, TC.expect);
  72. accumulate &= test0<U2, S1, Output>(TC.x, TC.y, TC.expect);
  73. accumulate &= test0<S1, U2, Output>(-TC.x, TC.y, TC.expect);
  74. accumulate &= test0<U2, S1, Output>(TC.x, -TC.y, TC.expect);
  75. }
  76. { // Test with mixed signs
  77. using Output = std::common_type_t<S2, U1>;
  78. accumulate &= test0<S2, U1, Output>(TC.x, TC.y, TC.expect);
  79. accumulate &= test0<U1, S2, Output>(TC.x, TC.y, TC.expect);
  80. accumulate &= test0<S2, U1, Output>(-TC.x, TC.y, TC.expect);
  81. accumulate &= test0<U1, S2, Output>(TC.x, -TC.y, TC.expect);
  82. }
  83. }
  84. return accumulate;
  85. }
  86. int main()
  87. {
  88. auto non_cce = std::rand(); // a value that can't possibly be constexpr
  89. static_assert(do_test<signed char>(), "");
  90. static_assert(do_test<short>(), "");
  91. static_assert(do_test<int>(), "");
  92. static_assert(do_test<long>(), "");
  93. static_assert(do_test<long long>(), "");
  94. assert(do_test<signed char>(non_cce));
  95. assert(do_test<short>(non_cce));
  96. assert(do_test<int>(non_cce));
  97. assert(do_test<long>(non_cce));
  98. assert(do_test<long long>(non_cce));
  99. static_assert(do_test<std::int8_t>(), "");
  100. static_assert(do_test<std::int16_t>(), "");
  101. static_assert(do_test<std::int32_t>(), "");
  102. static_assert(do_test<std::int64_t>(), "");
  103. assert(do_test<std::int8_t>(non_cce));
  104. assert(do_test<std::int16_t>(non_cce));
  105. assert(do_test<std::int32_t>(non_cce));
  106. assert(do_test<std::int64_t>(non_cce));
  107. static_assert(do_test<signed char, int>(), "");
  108. static_assert(do_test<int, signed char>(), "");
  109. static_assert(do_test<short, int>(), "");
  110. static_assert(do_test<int, short>(), "");
  111. static_assert(do_test<int, long>(), "");
  112. static_assert(do_test<long, int>(), "");
  113. static_assert(do_test<int, long long>(), "");
  114. static_assert(do_test<long long, int>(), "");
  115. assert((do_test<signed char, int>(non_cce)));
  116. assert((do_test<int, signed char>(non_cce)));
  117. assert((do_test<short, int>(non_cce)));
  118. assert((do_test<int, short>(non_cce)));
  119. assert((do_test<int, long>(non_cce)));
  120. assert((do_test<long, int>(non_cce)));
  121. assert((do_test<int, long long>(non_cce)));
  122. assert((do_test<long long, int>(non_cce)));
  123. // LWG#2837
  124. {
  125. auto res1 = std::lcm(static_cast<std::int64_t>(1234), INT32_MIN);
  126. (void)std::lcm(INT_MIN, 2UL); // this used to trigger UBSAN
  127. static_assert(std::is_same_v<decltype(res1), std::int64_t>, "");
  128. assert(res1 == 1324997410816LL);
  129. }
  130. }