[klibc] [klibc:master] tests: Add test program for string search functions
klibc-bot for Ben Hutchings
ben at decadent.org.uk
Fri Dec 30 14:03:11 PST 2022
Commit-ID: 9707c6b8d4e6292482bd159458d426cdf2ca9d33
Gitweb: http://git.kernel.org/?p=libs/klibc/klibc.git;a=commit;h=9707c6b8d4e6292482bd159458d426cdf2ca9d33
Author: Ben Hutchings <ben at decadent.org.uk>
AuthorDate: Fri, 30 Dec 2022 22:48:50 +0100
Committer: Ben Hutchings <ben at decadent.org.uk>
CommitDate: Fri, 30 Dec 2022 23:00:38 +0100
[klibc] tests: Add test program for string search functions
Add some test cases for str{,r}chr(), str{,c}spn(), strpbrk(), and
strstr(). One test case for strrchr() and one for strstr() currently
fail.
Signed-off-by: Ben Hutchings <ben at decadent.org.uk>
---
usr/klibc/tests/Kbuild | 7 +++
usr/klibc/tests/strsearch.c | 113 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 120 insertions(+)
diff --git a/usr/klibc/tests/Kbuild b/usr/klibc/tests/Kbuild
index 44229c70..31f03089 100644
--- a/usr/klibc/tests/Kbuild
+++ b/usr/klibc/tests/Kbuild
@@ -15,6 +15,12 @@ KLIBCCFLAGS_testvsnp.o := -Wno-format
# warning requires a value.
KLIBCCFLAGS_malloctest3.o := $(call cc-option,-Wno-alloc-size-larger-than)
+# Disable optimisation of these test cases based on compiler knowledge
+# of what the functions should do.
+KLIBCCFLAGS_strsearch.o := -fno-builtin-strchr -fno-builtin-strrchr \
+ -fno-builtin-strspn -fno-builtin-strcspn \
+ -fno-builtin-strpbrk -fno-builtin-strstr
+
static-y := $(test-files:.c=)
shared-y := $(addsuffix .shared, $(static-y))
@@ -49,6 +55,7 @@ stat.shared-y := stat.o
statfs.shared-y := statfs.o
stdio.shared-y := stdio.o
strlcpycat.shared-y := strlcpycat.o
+strsearch.shared-y := strsearch.o
strtoimax.shared-y := strtoimax.o
strtotime.shared-y := strtotime.o
sysconf.shared-y := sysconf.o
diff --git a/usr/klibc/tests/strsearch.c b/usr/klibc/tests/strsearch.c
new file mode 100644
index 00000000..01fd55d8
--- /dev/null
+++ b/usr/klibc/tests/strsearch.c
@@ -0,0 +1,113 @@
+#include <stdio.h>
+#include <string.h>
+
+int main(void)
+{
+ const char haystack[] = "haystack";
+ const char empty[] = "";
+ const char *p;
+ size_t len;
+
+ p = strchr(haystack, 'a');
+ printf("found 'a' at offset %zd\n", p ? p - haystack : -1);
+ if (p != haystack + 1)
+ goto error;
+ p = strchr(haystack, 'b');
+ printf("found 'b' at offset %zd\n", p ? p - haystack : -1);
+ if (p != NULL)
+ goto error;
+ p = strchr(haystack, 0);
+ printf("found 0 at offset %zd\n", p ? p - haystack : -1);
+ if (p != haystack + 8)
+ goto error;
+
+ p = strrchr(haystack, 'a');
+ printf("found 'a' at offset %zd\n", p ? p - haystack : -1);
+ if (p != haystack + 5)
+ goto error;
+ p = strrchr(haystack, 'b');
+ printf("found 'b' at offset %zd\n", p ? p - haystack : -1);
+ if (p != NULL)
+ goto error;
+ p = strrchr(haystack, 0);
+ printf("found 0 at offset %zd\n", p ? p - haystack : -1);
+ if (p != haystack + 8)
+ goto error;
+
+ len = strspn(haystack, "hasty");
+ printf("found %zu chars from 'hasty'\n", len);
+ if (len != 6)
+ goto error;
+ len = strspn(haystack, "haystack");
+ printf("found %zu chars from 'haystack'\n", len);
+ if (len != 8)
+ goto error;
+ len = strspn(haystack, "");
+ printf("found %zu chars from ''\n", len);
+ if (len != 0)
+ goto error;
+
+ len = strcspn(haystack, "stick");
+ printf("found %zu chars not from 'stick'\n", len);
+ if (len != 3)
+ goto error;
+ len = strcspn(haystack, "needle");
+ printf("found %zu chars not from 'needle'\n", len);
+ if (len != 8)
+ goto error;
+ len = strcspn(haystack, "");
+ printf("found %zu chars not from ''\n", len);
+ if (len != 8)
+ goto error;
+
+ p = strpbrk(haystack, "stick");
+ printf("found char from 'stick' at offset %zd\n", p ? p - haystack : -1);
+ if (p != haystack + 3)
+ goto error;
+ p = strpbrk(haystack, "needle");
+ printf("found char from 'needle' at offset %zd\n", p ? p - haystack : -1);
+ if (p != NULL)
+ goto error;
+ p = strpbrk(haystack, "");
+ printf("found char from '' at offset %zd\n", p ? p - haystack : -1);
+ if (p != NULL)
+ goto error;
+
+ p = strstr(haystack, "stack");
+ printf("found 'stack' at offset %zd\n", p ? p - haystack : -1);
+ if (p != haystack + 3)
+ goto error;
+ p = strstr(haystack, "tacks");
+ printf("found 'tacks' at offset %zd\n", p ? p - haystack : -1);
+ if (p != NULL)
+ goto error;
+ p = strstr(haystack, "needle");
+ printf("found 'needle' at offset %zd\n", p ? p - haystack : -1);
+ if (p != NULL)
+ goto error;
+ p = strstr(haystack, "k");
+ printf("found 'k' at offset %zd\n", p ? p - haystack : -1);
+ if (p != haystack + 7)
+ goto error;
+ p = strstr(haystack, "b");
+ printf("found 'b' at offset %zd\n", p ? p - haystack : -1);
+ if (p != NULL)
+ goto error;
+ p = strstr(haystack, "kk");
+ printf("found 'kk' at offset %zd\n", p ? p - haystack : -1);
+ if (p != NULL)
+ goto error;
+ p = strstr(haystack, "");
+ printf("found '' at offset %zd\n", p ? p - haystack : -1);
+ if (p != haystack)
+ goto error;
+ p = strstr(empty, "");
+ printf("found '' at offset %zd\n", p ? p - empty : -1);
+ if (p != empty)
+ goto error;
+
+ return 0;
+error:
+ printf("unexpected result\n");
+ return 1;
+}
More information about the klibc
mailing list