summaryrefslogtreecommitdiff
path: root/asl/tests/format_tests.cpp
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2024-10-16 22:54:34 +0200
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2024-12-20 15:35:58 +0100
commit8c95db33be58a545dd2e030428bded0bd958c4b6 (patch)
treeb78809fae2cec5eb2b9513b4960bfbbd697f3871 /asl/tests/format_tests.cpp
parent7193114b152d3a5b714a22f54ed89950c0ba9aba (diff)
Start work on the testing framework
Diffstat (limited to 'asl/tests/format_tests.cpp')
-rw-r--r--asl/tests/format_tests.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/asl/tests/format_tests.cpp b/asl/tests/format_tests.cpp
new file mode 100644
index 0000000..6e2430d
--- /dev/null
+++ b/asl/tests/format_tests.cpp
@@ -0,0 +1,106 @@
+#include "asl/format.hpp"
+
+#include <cstdlib>
+#include <cstring>
+#include <cassert>
+#include <cstdio>
+
+// @Todo Improve this to use our utilities, not the C stdlib
+
+static_assert(asl::formattable<decltype("Hello")>);
+
+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 main2()
+{
+ 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! <ERROR>") == 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(), "<ERROR> ") == 0);
+
+ sink.reset();
+ asl::format(&sink, "{", "CHEESE");
+ assert(strcmp(sink.cstr(), "<ERROR>") == 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);
+
+ sink.reset();
+ asl::format(&sink, "{} {} {}", 0, 1, 2);
+ assert(strcmp(sink.cstr(), "0 1 2") == 0);
+
+ sink.reset();
+ asl::format(&sink, "{} {} {}", 10, 11, 12);
+ assert(strcmp(sink.cstr(), "10 11 12") == 0);
+
+ sink.reset();
+ asl::format(&sink, "{} {} {}", 100, 101, 102);
+ assert(strcmp(sink.cstr(), "100 101 102") == 0);
+
+ sink.reset();
+ asl::format(&sink, "{} {} {}", 1000, 1001, 1002);
+ assert(strcmp(sink.cstr(), "1000 1001 1002") == 0);
+
+ sink.reset();
+ asl::format(&sink, "{} {} {} {}", -1, -23, -456, -7890);
+ assert(strcmp(sink.cstr(), "-1 -23 -456 -7890") == 0);
+
+ sink.reset();
+ asl::format(&sink, "{} {}", true, false);
+ assert(strcmp(sink.cstr(), "true false") == 0);
+
+ return 0;
+}