aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd/tests/src
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-04-13 16:26:45 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-04-13 16:26:45 +0200
commit6253705095086de8c6585acc9400942e6a267a5d (patch)
tree8dd4baafe46561d64fd72ae086ebc2ddaa719816 /libs/kstd/tests/src
parent4d2a1d028f8ba28b655026b93124e71a12562619 (diff)
parent3795115641bf5c1d1a3d60313408ba462057ba18 (diff)
downloadteachos-6253705095086de8c6585acc9400942e6a267a5d.tar.xz
teachos-6253705095086de8c6585acc9400942e6a267a5d.zip
Merge branch 'fmorgner/develop-BA-FS26/string-format' into 'develop-BA-FS26'
Implement `kstd::format` and `kstd::format_to` See merge request teachos/kernel!24
Diffstat (limited to 'libs/kstd/tests/src')
-rw-r--r--libs/kstd/tests/src/format.cpp116
1 files changed, 116 insertions, 0 deletions
diff --git a/libs/kstd/tests/src/format.cpp b/libs/kstd/tests/src/format.cpp
new file mode 100644
index 0000000..73c8102
--- /dev/null
+++ b/libs/kstd/tests/src/format.cpp
@@ -0,0 +1,116 @@
+#include <kstd/flat_map>
+#include <kstd/format>
+#include <kstd/tests/os_panic.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <iterator>
+#include <sstream>
+
+SCENARIO("Formatting to a new string", "[format]")
+{
+ GIVEN("a format string without any placeholders")
+ {
+ auto const & fmt = "This is a test";
+
+ WHEN("calling format with without any arguments.")
+ {
+ auto result = kstd::format(fmt);
+
+ THEN("the result is the unmodified string")
+ {
+ REQUIRE(result == "This is a test");
+ }
+ }
+
+ WHEN("calling format with additional arguments")
+ {
+ auto result = kstd::format(fmt, 1, 2, 3);
+
+ THEN("the result is the unmodified string")
+ {
+ REQUIRE(result == "This is a test");
+ }
+ }
+ }
+
+ GIVEN("a format string with placeholders")
+ {
+ auto const & fmt = "Here are some placeholders: {} {} {}";
+
+ WHEN("calling format with the same number of arguments as there are placeholders")
+ {
+ auto result = kstd::format(fmt, 1, true, 'a');
+
+ THEN("the result is the formatted string")
+ {
+ REQUIRE(result == "Here are some placeholders: 1 true a");
+ }
+ }
+
+ WHEN("calling format with too many arguments")
+ {
+ auto result = kstd::format(fmt, 2, false, 'b', 4, 5, 6);
+
+ THEN("the result is the formatted string")
+ {
+ REQUIRE(result == "Here are some placeholders: 2 false b");
+ }
+ }
+ }
+}
+
+SCENARIO("Formatting to an output iterator", "[format]")
+{
+ auto buffer = std::ostringstream{};
+
+ GIVEN("a format string without any placeholders")
+ {
+ auto const & fmt = "This is a test";
+
+ WHEN("calling format with without any arguments.")
+ {
+ kstd::format_to(std::ostream_iterator<char>{buffer}, fmt);
+
+ THEN("the unmodified string is written to the iterator")
+ {
+ REQUIRE(buffer.str() == "This is a test");
+ }
+ }
+
+ WHEN("calling format with additional arguments")
+ {
+ kstd::format_to(std::ostream_iterator<char>{buffer}, fmt, 1, 2, 3);
+
+ THEN("the unmodified string is written to the iterator")
+ {
+ REQUIRE(buffer.str() == "This is a test");
+ }
+ }
+ }
+
+ GIVEN("a format string with placeholders")
+ {
+ auto const & fmt = "Here are some placeholders: {} {} {}";
+
+ WHEN("calling format with the same number of arguments as there are placeholders")
+ {
+ kstd::format_to(std::ostream_iterator<char>{buffer}, fmt, 1, true, -100);
+
+ THEN("the formatted string is written to the iterator")
+ {
+ REQUIRE(buffer.str() == "Here are some placeholders: 1 true -100");
+ }
+ }
+
+ WHEN("calling format with too many arguments")
+ {
+ kstd::format_to(std::ostream_iterator<char>{buffer}, fmt, 2, false, -200, 4, 5, 6);
+
+ THEN("the formatted string is written to the iterator")
+ {
+ REQUIRE(buffer.str() == "Here are some placeholders: 2 false -200");
+ }
+ }
+ }
+} \ No newline at end of file