From 8dfd173285df515fe9b41d970321da9d049460f8 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Wed, 9 Oct 2024 23:08:48 +0200 Subject: More work on format --- asl/format_tests.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'asl/format_tests.cpp') diff --git a/asl/format_tests.cpp b/asl/format_tests.cpp index e69de29..2288a77 100644 --- a/asl/format_tests.cpp +++ b/asl/format_tests.cpp @@ -0,0 +1,82 @@ +#include "asl/format.hpp" + +#include +#include +#include +#include + +// @Todo Improve this to use our utilities, not the C stdlib + +static_assert(asl::formattable); + +class StringSink : public asl::writer +{ + int64_t m_current_len{}; + char* m_data{}; + +public: + void write(const char* str, int64_t len) override + { + m_data = (char*)realloc(m_data, (size_t)(m_current_len + len + 1)); + memcpy(m_data + m_current_len, str, (size_t)len); + m_current_len += len; + m_data[m_current_len] = '\0'; + } + + constexpr const char* cstr() const { return m_data; } + + void reset() + { + m_current_len = 0; + free(m_data); + m_data = nullptr; + } +}; + +int main() +{ + StringSink sink; + + // @Todo Use the testing framework + + asl::format(&sink, "Hello, world!"); + assert(strcmp(sink.cstr(), "Hello, world!") == 0); + + sink.reset(); + asl::format(&sink, ""); + assert(strcmp(sink.cstr(), "") == 0); + + sink.reset(); + asl::format(&sink, "Hello, {}!", "world"); + assert(strcmp(sink.cstr(), "Hello, world!") == 0); + + sink.reset(); + asl::format(&sink, "Hello, {}! {}", "world"); + assert(strcmp(sink.cstr(), "Hello, world! ") == 0); + + sink.reset(); + asl::format(&sink, "Hello, pup!", "world"); + assert(strcmp(sink.cstr(), "Hello, pup!") == 0); + + sink.reset(); + asl::format(&sink, "{}", "CHEESE"); + assert(strcmp(sink.cstr(), "CHEESE") == 0); + + sink.reset(); + asl::format(&sink, "{ ", "CHEESE"); + assert(strcmp(sink.cstr(), " ") == 0); + + sink.reset(); + asl::format(&sink, "{", "CHEESE"); + assert(strcmp(sink.cstr(), "") == 0); + + sink.reset(); + asl::format(&sink, "a{{b"); + assert(strcmp(sink.cstr(), "a{b") == 0); + + sink.reset(); + asl::format(&sink, "{{{}}} }", "CHEESE"); + assert(strcmp(sink.cstr(), "{CHEESE} }") == 0); + + return 0; +} -- cgit