| 1 | // Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++20 } } |
| 19 | // { dg-add-options no_pch } |
| 20 | |
| 21 | #include <numeric> |
| 22 | |
| 23 | #ifndef __cpp_lib_interpolate |
| 24 | # error "Feature-test macro for midpoint and lerp missing in <numeric>" |
| 25 | #elif __cpp_lib_interpolate != 201902L |
| 26 | # error "Feature-test macro for midpoint and lerp has wrong value in <numeric>" |
| 27 | #endif |
| 28 | |
| 29 | #include <climits> |
| 30 | #include <testsuite_hooks.h> |
| 31 | |
| 32 | static_assert(std::is_same_v<decltype(std::midpoint(0, 1)), int>); |
| 33 | static_assert(noexcept(std::midpoint(1, 2))); |
| 34 | |
| 35 | struct test_type { }; |
| 36 | template<typename T> decltype(std::midpoint<T>(T(), T())) try_midpoint(int); |
| 37 | template<typename T> test_type try_midpoint(...); |
| 38 | template<typename T> constexpr bool no_midpoint() |
| 39 | { return std::is_same_v<decltype(try_midpoint<T>()), test_type>; } |
| 40 | |
| 41 | static_assert(no_midpoint<bool>()); |
| 42 | static_assert(no_midpoint<const bool>()); |
| 43 | static_assert(no_midpoint<const int>()); |
| 44 | static_assert(no_midpoint<volatile int>()); |
| 45 | |
| 46 | static_assert( std::midpoint(0, 0) == 0 ); |
| 47 | static_assert( std::midpoint(1, 1) == 1 ); |
| 48 | static_assert( std::midpoint(0, 1) == 0 ); |
| 49 | static_assert( std::midpoint(1, 0) == 1 ); |
| 50 | static_assert( std::midpoint(0, 2) == 1 ); |
| 51 | static_assert( std::midpoint(3, 2) == 3 ); |
| 52 | static_assert( std::midpoint(-5, 4) == -1 ); |
| 53 | static_assert( std::midpoint(5, -4) == 1 ); |
| 54 | static_assert( std::midpoint(-5, -4) == -5 ); |
| 55 | static_assert( std::midpoint(-4, -5) == -4 ); |
| 56 | static_assert( std::midpoint(INT_MIN, INT_MAX) == -1 ); |
| 57 | static_assert( std::midpoint(INT_MAX, INT_MIN) == 0 ); |
| 58 | static_assert( std::midpoint(INT_MAX, INT_MAX) == INT_MAX ); |
| 59 | static_assert( std::midpoint(INT_MAX, INT_MAX-1) == INT_MAX ); |
| 60 | static_assert( std::midpoint(INT_MAX-1, INT_MAX-1) == INT_MAX-1 ); |
| 61 | static_assert( std::midpoint(INT_MAX-1, INT_MAX) == INT_MAX-1 ); |
| 62 | static_assert( std::midpoint(INT_MAX, INT_MAX-2) == INT_MAX-1 ); |
| 63 | |
| 64 | static_assert( std::midpoint(0u, 0u) == 0 ); |
| 65 | static_assert( std::midpoint(0u, 1u) == 0 ); |
| 66 | static_assert( std::midpoint(1u, 0u) == 1 ); |
| 67 | static_assert( std::midpoint(0u, 2u) == 1 ); |
| 68 | static_assert( std::midpoint(3u, 2u) == 3 ); |
| 69 | static_assert( std::midpoint(0u, UINT_MAX) == UINT_MAX/2 ); |
| 70 | static_assert( std::midpoint(UINT_MAX, 0u) == (UINT_MAX/2 + 1) ); |
| 71 | static_assert( std::midpoint(UINT_MAX, UINT_MAX) == UINT_MAX ); |
| 72 | static_assert( std::midpoint(UINT_MAX, UINT_MAX-1) == UINT_MAX ); |
| 73 | static_assert( std::midpoint(UINT_MAX-1, UINT_MAX-1) == UINT_MAX-1 ); |
| 74 | static_assert( std::midpoint(UINT_MAX-1, UINT_MAX) == UINT_MAX-1 ); |
| 75 | static_assert( std::midpoint(UINT_MAX, UINT_MAX-2) == UINT_MAX-1 ); |
| 76 | |
| 77 | static_assert( std::midpoint<short>(0, 0) == 0 ); |
| 78 | static_assert( std::midpoint<short>(0, 1) == 0 ); |
| 79 | static_assert( std::midpoint<short>(1, 0) == 1 ); |
| 80 | static_assert( std::midpoint<short>(0, 2) == 1 ); |
| 81 | static_assert( std::midpoint<short>(3, 2) == 3 ); |
| 82 | static_assert( std::midpoint<short>(-5, 4) == -1 ); |
| 83 | static_assert( std::midpoint<short>(5, -4) == 1 ); |
| 84 | static_assert( std::midpoint<short>(-5, -4) == -5 ); |
| 85 | static_assert( std::midpoint<short>(-4, -5) == -4 ); |
| 86 | static_assert( std::midpoint<short>(SHRT_MIN, SHRT_MAX) == -1 ); |
| 87 | static_assert( std::midpoint<short>(SHRT_MAX, SHRT_MIN) == 0 ); |
| 88 | static_assert( std::midpoint<short>(SHRT_MAX, SHRT_MAX) == SHRT_MAX ); |
| 89 | static_assert( std::midpoint<short>(SHRT_MAX, SHRT_MAX-1) == SHRT_MAX ); |
| 90 | static_assert( std::midpoint<short>(SHRT_MAX-1, SHRT_MAX-1) == SHRT_MAX-1 ); |
| 91 | static_assert( std::midpoint<short>(SHRT_MAX-1, SHRT_MAX) == SHRT_MAX-1 ); |
| 92 | static_assert( std::midpoint<short>(SHRT_MAX, SHRT_MAX-2) == SHRT_MAX-1 ); |
| 93 | |
| 94 | static_assert( std::midpoint<signed char>(0, 0) == 0 ); |
| 95 | static_assert( std::midpoint<signed char>(1, 1) == 1 ); |
| 96 | static_assert( std::midpoint<signed char>(0, 1) == 0 ); |
| 97 | static_assert( std::midpoint<signed char>(1, 0) == 1 ); |
| 98 | static_assert( std::midpoint<signed char>(0, 2) == 1 ); |
| 99 | static_assert( std::midpoint<signed char>(3, 2) == 3 ); |
| 100 | static_assert( std::midpoint<signed char>(-5, 4) == -1 ); |
| 101 | static_assert( std::midpoint<signed char>(5, -4) == 1 ); |
| 102 | static_assert( std::midpoint<signed char>(-5, -4) == -5 ); |
| 103 | static_assert( std::midpoint<signed char>(-4, -5) == -4 ); |
| 104 | static_assert( std::midpoint<signed char>(SCHAR_MIN, SCHAR_MAX) == -1 ); |
| 105 | static_assert( std::midpoint<signed char>(SCHAR_MAX, SCHAR_MIN) == 0 ); |
| 106 | static_assert( std::midpoint<signed char>(SCHAR_MAX, SCHAR_MAX) == SCHAR_MAX ); |
| 107 | static_assert( std::midpoint<signed char>(SCHAR_MAX, SCHAR_MAX-1) == SCHAR_MAX); |
| 108 | |
| 109 | void |
| 110 | test01() |
| 111 | { |
| 112 | // Test every possibility for signed char. |
| 113 | for (int a = SCHAR_MIN; a <= SCHAR_MAX; ++a) |
| 114 | for (int b = SCHAR_MIN; b <= SCHAR_MAX; ++b) |
| 115 | VERIFY( std::midpoint(a, b) == std::midpoint<int>(a, b) ); |
| 116 | } |
| 117 | |
| 118 | int main() |
| 119 | { |
| 120 | test01(); |
| 121 | } |