compile-fp-test.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Formatting library for C++ - formatting library tests
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #include "fmt/compile.h"
  8. #include "gmock/gmock.h"
  9. #if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806 && \
  10. defined(__cpp_constexpr) && __cpp_constexpr >= 201907 && \
  11. defined(__cpp_constexpr_dynamic_alloc) && \
  12. __cpp_constexpr_dynamic_alloc >= 201907 && FMT_CPLUSPLUS >= 202002L
  13. template <size_t max_string_length, typename Char = char> struct test_string {
  14. template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
  15. return fmt::basic_string_view<Char>(rhs).compare(buffer) == 0;
  16. }
  17. Char buffer[max_string_length]{};
  18. };
  19. template <size_t max_string_length, typename Char = char, typename... Args>
  20. consteval auto test_format(auto format, const Args&... args) {
  21. test_string<max_string_length, Char> string{};
  22. fmt::format_to(string.buffer, format, args...);
  23. return string;
  24. }
  25. TEST(compile_time_formatting_test, floating_point) {
  26. EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{}"), 0.0f));
  27. EXPECT_EQ("392.500000", test_format<11>(FMT_COMPILE("{0:f}"), 392.5f));
  28. EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{:}"), 0.0));
  29. EXPECT_EQ("0.000000", test_format<9>(FMT_COMPILE("{:f}"), 0.0));
  30. EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{:g}"), 0.0));
  31. EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:}"), 392.65));
  32. EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:g}"), 392.65));
  33. EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:G}"), 392.65));
  34. EXPECT_EQ("4.9014e+06", test_format<11>(FMT_COMPILE("{:g}"), 4.9014e6));
  35. EXPECT_EQ("-392.650000", test_format<12>(FMT_COMPILE("{:f}"), -392.65));
  36. EXPECT_EQ("-392.650000", test_format<12>(FMT_COMPILE("{:F}"), -392.65));
  37. EXPECT_EQ("3.926500e+02", test_format<13>(FMT_COMPILE("{0:e}"), 392.65));
  38. EXPECT_EQ("3.926500E+02", test_format<13>(FMT_COMPILE("{0:E}"), 392.65));
  39. EXPECT_EQ("+0000392.6", test_format<11>(FMT_COMPILE("{0:+010.4g}"), 392.65));
  40. EXPECT_EQ("9223372036854775808.000000",
  41. test_format<27>(FMT_COMPILE("{:f}"), 9223372036854775807.0));
  42. constexpr double nan = std::numeric_limits<double>::quiet_NaN();
  43. EXPECT_EQ("nan", test_format<4>(FMT_COMPILE("{}"), nan));
  44. EXPECT_EQ("+nan", test_format<5>(FMT_COMPILE("{:+}"), nan));
  45. if (std::signbit(-nan))
  46. EXPECT_EQ("-nan", test_format<5>(FMT_COMPILE("{}"), -nan));
  47. else
  48. fmt::print("Warning: compiler doesn't handle negative NaN correctly");
  49. constexpr double inf = std::numeric_limits<double>::infinity();
  50. EXPECT_EQ("inf", test_format<4>(FMT_COMPILE("{}"), inf));
  51. EXPECT_EQ("+inf", test_format<5>(FMT_COMPILE("{:+}"), inf));
  52. EXPECT_EQ("-inf", test_format<5>(FMT_COMPILE("{}"), -inf));
  53. }
  54. #endif