1*19b4035fSMarshall Clow //===----------------------------------------------------------------------===//
2*19b4035fSMarshall Clow //
3*19b4035fSMarshall Clow //                     The LLVM Compiler Infrastructure
4*19b4035fSMarshall Clow //
5*19b4035fSMarshall Clow // This file is dual licensed under the MIT and the University of Illinois Open
6*19b4035fSMarshall Clow // Source Licenses. See LICENSE.TXT for details.
7*19b4035fSMarshall Clow //
8*19b4035fSMarshall Clow //===----------------------------------------------------------------------===//
9*19b4035fSMarshall Clow //
10*19b4035fSMarshall Clow // UNSUPPORTED: c++98, c++03, c++11, c++14
11*19b4035fSMarshall Clow // <numeric>
12*19b4035fSMarshall Clow 
13*19b4035fSMarshall Clow // template<class _M, class _N>
14*19b4035fSMarshall Clow // constexpr common_type_t<_M,_N> gcd(_M __m, _N __n)
15*19b4035fSMarshall Clow 
16*19b4035fSMarshall Clow #include <numeric>
17*19b4035fSMarshall Clow #include <cassert>
18*19b4035fSMarshall Clow #include <cstdlib>    // for rand()
19*19b4035fSMarshall Clow #include <iostream>
20*19b4035fSMarshall Clow 
21*19b4035fSMarshall Clow constexpr struct {
22*19b4035fSMarshall Clow   int x;
23*19b4035fSMarshall Clow   int y;
24*19b4035fSMarshall Clow   int expect;
25*19b4035fSMarshall Clow } Cases[] = {
26*19b4035fSMarshall Clow     {0, 0, 0},
27*19b4035fSMarshall Clow     {1, 0, 1},
28*19b4035fSMarshall Clow     {0, 1, 1},
29*19b4035fSMarshall Clow     {1, 1, 1},
30*19b4035fSMarshall Clow     {2, 3, 1},
31*19b4035fSMarshall Clow     {2, 4, 2},
32*19b4035fSMarshall Clow     {36, 17, 1},
33*19b4035fSMarshall Clow     {36, 18, 18}
34*19b4035fSMarshall Clow };
35*19b4035fSMarshall Clow 
36*19b4035fSMarshall Clow 
37*19b4035fSMarshall Clow template <typename Input1, typename Input2, typename Output>
38*19b4035fSMarshall Clow constexpr bool test0(Input1 in1, Input2 in2, Output out)
39*19b4035fSMarshall Clow {
40*19b4035fSMarshall Clow     static_assert((std::is_same<Output, decltype(std::gcd(in1, in2))>::value), "" );
41*19b4035fSMarshall Clow     static_assert((std::is_same<Output, decltype(std::gcd(in2, in1))>::value), "" );
42*19b4035fSMarshall Clow     return out == std::gcd(in1, in2) ? true : (std::abort(), false);
43*19b4035fSMarshall Clow }
44*19b4035fSMarshall Clow 
45*19b4035fSMarshall Clow 
46*19b4035fSMarshall Clow template <typename Input1, typename Input2 = Input1>
47*19b4035fSMarshall Clow constexpr bool do_test(int dummy = 0)
48*19b4035fSMarshall Clow {
49*19b4035fSMarshall Clow     using S1 = typename std::make_signed<Input1>::type;
50*19b4035fSMarshall Clow     using S2 = typename std::make_signed<Input2>::type;
51*19b4035fSMarshall Clow     using U1 = typename std::make_unsigned<Input1>::type;
52*19b4035fSMarshall Clow     using U2 = typename std::make_unsigned<Input2>::type;
53*19b4035fSMarshall Clow     bool accumulate = true;
54*19b4035fSMarshall Clow     for (auto TC : Cases) {
55*19b4035fSMarshall Clow         { // Test with two signed types
56*19b4035fSMarshall Clow             using Output = std::common_type_t<S1, S2>;
57*19b4035fSMarshall Clow             accumulate &= test0<S1, S2, Output>(TC.x, TC.y, TC.expect);
58*19b4035fSMarshall Clow             accumulate &= test0<S1, S2, Output>(-TC.x, TC.y, TC.expect);
59*19b4035fSMarshall Clow             accumulate &= test0<S1, S2, Output>(TC.x, -TC.y, TC.expect);
60*19b4035fSMarshall Clow             accumulate &= test0<S1, S2, Output>(-TC.x, -TC.y, TC.expect);
61*19b4035fSMarshall Clow             accumulate &= test0<S2, S1, Output>(TC.x, TC.y, TC.expect);
62*19b4035fSMarshall Clow             accumulate &= test0<S2, S1, Output>(-TC.x, TC.y, TC.expect);
63*19b4035fSMarshall Clow             accumulate &= test0<S2, S1, Output>(TC.x, -TC.y, TC.expect);
64*19b4035fSMarshall Clow             accumulate &= test0<S2, S1, Output>(-TC.x, -TC.y, TC.expect);
65*19b4035fSMarshall Clow         }
66*19b4035fSMarshall Clow         { // test with two unsigned types
67*19b4035fSMarshall Clow             using Output = std::common_type_t<U1, U2>;
68*19b4035fSMarshall Clow             accumulate &= test0<U1, U2, Output>(TC.x, TC.y, TC.expect);
69*19b4035fSMarshall Clow             accumulate &= test0<U2, U1, Output>(TC.x, TC.y, TC.expect);
70*19b4035fSMarshall Clow         }
71*19b4035fSMarshall Clow         { // Test with mixed signs
72*19b4035fSMarshall Clow             using Output = std::common_type_t<S1, U2>;
73*19b4035fSMarshall Clow             accumulate &= test0<S1, U2, Output>(TC.x, TC.y, TC.expect);
74*19b4035fSMarshall Clow             accumulate &= test0<U2, S1, Output>(TC.x, TC.y, TC.expect);
75*19b4035fSMarshall Clow             accumulate &= test0<S1, U2, Output>(-TC.x, TC.y, TC.expect);
76*19b4035fSMarshall Clow             accumulate &= test0<U2, S1, Output>(TC.x, -TC.y, TC.expect);
77*19b4035fSMarshall Clow         }
78*19b4035fSMarshall Clow         { // Test with mixed signs
79*19b4035fSMarshall Clow             using Output = std::common_type_t<S2, U1>;
80*19b4035fSMarshall Clow             accumulate &= test0<S2, U1, Output>(TC.x, TC.y, TC.expect);
81*19b4035fSMarshall Clow             accumulate &= test0<U1, S2, Output>(TC.x, TC.y, TC.expect);
82*19b4035fSMarshall Clow             accumulate &= test0<S2, U1, Output>(-TC.x, TC.y, TC.expect);
83*19b4035fSMarshall Clow             accumulate &= test0<U1, S2, Output>(TC.x, -TC.y, TC.expect);
84*19b4035fSMarshall Clow         }
85*19b4035fSMarshall Clow     }
86*19b4035fSMarshall Clow     return accumulate;
87*19b4035fSMarshall Clow }
88*19b4035fSMarshall Clow 
89*19b4035fSMarshall Clow int main()
90*19b4035fSMarshall Clow {
91*19b4035fSMarshall Clow     auto non_cce = std::rand(); // a value that can't possibly be constexpr
92*19b4035fSMarshall Clow 
93*19b4035fSMarshall Clow     static_assert(do_test<signed char>(), "");
94*19b4035fSMarshall Clow     static_assert(do_test<short>(), "");
95*19b4035fSMarshall Clow     static_assert(do_test<int>(), "");
96*19b4035fSMarshall Clow     static_assert(do_test<long>(), "");
97*19b4035fSMarshall Clow     static_assert(do_test<long long>(), "");
98*19b4035fSMarshall Clow 
99*19b4035fSMarshall Clow     assert(do_test<signed char>(non_cce));
100*19b4035fSMarshall Clow     assert(do_test<short>(non_cce));
101*19b4035fSMarshall Clow     assert(do_test<int>(non_cce));
102*19b4035fSMarshall Clow     assert(do_test<long>(non_cce));
103*19b4035fSMarshall Clow     assert(do_test<long long>(non_cce));
104*19b4035fSMarshall Clow 
105*19b4035fSMarshall Clow     static_assert(do_test< int8_t>(), "");
106*19b4035fSMarshall Clow     static_assert(do_test<int16_t>(), "");
107*19b4035fSMarshall Clow     static_assert(do_test<int32_t>(), "");
108*19b4035fSMarshall Clow     static_assert(do_test<int64_t>(), "");
109*19b4035fSMarshall Clow 
110*19b4035fSMarshall Clow     assert(do_test< int8_t>(non_cce));
111*19b4035fSMarshall Clow     assert(do_test<int16_t>(non_cce));
112*19b4035fSMarshall Clow     assert(do_test<int32_t>(non_cce));
113*19b4035fSMarshall Clow     assert(do_test<int64_t>(non_cce));
114*19b4035fSMarshall Clow 
115*19b4035fSMarshall Clow     static_assert(do_test<signed char, int>(), "");
116*19b4035fSMarshall Clow     static_assert(do_test<int, signed char>(), "");
117*19b4035fSMarshall Clow     static_assert(do_test<short, int>(), "");
118*19b4035fSMarshall Clow     static_assert(do_test<int, short>(), "");
119*19b4035fSMarshall Clow     static_assert(do_test<int, long>(), "");
120*19b4035fSMarshall Clow     static_assert(do_test<long, int>(), "");
121*19b4035fSMarshall Clow     static_assert(do_test<int, long long>(), "");
122*19b4035fSMarshall Clow     static_assert(do_test<long long, int>(), "");
123*19b4035fSMarshall Clow 
124*19b4035fSMarshall Clow     assert((do_test<signed char, int>(non_cce)));
125*19b4035fSMarshall Clow     assert((do_test<int, signed char>(non_cce)));
126*19b4035fSMarshall Clow     assert((do_test<short, int>(non_cce)));
127*19b4035fSMarshall Clow     assert((do_test<int, short>(non_cce)));
128*19b4035fSMarshall Clow     assert((do_test<int, long>(non_cce)));
129*19b4035fSMarshall Clow     assert((do_test<long, int>(non_cce)));
130*19b4035fSMarshall Clow     assert((do_test<int, long long>(non_cce)));
131*19b4035fSMarshall Clow     assert((do_test<long long, int>(non_cce)));
132*19b4035fSMarshall Clow }
133