1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
12 // template <class _Tp>
13 // _Tp midpoint(_Tp __a, _Tp __b) noexcept
22 #include "test_macros.h"
33 ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T
);
34 ASSERT_NOEXCEPT( std::midpoint(T(), T()));
35 using limits
= std::numeric_limits
<T
>;
37 static_assert(std::midpoint(one
, three
) == two
, "");
38 static_assert(std::midpoint(three
, one
) == two
, "");
40 assert(std::midpoint(zero
, zero
) == zero
);
41 assert(std::midpoint(zero
, two
) == one
);
42 assert(std::midpoint(two
, zero
) == one
);
43 assert(std::midpoint(two
, two
) == two
);
45 assert(std::midpoint(one
, four
) == two
);
46 assert(std::midpoint(four
, one
) == three
);
47 assert(std::midpoint(three
, four
) == three
);
48 assert(std::midpoint(four
, three
) == four
);
50 assert(std::midpoint(T( 3), T( 4)) == T(3));
51 assert(std::midpoint(T( 4), T( 3)) == T(4));
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(0));
55 assert(std::midpoint(T( 4), T(-3)) == T(1));
56 assert(std::midpoint(T(-3), T(-4)) == T(-3));
57 assert(std::midpoint(T(-4), T(-3)) == T(-4));
59 static_assert(std::midpoint(limits::min(), limits::max()) == T(-1), "");
60 static_assert(std::midpoint(limits::max(), limits::min()) == T( 0), "");
62 static_assert(std::midpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
63 assert( std::midpoint(T(6), limits::min()) == limits::min()/2 + 3);
64 assert( std::midpoint(limits::max(), T(6)) == limits::max()/2 + 4);
65 static_assert(std::midpoint(T(6), limits::max()) == limits::max()/2 + 3, "");
67 assert( std::midpoint(limits::min(), T(-6)) == limits::min()/2 - 3);
68 static_assert(std::midpoint(T(-6), limits::min()) == limits::min()/2 - 3, "");
69 static_assert(std::midpoint(limits::max(), T(-6)) == limits::max()/2 - 2, "");
70 assert( std::midpoint(T(-6), limits::max()) == limits::max()/2 - 3);
82 ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T
);
83 ASSERT_NOEXCEPT( std::midpoint(T(), T()));
84 using limits
= std::numeric_limits
<T
>;
85 const T half_way
= (limits::max() - limits::min())/2;
87 static_assert(std::midpoint(one
, three
) == two
, "");
88 static_assert(std::midpoint(three
, one
) == two
, "");
90 assert(std::midpoint(zero
, zero
) == zero
);
91 assert(std::midpoint(zero
, two
) == one
);
92 assert(std::midpoint(two
, zero
) == one
);
93 assert(std::midpoint(two
, two
) == two
);
95 assert(std::midpoint(one
, four
) == two
);
96 assert(std::midpoint(four
, one
) == three
);
97 assert(std::midpoint(three
, four
) == three
);
98 assert(std::midpoint(four
, three
) == four
);
100 assert(std::midpoint(limits::min(), limits::max()) == T(half_way
));
101 assert(std::midpoint(limits::max(), limits::min()) == T(half_way
+ 1));
103 static_assert(std::midpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
104 assert( std::midpoint(T(6), limits::min()) == limits::min()/2 + 3);
105 assert( std::midpoint(limits::max(), T(6)) == half_way
+ 4);
106 static_assert(std::midpoint(T(6), limits::max()) == half_way
+ 3, "");
110 int main(int, char**)
112 signed_test
<signed char>();
113 signed_test
<short>();
116 signed_test
<long long>();
118 signed_test
<std::int8_t>();
119 signed_test
<std::int16_t>();
120 signed_test
<std::int32_t>();
121 signed_test
<std::int64_t>();
123 unsigned_test
<unsigned char>();
124 unsigned_test
<unsigned short>();
125 unsigned_test
<unsigned int>();
126 unsigned_test
<unsigned long>();
127 unsigned_test
<unsigned long long>();
129 unsigned_test
<std::uint8_t>();
130 unsigned_test
<std::uint16_t>();
131 unsigned_test
<std::uint32_t>();
132 unsigned_test
<std::uint64_t>();
134 #ifndef TEST_HAS_NO_INT128
135 unsigned_test
<__uint128_t
>();
136 signed_test
<__int128_t
>();
140 signed_test
<std::ptrdiff_t>();
141 unsigned_test
<std::size_t>();