From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] test: self: string: add test cases for strsep_unescaped
Date: Wed, 28 May 2025 07:58:14 +0200 [thread overview]
Message-ID: <20250528055814.1368888-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250528055814.1368888-1-a.fatoum@pengutronix.de>
Just to make sure the function works as intended, add some unit tests.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
test/self/string.c | 138 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 136 insertions(+), 2 deletions(-)
diff --git a/test/self/string.c b/test/self/string.c
index d3d17cdc096f..f8da83c72a71 100644
--- a/test/self/string.c
+++ b/test/self/string.c
@@ -172,9 +172,9 @@ static void __expect_streq(const char *func, int line,
char *is, const char *expect, bool free_is)
{
total_tests++;
- if (strcmp(is, expect)) {
+ if (!streq_ptr(is, expect)) {
failed_tests++;
- printf("%s:%d: got %s, but %s expected\n", func, line, is, expect);
+ printf("%s:%d: got '%s', but '%s' expected\n", func, line, is, expect);
}
if (free_is)
@@ -183,6 +183,13 @@ static void __expect_streq(const char *func, int line,
#define expect_dynstreq(args...) \
__expect_streq(__func__, __LINE__, args, true)
+#define expect_streq(args...) \
+ __expect_streq(__func__, __LINE__, args, false)
+#define expect_chreq(ch1, ch2) do { \
+ char s1[2] = { ch1, '\0' }; \
+ char s2[2] = { ch2, '\0' }; \
+ __expect_streq(__func__, __LINE__, s1, s2, false); \
+} while (0);
static void test_strjoin(void)
{
@@ -195,9 +202,136 @@ static void test_strjoin(void)
expect_dynstreq(strjoin(" ", NULL, 0), "");
}
+static void test_strsep_unescaped_basic(void)
+{
+ char str[] = "ayy,bee,cee", *s = str, delim;
+
+ expect_streq(strsep_unescaped(&s, ",", NULL), "ayy");
+ expect_streq(strsep_unescaped(&s, ",", &delim), "bee");
+ expect_chreq(delim, ',');
+ expect_streq(strsep_unescaped(&s, ",", NULL), "cee");
+ delim = '!';
+ expect_streq(strsep_unescaped(&s, ",", &delim), NULL);
+ expect_chreq(delim, '!');
+}
+
+static void test_strsep_unescaped_with_escape(void)
+{
+ char str[] = "ayy\\,bee,cee", *s = str;
+
+ expect_streq(strsep_unescaped(&s, ",", NULL), "ayy,bee");
+ expect_streq(strsep_unescaped(&s, ",", NULL), "cee");
+ expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_double_backslash(void)
+{
+ char str[] = "ayy\\\\,bee", *s = str;
+
+ expect_streq(strsep_unescaped(&s, ",", NULL), "ayy\\");
+ expect_streq(strsep_unescaped(&s, ",", NULL), "bee");
+ expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_trailing_escape(void)
+{
+ char str[] = "ayy\\", *s = str;
+
+ expect_streq(strsep_unescaped(&s, ",", NULL), "ayy");
+ expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_multiple_escaped(void)
+{
+ char str[] = "ayy\\,\\,bee\\,cee,dee", *s = str;
+
+ expect_streq(strsep_unescaped(&s, ",", NULL), "ayy,,bee,cee");
+ expect_streq(strsep_unescaped(&s, ",", NULL), "dee");
+ expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_multiple_delims(void)
+{
+ char str[] = "ayy\\ \\:bee:cee dee", *s = str;
+
+ expect_streq(strsep_unescaped(&s, ": ", NULL), "ayy :bee");
+ expect_streq(strsep_unescaped(&s, ": ", NULL), "cee");
+ expect_streq(strsep_unescaped(&s, ": ", NULL), "dee");
+ expect_streq(strsep_unescaped(&s, ": ", NULL), NULL);
+}
+
+static void test_strsep_unescaped_multiple_delims_empty(void)
+{
+ char str[] = "ayy\\ \\:bee::cee dee", *s = str, delim;
+
+ expect_streq(strsep_unescaped(&s, ": ", &delim), "ayy :bee");
+ expect_chreq(delim, ':');
+ expect_streq(strsep_unescaped(&s, ": ", &delim), "");
+ expect_chreq(delim, ':');
+ expect_streq(strsep_unescaped(&s, ": ", &delim), "cee");
+ expect_chreq(delim, ' ');
+ expect_streq(strsep_unescaped(&s, ": ", &delim), "");
+ expect_chreq(delim, ' ');
+ expect_streq(strsep_unescaped(&s, ": ", &delim), "dee");
+ expect_chreq(delim, '\0');
+ expect_streq(strsep_unescaped(&s, ": ", NULL), NULL);
+}
+
+static void test_strsep_unescaped_no_delim(void)
+{
+ char str[] = "abc", *s = str, delim;
+
+ expect_streq(strsep_unescaped(&s, ",", &delim), "abc");
+ expect_chreq(delim, '\0');
+ expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_null_string(void)
+{
+ char *s = NULL;
+
+ expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_empty_string(void)
+{
+ char str[] = "", *s = str, delim;
+
+ expect_streq(strsep_unescaped(&s, ",", &delim), "");
+ expect_chreq(delim, '\0');
+ expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped_only_delimiters(void)
+{
+ char str[] = ",,,", *s = str;
+
+ expect_streq(strsep_unescaped(&s, ",", NULL), "");
+ expect_streq(strsep_unescaped(&s, ",", NULL), "");
+ expect_streq(strsep_unescaped(&s, ",", NULL), "");
+ expect_streq(strsep_unescaped(&s, ",", NULL), "");
+ expect_streq(strsep_unescaped(&s, ",", NULL), NULL);
+}
+
+static void test_strsep_unescaped(void)
+{
+ test_strsep_unescaped_basic();
+ test_strsep_unescaped_with_escape();
+ test_strsep_unescaped_double_backslash();
+ test_strsep_unescaped_trailing_escape();
+ test_strsep_unescaped_multiple_escaped();
+ test_strsep_unescaped_multiple_delims();
+ test_strsep_unescaped_multiple_delims_empty();
+ test_strsep_unescaped_no_delim();
+ test_strsep_unescaped_null_string();
+ test_strsep_unescaped_empty_string();
+ test_strsep_unescaped_only_delimiters();
+}
+
static void test_string(void)
{
test_strverscmp();
test_strjoin();
+ test_strsep_unescaped();
}
bselftest(parser, test_string);
--
2.39.5
prev parent reply other threads:[~2025-05-28 5:58 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-28 5:58 [PATCH 1/2] string: add delimiter output parameter to strsep_unescaped Ahmad Fatoum
2025-05-28 5:58 ` Ahmad Fatoum [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250528055814.1368888-2-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox