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