diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/kernel/tests/main.c | 1 | ||||
-rw-r--r-- | src/kernel/tests/tests.h | 2 | ||||
-rw-r--r-- | src/kernel/tests/util.c | 26 | ||||
-rw-r--r-- | src/kernel/util.c | 11 | ||||
-rw-r--r-- | src/kernel/util.h | 2 |
5 files changed, 42 insertions, 0 deletions
diff --git a/src/kernel/tests/main.c b/src/kernel/tests/main.c index a7e9534..41ebe8d 100644 --- a/src/kernel/tests/main.c +++ b/src/kernel/tests/main.c @@ -13,6 +13,7 @@ void tests_all() { _did_tests_fail = false; TEST_RUN(basic_math); + tests_utils(); if (_did_tests_fail) panic(); diff --git a/src/kernel/tests/tests.h b/src/kernel/tests/tests.h index d9abce4..78397da 100644 --- a/src/kernel/tests/tests.h +++ b/src/kernel/tests/tests.h @@ -1,3 +1,5 @@ #pragma once void tests_all(); + +void tests_utils(); diff --git a/src/kernel/tests/util.c b/src/kernel/tests/util.c new file mode 100644 index 0000000..5bc8656 --- /dev/null +++ b/src/kernel/tests/util.c @@ -0,0 +1,26 @@ +#include <kernel/tests/base.h> +#include <kernel/util.h> + +TEST(memcmp) { + // basic equality checks + TEST_IF(0 == memcmp("some", "thing", 0)) + TEST_IF(0 != memcmp("some", "thing", 1)) + TEST_IF(0 != memcmp("some", "thing", 4)) + + TEST_IF(0 == memcmp("test", "tennis", 0)); + TEST_IF(0 == memcmp("test", "tennis", 1)); + TEST_IF(0 == memcmp("test", "tennis", 2)); + TEST_IF(0 != memcmp("test", "tennis", 3)); + TEST_IF(0 != memcmp("test", "tennis", 4)); + TEST_IF(0 != memcmp("test", "tennis", 5)); + + // test signs. does anyone even use that? + TEST_IF(0 > memcmp("foo", "moo", 4)); + TEST_IF(0 < memcmp("moo", "foo", 4)); + TEST_IF(0 > memcmp("555", "654", 3)); + TEST_IF(0 < memcmp("654", "555", 3)); +} + +void tests_utils() { + TEST_RUN(memcmp); +} diff --git a/src/kernel/util.c b/src/kernel/util.c index f4dc5e1..0977745 100644 --- a/src/kernel/util.c +++ b/src/kernel/util.c @@ -15,3 +15,14 @@ void *memset(void *s, int c, size_t n) { s2[i] = c; return s; } + +int memcmp(const void *s1, const void *s2, size_t n) { + const unsigned char *c1 = s1, *c2 = s2; + for (size_t i = 0; i < n; i++) { + if (c1[i] != c2[i]) { + if (c1[i] < c2[i]) return -1; + else return 1; + } + } + return 0; +} diff --git a/src/kernel/util.h b/src/kernel/util.h index 39301d6..b50810d 100644 --- a/src/kernel/util.h +++ b/src/kernel/util.h @@ -6,3 +6,5 @@ void *memcpy(void *dest, const void *src, size_t n); void *memset(void *s, int c, size_t n); + +int memcmp(const void *s1, const void *s2, size_t n); |