From a01128d429b8b4c684cc35ffb6fcd15498d44978 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 23 Dec 2023 06:53:25 -0800 Subject: [PATCH] Simplify test --- test/scan-test.cc | 49 +++++++++++++++++++++++++---------------------- test/scan.h | 1 + 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/test/scan-test.cc b/test/scan-test.cc index 993a4b3f8cc77..982b80e1b7f3c 100644 --- a/test/scan-test.cc +++ b/test/scan-test.cc @@ -68,40 +68,43 @@ TEST(scan_test, read_string_view) { EXPECT_EQ(s, "foo"); } +TEST(scan_test, separator) { + int n1 = 0, n2 = 0; + fmt::scan("10 20", "{} {}", n1, n2); + EXPECT_EQ(n1, 10); + EXPECT_EQ(n2, 20); +} + #ifdef FMT_HAVE_STRPTIME +struct num { + int value; +}; + namespace fmt { -template <> struct scanner { - std::string format; +template <> struct scanner { + bool hex = false; auto parse(scan_parse_context& ctx) -> scan_parse_context::iterator { - auto it = ctx.begin(); - if (it != ctx.end() && *it == ':') ++it; - auto end = it; - while (end != ctx.end() && *end != '}') ++end; - format.reserve(detail::to_unsigned(end - it + 1)); - format.append(it, end); - format.push_back('\0'); - return end; + auto it = ctx.begin(), end = ctx.end(); + if (it != end && *it == 'x') hex = true; + if (it != end && *it != '}') throw_format_error("invalid format"); + return it; } template - auto scan(tm&, ScanContext& ctx) const -> typename ScanContext::iterator { - // TODO: replace strptime with get_time - // auto result = strptime(ctx.begin(), format.c_str(), &t); - // if (!result) throw format_error("failed to parse time"); - // return result; + auto scan(num&, ScanContext& ctx) const -> typename ScanContext::iterator { + // TODO + //return fmt::scan({ctx.begin(), ctx.end()}, "{}", n.value); return ctx.begin(); } }; } // namespace fmt TEST(scan_test, read_custom) { - /*auto input = "Date: 1985-10-25"; - auto t = tm(); - fmt::scan(input, "Date: {0:%Y-%m-%d}", t); - EXPECT_EQ(t.tm_year, 85); - EXPECT_EQ(t.tm_mon, 9); - EXPECT_EQ(t.tm_mday, 25);*/ + auto input = "42"; + auto n = num(); + fmt::scan(input, "{}", n); + //EXPECT_EQ(n, 42); } #endif @@ -113,8 +116,8 @@ TEST(scan_test, invalid_format) { } TEST(scan_test, example) { - auto key = std::string(); - auto value = int(); + std::string key; + int value = 0; fmt::scan("answer = 42", "{} = {}", key, value); EXPECT_EQ(key, "answer"); EXPECT_EQ(value, 42); diff --git a/test/scan.h b/test/scan.h index c48357fcb78b9..2ea7bfc25bdba 100644 --- a/test/scan.h +++ b/test/scan.h @@ -234,6 +234,7 @@ class file_scan_buffer : public scan_buffer { void consume() override { // Consume the current buffer content. + // TODO: do it more efficiently for (size_t i = 0, n = file_.buffer().size(); i != n; ++i) file_.get(); fill(); }