xref: /llvm-project/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.pointer.pass.cpp (revision c9535d7b61a37d6fa58e51222a9ccecc54431193)
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 
11 // MSVC warning C5215: a function parameter with a volatile qualified type is deprecated in C++20
12 // MSVC warning C5216: a volatile qualified return type is deprecated in C++20
13 // ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd5215 /wd5216
14 
15 // <numeric>
16 
17 // template <class _Tp>
18 // _Tp* midpoint(_Tp* __a, _Tp* __b) noexcept
19 //
20 
21 #include <numeric>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 
26 
27 
28 template <typename T>
constexpr_test()29 constexpr void constexpr_test()
30 {
31     constexpr T array[1000] = {};
32     ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), const T*);
33     ASSERT_NOEXCEPT(          std::midpoint(array, array));
34 
35     static_assert(std::midpoint(array, array)        == array, "");
36     static_assert(std::midpoint(array, array + 1000) == array + 500, "");
37 
38     static_assert(std::midpoint(array, array +    9) == array + 4, "");
39     static_assert(std::midpoint(array, array +   10) == array + 5, "");
40     static_assert(std::midpoint(array, array +   11) == array + 5, "");
41     static_assert(std::midpoint(array +    9, array) == array + 5, "");
42     static_assert(std::midpoint(array +   10, array) == array + 5, "");
43     static_assert(std::midpoint(array +   11, array) == array + 6, "");
44 }
45 
46 template <typename T>
runtime_test()47 void runtime_test()
48 {
49     T array[1000] = {}; // we need an array to make valid pointers
50     ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), T*);
51     ASSERT_NOEXCEPT(          std::midpoint(array, array));
52 
53     assert(std::midpoint(array, array)        == array);
54     assert(std::midpoint(array, array + 1000) == array + 500);
55 
56     assert(std::midpoint(array, array +    9) == array + 4);
57     assert(std::midpoint(array, array +   10) == array + 5);
58     assert(std::midpoint(array, array +   11) == array + 5);
59     assert(std::midpoint(array +    9, array) == array + 5);
60     assert(std::midpoint(array +   10, array) == array + 5);
61     assert(std::midpoint(array +   11, array) == array + 6);
62 
63     // explicit instantiation
64     ASSERT_SAME_TYPE(decltype(std::midpoint<T>(array, array)), T*);
65     ASSERT_NOEXCEPT(std::midpoint<T>(array, array));
66     assert(std::midpoint<T>(array, array) == array);
67     assert(std::midpoint<T>(array, array + 1000) == array + 500);
68 }
69 
70 template <typename T>
pointer_test()71 void pointer_test()
72 {
73     runtime_test<               T>();
74     runtime_test<const          T>();
75     runtime_test<      volatile T>();
76     runtime_test<const volatile T>();
77 
78 //  The constexpr tests are always const, but we can test them anyway.
79     constexpr_test<               T>();
80     constexpr_test<const          T>();
81 
82 //  GCC 9.0.1 (unreleased as of 2019-03) barfs on this, but we have a bot for it.
83 //  Uncomment when gcc 9.1 is released
84 #ifndef TEST_COMPILER_GCC
85     constexpr_test<      volatile T>();
86     constexpr_test<const volatile T>();
87 #endif
88 }
89 
90 
main(int,char **)91 int main(int, char**)
92 {
93     pointer_test<char>();
94     pointer_test<int>();
95     pointer_test<double>();
96 
97     return 0;
98 }
99