1 //===----------------------------------------------------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // <numeric>
11
12 // template <class _Tp>
13 // _Tp midpoint(_Tp __a, _Tp __b) noexcept
14 //
15
16 #include <stdint.h>
17 #include <limits>
18 #include <numeric>
19 #include <cassert>
20 #include "test_macros.h"
21
22 template <typename T>
signed_test()23 void signed_test()
24 {
25 constexpr T zero{0};
26 constexpr T one{1};
27 constexpr T two{2};
28 constexpr T three{3};
29 constexpr T four{4};
30
31 ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T);
32 ASSERT_NOEXCEPT( std::midpoint(T(), T()));
33 using limits = std::numeric_limits<T>;
34
35 static_assert(std::midpoint(one, three) == two, "");
36 static_assert(std::midpoint(three, one) == two, "");
37
38 assert(std::midpoint(zero, zero) == zero);
39 assert(std::midpoint(zero, two) == one);
40 assert(std::midpoint(two, zero) == one);
41 assert(std::midpoint(two, two) == two);
42
43 assert(std::midpoint(one, four) == two);
44 assert(std::midpoint(four, one) == three);
45 assert(std::midpoint(three, four) == three);
46 assert(std::midpoint(four, three) == four);
47
48 assert(std::midpoint(T( 3), T( 4)) == T(3));
49 assert(std::midpoint(T( 4), T( 3)) == T(4));
50 assert(std::midpoint(T(-3), T( 4)) == T(0));
51 assert(std::midpoint(T(-4), T( 3)) == T(-1));
52 assert(std::midpoint(T( 3), T(-4)) == T(0));
53 assert(std::midpoint(T( 4), T(-3)) == T(1));
54 assert(std::midpoint(T(-3), T(-4)) == T(-3));
55 assert(std::midpoint(T(-4), T(-3)) == T(-4));
56
57 static_assert(std::midpoint(limits::min(), limits::max()) == T(-1), "");
58 static_assert(std::midpoint(limits::max(), limits::min()) == T( 0), "");
59
60 static_assert(std::midpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
61 assert( std::midpoint(T(6), limits::min()) == limits::min()/2 + 3);
62 assert( std::midpoint(limits::max(), T(6)) == limits::max()/2 + 4);
63 static_assert(std::midpoint(T(6), limits::max()) == limits::max()/2 + 3, "");
64
65 assert( std::midpoint(limits::min(), T(-6)) == limits::min()/2 - 3);
66 static_assert(std::midpoint(T(-6), limits::min()) == limits::min()/2 - 3, "");
67 static_assert(std::midpoint(limits::max(), T(-6)) == limits::max()/2 - 2, "");
68 assert( std::midpoint(T(-6), limits::max()) == limits::max()/2 - 3);
69 }
70
71 template <typename T>
unsigned_test()72 void unsigned_test()
73 {
74 constexpr T zero{0};
75 constexpr T one{1};
76 constexpr T two{2};
77 constexpr T three{3};
78 constexpr T four{4};
79
80 ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T);
81 ASSERT_NOEXCEPT( std::midpoint(T(), T()));
82 using limits = std::numeric_limits<T>;
83 const T half_way = (limits::max() - limits::min())/2;
84
85 static_assert(std::midpoint(one, three) == two, "");
86 static_assert(std::midpoint(three, one) == two, "");
87
88 assert(std::midpoint(zero, zero) == zero);
89 assert(std::midpoint(zero, two) == one);
90 assert(std::midpoint(two, zero) == one);
91 assert(std::midpoint(two, two) == two);
92
93 assert(std::midpoint(one, four) == two);
94 assert(std::midpoint(four, one) == three);
95 assert(std::midpoint(three, four) == three);
96 assert(std::midpoint(four, three) == four);
97
98 assert(std::midpoint(limits::min(), limits::max()) == T(half_way));
99 assert(std::midpoint(limits::max(), limits::min()) == T(half_way + 1));
100
101 static_assert(std::midpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
102 assert( std::midpoint(T(6), limits::min()) == limits::min()/2 + 3);
103 assert( std::midpoint(limits::max(), T(6)) == half_way + 4);
104 static_assert(std::midpoint(T(6), limits::max()) == half_way + 3, "");
105 }
106
107
main(int,char **)108 int main(int, char**)
109 {
110 signed_test<signed char>();
111 signed_test<short>();
112 signed_test<int>();
113 signed_test<long>();
114 signed_test<long long>();
115
116 signed_test<int8_t>();
117 signed_test<int16_t>();
118 signed_test<int32_t>();
119 signed_test<int64_t>();
120
121 unsigned_test<unsigned char>();
122 unsigned_test<unsigned short>();
123 unsigned_test<unsigned int>();
124 unsigned_test<unsigned long>();
125 unsigned_test<unsigned long long>();
126
127 unsigned_test<uint8_t>();
128 unsigned_test<uint16_t>();
129 unsigned_test<uint32_t>();
130 unsigned_test<uint64_t>();
131
132 #ifndef _LIBCPP_HAS_NO_INT128
133 unsigned_test<__uint128_t>();
134 signed_test<__int128_t>();
135 #endif
136
137 // int_test<char>();
138 signed_test<ptrdiff_t>();
139 unsigned_test<size_t>();
140
141 return 0;
142 }
143