aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2022-11-06 14:48:41 +0200
committeraqua <aqua@iserlohn-fortress.net>2022-11-06 15:03:06 +0200
commit6d53f16aa508c445309aa4ebe965baa9e17401a1 (patch)
treebf42dc7dc1105d819a83c1e5a8c8595df19401b6 /lib
parentlib/malloc: add linked list implementation (diff)
downloadkernel-6d53f16aa508c445309aa4ebe965baa9e17401a1.tar.xz
lib: migrate tests to gtest
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile5
-rw-r--r--lib/string/itoa.c24
-rw-r--r--lib/tst/mem.c17
-rw-r--r--lib/tst/mem.cc29
-rw-r--r--lib/tst/string.c34
-rw-r--r--lib/tst/string.cc20
6 files changed, 64 insertions, 65 deletions
diff --git a/lib/Makefile b/lib/Makefile
index 042eccc..dec0769 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -7,5 +7,6 @@ libk.SRCS = memcpy.c memset.c stdio/printf.c string/itoa.c
include ../rules.mk
.PHONY: test
-test: tst/test_linked_list_allocator
- @$(foreach f,$^,./$f)
+test: tst/test_mem tst/test_string tst/test_linked_list_allocator
+ @echo $^
+ $(foreach f,$^,./$f;)
diff --git a/lib/string/itoa.c b/lib/string/itoa.c
index 3708be2..2db9768 100644
--- a/lib/string/itoa.c
+++ b/lib/string/itoa.c
@@ -4,18 +4,6 @@
static const char *numbers = "0123456789abcdef";
char *
-itoa(char *p, int x, int base)
-{
- const bool is_negative = (x < 0);
- if (is_negative) x = -x;
-
- p = utoa(p, x, base);
-
- if (is_negative) *--p = '-';
- return p;
-}
-
-char *
utoa(char *p, unsigned x, int base)
{
p += 3 * sizeof(unsigned);
@@ -28,3 +16,15 @@ utoa(char *p, unsigned x, int base)
return p;
}
+
+char *
+itoa(char *p, int x, int base)
+{
+ const bool is_negative = (x < 0);
+ if (is_negative) x = -x;
+
+ p = utoa(p, x, base);
+
+ if (is_negative) *--p = '-';
+ return p;
+}
diff --git a/lib/tst/mem.c b/lib/tst/mem.c
deleted file mode 100644
index bccb3a3..0000000
--- a/lib/tst/mem.c
+++ /dev/null
@@ -1,17 +0,0 @@
-#include <assert.h>
-
-static const unsigned char data[] = {0xde, 0xca, 0xfa, 0xde};
-static unsigned char buffer[4];
-
-void *memset(void *s, int c, long unsigned n);
-void *memcpy(void *restrict dest, const void *restrict src, long unsigned n);
-
-int
-main(void)
-{
- memset(buffer, 0xae, sizeof(data));
- for (unsigned i = 0; i < sizeof(data); ++i) assert(buffer[i] == 0xae);
-
- memcpy(buffer, data, sizeof(data));
- for (unsigned i = 0; i < sizeof(data); ++i) assert(buffer[i] == data[i]);
-}
diff --git a/lib/tst/mem.cc b/lib/tst/mem.cc
new file mode 100644
index 0000000..b233da8
--- /dev/null
+++ b/lib/tst/mem.cc
@@ -0,0 +1,29 @@
+#include <gtest/gtest.h>
+
+#define restrict __restrict__
+
+namespace libk {
+#include "../memcpy.c"
+#include "../memset.c"
+} // namespace libk
+
+TEST(mem, memset)
+{
+ auto *buffer = new unsigned char[2048];
+
+ libk::memset(buffer, 0xae, sizeof(buffer));
+ for (unsigned i = 0; i < sizeof(buffer); ++i) EXPECT_EQ(buffer[i], 0xae);
+
+ delete[] buffer;
+}
+
+TEST(mem, memcpy)
+{
+ const unsigned char data[] = {0xde, 0xca, 0xfa, 0xde};
+ auto *buffer = new unsigned char[sizeof(data)];
+
+ memcpy(buffer, data, sizeof(data));
+ for (unsigned i = 0; i < sizeof(data); ++i) EXPECT_EQ(buffer[i], data[i]);
+
+ delete[] buffer;
+}
diff --git a/lib/tst/string.c b/lib/tst/string.c
deleted file mode 100644
index 725d547..0000000
--- a/lib/tst/string.c
+++ /dev/null
@@ -1,34 +0,0 @@
-#include "../string.h"
-#include <assert.h>
-
-static const char *dec = "12341234";
-static const char *neg_dec = "-12341234";
-static const char *u32_hex = "decafade";
-static const char *i32_hex = "7fffffff";
-static char buffer[64];
-
-int
-main()
-{
- { // utoa
- char *r;
-
- r = utoa(buffer, 12341234u, 10);
- for (unsigned i = 0; dec[i] != '\0'; ++i) assert(r[i] == dec[i]);
-
- r = utoa(buffer, 0xdecafade, 16);
- for (unsigned i = 0; u32_hex[i] != '\0'; ++i) assert(r[i] == u32_hex[i]);
- }
- { // itoa
- char *r;
-
- r = itoa(buffer, 12341234, 10);
- for (unsigned i = 0; dec[i] != '\0'; ++i) assert(r[i] == dec[i]);
-
- r = itoa(buffer, -12341234, 10);
- for (unsigned i = 0; neg_dec[i] != '\0'; ++i) assert(r[i] == neg_dec[i]);
-
- r = itoa(buffer, 0x7fffffff, 16);
- for (unsigned i = 0; i32_hex[i] != '\0'; ++i) assert(r[i] == i32_hex[i]);
- }
-}
diff --git a/lib/tst/string.cc b/lib/tst/string.cc
new file mode 100644
index 0000000..8dec190
--- /dev/null
+++ b/lib/tst/string.cc
@@ -0,0 +1,20 @@
+#include <gtest/gtest.h>
+
+namespace libk {
+#include "../string/itoa.c"
+}
+
+char buffer[64];
+
+TEST(itoa, itoa)
+{
+ EXPECT_STREQ(libk::itoa(buffer, 12341234, 10), "12341234");
+ EXPECT_STREQ(libk::itoa(buffer, -12341234, 10), "-12341234");
+ EXPECT_STREQ(libk::itoa(buffer, 0x7fffffff, 16), "7fffffff");
+}
+
+TEST(itoa, utoa)
+{
+ EXPECT_STREQ(libk::utoa(buffer, 12341234u, 10), "12341234");
+ EXPECT_STREQ(libk::utoa(buffer, 0xdecafade, 16), "decafade");
+}