| 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 <cmath> |
| 22 |
|
| 23 |
#ifndef __cpp_lib_interpolate |
| 24 |
# error "Feature-test macro for midpoint and lerp missing in <cmath>" |
| 25 |
#elif __cpp_lib_interpolate != 201902L |
| 26 |
# error "Feature-test macro for midpoint and lerp has wrong value in <cmath>" |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <limits> |
| 30 |
#include <testsuite_hooks.h> |
| 31 |
|
| 32 |
void |
| 33 |
test01() |
| 34 |
{ |
| 35 |
using lim = std::numeric_limits<double>; |
| 36 |
|
| 37 |
VERIFY( std::lerp(0.0, 1.0, 0.0) == 0.0 ); |
| 38 |
VERIFY( std::lerp(-2.0, 10.0, 1.0) == 10.0 ); |
| 39 |
VERIFY( std::lerp(2.0, -10.0, 1.0) == -10.0 ); |
| 40 |
VERIFY( std::lerp(-8.0, 10.0, 0.5) == 1.0 ); |
| 41 |
VERIFY( std::lerp(-10.0, 10.0, 0.25) == -5.0 ); |
| 42 |
VERIFY( std::lerp(10.0, -10.0, 0.375) == 2.5 ); |
| 43 |
|
| 44 |
VERIFY( std::lerp(2.0, 2.0, 200.0) == 2.0 ); |
| 45 |
VERIFY( std::lerp(2.0, 4.0, 200.0) == 402.0 ); |
| 46 |
VERIFY( std::lerp(2.0, 4.0, -20.0) == -38.0 ); |
| 47 |
|
| 48 |
VERIFY( std::lerp(1.1, 30201.1, 0) == 1.1 ); |
| 49 |
VERIFY( std::lerp(1.1, 30201.1, 1) == 30201.1 ); |
| 50 |
VERIFY( std::lerp(1.1, -30201.1, 0) == 1.1 ); |
| 51 |
VERIFY( std::lerp(1.1, -30201.1, 1) == -30201.1 ); |
| 52 |
|
| 53 |
VERIFY( std::lerp(1.1, 1.1, lim::infinity()) == 1.1 ); |
| 54 |
VERIFY( std::isfinite(std::lerp(1.1, 1.1+lim::min(), lim::max())) ); |
| 55 |
|
| 56 |
VERIFY( std::lerp(lim::max(), lim::max(), 1) == lim::max() ); |
| 57 |
VERIFY( std::lerp(lim::max(), lim::max()/9e9, 0) == lim::max() ); |
| 58 |
VERIFY( std::lerp(lim::max()/9e9, lim::max(), 1) == lim::max() ); |
| 59 |
} |
| 60 |
|
| 61 |
void |
| 62 |
test02() |
| 63 |
{ |
| 64 |
using lim = std::numeric_limits<float>; |
| 65 |
|
| 66 |
VERIFY( std::lerp(0.0f, 1.0f, 0.0f) == 0.0f ); |
| 67 |
VERIFY( std::lerp(-2.0f, 10.0f, 1.0f) == 10.0f ); |
| 68 |
VERIFY( std::lerp(2.0f, -10.0f, 1.0f) == -10.0f ); |
| 69 |
VERIFY( std::lerp(-8.0f, 10.0f, 0.5f) == 1.0f ); |
| 70 |
VERIFY( std::lerp(-10.0f, 10.0f, 0.25f) == -5.0f ); |
| 71 |
VERIFY( std::lerp(10.0f, -10.0f, 0.375f) == 2.5f ); |
| 72 |
|
| 73 |
VERIFY( std::lerp(2.0f, 2.0f, 200.0f) == 2.0f ); |
| 74 |
VERIFY( std::lerp(2.0f, 4.0f, 200.0f) == 402.0f ); |
| 75 |
VERIFY( std::lerp(2.0f, 4.0f, -20.0f) == -38.0f ); |
| 76 |
|
| 77 |
VERIFY( std::lerp(1.1f, 30201.1f, 0) == 1.1f ); |
| 78 |
VERIFY( std::lerp(1.1f, 30201.1f, 1) == 30201.1f ); |
| 79 |
VERIFY( std::lerp(1.1f, -30201.1f, 0) == 1.1f ); |
| 80 |
VERIFY( std::lerp(1.1f, -30201.1f, 1) == -30201.1f ); |
| 81 |
|
| 82 |
VERIFY( std::lerp(1.1f, 1.1f, lim::infinity()) == 1.1f ); |
| 83 |
VERIFY( std::isfinite(std::lerp(1.1f, 1.1f+lim::min(), lim::max())) ); |
| 84 |
|
| 85 |
VERIFY( std::lerp(lim::max(), lim::max(), 1) == lim::max() ); |
| 86 |
VERIFY( std::lerp(lim::max(), lim::max()/9e9f, 0) == lim::max() ); |
| 87 |
VERIFY( std::lerp(lim::max()/9e9f, lim::max(), 1) == lim::max() ); |
| 88 |
} |
| 89 |
|
| 90 |
void |
| 91 |
test03() |
| 92 |
{ |
| 93 |
using lim = std::numeric_limits<long double>; |
| 94 |
|
| 95 |
VERIFY( std::lerp(0.0l, 1.0l, 0.0l) == 0.0l ); |
| 96 |
VERIFY( std::lerp(-2.0l, 10.0l, 1.0l) == 10.0l ); |
| 97 |
VERIFY( std::lerp(2.0l, -10.0l, 1.0l) == -10.0l ); |
| 98 |
VERIFY( std::lerp(-8.0l, 10.0l, 0.5l) == 1.0l ); |
| 99 |
VERIFY( std::lerp(-10.0l, 10.0l, 0.25l) == -5.0l ); |
| 100 |
VERIFY( std::lerp(10.0l, -10.0l, 0.375l) == 2.5l ); |
| 101 |
|
| 102 |
VERIFY( std::lerp(2.0l, 2.0l, 200.0l) == 2.0l ); |
| 103 |
VERIFY( std::lerp(2.0l, 4.0l, 200.0l) == 402.0l ); |
| 104 |
VERIFY( std::lerp(2.0l, 4.0l, -20.0l) == -38.0l ); |
| 105 |
|
| 106 |
VERIFY( std::lerp(1.1l, 30201.1l, 0) == 1.1l ); |
| 107 |
VERIFY( std::lerp(1.1l, 30201.1l, 1) == 30201.1l ); |
| 108 |
VERIFY( std::lerp(1.1l, -30201.1l, 0) == 1.1l ); |
| 109 |
VERIFY( std::lerp(1.1l, -30201.1l, 1) == -30201.1l ); |
| 110 |
|
| 111 |
VERIFY( std::lerp(1.1l, 1.1l, lim::infinity()) == 1.1l ); |
| 112 |
VERIFY( std::isfinite(std::lerp(1.1l, 1.1l+lim::min(), lim::max())) ); |
| 113 |
|
| 114 |
VERIFY( std::lerp(lim::max(), lim::max(), 1) == lim::max() ); |
| 115 |
VERIFY( std::lerp(lim::max(), lim::max()/9e9l, 0) == lim::max() ); |
| 116 |
VERIFY( std::lerp(lim::max()/9e9l, lim::max(), 1) == lim::max() ); |
| 117 |
} |
| 118 |
|
| 119 |
int main() |
| 120 |
{ |
| 121 |
test01(); |
| 122 |
test02(); |
| 123 |
test03(); |
| 124 |
} |