blob: 22f2a6333014ea2a381ce38e6fb98b4a02eededa [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
Marshall Clowa2e54b62017-02-08 07:40:59 +000010
Marshall Clow19b40352016-07-26 14:29:45 +000011// <numeric>
12
13// template<class _M, class _N>
14// constexpr common_type_t<_M,_N> gcd(_M __m, _N __n)
15
16#include <numeric>
17#include <cassert>
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000018#include <climits>
19#include <cstdint>
Marshall Clow19b40352016-07-26 14:29:45 +000020#include <cstdlib> // for rand()
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000021#include <type_traits>
Marshall Clow19b40352016-07-26 14:29:45 +000022
Marshall Clow7fc6a552019-05-31 18:35:30 +000023#include "test_macros.h"
24
Marshall Clow19b40352016-07-26 14:29:45 +000025constexpr struct {
26 int x;
27 int y;
28 int expect;
29} Cases[] = {
30 {0, 0, 0},
31 {1, 0, 1},
32 {0, 1, 1},
33 {1, 1, 1},
34 {2, 3, 1},
35 {2, 4, 2},
36 {36, 17, 1},
37 {36, 18, 18}
38};
39
40
41template <typename Input1, typename Input2, typename Output>
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000042constexpr bool test0(int in1, int in2, int out)
Marshall Clow19b40352016-07-26 14:29:45 +000043{
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000044 auto value1 = static_cast<Input1>(in1);
45 auto value2 = static_cast<Input2>(in2);
46 static_assert(std::is_same_v<Output, decltype(std::gcd(value1, value2))>, "");
47 static_assert(std::is_same_v<Output, decltype(std::gcd(value2, value1))>, "");
48 assert(static_cast<Output>(out) == std::gcd(value1, value2));
49 return true;
Marshall Clow19b40352016-07-26 14:29:45 +000050}
51
52
53template <typename Input1, typename Input2 = Input1>
Eric Fiselierfd838222016-12-23 23:37:52 +000054constexpr bool do_test(int = 0)
Marshall Clow19b40352016-07-26 14:29:45 +000055{
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +000056 using S1 = std::make_signed_t<Input1>;
57 using S2 = std::make_signed_t<Input2>;
58 using U1 = std::make_unsigned_t<Input1>;
59 using U2 = std::make_unsigned_t<Input2>;
Marshall Clow19b40352016-07-26 14:29:45 +000060 bool accumulate = true;
61 for (auto TC : Cases) {
62 { // Test with two signed types
63 using Output = std::common_type_t<S1, S2>;
64 accumulate &= test0<S1, S2, Output>(TC.x, TC.y, TC.expect);
65 accumulate &= test0<S1, S2, Output>(-TC.x, TC.y, TC.expect);
66 accumulate &= test0<S1, S2, Output>(TC.x, -TC.y, TC.expect);
67 accumulate &= test0<S1, S2, Output>(-TC.x, -TC.y, TC.expect);
68 accumulate &= test0<S2, S1, Output>(TC.x, TC.y, TC.expect);
69 accumulate &= test0<S2, S1, Output>(-TC.x, TC.y, TC.expect);
70 accumulate &= test0<S2, S1, Output>(TC.x, -TC.y, TC.expect);
71 accumulate &= test0<S2, S1, Output>(-TC.x, -TC.y, TC.expect);
72 }
73 { // test with two unsigned types
74 using Output = std::common_type_t<U1, U2>;
75 accumulate &= test0<U1, U2, Output>(TC.x, TC.y, TC.expect);
76 accumulate &= test0<U2, U1, Output>(TC.x, TC.y, TC.expect);
77 }
78 { // Test with mixed signs
79 using Output = std::common_type_t<S1, U2>;
80 accumulate &= test0<S1, U2, Output>(TC.x, TC.y, TC.expect);
81 accumulate &= test0<U2, S1, Output>(TC.x, TC.y, TC.expect);
82 accumulate &= test0<S1, U2, Output>(-TC.x, TC.y, TC.expect);
83 accumulate &= test0<U2, S1, Output>(TC.x, -TC.y, TC.expect);
84 }
85 { // Test with mixed signs
86 using Output = std::common_type_t<S2, U1>;
87 accumulate &= test0<S2, U1, Output>(TC.x, TC.y, TC.expect);
88 accumulate &= test0<U1, S2, Output>(TC.x, TC.y, TC.expect);
89 accumulate &= test0<S2, U1, Output>(-TC.x, TC.y, TC.expect);
90 accumulate &= test0<U1, S2, Output>(TC.x, -TC.y, TC.expect);
91 }
92 }
93 return accumulate;
94}
95
JF Bastien2df59c52019-02-04 20:31:13 +000096int main(int, char**)
Marshall Clow19b40352016-07-26 14:29:45 +000097{
98 auto non_cce = std::rand(); // a value that can't possibly be constexpr
99
100 static_assert(do_test<signed char>(), "");
101 static_assert(do_test<short>(), "");
102 static_assert(do_test<int>(), "");
103 static_assert(do_test<long>(), "");
104 static_assert(do_test<long long>(), "");
105
106 assert(do_test<signed char>(non_cce));
107 assert(do_test<short>(non_cce));
108 assert(do_test<int>(non_cce));
109 assert(do_test<long>(non_cce));
110 assert(do_test<long long>(non_cce));
111
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +0000112 static_assert(do_test<std::int8_t>(), "");
113 static_assert(do_test<std::int16_t>(), "");
114 static_assert(do_test<std::int32_t>(), "");
115 static_assert(do_test<std::int64_t>(), "");
Marshall Clow19b40352016-07-26 14:29:45 +0000116
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +0000117 assert(do_test<std::int8_t>(non_cce));
118 assert(do_test<std::int16_t>(non_cce));
119 assert(do_test<std::int32_t>(non_cce));
120 assert(do_test<std::int64_t>(non_cce));
Marshall Clow19b40352016-07-26 14:29:45 +0000121
122 static_assert(do_test<signed char, int>(), "");
123 static_assert(do_test<int, signed char>(), "");
124 static_assert(do_test<short, int>(), "");
125 static_assert(do_test<int, short>(), "");
126 static_assert(do_test<int, long>(), "");
127 static_assert(do_test<long, int>(), "");
128 static_assert(do_test<int, long long>(), "");
129 static_assert(do_test<long long, int>(), "");
130
131 assert((do_test<signed char, int>(non_cce)));
132 assert((do_test<int, signed char>(non_cce)));
133 assert((do_test<short, int>(non_cce)));
134 assert((do_test<int, short>(non_cce)));
135 assert((do_test<int, long>(non_cce)));
136 assert((do_test<long, int>(non_cce)));
137 assert((do_test<int, long long>(non_cce)));
138 assert((do_test<long long, int>(non_cce)));
Marshall Clow7a1c0ef2017-02-06 16:03:23 +0000139
140// LWG#2837
141 {
Billy Robert O'Neal III8c401172017-05-08 21:52:05 +0000142 auto res = std::gcd(static_cast<std::int64_t>(1234), INT32_MIN);
143 static_assert(std::is_same_v<decltype(res), std::int64_t>, "");
144 assert(res == 2);
Marshall Clow7a1c0ef2017-02-06 16:03:23 +0000145 }
JF Bastien2df59c52019-02-04 20:31:13 +0000146
147 return 0;
Marshall Clow19b40352016-07-26 14:29:45 +0000148}