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 <numeric>
17 #include <cassert>
18
19 #include "test_macros.h"
20
21
22
23 template <typename T>
constexpr_test()24 constexpr void constexpr_test()
25 {
26 constexpr T array[1000] = {};
27 ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), const T*);
28 ASSERT_NOEXCEPT( std::midpoint(array, array));
29
30 static_assert(std::midpoint(array, array) == array, "");
31 static_assert(std::midpoint(array, array + 1000) == array + 500, "");
32
33 static_assert(std::midpoint(array, array + 9) == array + 4, "");
34 static_assert(std::midpoint(array, array + 10) == array + 5, "");
35 static_assert(std::midpoint(array, array + 11) == array + 5, "");
36 static_assert(std::midpoint(array + 9, array) == array + 5, "");
37 static_assert(std::midpoint(array + 10, array) == array + 5, "");
38 static_assert(std::midpoint(array + 11, array) == array + 6, "");
39 }
40
41 template <typename T>
runtime_test()42 void runtime_test()
43 {
44 T array[1000] = {}; // we need an array to make valid pointers
45 ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), T*);
46 ASSERT_NOEXCEPT( std::midpoint(array, array));
47
48 assert(std::midpoint(array, array) == array);
49 assert(std::midpoint(array, array + 1000) == array + 500);
50
51 assert(std::midpoint(array, array + 9) == array + 4);
52 assert(std::midpoint(array, array + 10) == array + 5);
53 assert(std::midpoint(array, array + 11) == array + 5);
54 assert(std::midpoint(array + 9, array) == array + 5);
55 assert(std::midpoint(array + 10, array) == array + 5);
56 assert(std::midpoint(array + 11, array) == array + 6);
57 }
58
59 template <typename T>
pointer_test()60 void pointer_test()
61 {
62 runtime_test< T>();
63 runtime_test<const T>();
64 runtime_test< volatile T>();
65 runtime_test<const volatile T>();
66
67 // The constexpr tests are always const, but we can test them anyway.
68 constexpr_test< T>();
69 constexpr_test<const T>();
70
71 // GCC 9.0.1 (unreleased as of 2019-03) barfs on this, but we have a bot for it.
72 // Uncomment when gcc 9.1 is released
73 #ifndef TEST_COMPILER_GCC
74 constexpr_test< volatile T>();
75 constexpr_test<const volatile T>();
76 #endif
77 }
78
79
main(int,char **)80 int main(int, char**)
81 {
82 pointer_test<char>();
83 pointer_test<int>();
84 pointer_test<double>();
85
86 return 0;
87 }
88