]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/experimental/numeric/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / experimental / numeric / numeric.ops / numeric.ops.gcd / gcd.pass.cpp
1 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 //
10 // UNSUPPORTED: c++98, c++03, c++11
11 // <numeric>
12
13 // template<class _M, class _N>
14 // constexpr common_type_t<_M,_N> gcd(_M __m, _N __n)
15
16 #include <experimental/numeric>
17 #include <cassert>
18 #include <cstdlib>    // for rand()
19 #include <iostream>
20
21 constexpr struct {
22   int x;
23   int y;
24   int expect;
25 } Cases[] = {
26     {0, 0, 0},
27     {1, 0, 1},
28     {0, 1, 1},
29     {1, 1, 1},
30     {2, 3, 1},
31     {2, 4, 2},
32     {36, 17, 1},
33     {36, 18, 18}
34 };
35
36
37 template <typename Input1, typename Input2, typename Output>
38 constexpr bool test0(Input1 in1, Input2 in2, Output out)
39 {
40     static_assert((std::is_same<Output, decltype(std::experimental::gcd(in1, in2))>::value), "" );
41     static_assert((std::is_same<Output, decltype(std::experimental::gcd(in2, in1))>::value), "" );
42     return out == std::experimental::gcd(in1, in2) ? true : (std::abort(), false);
43 }
44
45
46 template <typename Input1, typename Input2 = Input1>
47 constexpr bool do_test(int = 0)
48 {
49     using S1 = typename std::make_signed<Input1>::type;
50     using S2 = typename std::make_signed<Input2>::type;
51     using U1 = typename std::make_unsigned<Input1>::type;
52     using U2 = typename std::make_unsigned<Input2>::type;
53     bool accumulate = true;
54     for (auto TC : Cases) {
55         { // Test with two signed types
56             using Output = std::common_type_t<S1, S2>;
57             accumulate &= test0<S1, S2, Output>(TC.x, TC.y, TC.expect);
58             accumulate &= test0<S1, S2, Output>(-TC.x, TC.y, TC.expect);
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<S2, S1, Output>(TC.x, TC.y, TC.expect);
62             accumulate &= test0<S2, S1, 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         }
66         { // test with two unsigned types
67             using Output = std::common_type_t<U1, U2>;
68             accumulate &= test0<U1, U2, Output>(TC.x, TC.y, TC.expect);
69             accumulate &= test0<U2, U1, Output>(TC.x, TC.y, TC.expect);
70         }
71         { // Test with mixed signs
72             using Output = std::common_type_t<S1, U2>;
73             accumulate &= test0<S1, U2, Output>(TC.x, TC.y, TC.expect);
74             accumulate &= test0<U2, S1, Output>(TC.x, TC.y, TC.expect);
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         }
78         { // Test with mixed signs
79             using Output = std::common_type_t<S2, U1>;
80             accumulate &= test0<S2, U1, Output>(TC.x, TC.y, TC.expect);
81             accumulate &= test0<U1, S2, Output>(TC.x, TC.y, TC.expect);
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         }
85     }
86     return accumulate;
87 }
88
89 int main()
90 {
91     auto non_cce = std::rand(); // a value that can't possibly be constexpr
92
93     static_assert(do_test<signed char>(), "");
94     static_assert(do_test<short>(), "");
95     static_assert(do_test<int>(), "");
96     static_assert(do_test<long>(), "");
97     static_assert(do_test<long long>(), "");
98
99     assert(do_test<signed char>(non_cce));
100     assert(do_test<short>(non_cce));
101     assert(do_test<int>(non_cce));
102     assert(do_test<long>(non_cce));
103     assert(do_test<long long>(non_cce));
104
105     static_assert(do_test< int8_t>(), "");
106     static_assert(do_test<int16_t>(), "");
107     static_assert(do_test<int32_t>(), "");
108     static_assert(do_test<int64_t>(), "");
109
110     assert(do_test< int8_t>(non_cce));
111     assert(do_test<int16_t>(non_cce));
112     assert(do_test<int32_t>(non_cce));
113     assert(do_test<int64_t>(non_cce));
114
115     static_assert(do_test<signed char, int>(), "");
116     static_assert(do_test<int, signed char>(), "");
117     static_assert(do_test<short, int>(), "");
118     static_assert(do_test<int, short>(), "");
119     static_assert(do_test<int, long>(), "");
120     static_assert(do_test<long, int>(), "");
121     static_assert(do_test<int, long long>(), "");
122     static_assert(do_test<long long, int>(), "");
123
124     assert((do_test<signed char, int>(non_cce)));
125     assert((do_test<int, signed char>(non_cce)));
126     assert((do_test<short, int>(non_cce)));
127     assert((do_test<int, short>(non_cce)));
128     assert((do_test<int, long>(non_cce)));
129     assert((do_test<long, int>(non_cce)));
130     assert((do_test<int, long long>(non_cce)));
131     assert((do_test<long long, int>(non_cce)));
132 }