lcm.pass.cpp 5.1 KB

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