xref: /llvm-project/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.integer.pass.cpp (revision d868135691bb0d5c924b8fd2ae26171fbf5d1387)
1330ab33fSMarshall Clow //===----------------------------------------------------------------------===//
2330ab33fSMarshall Clow //
3330ab33fSMarshall Clow // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4330ab33fSMarshall Clow // See https://llvm.org/LICENSE.txt for license information.
5330ab33fSMarshall Clow // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6330ab33fSMarshall Clow //
7330ab33fSMarshall Clow //===----------------------------------------------------------------------===//
8330ab33fSMarshall Clow //
931cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14, c++17
10330ab33fSMarshall Clow // <numeric>
11330ab33fSMarshall Clow 
12330ab33fSMarshall Clow // template <class _Tp>
13330ab33fSMarshall Clow // _Tp midpoint(_Tp __a, _Tp __b) noexcept
14330ab33fSMarshall Clow //
15330ab33fSMarshall Clow 
16bd7db5acSBilly Robert O'Neal III #include <stdint.h>
17bd7db5acSBilly Robert O'Neal III #include <limits>
18330ab33fSMarshall Clow #include <numeric>
19330ab33fSMarshall Clow #include <cassert>
20e0a66116SNikolas Klauser #include <cstddef>
21da79d6e1SMark de Wever #include <cstdint>
22330ab33fSMarshall Clow #include "test_macros.h"
23330ab33fSMarshall Clow 
24330ab33fSMarshall Clow template <typename T>
signed_test()25330ab33fSMarshall Clow void signed_test()
26330ab33fSMarshall Clow {
27330ab33fSMarshall Clow     constexpr T zero{0};
28330ab33fSMarshall Clow     constexpr T one{1};
29330ab33fSMarshall Clow     constexpr T two{2};
30330ab33fSMarshall Clow     constexpr T three{3};
31330ab33fSMarshall Clow     constexpr T four{4};
32330ab33fSMarshall Clow 
33330ab33fSMarshall Clow     ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T);
34330ab33fSMarshall Clow     ASSERT_NOEXCEPT(          std::midpoint(T(), T()));
35330ab33fSMarshall Clow     using limits = std::numeric_limits<T>;
36330ab33fSMarshall Clow 
37330ab33fSMarshall Clow     static_assert(std::midpoint(one, three) == two, "");
38330ab33fSMarshall Clow     static_assert(std::midpoint(three, one) == two, "");
39330ab33fSMarshall Clow 
40330ab33fSMarshall Clow     assert(std::midpoint(zero, zero) == zero);
41330ab33fSMarshall Clow     assert(std::midpoint(zero, two)  == one);
42330ab33fSMarshall Clow     assert(std::midpoint(two, zero)  == one);
43330ab33fSMarshall Clow     assert(std::midpoint(two, two)   == two);
44330ab33fSMarshall Clow 
45330ab33fSMarshall Clow     assert(std::midpoint(one, four)    == two);
46330ab33fSMarshall Clow     assert(std::midpoint(four, one)    == three);
47330ab33fSMarshall Clow     assert(std::midpoint(three, four)  == three);
48330ab33fSMarshall Clow     assert(std::midpoint(four, three)  == four);
49330ab33fSMarshall Clow 
50330ab33fSMarshall Clow     assert(std::midpoint(T( 3), T( 4)) == T(3));
51330ab33fSMarshall Clow     assert(std::midpoint(T( 4), T( 3)) == T(4));
52330ab33fSMarshall Clow     assert(std::midpoint(T(-3), T( 4)) == T(0));
53330ab33fSMarshall Clow     assert(std::midpoint(T(-4), T( 3)) == T(-1));
54330ab33fSMarshall Clow     assert(std::midpoint(T( 3), T(-4)) == T(0));
55330ab33fSMarshall Clow     assert(std::midpoint(T( 4), T(-3)) == T(1));
56330ab33fSMarshall Clow     assert(std::midpoint(T(-3), T(-4)) == T(-3));
57330ab33fSMarshall Clow     assert(std::midpoint(T(-4), T(-3)) == T(-4));
58330ab33fSMarshall Clow 
59330ab33fSMarshall Clow     static_assert(std::midpoint(limits::min(), limits::max()) == T(-1), "");
60330ab33fSMarshall Clow     static_assert(std::midpoint(limits::max(), limits::min()) == T( 0), "");
61330ab33fSMarshall Clow 
62330ab33fSMarshall Clow     static_assert(std::midpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
63330ab33fSMarshall Clow     assert(       std::midpoint(T(6), limits::min()) == limits::min()/2 + 3);
64330ab33fSMarshall Clow     assert(       std::midpoint(limits::max(), T(6)) == limits::max()/2 + 4);
65330ab33fSMarshall Clow     static_assert(std::midpoint(T(6), limits::max()) == limits::max()/2 + 3, "");
66330ab33fSMarshall Clow 
67330ab33fSMarshall Clow     assert(       std::midpoint(limits::min(), T(-6)) == limits::min()/2 - 3);
68330ab33fSMarshall Clow     static_assert(std::midpoint(T(-6), limits::min()) == limits::min()/2 - 3, "");
69330ab33fSMarshall Clow     static_assert(std::midpoint(limits::max(), T(-6)) == limits::max()/2 - 2, "");
70330ab33fSMarshall Clow     assert(       std::midpoint(T(-6), limits::max()) == limits::max()/2 - 3);
71330ab33fSMarshall Clow }
72330ab33fSMarshall Clow 
73330ab33fSMarshall Clow template <typename T>
unsigned_test()74330ab33fSMarshall Clow void unsigned_test()
75330ab33fSMarshall Clow {
76330ab33fSMarshall Clow     constexpr T zero{0};
77330ab33fSMarshall Clow     constexpr T one{1};
78330ab33fSMarshall Clow     constexpr T two{2};
79330ab33fSMarshall Clow     constexpr T three{3};
80330ab33fSMarshall Clow     constexpr T four{4};
81330ab33fSMarshall Clow 
82330ab33fSMarshall Clow     ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T);
83330ab33fSMarshall Clow     ASSERT_NOEXCEPT(          std::midpoint(T(), T()));
84330ab33fSMarshall Clow     using limits = std::numeric_limits<T>;
85330ab33fSMarshall Clow     const T half_way = (limits::max() - limits::min())/2;
86330ab33fSMarshall Clow 
87330ab33fSMarshall Clow     static_assert(std::midpoint(one, three) == two, "");
88330ab33fSMarshall Clow     static_assert(std::midpoint(three, one) == two, "");
89330ab33fSMarshall Clow 
90330ab33fSMarshall Clow     assert(std::midpoint(zero, zero) == zero);
91330ab33fSMarshall Clow     assert(std::midpoint(zero, two)  == one);
92330ab33fSMarshall Clow     assert(std::midpoint(two, zero)  == one);
93330ab33fSMarshall Clow     assert(std::midpoint(two, two)   == two);
94330ab33fSMarshall Clow 
95330ab33fSMarshall Clow     assert(std::midpoint(one, four)    == two);
96330ab33fSMarshall Clow     assert(std::midpoint(four, one)    == three);
97330ab33fSMarshall Clow     assert(std::midpoint(three, four)  == three);
98330ab33fSMarshall Clow     assert(std::midpoint(four, three)  == four);
99330ab33fSMarshall Clow 
100330ab33fSMarshall Clow     assert(std::midpoint(limits::min(), limits::max()) == T(half_way));
101330ab33fSMarshall Clow     assert(std::midpoint(limits::max(), limits::min()) == T(half_way + 1));
102330ab33fSMarshall Clow 
103330ab33fSMarshall Clow     static_assert(std::midpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
104330ab33fSMarshall Clow     assert(       std::midpoint(T(6), limits::min()) == limits::min()/2 + 3);
105330ab33fSMarshall Clow     assert(       std::midpoint(limits::max(), T(6)) == half_way + 4);
106330ab33fSMarshall Clow     static_assert(std::midpoint(T(6), limits::max()) == half_way + 3, "");
107330ab33fSMarshall Clow }
108330ab33fSMarshall Clow 
109330ab33fSMarshall Clow 
main(int,char **)110330ab33fSMarshall Clow int main(int, char**)
111330ab33fSMarshall Clow {
112330ab33fSMarshall Clow     signed_test<signed char>();
113330ab33fSMarshall Clow     signed_test<short>();
114330ab33fSMarshall Clow     signed_test<int>();
115330ab33fSMarshall Clow     signed_test<long>();
116330ab33fSMarshall Clow     signed_test<long long>();
117330ab33fSMarshall Clow 
118bd5d0feeSMark de Wever     signed_test<std::int8_t>();
119bd5d0feeSMark de Wever     signed_test<std::int16_t>();
120bd5d0feeSMark de Wever     signed_test<std::int32_t>();
121bd5d0feeSMark de Wever     signed_test<std::int64_t>();
122330ab33fSMarshall Clow 
123330ab33fSMarshall Clow     unsigned_test<unsigned char>();
124330ab33fSMarshall Clow     unsigned_test<unsigned short>();
125330ab33fSMarshall Clow     unsigned_test<unsigned int>();
126330ab33fSMarshall Clow     unsigned_test<unsigned long>();
127330ab33fSMarshall Clow     unsigned_test<unsigned long long>();
128330ab33fSMarshall Clow 
129bd5d0feeSMark de Wever     unsigned_test<std::uint8_t>();
130bd5d0feeSMark de Wever     unsigned_test<std::uint16_t>();
131da79d6e1SMark de Wever     unsigned_test<std::uint32_t>();
132bd5d0feeSMark de Wever     unsigned_test<std::uint64_t>();
1336f8dddf1SMarshall Clow 
1348f972cb0SMark de Wever #ifndef TEST_HAS_NO_INT128
135330ab33fSMarshall Clow     unsigned_test<__uint128_t>();
1366f8dddf1SMarshall Clow     signed_test<__int128_t>();
1376f8dddf1SMarshall Clow #endif
138330ab33fSMarshall Clow 
139330ab33fSMarshall Clow //     int_test<char>();
140*d8681356SMark de Wever     signed_test<std::ptrdiff_t>();
141fb855eb9SMark de Wever     unsigned_test<std::size_t>();
142330ab33fSMarshall Clow 
143330ab33fSMarshall Clow     return 0;
144330ab33fSMarshall Clow }
145