From deecf07bd1feadf76aadba127c7b64f931800c20 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Thu, 23 Jan 2025 23:59:32 +0100 Subject: Add StringWriter, and use it instead of StringSink --- asl/tests/format_tests.cpp | 130 ++++++++++++++----------------------- asl/tests/status_tests.cpp | 20 ++---- asl/tests/string_builder_tests.cpp | 4 +- asl/tests/test_types.hpp | 37 ----------- 4 files changed, 58 insertions(+), 133 deletions(-) (limited to 'asl/tests') diff --git a/asl/tests/format_tests.cpp b/asl/tests/format_tests.cpp index a08d052..b263abc 100644 --- a/asl/tests/format_tests.cpp +++ b/asl/tests/format_tests.cpp @@ -1,121 +1,91 @@ #include "asl/format.hpp" #include "asl/testing/testing.hpp" #include "asl/float.hpp" -#include "asl/tests/test_types.hpp" +#include "asl/string_builder.hpp" static_assert(asl::formattable); ASL_TEST(format_args) { - StringSink sink; - // @Todo Introduce ASL_TEST_EXPECT_EQ, or ASL_TEST_EXPECT_STREQ - asl::format(&sink, "Hello, world!"); - ASL_TEST_EXPECT(sink.str() == "Hello, world!"_sv); + auto s = asl::format_to_string("Hello, world!"); + ASL_TEST_EXPECT(s == "Hello, world!"_sv); - sink.reset(); - asl::format(&sink, ""); - ASL_TEST_EXPECT(sink.str() == ""_sv); + s = asl::format_to_string(""); + ASL_TEST_EXPECT(s == ""_sv); - sink.reset(); - asl::format(&sink, "Hello, {}!", "world"); - ASL_TEST_EXPECT(sink.str() == "Hello, world!"_sv); + s = asl::format_to_string("Hello, {}!", "world"); + ASL_TEST_EXPECT(s == "Hello, world!"_sv); - sink.reset(); - asl::format(&sink, "Hello, {}! {}", "world"); - ASL_TEST_EXPECT(sink.str() == "Hello, world! "_sv); + s = asl::format_to_string("Hello, {}! {}", "world"); + ASL_TEST_EXPECT(s == "Hello, world! "_sv); - sink.reset(); - asl::format(&sink, "Hello, pup!", "world"); - ASL_TEST_EXPECT(sink.str() == "Hello, pup!"_sv); + s = asl::format_to_string("Hello, pup!", "world"); + ASL_TEST_EXPECT(s == "Hello, pup!"_sv); - sink.reset(); - asl::format(&sink, "{}", "CHEESE"); - ASL_TEST_EXPECT(sink.str() == "CHEESE"_sv); + s = asl::format_to_string("{}", "CHEESE"); + ASL_TEST_EXPECT(s == "CHEESE"_sv); - sink.reset(); - asl::format(&sink, "{ ", "CHEESE"); - ASL_TEST_EXPECT(sink.str() == " "_sv); + s = asl::format_to_string("{ ", "CHEESE"); + ASL_TEST_EXPECT(s == " "_sv); - sink.reset(); - asl::format(&sink, "{", "CHEESE"); - ASL_TEST_EXPECT(sink.str() == ""_sv); + s = asl::format_to_string("{", "CHEESE"); + ASL_TEST_EXPECT(s == ""_sv); - sink.reset(); - asl::format(&sink, "a{{b"); - ASL_TEST_EXPECT(sink.str() == "a{b"_sv); + s = asl::format_to_string("a{{b"); + ASL_TEST_EXPECT(s == "a{b"_sv); - sink.reset(); - asl::format(&sink, "{{{}}} }", "CHEESE"); - ASL_TEST_EXPECT(sink.str() == "{CHEESE} }"_sv); + s = asl::format_to_string("{{{}}} }", "CHEESE"); + ASL_TEST_EXPECT(s == "{CHEESE} }"_sv); } ASL_TEST(format_integers) { - StringSink sink; - - sink.reset(); - asl::format(&sink, "{} {} {}", 0, 1, 2); - ASL_TEST_EXPECT(sink.str() == "0 1 2"_sv); + auto s = asl::format_to_string("{} {} {}", 0, 1, 2); + ASL_TEST_EXPECT(s == "0 1 2"_sv); - sink.reset(); - asl::format(&sink, "{} {} {}", 10, 11, 12); - ASL_TEST_EXPECT(sink.str() == "10 11 12"_sv); + s = asl::format_to_string("{} {} {}", 10, 11, 12); + ASL_TEST_EXPECT(s == "10 11 12"_sv); - sink.reset(); - asl::format(&sink, "{} {} {}", 100, 101, 102); - ASL_TEST_EXPECT(sink.str() == "100 101 102"_sv); + s = asl::format_to_string("{} {} {}", 100, 101, 102); + ASL_TEST_EXPECT(s == "100 101 102"_sv); - sink.reset(); - asl::format(&sink, "{} {} {}", 1000, 1001, 1002); - ASL_TEST_EXPECT(sink.str() == "1000 1001 1002"_sv); + s = asl::format_to_string("{} {} {}", 1000, 1001, 1002); + ASL_TEST_EXPECT(s == "1000 1001 1002"_sv); - sink.reset(); - asl::format(&sink, "{} {} {} {}", -1, -23, -456, -7890); - ASL_TEST_EXPECT(sink.str() == "-1 -23 -456 -7890"_sv); + s = asl::format_to_string("{} {} {} {}", -1, -23, -456, -7890); + ASL_TEST_EXPECT(s == "-1 -23 -456 -7890"_sv); } ASL_TEST(format_floats) { - StringSink sink; - - sink.reset(); - asl::format(&sink, "{} {} {}", 0.0F, 1.0, 2.0F); - ASL_TEST_EXPECT(sink.str() == "0 1 2"_sv); + auto s = asl::format_to_string("{} {} {}", 0.0F, 1.0, 2.0F); + ASL_TEST_EXPECT(s == "0 1 2"_sv); - sink.reset(); - asl::format(&sink, "{} {} {}", 0.1F, 0.001F, 0.123F); - ASL_TEST_EXPECT(sink.str() == "0.1 0.001 0.123"_sv); + s = asl::format_to_string("{} {} {}", 0.1F, 0.001F, 0.123F); + ASL_TEST_EXPECT(s == "0.1 0.001 0.123"_sv); - sink.reset(); - asl::format(&sink, "{} {}", 1.25F, -22.3); - ASL_TEST_EXPECT(sink.str() == "1.25 -22.3"_sv); + s = asl::format_to_string("{} {}", 1.25F, -22.3); + ASL_TEST_EXPECT(s == "1.25 -22.3"_sv); - sink.reset(); - asl::format(&sink, "{}", 1e32); - ASL_TEST_EXPECT(sink.str() == "100000000000000000000000000000000"_sv); + s = asl::format_to_string("{}", 1e32); + ASL_TEST_EXPECT(s == "100000000000000000000000000000000"_sv); - sink.reset(); - asl::format(&sink, "{}", 123e-8); - ASL_TEST_EXPECT(sink.str() == "0.00000123"_sv); + s = asl::format_to_string("{}", 123e-8); + ASL_TEST_EXPECT(s == "0.00000123"_sv); - sink.reset(); - asl::format(&sink, "{} {}", asl::infinity(), -asl::infinity()); - ASL_TEST_EXPECT(sink.str() == "Infinity -Infinity"_sv); + s = asl::format_to_string("{} {}", asl::infinity(), -asl::infinity()); + ASL_TEST_EXPECT(s == "Infinity -Infinity"_sv); - sink.reset(); - asl::format(&sink, "{}", asl::nan()); - ASL_TEST_EXPECT(sink.str() == "NaN"_sv); + s = asl::format_to_string("{}", asl::nan()); + ASL_TEST_EXPECT(s == "NaN"_sv); } ASL_TEST(format_boolean) { - StringSink sink; - - sink.reset(); - asl::format(&sink, "{} {}", true, false); - ASL_TEST_EXPECT(sink.str() == "true false"_sv); + auto s = asl::format_to_string("{} {}", true, false); + ASL_TEST_EXPECT(s == "true false"_sv); } struct CustomFormat @@ -135,8 +105,6 @@ static_assert(asl::formattable); ASL_TEST(format_custom) { - StringSink sink; - - asl::format(&sink, "{}", CustomFormat{37}); - ASL_TEST_EXPECT(sink.str() == "(37)"_sv); + auto s = asl::format_to_string("{}", CustomFormat{37}); + ASL_TEST_EXPECT(s == "(37)"_sv); } diff --git a/asl/tests/status_tests.cpp b/asl/tests/status_tests.cpp index b598389..d8aa923 100644 --- a/asl/tests/status_tests.cpp +++ b/asl/tests/status_tests.cpp @@ -1,7 +1,7 @@ #include "asl/status.hpp" #include "asl/testing/testing.hpp" #include "asl/format.hpp" -#include "asl/tests/test_types.hpp" +#include "asl/string_builder.hpp" ASL_TEST(simple_ok) { @@ -68,21 +68,15 @@ static_assert(asl::formattable); ASL_TEST(format) { - StringSink sink; + auto s = asl::format_to_string("-{}-", asl::ok()); + ASL_TEST_EXPECT(s == "-[ok]-"_sv); - asl::format(&sink, "-{}-", asl::ok()); - ASL_TEST_EXPECT(sink.str() == "-[ok]-"_sv); - - sink.reset(); - asl::format(&sink, "-{}-", asl::internal_error("hello")); - ASL_TEST_EXPECT(sink.str() == "-[internal: hello]-"_sv); + s = asl::format_to_string("-{}-", asl::internal_error("hello")); + ASL_TEST_EXPECT(s == "-[internal: hello]-"_sv); } ASL_TEST(make_with_format) { - StringSink sink; - - sink.reset(); - asl::format(&sink, "-{}-", asl::internal_error("hello, {}, {}", 45, "world")); - ASL_TEST_EXPECT(sink.str() == "-[internal: hello, 45, world]-"_sv); + auto s = asl::format_to_string("-{}-", asl::internal_error("hello, {}, {}", 45, "world")); + ASL_TEST_EXPECT(s == "-[internal: hello, 45, world]-"_sv); } diff --git a/asl/tests/string_builder_tests.cpp b/asl/tests/string_builder_tests.cpp index 9bb25bd..dddf2a9 100644 --- a/asl/tests/string_builder_tests.cpp +++ b/asl/tests/string_builder_tests.cpp @@ -3,7 +3,7 @@ ASL_TEST(string_builder) { - asl::string_builder b; + asl::StringBuilder b; b.push('a'); b.push("bcdef"); b.push('g'); @@ -16,7 +16,7 @@ ASL_TEST(string_builder) ASL_TEST(string_builder_rvalue) { - asl::string s = asl::string_builder{}.push('a').push("bcdef").push('g').finish(); + asl::string s = asl::StringBuilder{}.push('a').push("bcdef").push('g').finish(); ASL_TEST_EXPECT(s == "abcdefg"); } diff --git a/asl/tests/test_types.hpp b/asl/tests/test_types.hpp index 3c95d95..c4e109e 100644 --- a/asl/tests/test_types.hpp +++ b/asl/tests/test_types.hpp @@ -90,40 +90,3 @@ struct DestructorObserver } } }; - -class StringSink : public asl::Writer -{ - // @Todo Use string, once we have it, or a buffer - isize_t m_current_len{}; - char* m_data{}; - -public: - ~StringSink() override - { - reset(); - } - - void write(asl::span str) override - { - m_data = reinterpret_cast(asl::GlobalHeap::realloc( - m_data, - asl::layout::array(m_current_len), - asl::layout::array(m_current_len + str.size()))); - - asl::memcpy(m_data + m_current_len, str.data(), str.size()); // NOLINT - - m_current_len += str.size(); - } - - constexpr asl::string_view str() const { return {m_data, m_current_len}; } - - void reset() - { - if (m_data != nullptr) - { - m_current_len = 0; - asl::GlobalHeap::dealloc(m_data, asl::layout::array(m_current_len)); - m_data = nullptr; - } - } -}; -- cgit