Commit 494dd33a authored by Michael Tesch's avatar Michael Tesch
Browse files

Fix fmt formatter unconditional parentheses

parent 2e3c4e10
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
#
cmake_minimum_required (VERSION 3.14)
project (cppduals
  VERSION 0.7.1
  VERSION 0.7.2
  LANGUAGES C CXX
  )
include (GNUInstallDirs)
+11 −4
Original line number Diff line number Diff line
@@ -1898,6 +1898,8 @@ struct fmt::formatter<duals::dual<T>,Char> : public fmt::formatter<T,Char>
    }

    auto out = ctx.out();
    bool need_parens = (style_ == style::pair) || (x.rpart() && x.dpart());
    if (need_parens)
      *out++ = '(';
    if (style_ == style::pair) {
      out = detail::write<Char>(out, x.rpart(), specs, ctx.locale());
@@ -1921,6 +1923,7 @@ struct fmt::formatter<duals::dual<T>,Char> : public fmt::formatter<T,Char>
        if (std::is_same<typename std::decay<T>::type,long double>::value) *out++ = 'l';
      }
    }
    if (need_parens)
      *out++ = ')';
    return out;
  }
@@ -1979,6 +1982,8 @@ struct fmt::formatter<std::complex<T>,Char> : public fmt::formatter<T,Char>

  template <typename FormatCtx>
  auto format(const std::complex<T> & x, FormatCtx & ctx) const -> decltype(ctx.out()) {
    bool need_parens = (style_ == style::pair) || (x.real() && x.imag());
    if (need_parens)
      format_to(ctx.out(), "(");
    if (style_ == style::pair) {
      base::format(x.real(), ctx);
@@ -2020,7 +2025,9 @@ struct fmt::formatter<std::complex<T>,Char> : public fmt::formatter<T,Char>
        if (std::is_same<typename std::decay<T>::type,long double>::value) format_to(ctx.out(), "l");
      }
    }
    if (need_parens)
      return format_to(ctx.out(), ")");
    return ctx.out();
  }
};
#endif // CPPDUALS_LIBFMT_COMPLEX
+4 −2
Original line number Diff line number Diff line
@@ -170,8 +170,10 @@ TEST(init, Constant) {
  EXPECT_EQ(a(1,1), 5 + 8_ef);
  EXPECT_EQ(b(0,1), cdualf(4,2 + 3_ef));
  EXPECT_TRUE((a.array() < 6).all());
  EXPECT_FALSE((a.array() != 5 + 8_ef).any());
  EXPECT_EQ((a.array() == 5 + 8_ef).count(), 9);
  // Avoid Eigen array operator!=/== which breaks with C++20 rewritten comparison candidates
  for (int r = 0; r < a.rows(); ++r)
    for (int c = 0; c < a.cols(); ++c)
      EXPECT_EQ(a(r,c), dualf(5 + 8_ef));
}

TEST(init, setIdentity) {
+15 −15
Original line number Diff line number Diff line
@@ -116,13 +116,13 @@ TEST(libfmt, complex_flags_b) {
TEST(libfmt, complex_all_real)  {
  std::string s;
  s = fmt::format("{}", 2. + 0i);
  EXPECT_EQ(s, "(2)");
  EXPECT_EQ(s, "2");

  s = fmt::format("{:.1f}", 2. + 0i);
  EXPECT_EQ(s, "(2.0)");
  EXPECT_EQ(s, "2.0");

  s = fmt::format("{:*}", 2. + 0i);
  EXPECT_EQ(s, "(2)");
  EXPECT_EQ(s, "2");

  s = fmt::format("{:,}", 2. + 0i);
  EXPECT_EQ(s, "(2,0)");
@@ -135,13 +135,13 @@ TEST(libfmt, complex_all_imag) {
  std::string s;

  s = fmt::format("{}", 0. + 3i);
  EXPECT_EQ(s, "(3i)");
  EXPECT_EQ(s, "3i");

  s = fmt::format("{:.1f}", 0. + 3i);
  EXPECT_EQ(s, "(3.0i)");
  EXPECT_EQ(s, "3.0i");

  s = fmt::format("{:*}", 0. + 3i);
  EXPECT_EQ(s, "(3*i)");
  EXPECT_EQ(s, "3*i");

  s = fmt::format("{:,}", 0. + 3i);
  EXPECT_EQ(s, "(0,3)");
@@ -162,7 +162,7 @@ TEST(libfmt, complex_g) {
}
TEST(libfmt, complex_gs) {
  std::string s = fmt::format("{:*g}", 3i);
  EXPECT_EQ(s, "(3*i)");
  EXPECT_EQ(s, "3*i");
}
TEST(libfmt, complex_gel) {
  std::string s = fmt::format("{:g}", 2.l + 3il);
@@ -236,9 +236,9 @@ TEST(libfmt, dual_all_real) {
  std::string s;

  s = fmt::format("{}", 2 + 0_e);
  EXPECT_EQ(s, "(2)");
  EXPECT_EQ(s, "2");
  s = fmt::format("{:*}", 2 + 0_e);
  EXPECT_EQ(s, "(2)");
  EXPECT_EQ(s, "2");
  s = fmt::format("{:,}", 2 + 0_e);
  EXPECT_EQ(s, "(2,0)");
}
@@ -246,11 +246,11 @@ TEST(libfmt, dual_all_dual) {
  std::string s;

  s = fmt::format("a{}b", 0 + 3_e);
  EXPECT_EQ(s, "a(3_e)b");
  EXPECT_EQ(s, "a3_eb");
  s = fmt::format("a{:.1f}b", 0 + 3_e);
  EXPECT_EQ(s, "a(3.0_e)b");
  EXPECT_EQ(s, "a3.0_eb");
  s = fmt::format("a{:*}b", 0 + 3_e);
  EXPECT_EQ(s, "a(3*e)b");
  EXPECT_EQ(s, "a3*eb");
  s = fmt::format("a{:,}b", 0 + 3_e);
  EXPECT_EQ(s, "a(0,3)b");
  s = fmt::format("a{:,.1f}b", 0 + 3_e);
@@ -273,10 +273,10 @@ TEST(libfmt, dual_g) {
}
TEST(libfmt, dual_gs) {
  std::string s = fmt::format("a{:*g}b", 3_e);
  EXPECT_EQ(s, "a(3*e)b");
  EXPECT_EQ(s, "a3*eb");

  s = fmt::format("{:*+g}", 3_e);
  EXPECT_EQ(s, "(+3*e)");
  EXPECT_EQ(s, "+3*e");
}
TEST(libfmt, dual_gel) {
  std::string s = fmt::format("{:g}", 2 + 3_el);