blob: a42303776b2d5e38db8f6753de2ce14ca13fc091 [file] [log] [blame]
Marshall Clow19b40352016-07-26 14:29:45 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Marshall Clow19b40352016-07-26 14:29:45 +00006//
7//===----------------------------------------------------------------------===//
Stephan T. Lavavejd72ece62016-11-23 22:03:28 +00008//
Marshall Clow19b40352016-07-26 14:29:45 +00009// UNSUPPORTED: c++98, c++03, c++11, c++14
10// <numeric>
11
12// template<class _M, class _N>
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000013// constexpr common_type_t<_M,_N> lcm(_M __m, _N __n)
Marshall Clow19b40352016-07-26 14:29:45 +000014
15#include <numeric>
16#include <cassert>
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000017#include <climits>
18#include <cstdint>
Marshall Clow19b40352016-07-26 14:29:45 +000019#include <cstdlib>
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000020#include <type_traits>
Billy Robert O'Neal IIIba40b052017-11-21 21:37:26 +000021#include "test_macros.h"
Marshall Clow19b40352016-07-26 14:29:45 +000022
23constexpr struct {
24 int x;
25 int y;
26 int expect;
27} Cases[] = {
28 {0, 0, 0},
29 {1, 0, 0},
30 {0, 1, 0},
31 {1, 1, 1},
32 {2, 3, 6},
33 {2, 4, 4},
34 {3, 17, 51},
35 {36, 18, 36}
36};
37
38template <typename Input1, typename Input2, typename Output>
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000039constexpr bool test0(int in1, int in2, int out)
Marshall Clow19b40352016-07-26 14:29:45 +000040{
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000041 auto value1 = static_cast<Input1>(in1);
42 auto value2 = static_cast<Input2>(in2);
43 static_assert(std::is_same_v<Output, decltype(std::lcm(value1, value2))>, "");
44 static_assert(std::is_same_v<Output, decltype(std::lcm(value2, value1))>, "");
45 assert(static_cast<Output>(out) == std::lcm(value1, value2));
46 return true;
Marshall Clow19b40352016-07-26 14:29:45 +000047}
48
49
50template <typename Input1, typename Input2 = Input1>
Eric Fiselierfd838222016-12-23 23:37:52 +000051constexpr bool do_test(int = 0)
Marshall Clow19b40352016-07-26 14:29:45 +000052{
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000053 using S1 = std::make_signed_t<Input1>;
54 using S2 = std::make_signed_t<Input2>;
55 using U1 = std::make_unsigned_t<Input1>;
56 using U2 = std::make_unsigned_t<Input2>;
Marshall Clow19b40352016-07-26 14:29:45 +000057 bool accumulate = true;
58 for (auto TC : Cases) {
59 { // Test with two signed types
60 using Output = std::common_type_t<S1, S2>;
61 accumulate &= test0<S1, S2, Output>(TC.x, TC.y, TC.expect);
62 accumulate &= test0<S1, S2, Output>(-TC.x, TC.y, TC.expect);
63 accumulate &= test0<S1, S2, Output>(TC.x, -TC.y, TC.expect);
64 accumulate &= test0<S1, S2, Output>(-TC.x, -TC.y, TC.expect);
65 accumulate &= test0<S2, S1, Output>(TC.x, TC.y, TC.expect);
66 accumulate &= test0<S2, S1, Output>(-TC.x, TC.y, TC.expect);
67 accumulate &= test0<S2, S1, Output>(TC.x, -TC.y, TC.expect);
68 accumulate &= test0<S2, S1, Output>(-TC.x, -TC.y, TC.expect);
69 }
70 { // test with two unsigned types
71 using Output = std::common_type_t<U1, U2>;
72 accumulate &= test0<U1, U2, Output>(TC.x, TC.y, TC.expect);
73 accumulate &= test0<U2, U1, Output>(TC.x, TC.y, TC.expect);
74 }
75 { // Test with mixed signs
76 using Output = std::common_type_t<S1, U2>;
77 accumulate &= test0<S1, U2, Output>(TC.x, TC.y, TC.expect);
78 accumulate &= test0<U2, S1, Output>(TC.x, TC.y, TC.expect);
79 accumulate &= test0<S1, U2, Output>(-TC.x, TC.y, TC.expect);
80 accumulate &= test0<U2, S1, Output>(TC.x, -TC.y, TC.expect);
81 }
82 { // Test with mixed signs
83 using Output = std::common_type_t<S2, U1>;
84 accumulate &= test0<S2, U1, Output>(TC.x, TC.y, TC.expect);
85 accumulate &= test0<U1, S2, Output>(TC.x, TC.y, TC.expect);
86 accumulate &= test0<S2, U1, Output>(-TC.x, TC.y, TC.expect);
87 accumulate &= test0<U1, S2, Output>(TC.x, -TC.y, TC.expect);
88 }
89 }
90 return accumulate;
91}
92
93int main()
94{
95 auto non_cce = std::rand(); // a value that can't possibly be constexpr
96
97 static_assert(do_test<signed char>(), "");
98 static_assert(do_test<short>(), "");
99 static_assert(do_test<int>(), "");
100 static_assert(do_test<long>(), "");
101 static_assert(do_test<long long>(), "");
102
103 assert(do_test<signed char>(non_cce));
104 assert(do_test<short>(non_cce));
105 assert(do_test<int>(non_cce));
106 assert(do_test<long>(non_cce));
107 assert(do_test<long long>(non_cce));
108
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +0000109 static_assert(do_test<std::int8_t>(), "");
110 static_assert(do_test<std::int16_t>(), "");
111 static_assert(do_test<std::int32_t>(), "");
112 static_assert(do_test<std::int64_t>(), "");
Marshall Clow19b40352016-07-26 14:29:45 +0000113
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +0000114 assert(do_test<std::int8_t>(non_cce));
115 assert(do_test<std::int16_t>(non_cce));
116 assert(do_test<std::int32_t>(non_cce));
117 assert(do_test<std::int64_t>(non_cce));
Marshall Clow19b40352016-07-26 14:29:45 +0000118
119 static_assert(do_test<signed char, int>(), "");
120 static_assert(do_test<int, signed char>(), "");
121 static_assert(do_test<short, int>(), "");
122 static_assert(do_test<int, short>(), "");
123 static_assert(do_test<int, long>(), "");
124 static_assert(do_test<long, int>(), "");
125 static_assert(do_test<int, long long>(), "");
126 static_assert(do_test<long long, int>(), "");
127
128 assert((do_test<signed char, int>(non_cce)));
129 assert((do_test<int, signed char>(non_cce)));
130 assert((do_test<short, int>(non_cce)));
131 assert((do_test<int, short>(non_cce)));
132 assert((do_test<int, long>(non_cce)));
133 assert((do_test<long, int>(non_cce)));
134 assert((do_test<int, long long>(non_cce)));
135 assert((do_test<long long, int>(non_cce)));
Marshall Clow7a1c0ef2017-02-06 16:03:23 +0000136
137// LWG#2837
138 {
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +0000139 auto res1 = std::lcm(static_cast<std::int64_t>(1234), INT32_MIN);
Billy Robert O'Neal IIIba40b052017-11-21 21:37:26 +0000140 TEST_IGNORE_NODISCARD std::lcm(INT_MIN, 2UL); // this used to trigger UBSAN
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +0000141 static_assert(std::is_same_v<decltype(res1), std::int64_t>, "");
142 assert(res1 == 1324997410816LL);
Marshall Clow7a1c0ef2017-02-06 16:03:23 +0000143 }
Marshall Clow19b40352016-07-26 14:29:45 +0000144}