aboutsummaryrefslogtreecommitdiff
path: root/lib/tst
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tst')
-rw-r--r--lib/tst/allocator.hh22
-rw-r--r--lib/tst/linked_list_allocator.cc98
-rw-r--r--lib/tst/mem.c17
-rw-r--r--lib/tst/string.c34
4 files changed, 171 insertions, 0 deletions
diff --git a/lib/tst/allocator.hh b/lib/tst/allocator.hh
new file mode 100644
index 0000000..3bc1715
--- /dev/null
+++ b/lib/tst/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/tst/linked_list_allocator.cc b/lib/tst/linked_list_allocator.cc
new file mode 100644
index 0000000..f3af8e2
--- /dev/null
+++ b/lib/tst/linked_list_allocator.cc
@@ -0,0 +1,98 @@
+#include <gtest/gtest.h>
+#include <iomanip>
+#include <iostream>
+
+namespace libk {
+#include "../stdlib/linked_list_allocator.c"
+
+std::ostream &
+operator<<(std::ostream &os, const Chunk &begin)
+{
+ for (const Chunk *iter = &begin; 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 "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/tst/mem.c b/lib/tst/mem.c
new file mode 100644
index 0000000..bccb3a3
--- /dev/null
+++ b/lib/tst/mem.c
@@ -0,0 +1,17 @@
+#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/string.c b/lib/tst/string.c
new file mode 100644
index 0000000..725d547
--- /dev/null
+++ b/lib/tst/string.c
@@ -0,0 +1,34 @@
+#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]);
+ }
+}