summaryrefslogtreecommitdiffstats
path: root/src/fmt/test/compile-fp-test.cc
blob: db0cd906c5b13b8ea04676b384b08bcd82d04f1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Formatting library for C++ - formatting library tests
//
// Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.

#include "fmt/compile.h"
#include "gmock/gmock.h"

#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806 && \
    defined(__cpp_constexpr) && __cpp_constexpr >= 201907 &&       \
    defined(__cpp_constexpr_dynamic_alloc) &&                      \
    __cpp_constexpr_dynamic_alloc >= 201907 && FMT_CPLUSPLUS >= 202002L
template <size_t max_string_length, typename Char = char> struct test_string {
  template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
    return fmt::basic_string_view<Char>(rhs).compare(buffer) == 0;
  }
  Char buffer[max_string_length]{};
};

template <size_t max_string_length, typename Char = char, typename... Args>
consteval auto test_format(auto format, const Args&... args) {
  test_string<max_string_length, Char> string{};
  fmt::format_to(string.buffer, format, args...);
  return string;
}

TEST(compile_time_formatting_test, floating_point) {
  EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{}"), 0.0f));
  EXPECT_EQ("392.500000", test_format<11>(FMT_COMPILE("{0:f}"), 392.5f));

  EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{:}"), 0.0));
  EXPECT_EQ("0.000000", test_format<9>(FMT_COMPILE("{:f}"), 0.0));
  EXPECT_EQ("0", test_format<2>(FMT_COMPILE("{:g}"), 0.0));
  EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:}"), 392.65));
  EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:g}"), 392.65));
  EXPECT_EQ("392.65", test_format<7>(FMT_COMPILE("{:G}"), 392.65));
  EXPECT_EQ("4.9014e+06", test_format<11>(FMT_COMPILE("{:g}"), 4.9014e6));
  EXPECT_EQ("-392.650000", test_format<12>(FMT_COMPILE("{:f}"), -392.65));
  EXPECT_EQ("-392.650000", test_format<12>(FMT_COMPILE("{:F}"), -392.65));

  EXPECT_EQ("3.926500e+02", test_format<13>(FMT_COMPILE("{0:e}"), 392.65));
  EXPECT_EQ("3.926500E+02", test_format<13>(FMT_COMPILE("{0:E}"), 392.65));
  EXPECT_EQ("+0000392.6", test_format<11>(FMT_COMPILE("{0:+010.4g}"), 392.65));
  EXPECT_EQ("9223372036854775808.000000",
            test_format<27>(FMT_COMPILE("{:f}"), 9223372036854775807.0));

  constexpr double nan = std::numeric_limits<double>::quiet_NaN();
  EXPECT_EQ("nan", test_format<4>(FMT_COMPILE("{}"), nan));
  EXPECT_EQ("+nan", test_format<5>(FMT_COMPILE("{:+}"), nan));
  if (std::signbit(-nan))
    EXPECT_EQ("-nan", test_format<5>(FMT_COMPILE("{}"), -nan));
  else
    fmt::print("Warning: compiler doesn't handle negative NaN correctly");

  constexpr double inf = std::numeric_limits<double>::infinity();
  EXPECT_EQ("inf", test_format<4>(FMT_COMPILE("{}"), inf));
  EXPECT_EQ("+inf", test_format<5>(FMT_COMPILE("{:+}"), inf));
  EXPECT_EQ("-inf", test_format<5>(FMT_COMPILE("{}"), -inf));
}
#endif