summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-12-04 09:31:40 -0500
committerJakob Kaivo <jkk@ung.org>2019-12-04 09:31:40 -0500
commit0358353af5b2619b5c42970d48831dad27886370 (patch)
tree0f626380d838264716f604782de22d3f59313bea /string.c
parentf602ac37820294b4e38f9a06ae7253be8426e8b3 (diff)
parentc9d408f4a83e8dd569699b0a8393d9311aafbcc3 (diff)
Merge branch 'master' of gitlab.com:jkaivo/libc-tests
Diffstat (limited to 'string.c')
-rw-r--r--string.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/string.c b/string.c
index 86a270c..fa44d78 100644
--- a/string.c
+++ b/string.c
@@ -4,10 +4,71 @@
void test_string_h(void)
{
size_t size;
+ char dst[64] = { 0 };
+ char less[] = "a";
+ char more[] = "b";
+ char haystack[] = "the five boxing wizards jump quickly";
testing_header("string.h");
test_true(NULL == 0);
+ test_string(memcpy(dst + 1, "foo", 3), "foo");
+ test_string(memmove(dst, dst + 1, 3), "fooo");
+
+ test_string(strcpy(dst, "foo"), "foo");
+ memset(dst, '\0', sizeof(dst));
+ test_string(strncpy(dst, "foo", 1), "f");
+ test_string(strcat(dst, "oo"), "foo");
+ test_string(strncat(dst, "barf", 3), "foobar");
+
+ test_int_equals(memcmp(less, less, 2), 0);
+ test_true(memcmp(less, more, 2) < 0);
+ test_true(memcmp(more, less, 2) > 0);
+
+ test_int_equals(strcmp(less, less), 0);
+ test_true(strcmp(less, more) < 0);
+ test_true(strcmp(more, less) > 0);
+
+ test_int_equals(strcoll(less, less), 0);
+ test_true(strcoll(less, more) < 0);
+ test_true(strcoll(more, less) > 0);
+
+ test_int_equals(strncmp(less, less, 2), 0);
+ test_true(strncmp(less, more, 2) < 0);
+ test_true(strncmp(more, less, 2) > 0);
+
+ /* TODO: strxfrm */
+
+ test_true(memchr(haystack, '1', sizeof(haystack)) == NULL);
+ test_true(memchr(haystack, 'f', sizeof(haystack)) == haystack + 4);
+
+ test_true(strchr(haystack, '1') == NULL);
+ test_true(strchr(haystack, 'f') == haystack + 4);
+
+ test_true(strcspn(haystack, " abcd") == 3);
+ test_true(strcspn(haystack, "eht") == 0);
+ test_true(strcspn(haystack, "12345") == sizeof(haystack) - 1);
+
+ test_true(strpbrk(haystack, "12345") == NULL);
+ test_true(strpbrk(haystack, "abcd") == haystack + 9);
+
+ test_true(strrchr(haystack, '1') == NULL);
+ test_true(strrchr(haystack, 'f') == haystack + 4);
+
+ test_true(strspn(haystack, "12345") == 0);
+ test_true(strspn(haystack, "eht") == 3);
+
+ test_true(strstr(haystack, "wizard") == haystack + 16);
+ test_true(strstr(haystack, "rogue") == NULL);
+
+ /* TODO: strtok */
+
+ /* TODO: memset */
+
+ /* TODO: strerror */
+
+ test_true(strlen(haystack) == sizeof(haystack) - 1);
+
testing_end();
}