diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-13 23:58:18 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | 75e956eac30050bb10d131b8f14ecbc396abcf17 (patch) | |
tree | 116bef4e8208506cbb17dcf40006ed379cc6f0c1 /asl/tests | |
parent | ae9d36675a21b5a81bc87279dc9ee1224cdcb963 (diff) |
Use string_view for formatting
Diffstat (limited to 'asl/tests')
-rw-r--r-- | asl/tests/format_tests.cpp | 35 | ||||
-rw-r--r-- | asl/tests/string_view_tests.cpp | 35 |
2 files changed, 53 insertions, 17 deletions
diff --git a/asl/tests/format_tests.cpp b/asl/tests/format_tests.cpp index f051034..dd31efc 100644 --- a/asl/tests/format_tests.cpp +++ b/asl/tests/format_tests.cpp @@ -1,5 +1,6 @@ #include "asl/format.hpp"
#include "asl/testing/testing.hpp"
+#include "asl/print.hpp"
#include <cstdlib>
#include <cstring>
@@ -38,47 +39,47 @@ ASL_TEST(format_args) {
StringSink sink;
- // @Todo Introduce ASL_TEST_ASSERT_EQ, or ASL_TEST_ASSERT_STREQ
+ // @Todo Introduce ASL_TEST_EXPECT_EQ, or ASL_TEST_EXPECT_STREQ
// @Todo Don't use strcmp for string comparison
asl::format(&sink, "Hello, world!");
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "Hello, world!") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "Hello, world!") == 0);
sink.reset();
asl::format(&sink, "");
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "") == 0);
sink.reset();
asl::format(&sink, "Hello, {}!", "world");
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "Hello, world!") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "Hello, world!") == 0);
sink.reset();
asl::format(&sink, "Hello, {}! {}", "world");
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "Hello, world! <ERROR>") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "Hello, world! <ERROR>") == 0);
sink.reset();
asl::format(&sink, "Hello, pup!", "world");
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "Hello, pup!") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "Hello, pup!") == 0);
sink.reset();
asl::format(&sink, "{}", "CHEESE");
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "CHEESE") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "CHEESE") == 0);
sink.reset();
asl::format(&sink, "{ ", "CHEESE");
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "<ERROR> ") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "<ERROR> ") == 0);
sink.reset();
asl::format(&sink, "{", "CHEESE");
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "<ERROR>") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "<ERROR>") == 0);
sink.reset();
asl::format(&sink, "a{{b");
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "a{b") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "a{b") == 0);
sink.reset();
asl::format(&sink, "{{{}}} }", "CHEESE");
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "{CHEESE} }") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "{CHEESE} }") == 0);
}
ASL_TEST(format_integers)
@@ -87,23 +88,23 @@ ASL_TEST(format_integers) sink.reset();
asl::format(&sink, "{} {} {}", 0, 1, 2);
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "0 1 2") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "0 1 2") == 0);
sink.reset();
asl::format(&sink, "{} {} {}", 10, 11, 12);
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "10 11 12") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "10 11 12") == 0);
sink.reset();
asl::format(&sink, "{} {} {}", 100, 101, 102);
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "100 101 102") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "100 101 102") == 0);
sink.reset();
asl::format(&sink, "{} {} {}", 1000, 1001, 1002);
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "1000 1001 1002") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "1000 1001 1002") == 0);
sink.reset();
asl::format(&sink, "{} {} {} {}", -1, -23, -456, -7890);
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "-1 -23 -456 -7890") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "-1 -23 -456 -7890") == 0);
}
ASL_TEST(format_boolean)
@@ -112,5 +113,5 @@ ASL_TEST(format_boolean) sink.reset();
asl::format(&sink, "{} {}", true, false);
- ASL_TEST_ASSERT(strcmp(sink.cstr(), "true false") == 0);
+ ASL_TEST_EXPECT(strcmp(sink.cstr(), "true false") == 0);
}
diff --git a/asl/tests/string_view_tests.cpp b/asl/tests/string_view_tests.cpp index dafb91c..1c12603 100644 --- a/asl/tests/string_view_tests.cpp +++ b/asl/tests/string_view_tests.cpp @@ -25,3 +25,38 @@ ASL_TEST(from_literal) asl::string_view s2 = ""_sv; ASL_TEST_EXPECT(s2.is_empty()); } + +ASL_TEST(substr1) +{ + asl::string_view s1 = "abcd"; + + asl::string_view s2 = s1.substr(0); + ASL_TEST_ASSERT(s2.size() == 4); + ASL_TEST_EXPECT(memcmp(s2.data(), "abcd", 4) == 0); + + s2 = s1.substr(2); + ASL_TEST_ASSERT(s2.size() == 2); + ASL_TEST_EXPECT(memcmp(s2.data(), "cd", 2) == 0); + + s2 = s1.substr(4); + ASL_TEST_ASSERT(s2.size() == 0); +} + +ASL_TEST(substr2) +{ + asl::string_view s1 = "abcd"; + + asl::string_view s2 = s1.substr(0, 4); + ASL_TEST_ASSERT(s2.size() == 4); + ASL_TEST_EXPECT(memcmp(s2.data(), "abcd", 4) == 0); + + s2 = s1.substr(1, 2); + ASL_TEST_ASSERT(s2.size() == 2); + ASL_TEST_EXPECT(memcmp(s2.data(), "bc", 2) == 0); + + s2 = s1.substr(4, 0); + ASL_TEST_ASSERT(s2.size() == 0); + + s2 = s1.substr(1, 0); + ASL_TEST_ASSERT(s2.size() == 0); +} |