Repository: fmtlib/fmt
File: test/format-test.cc (C++)

Code before:
  write_end.close();
  auto read_end = pipe.read_end.fdopen("r");
  int n = 0;
  int result = fscanf(read_end.get(), "%d", &n);
  (void)result;
  EXPECT_EQ(n, 42);
#endif

  auto s = fmt::string_buffer();
  fmt::writer(s).print("foo");
  EXPECT_EQ(s.str(), "foo");
}

#if FMT_USE_BITINT
#  pragma clang diagnostic push
#  pragma clang diagnostic ignored "-Wbit-int-extension"

template <size_t N, bool is_signed>
using bitint_helper =
    fmt::conditional_t<is_signed, _BitInt(N), unsigned _BitInt(N)>;
template <size_t N> using signed_bitint = bitint_helper<N, true>;
template <size_t N> using unsigned_bitint = bitint_helper<N, false>;

#  if FMT_USE_BITINT
TEST(format_test, bitint) {
  EXPECT_EQ(fmt::format("{}", unsigned_bitint<3>{7}), "7");
  EXPECT_EQ(fmt::format("{}", signed_bitint<7>{}), "0");

  EXPECT_EQ(fmt::format("{}", unsigned_bitint<15>{31000}), "31000");
  EXPECT_EQ(fmt::format("{}", signed_bitint<16>{INT16_MIN}), "-32768");
  EXPECT_EQ(fmt::format("{}", signed_bitint<16>{INT16_MAX}), "32767");

  EXPECT_EQ(fmt::format("{}", unsigned_bitint<32>{4294967295}), "4294967295");

  EXPECT_EQ(fmt::format("{}", unsigned_bitint<47>{140737488355327ULL}),
            "140737488355327");
  EXPECT_EQ(fmt::format("{}", signed_bitint<47>{-40737488355327LL}),
            "-40737488355327");

  // Check lvalues and const
  auto a = signed_bitint<8>{0};
  auto b = unsigned_bitint<32>{4294967295};
  const auto c = signed_bitint<7>{0};
  const auto d = unsigned_bitint<32>{4294967295};
  EXPECT_EQ(fmt::format("{}", a), "0");
  EXPECT_EQ(fmt::format("{}", b), "4294967295");
  EXPECT_EQ(fmt::format("{}", c), "0");
  EXPECT_EQ(fmt::format("{}", d), "4294967295");

  static_assert(fmt::is_formattable<signed_bitint<64>, char>{}, "");
  static_assert(fmt::is_formattable<unsigned_bitint<64>, char>{}, "");

Review comment (suggestion):
We only use braced initialization for aggregates in {fmt}: `unsigned_bitint<3>{7} -> unsigned_bitint<3>(7)`.

Code after:
  write_end.close();
  auto read_end = pipe.read_end.fdopen("r");
  int n = 0;
  int result = fscanf(read_end.get(), "%d", &n);
  (void)result;
  EXPECT_EQ(n, 42);
#endif

  auto s = fmt::string_buffer();
  fmt::writer(s).print("foo");
  EXPECT_EQ(s.str(), "foo");
}

#if FMT_USE_BITINT
#  pragma clang diagnostic ignored "-Wbit-int-extension"

template <size_t N, bool is_signed>
using bitint_helper =
    fmt::conditional_t<is_signed, _BitInt(N), unsigned _BitInt(N)>;
template <size_t N> using signed_bitint = bitint_helper<N, true>;
template <size_t N> using unsigned_bitint = bitint_helper<N, false>;

TEST(format_test, bitint) {
  EXPECT_EQ(fmt::format("{}", unsigned_bitint<3>(7)), "7");
  EXPECT_EQ(fmt::format("{}", signed_bitint<7>()), "0");

  EXPECT_EQ(fmt::format("{}", unsigned_bitint<15>(31000)), "31000");
  EXPECT_EQ(fmt::format("{}", signed_bitint<16>(INT16_MIN)), "-32768");
  EXPECT_EQ(fmt::format("{}", signed_bitint<16>(INT16_MAX)), "32767");

  EXPECT_EQ(fmt::format("{}", unsigned_bitint<32>(4294967295)), "4294967295");

  EXPECT_EQ(fmt::format("{}", unsigned_bitint<47>(140737488355327ULL)),
            "140737488355327");
  EXPECT_EQ(fmt::format("{}", signed_bitint<47>(-40737488355327LL)),
            "-40737488355327");

  // Check lvalues and const
  auto a = signed_bitint<8>(0);
  auto b = unsigned_bitint<32>(4294967295);
  const auto c = signed_bitint<7>(0);
  const auto d = unsigned_bitint<32>(4294967295);
  EXPECT_EQ(fmt::format("{}", a), "0");
  EXPECT_EQ(fmt::format("{}", b), "4294967295");
  EXPECT_EQ(fmt::format("{}", c), "0");
  EXPECT_EQ(fmt::format("{}", d), "4294967295");

  static_assert(fmt::is_formattable<signed_bitint<64>, char>{}, "");
  static_assert(fmt::is_formattable<unsigned_bitint<64>, char>{}, "");

#  if FMT_USE_INT128