aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen Hutchings <ben@decadent.org.uk>2022-12-30 22:48:50 +0100
committerBen Hutchings <ben@decadent.org.uk>2022-12-30 23:00:38 +0100
commit9707c6b8d4e6292482bd159458d426cdf2ca9d33 (patch)
treecdee696ce214fb0e2a497203716cab24cbf8d07a
parent4a9d3b35a2d40bbf4f821f2eddebc1e1846059cd (diff)
downloadklibc-9707c6b8d4e6292482bd159458d426cdf2ca9d33.tar.gz
[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@decadent.org.uk>
-rw-r--r--usr/klibc/tests/Kbuild7
-rw-r--r--usr/klibc/tests/strsearch.c113
2 files changed, 120 insertions, 0 deletions
diff --git a/usr/klibc/tests/Kbuild b/usr/klibc/tests/Kbuild
index 44229c70bd2921..31f03089b291a6 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 00000000000000..01fd55d835fb06
--- /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;
+}