aboutsummaryrefslogtreecommitdiff
path: root/lib/libk
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2023-03-05 14:38:54 +0200
committeraqua <aqua@iserlohn-fortress.net>2023-03-05 14:38:54 +0200
commit787c1a6016dd2fdb51f20fcb5ca0ac5e461892d6 (patch)
tree7db9f5102adc3d50ca85a8175ce67465256b8908 /lib/libk
parentAdd TARGETBIN and TARGETLIB rules (diff)
downloadkernel-787c1a6016dd2fdb51f20fcb5ca0ac5e461892d6.tar.xz
Move all tests next to the code they're testing
Diffstat (limited to 'lib/libk')
-rw-r--r--lib/libk/endian/test_endian_little.cc38
-rw-r--r--lib/libk/stdlib/test_allocator.hh22
-rw-r--r--lib/libk/stdlib/test_linked_list_allocator.cc98
-rw-r--r--lib/libk/stdlib/test_mem.cc29
-rw-r--r--lib/libk/string/test_string.cc20
5 files changed, 207 insertions, 0 deletions
diff --git a/lib/libk/endian/test_endian_little.cc b/lib/libk/endian/test_endian_little.cc
new file mode 100644
index 0000000..97ee286
--- /dev/null
+++ b/lib/libk/endian/test_endian_little.cc
@@ -0,0 +1,38 @@
+#include <endian.h>
+#include <gtest/gtest.h>
+
+namespace libk {
+#include "little.c"
+} // namespace libk
+
+TEST(endian_little, htole16)
+{
+ EXPECT_EQ(static_cast<uint16_t>(0xabcd), libk::htole16(0xabcd));
+ EXPECT_EQ(libk::htole16(0xabcd), htole16(0xabcd));
+}
+TEST(endian_little, htole32)
+{
+ EXPECT_EQ(static_cast<uint32_t>(0xabcd0123), libk::htole32(0xabcd0123));
+ EXPECT_EQ(libk::htole32(0xabcd0123), htole32(0xabcd0123));
+}
+TEST(endian_little, htole64)
+{
+ EXPECT_EQ(static_cast<uint64_t>(0x0123456789abcdef), libk::htole64(0x0123456789abcdef));
+ EXPECT_EQ(libk::htole64(0xabcdef0123456789), htole64(0xabcdef0123456789));
+}
+
+TEST(endian_little, htobe16)
+{
+ EXPECT_EQ(static_cast<uint16_t>(0xabcd), libk::htobe16(0xcdab));
+ EXPECT_EQ(libk::htobe16(0xabcd), htobe16(0xabcd));
+}
+TEST(endian_little, htobe32)
+{
+ EXPECT_EQ(static_cast<uint32_t>(0xabcd0123), libk::htobe32(0x2301cdab));
+ EXPECT_EQ(libk::htobe32(0xabcd0123), htobe32(0xabcd0123));
+}
+TEST(endian_little, htobe64)
+{
+ EXPECT_EQ(static_cast<uint64_t>(0x0123456789abcdef), libk::htobe64(0xefcdab8967452301));
+ EXPECT_EQ(libk::htobe64(0xabcdef0123456789), htobe64(0xabcdef0123456789));
+}
diff --git a/lib/libk/stdlib/test_allocator.hh b/lib/libk/stdlib/test_allocator.hh
new file mode 100644
index 0000000..3bc1715
--- /dev/null
+++ b/lib/libk/stdlib/test_allocator.hh
@@ -0,0 +1,22 @@
+#pragma once
+
+class TestAllocator : public ::testing::Test {
+protected:
+ void
+ SetUp() override
+ {
+ memory = malloc(memory_size);
+ libk::alloc_init(memory, memory_size);
+ ASSERT_EQ(libk::begin, memory);
+ }
+
+ void
+ TearDown() override
+ {
+ free(memory);
+ libk::begin = nullptr;
+ }
+
+ const size_t memory_size = 4096;
+ void *memory = nullptr;
+};
diff --git a/lib/libk/stdlib/test_linked_list_allocator.cc b/lib/libk/stdlib/test_linked_list_allocator.cc
new file mode 100644
index 0000000..5963ce1
--- /dev/null
+++ b/lib/libk/stdlib/test_linked_list_allocator.cc
@@ -0,0 +1,98 @@
+#include <gtest/gtest.h>
+#include <iomanip>
+#include <iostream>
+
+namespace libk {
+#include "linked_list_allocator.c"
+
+std::ostream &
+operator<<(std::ostream &os, const Chunk &b)
+{
+ for (const Chunk *iter = &b; iter != nullptr; iter = iter->next) {
+ os << iter << " used=" << iter->used << " size=" << std::setw(4) << iter->size << " next=" << iter->next
+ << std::endl;
+ }
+ return os;
+}
+}; // namespace libk
+
+#include "test_allocator.hh"
+
+TEST(UninitializedAllocator, malloc) { EXPECT_EQ(libk::malloc(1024), nullptr); }
+
+TEST_F(TestAllocator, mallocMoreThanAvialable)
+{
+ void *ptr = libk::malloc(memory_size);
+ EXPECT_EQ(ptr, nullptr) << *libk::begin;
+}
+
+TEST_F(TestAllocator, mallocExactlyAvialable)
+{
+ void *ptr = libk::malloc(memory_size - sizeof(libk::Chunk));
+ EXPECT_NE(ptr, nullptr) << *libk::begin;
+}
+
+TEST_F(TestAllocator, malloc)
+{
+ libk::Chunk *begin = libk::begin;
+
+ void *ptr0 = libk::malloc(1024);
+ EXPECT_NE(ptr0, nullptr) << *libk::begin;
+ EXPECT_EQ(reinterpret_cast<std::uintptr_t>(ptr0),
+ reinterpret_cast<std::uintptr_t>(libk::begin) + sizeof(libk::Chunk));
+ EXPECT_EQ(begin->used, 1);
+ EXPECT_EQ(begin->size, 1024);
+ ASSERT_NE(begin->next, nullptr);
+ begin = begin->next;
+
+ void *ptr1 = libk::malloc(512);
+ EXPECT_NE(ptr1, nullptr) << *libk::begin;
+ EXPECT_EQ(reinterpret_cast<std::uintptr_t>(ptr1),
+ reinterpret_cast<std::uintptr_t>(libk::begin) + 2 * sizeof(libk::Chunk) + 1024);
+ EXPECT_EQ(begin->used, 1);
+ EXPECT_EQ(begin->size, 512);
+ ASSERT_NE(begin->next, nullptr);
+}
+
+TEST_F(TestAllocator, freeNullptr)
+{
+ void *ptr0 = libk::malloc(1024);
+ EXPECT_NE(ptr0, nullptr) << *libk::begin;
+ void *ptr1 = libk::malloc(512);
+ EXPECT_NE(ptr1, nullptr) << *libk::begin;
+
+ libk::free(nullptr);
+ libk::Chunk *begin = libk::begin;
+ EXPECT_EQ(begin->used, 1);
+ EXPECT_NE(begin->next, nullptr);
+ begin = begin->next;
+ EXPECT_EQ(begin->used, 1);
+ EXPECT_NE(begin->next, nullptr);
+ begin = begin->next;
+ EXPECT_EQ(begin->used, 0);
+ EXPECT_EQ(begin->next, nullptr);
+}
+
+TEST_F(TestAllocator, free)
+{
+ void *ptr0 = libk::malloc(1024);
+ EXPECT_NE(ptr0, nullptr) << *libk::begin;
+ void *ptr1 = libk::malloc(512);
+ EXPECT_NE(ptr1, nullptr) << *libk::begin;
+
+ libk::free(ptr0);
+ libk::Chunk *begin = libk::begin;
+ EXPECT_EQ(begin->used, 0) << ptr0 << ": ptr0" << std::endl << *libk::begin;
+ EXPECT_NE(begin->next, nullptr);
+ begin = begin->next;
+ EXPECT_EQ(begin->used, 1);
+ EXPECT_NE(begin->next, nullptr);
+ begin = begin->next;
+ EXPECT_EQ(begin->used, 0);
+ EXPECT_EQ(begin->next, nullptr);
+
+ libk::free(ptr1);
+ begin = libk::begin;
+ EXPECT_EQ(begin->used, 0) << ptr0 << ": ptr0" << std::endl << *libk::begin;
+ EXPECT_EQ(begin->next, nullptr);
+}
diff --git a/lib/libk/stdlib/test_mem.cc b/lib/libk/stdlib/test_mem.cc
new file mode 100644
index 0000000..f8a5e18
--- /dev/null
+++ b/lib/libk/stdlib/test_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/libk/string/test_string.cc b/lib/libk/string/test_string.cc
new file mode 100644
index 0000000..d12b318
--- /dev/null
+++ b/lib/libk/string/test_string.cc
@@ -0,0 +1,20 @@
+#include <gtest/gtest.h>
+
+namespace libk {
+#include "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");
+}