|
| 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 | +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 |
| 9 | +// <cmath> |
| 10 | + |
| 11 | +// constexpr float lerp(float a, float b, float t); |
| 12 | +// constexpr double lerp(double a, double b, double t); |
| 13 | +// constexpr long double lerp(long double a, long double b, long double t); |
| 14 | + |
| 15 | + |
| 16 | +#include <cmath> |
| 17 | +#include <limits> |
| 18 | +#include <type_traits> |
| 19 | +#include <cassert> |
| 20 | + |
| 21 | +#include "test_macros.h" |
| 22 | +#include "fp_compare.h" |
| 23 | + |
| 24 | +template <typename T> |
| 25 | +constexpr bool constexpr_test() |
| 26 | +{ |
| 27 | + return std::lerp(T( 0), T(12), T(0)) == T(0) |
| 28 | + && std::lerp(T(12), T( 0), T(0.5)) == T(6) |
| 29 | + && std::lerp(T( 0), T(12), T(2)) == T(24); |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +template <typename T> |
| 34 | +void test() |
| 35 | +{ |
| 36 | + ASSERT_SAME_TYPE(T, decltype(std::lerp(T(), T(), T()))); |
| 37 | + LIBCPP_ASSERT_NOEXCEPT( std::lerp(T(), T(), T())); |
| 38 | + |
| 39 | +// constexpr T minV = std::numeric_limits<T>::min(); |
| 40 | + constexpr T maxV = std::numeric_limits<T>::max(); |
| 41 | + constexpr T inf = std::numeric_limits<T>::infinity(); |
| 42 | + |
| 43 | +// Things that can be compared exactly |
| 44 | + assert((std::lerp(T( 0), T(12), T(0)) == T(0))); |
| 45 | + assert((std::lerp(T( 0), T(12), T(1)) == T(12))); |
| 46 | + assert((std::lerp(T(12), T( 0), T(0)) == T(12))); |
| 47 | + assert((std::lerp(T(12), T( 0), T(1)) == T(0))); |
| 48 | + |
| 49 | + assert((std::lerp(T( 0), T(12), T(0.5)) == T(6))); |
| 50 | + assert((std::lerp(T(12), T( 0), T(0.5)) == T(6))); |
| 51 | + assert((std::lerp(T( 0), T(12), T(2)) == T(24))); |
| 52 | + assert((std::lerp(T(12), T( 0), T(2)) == T(-12))); |
| 53 | + |
| 54 | + assert((std::lerp(maxV, maxV/10, T(0)) == maxV)); |
| 55 | + assert((std::lerp(maxV/10, maxV, T(1)) == maxV)); |
| 56 | + |
| 57 | + assert((std::lerp(T(2.3), T(2.3), inf) == T(2.3))); |
| 58 | + |
| 59 | + assert(std::lerp(T( 0), T( 0), T(23)) == T(0)); |
| 60 | + assert(std::isnan(std::lerp(T( 0), T( 0), inf))); |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +int main(int, char**) |
| 65 | +{ |
| 66 | + static_assert(constexpr_test<float>(), ""); |
| 67 | + static_assert(constexpr_test<double>(), ""); |
| 68 | + static_assert(constexpr_test<long double>(), ""); |
| 69 | + |
| 70 | + test<float>(); |
| 71 | + test<double>(); |
| 72 | + test<long double>(); |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
0 commit comments