xref: /llvm-project/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.pointer.pass.cpp (revision 330ab33f7c137ad3debd813ab7e7599e44e19346)
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++98, 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 <numeric>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 
22 
23 template <typename T>
24 void pointer_test()
25 {
26     T array[1000] = {}; // we need an array to make valid pointers
27     constexpr T cArray[2] = {};
28     ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), T*);
29     ASSERT_NOEXCEPT(          std::midpoint(array, array));
30 
31     static_assert(std::midpoint(cArray, cArray + 2) == cArray + 1, "");
32     static_assert(std::midpoint(cArray + 2, cArray) == cArray + 1, "");
33 
34     assert(std::midpoint(array, array)        == array);
35     assert(std::midpoint(array, array + 1000) == array + 500);
36 
37     assert(std::midpoint(array, array +    9) == array + 4);
38     assert(std::midpoint(array, array +   10) == array + 5);
39     assert(std::midpoint(array, array +   11) == array + 5);
40     assert(std::midpoint(array +    9, array) == array + 5);
41     assert(std::midpoint(array +   10, array) == array + 5);
42     assert(std::midpoint(array +   11, array) == array + 6);
43 }
44 
45 
46 int main(int, char**)
47 {
48     pointer_test<               char>();
49     pointer_test<const          char>();
50     pointer_test<      volatile char>();
51     pointer_test<const volatile char>();
52 
53     pointer_test<int>();
54     pointer_test<double>();
55 
56     return 0;
57 }
58