From f8217d240d598f39f70047f7a623dd46312542c6 Mon Sep 17 00:00:00 2001
From: 3gg <3gg@shellblade.net>
Date: Sat, 4 Dec 2021 16:01:12 -0800
Subject: Initial commit.

---
 list/CMakeLists.txt   |  23 +++++++
 list/include/list.h   |  21 ++++++
 list/src/list.c       |  14 ++++
 list/test/list_test.c |  34 ++++++++++
 list/test/test.h      | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 277 insertions(+)
 create mode 100644 list/CMakeLists.txt
 create mode 100644 list/include/list.h
 create mode 100644 list/src/list.c
 create mode 100644 list/test/list_test.c
 create mode 100644 list/test/test.h

(limited to 'list')

diff --git a/list/CMakeLists.txt b/list/CMakeLists.txt
new file mode 100644
index 0000000..5d11d28
--- /dev/null
+++ b/list/CMakeLists.txt
@@ -0,0 +1,23 @@
+cmake_minimum_required(VERSION 3.16)
+
+project(list)
+
+# Library
+
+add_library(list
+  src/list.c)
+
+target_include_directories(list PUBLIC
+  include)
+
+target_compile_options(list PRIVATE -Wall -Wextra)
+
+# Test
+
+add_executable(list_test
+  test/list_test.c)
+
+target_link_libraries(list_test
+  list)
+
+target_compile_options(list_test PRIVATE -DUNIT_TEST -Wall -Wextra)
diff --git a/list/include/list.h b/list/include/list.h
new file mode 100644
index 0000000..b00b48b
--- /dev/null
+++ b/list/include/list.h
@@ -0,0 +1,21 @@
+/// A doubly linked list.
+///
+/// This list does not hold user data. Instead, the list can be used as an
+/// intrusive list or as part as a more complex data structure.
+#pragma once
+
+#include <stddef.h>
+
+typedef struct list list;
+
+typedef struct list {
+  list* prev;
+  list* next;
+} list;
+
+/// Create a new list from an array of `size` items.
+void list_make(list* list, size_t size);
+
+/// Iterates over all the items in the list.
+#define list_foreach(LIST, iter)                                               \
+  for (struct list* iter = LIST; iter; iter = iter->next)
diff --git a/list/src/list.c b/list/src/list.c
new file mode 100644
index 0000000..f5b6507
--- /dev/null
+++ b/list/src/list.c
@@ -0,0 +1,14 @@
+#include "list.h"
+
+#include <assert.h>
+
+void list_make(list* list, size_t size) {
+  if (size == 0) {
+    return;
+  }
+  assert(list);
+  for (size_t i = 0; i < size; ++i) {
+    list[i].prev = (i == 0 ? 0 : &list[i - 1]);
+    list[i].next = (i == size - 1 ? 0 : &list[i + 1]);
+  }
+}
diff --git a/list/test/list_test.c b/list/test/list_test.c
new file mode 100644
index 0000000..a11c713
--- /dev/null
+++ b/list/test/list_test.c
@@ -0,0 +1,34 @@
+#include "list.h"
+
+#include "test.h"
+
+#define TEST_LIST_SIZE 10
+
+// Create an empty list.
+TEST_CASE(list_create_empty) { list_make(0, 0); }
+
+// Create a list of a given size.
+TEST_CASE(list_create) {
+  struct list list[TEST_LIST_SIZE];
+  list_make(list, TEST_LIST_SIZE);
+}
+
+// Iterate over a list.
+TEST_CASE(list_traverse) {
+  int numbers[TEST_LIST_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+
+  struct list list[TEST_LIST_SIZE];
+  list_make(list, TEST_LIST_SIZE);
+
+  int count = 0;
+  int sum = 0;
+  list_foreach(list, item) {
+    count++;
+    sum += numbers[item - list];
+  }
+
+  TEST_EQUAL(count, TEST_LIST_SIZE);
+  TEST_EQUAL(sum, TEST_LIST_SIZE * (TEST_LIST_SIZE + 1) / 2);
+}
+
+int main() { return 0; }
diff --git a/list/test/test.h b/list/test/test.h
new file mode 100644
index 0000000..fd8dc22
--- /dev/null
+++ b/list/test/test.h
@@ -0,0 +1,185 @@
+// SPDX-License-Identifier: MIT
+#pragma once
+
+#ifdef UNIT_TEST
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) ||     \
+    defined(__NetBSD__) || defined(__OpenBSD__)
+#define USE_SYSCTL_FOR_ARGS 1
+// clang-format off
+#include <sys/types.h>
+#include <sys/sysctl.h>
+// clang-format on
+#include <unistd.h>        // getpid
+#endif
+
+struct test_file_metadata;
+
+struct test_failure {
+	bool present;
+	const char *message;
+	const char *file;
+	int line;
+};
+
+struct test_case_metadata {
+	void (*fn)(struct test_case_metadata *, struct test_file_metadata *);
+	struct test_failure failure;
+	const char *name;
+	struct test_case_metadata *next;
+};
+
+struct test_file_metadata {
+	bool registered;
+	const char *name;
+	struct test_file_metadata *next;
+	struct test_case_metadata *tests;
+};
+
+struct test_file_metadata __attribute__((weak)) * test_file_head;
+
+#define SET_FAILURE(_message)                                                             \
+	metadata->failure = (struct test_failure) {                                       \
+		.message = _message, .file = __FILE__, .line = __LINE__, .present = true, \
+	}
+
+#define TEST_EQUAL(a, b)                                                                 \
+	do {                                                                             \
+		if ((a) != (b)) {                                                        \
+			SET_FAILURE(#a " != " #b);                                       \
+			return;                                                          \
+		}                                                                        \
+	} while (0)
+
+#define TEST_TRUE(a)                                                                     \
+	do {                                                                             \
+		if (!(a)) {                                                              \
+			SET_FAILURE(#a " is not true");                                  \
+			return;                                                          \
+		}                                                                        \
+	} while (0)
+
+#define TEST_STREQUAL(a, b)                                                              \
+	do {                                                                             \
+		if (strcmp(a, b) != 0) {                                                 \
+			SET_FAILURE(#a " != " #b);                                       \
+			return;                                                          \
+		}                                                                        \
+	} while (0)
+
+#define TEST_CASE(_name)                                                                  \
+	static void __test_h_##_name(struct test_case_metadata *,                         \
+	                             struct test_file_metadata *);                        \
+	static struct test_file_metadata __test_h_file;                                   \
+	static struct test_case_metadata __test_h_meta_##_name = {                        \
+	    .name = #_name,                                                               \
+	    .fn = __test_h_##_name,                                                       \
+	};                                                                                \
+	static void __attribute__((constructor(101))) __test_h_##_name##_register(void) { \
+		__test_h_meta_##_name.next = __test_h_file.tests;                         \
+		__test_h_file.tests = &__test_h_meta_##_name;                             \
+		if (!__test_h_file.registered) {                                          \
+			__test_h_file.name = __FILE__;                                    \
+			__test_h_file.next = test_file_head;                              \
+			test_file_head = &__test_h_file;                                  \
+			__test_h_file.registered = true;                                  \
+		}                                                                         \
+	}                                                                                 \
+	static void __test_h_##_name(                                                     \
+	    struct test_case_metadata *metadata __attribute__((unused)),                  \
+	    struct test_file_metadata *file_metadata __attribute__((unused)))
+
+extern void __attribute__((weak)) (*test_h_unittest_setup)(void);
+/// Run defined tests, return true if all tests succeeds
+/// @param[out] tests_run if not NULL, set to whether tests were run
+static inline void __attribute__((constructor(102))) run_tests(void) {
+	bool should_run = false;
+#ifdef USE_SYSCTL_FOR_ARGS
+	int mib[] = {
+		CTL_KERN,
+#if defined(__NetBSD__) || defined(__OpenBSD__)
+		KERN_PROC_ARGS,
+		getpid(),
+		KERN_PROC_ARGV,
+#else
+		KERN_PROC,
+		KERN_PROC_ARGS,
+		getpid(),
+#endif
+	};
+	char *arg = NULL;
+	size_t arglen;
+	sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &arglen, NULL, 0);
+	arg = malloc(arglen);
+	sysctl(mib, sizeof(mib) / sizeof(mib[0]), arg, &arglen, NULL, 0);
+#else
+	FILE *cmdlinef = fopen("/proc/self/cmdline", "r");
+	char *arg = NULL;
+	int arglen;
+	fscanf(cmdlinef, "%ms%n", &arg, &arglen);
+	fclose(cmdlinef);
+#endif
+	for (char *pos = arg; pos < arg + arglen; pos += strlen(pos) + 1) {
+		if (strcmp(pos, "--unittest") == 0) {
+			should_run = true;
+			break;
+		}
+	}
+	free(arg);
+
+	if (!should_run) {
+		return;
+	}
+
+	if (&test_h_unittest_setup) {
+		test_h_unittest_setup();
+	}
+
+	struct test_file_metadata *i = test_file_head;
+	int failed = 0, success = 0;
+	while (i) {
+		fprintf(stderr, "Running tests from %s:\n", i->name);
+		struct test_case_metadata *j = i->tests;
+		while (j) {
+			fprintf(stderr, "\t%s ... ", j->name);
+			j->failure.present = false;
+			j->fn(j, i);
+			if (j->failure.present) {
+				fprintf(stderr, "failed (%s at %s:%d)\n", j->failure.message,
+				        j->failure.file, j->failure.line);
+				failed++;
+			} else {
+				fprintf(stderr, "passed\n");
+				success++;
+			}
+			j = j->next;
+		}
+		fprintf(stderr, "\n");
+		i = i->next;
+	}
+	int total = failed + success;
+	fprintf(stderr, "Test results: passed %d/%d, failed %d/%d\n", success, total,
+	        failed, total);
+	exit(failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+
+#else
+
+#include <stdbool.h>
+
+#define TEST_CASE(name) static void __attribute__((unused)) __test_h_##name(void)
+
+#define TEST_EQUAL(a, b)                                                                 \
+	(void)(a);                                                                       \
+	(void)(b)
+#define TEST_TRUE(a) (void)(a)
+#define TEST_STREQUAL(a, b)                                                              \
+	(void)(a);                                                                       \
+	(void)(b)
+
+#endif
-- 
cgit v1.2.3