From 34f4a68a41c27ff2d40c7fd6b168d40aa3481250 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 6 Mar 2022 17:59:02 +0100 Subject: shared: add strcmp() testcases, fix invalid implementation --- src/kernel/tests/util.c | 9 +++++++++ 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) { -- cgit v1.2.3