diff options
author | dzwdz | 2022-03-06 17:59:02 +0100 |
---|---|---|
committer | dzwdz | 2022-03-06 17:59:02 +0100 |
commit | 34f4a68a41c27ff2d40c7fd6b168d40aa3481250 (patch) | |
tree | 615bc7dd96b1f354466fdc259f96897fe074b0eb | |
parent | 970b5b7c6a7778579a4398cdb8d0bfb6bcd33da1 (diff) |
shared: add strcmp() testcases, fix invalid implementation
-rw-r--r-- | src/kernel/tests/util.c | 9 | ||||
-rw-r--r-- | src/shared/mem.c | 10 |
2 files changed, 13 insertions, 6 deletions
diff --git a/src/kernel/tests/util.c b/src/kernel/tests/util.c index 05bba24..e7dcb82 100644 --- a/src/kernel/tests/util.c +++ b/src/kernel/tests/util.c @@ -22,6 +22,15 @@ TEST(memcmp) { TEST_COND(0 < memcmp("654", "555", 3)); } +TEST(strcmp) { + TEST_COND(0 == strcmp("string", "string")); + TEST_COND(0 > strcmp("str", "string")); + TEST_COND(0 < strcmp("string", "str")); + + TEST_COND(0 != strcmp("stress", "string")); +} + void tests_utils(void) { TEST_RUN(memcmp); + TEST_RUN(strcmp); } diff --git a/src/shared/mem.c b/src/shared/mem.c index 9185c9e..eb34445 100644 --- a/src/shared/mem.c +++ b/src/shared/mem.c @@ -28,14 +28,12 @@ void *memset(void *s, int c, size_t n) { } int strcmp(const char *s1, const char *s2) { - while (*s1) { - if (*s1 != *s2) { - if (*s1 < *s2) return -1; - else return 1; - } + while (*s1 && *s1 == *s2) { s1++; s2++; } - return 0; + if (*s1 == *s2) return 0; + if (*s1 < *s2) return -1; + else return 1; } size_t strlen(const char *s) { |