c.math.lerp.pass.cpp 2.3 KB

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