blob: 0774bff40d15737702fd92ec433fed52fe2e8ba9 [file] [log] [blame]
Marshall Clow19b40352016-07-26 14:29:45 +00001//===----------------------------------------------------------------------===//
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//===----------------------------------------------------------------------===//
Stephan T. Lavavejd72ece62016-11-23 22:03:28 +00009//
Marshall Clow19b40352016-07-26 14:29:45 +000010// UNSUPPORTED: c++98, c++03, c++11, c++14
Marshall Clowa2e54b62017-02-08 07:40:59 +000011// XFAIL: ubsan
12
Marshall Clow19b40352016-07-26 14:29:45 +000013// <numeric>
14
15// template<class _M, class _N>
16// constexpr common_type_t<_M,_N> gcd(_M __m, _N __n)
17
18#include <numeric>
19#include <cassert>
20#include <cstdlib> // for rand()
21#include <iostream>
22
23constexpr struct {
24 int x;
25 int y;
26 int expect;
27} Cases[] = {
28 {0, 0, 0},
29 {1, 0, 1},
30 {0, 1, 1},
31 {1, 1, 1},
32 {2, 3, 1},
33 {2, 4, 2},
34 {36, 17, 1},
35 {36, 18, 18}
36};
37
38
39template <typename Input1, typename Input2, typename Output>
40constexpr bool test0(Input1 in1, Input2 in2, Output out)
41{
42 static_assert((std::is_same<Output, decltype(std::gcd(in1, in2))>::value), "" );
43 static_assert((std::is_same<Output, decltype(std::gcd(in2, in1))>::value), "" );
44 return out == std::gcd(in1, in2) ? true : (std::abort(), false);
45}
46
47
48template <typename Input1, typename Input2 = Input1>
Eric Fiselierfd838222016-12-23 23:37:52 +000049constexpr bool do_test(int = 0)
Marshall Clow19b40352016-07-26 14:29:45 +000050{
51 using S1 = typename std::make_signed<Input1>::type;
52 using S2 = typename std::make_signed<Input2>::type;
53 using U1 = typename std::make_unsigned<Input1>::type;
54 using U2 = typename std::make_unsigned<Input2>::type;
55 bool accumulate = true;
56 for (auto TC : Cases) {
57 { // Test with two signed types
58 using Output = std::common_type_t<S1, S2>;
59 accumulate &= test0<S1, S2, Output>(TC.x, TC.y, TC.expect);
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<S2, S1, 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 }
68 { // test with two unsigned types
69 using Output = std::common_type_t<U1, U2>;
70 accumulate &= test0<U1, U2, Output>(TC.x, TC.y, TC.expect);
71 accumulate &= test0<U2, U1, Output>(TC.x, TC.y, TC.expect);
72 }
73 { // Test with mixed signs
74 using Output = std::common_type_t<S1, U2>;
75 accumulate &= test0<S1, U2, Output>(TC.x, TC.y, TC.expect);
76 accumulate &= test0<U2, S1, Output>(TC.x, TC.y, TC.expect);
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 }
80 { // Test with mixed signs
81 using Output = std::common_type_t<S2, U1>;
82 accumulate &= test0<S2, U1, Output>(TC.x, TC.y, TC.expect);
83 accumulate &= test0<U1, S2, Output>(TC.x, TC.y, TC.expect);
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 }
87 }
88 return accumulate;
89}
90
91int main()
92{
93 auto non_cce = std::rand(); // a value that can't possibly be constexpr
94
95 static_assert(do_test<signed char>(), "");
96 static_assert(do_test<short>(), "");
97 static_assert(do_test<int>(), "");
98 static_assert(do_test<long>(), "");
99 static_assert(do_test<long long>(), "");
100
101 assert(do_test<signed char>(non_cce));
102 assert(do_test<short>(non_cce));
103 assert(do_test<int>(non_cce));
104 assert(do_test<long>(non_cce));
105 assert(do_test<long long>(non_cce));
106
107 static_assert(do_test< int8_t>(), "");
108 static_assert(do_test<int16_t>(), "");
109 static_assert(do_test<int32_t>(), "");
110 static_assert(do_test<int64_t>(), "");
111
112 assert(do_test< int8_t>(non_cce));
113 assert(do_test<int16_t>(non_cce));
114 assert(do_test<int32_t>(non_cce));
115 assert(do_test<int64_t>(non_cce));
116
117 static_assert(do_test<signed char, int>(), "");
118 static_assert(do_test<int, signed char>(), "");
119 static_assert(do_test<short, int>(), "");
120 static_assert(do_test<int, short>(), "");
121 static_assert(do_test<int, long>(), "");
122 static_assert(do_test<long, int>(), "");
123 static_assert(do_test<int, long long>(), "");
124 static_assert(do_test<long long, int>(), "");
125
126 assert((do_test<signed char, int>(non_cce)));
127 assert((do_test<int, signed char>(non_cce)));
128 assert((do_test<short, int>(non_cce)));
129 assert((do_test<int, short>(non_cce)));
130 assert((do_test<int, long>(non_cce)));
131 assert((do_test<long, int>(non_cce)));
132 assert((do_test<int, long long>(non_cce)));
133 assert((do_test<long long, int>(non_cce)));
Marshall Clow7a1c0ef2017-02-06 16:03:23 +0000134
135// LWG#2837
136 {
137 auto res = std::gcd((int64_t)1234, (int32_t)-2147483648);
138 static_assert( std::is_same<decltype(res), std::common_type<int64_t, int32_t>::type>::value, "");
139 assert(res == 2);
140 }
Marshall Clow19b40352016-07-26 14:29:45 +0000141}