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