• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "fmt/compile.h"
9 #include "gmock/gmock.h"
10 
11 #if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806 && \
12     defined(__cpp_constexpr) && __cpp_constexpr >= 201907 &&       \
13     defined(__cpp_constexpr_dynamic_alloc) &&                      \
14     __cpp_constexpr_dynamic_alloc >= 201907 && FMT_CPLUSPLUS >= 202002L
15 
16 template <size_t max_string_length, typename Char = char> struct test_string {
operator ==test_string17   template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
18     return fmt::basic_string_view<Char>(rhs).compare(buffer) == 0;
19   }
20   Char buffer[max_string_length]{};
21 };
22 
23 template <size_t max_string_length, typename Char = char, typename... Args>
test_format(auto format,const Args &...args)24 consteval auto test_format(auto format, const Args&... args) {
25   test_string<max_string_length, Char> string{};
26   fmt::format_to(string.buffer, format, args...);
27   return string;
28 }
29 
TEST(compile_time_formatting_test,floating_point)30 TEST(compile_time_formatting_test, floating_point) {
31   EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{}"), 0.0f));
32   EXPECT_EQ("392.500000", test_format<11>(FMT_COMPILE("{0:f}"), 392.5f));
33 
34   EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{:}"), 0.0));
35   EXPECT_EQ("0.000000", test_format<9>(FMT_COMPILE("{:f}"), 0.0));
36   EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{:g}"), 0.0));
37   EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:}"), 392.65));
38   EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:g}"), 392.65));
39   EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:G}"), 392.65));
40   EXPECT_EQ("4.9014e+06", test_format<11>(FMT_COMPILE("{:g}"), 4.9014e6));
41   EXPECT_EQ("-392.650000", test_format<12>(FMT_COMPILE("{:f}"), -392.65));
42   EXPECT_EQ("-392.650000", test_format<12>(FMT_COMPILE("{:F}"), -392.65));
43 
44   EXPECT_EQ("3.926500e+02", test_format<13>(FMT_COMPILE("{0:e}"), 392.65));
45   EXPECT_EQ("3.926500E+02", test_format<13>(FMT_COMPILE("{0:E}"), 392.65));
46   EXPECT_EQ("+0000392.6", test_format<11>(FMT_COMPILE("{0:+010.4g}"), 392.65));
47   EXPECT_EQ("9223372036854775808.000000",
48             test_format<27>(FMT_COMPILE("{:f}"), 9223372036854775807.0));
49 
50   constexpr double nan = std::numeric_limits<double>::quiet_NaN();
51   EXPECT_EQ("nan", test_format<4>(FMT_COMPILE("{}"), nan));
52   EXPECT_EQ("+nan", test_format<5>(FMT_COMPILE("{:+}"), nan));
53   if (std::signbit(-nan))
54     EXPECT_EQ("-nan", test_format<5>(FMT_COMPILE("{}"), -nan));
55   else
56     fmt::print("Warning: compiler doesn't handle negative NaN correctly");
57 
58   constexpr double inf = std::numeric_limits<double>::infinity();
59   EXPECT_EQ("inf", test_format<4>(FMT_COMPILE("{}"), inf));
60   EXPECT_EQ("+inf", test_format<5>(FMT_COMPILE("{:+}"), inf));
61   EXPECT_EQ("-inf", test_format<5>(FMT_COMPILE("{}"), -inf));
62 }
63 #endif
64