aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLion Kortlepel <[email protected]>2026-01-14 22:08:55 +0100
committerLion Kortlepel <[email protected]>2026-01-14 22:08:55 +0100
commitf93efff02151e4a3affa4b2d0031e957fe7553aa (patch)
tree806c714926848a98447d4fbc190c76ba891c0fd3
downloadqueue-f93efff02151e4a3affa4b2d0031e957fe7553aa.tar.zst
queue-f93efff02151e4a3affa4b2d0031e957fe7553aa.zip
initial commit
-rw-r--r--.clang-format4
-rw-r--r--.clangd2
-rw-r--r--.gitignore3
-rw-r--r--Makefile17
-rw-r--r--README.md6
-rw-r--r--compile_commands.json40
-rw-r--r--queue.h64
-rw-r--r--tests/ls_test.h288
-rw-r--r--tests/tests.c16
9 files changed, 440 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..a65181b
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,4 @@
+BasedOnStyle: WebKit
+BreakBeforeBraces: Attach
+SpaceAfterTemplateKeyword: false
+ColumnLimit: 80
diff --git a/.clangd b/.clangd
new file mode 100644
index 0000000..29788b8
--- /dev/null
+++ b/.clangd
@@ -0,0 +1,2 @@
+CompileFlags:
+ CompilationDatabase: .
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4e5193b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+tests/tests
+.cache/
+queue.o
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..7dc331d
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+tests/tests: queue.o tests/tests.c tests/ls_test.h
+ $(CC) -o $@ queue.o tests/tests.c -Itests -I.
+
+# Usually you wouldn't do this, but for tests we want this compiled with the
+# most pedantic settings.
+# Dont use this.
+queue.o: queue.h
+ $(CC) -c -x c -o $@ $^ -Wall -Wextra -Wpedantic -Werror -ansi \-std=c89 \
+ -DLS_QUEUE_IMPLEMENTATION \
+ -Wno-error=pragma-once-outside-header \
+ -Wno-pragma-once-outside-header
+
+.PHONY: clean
+
+clean:
+ rm -f tests/tests
+ rm -f queue.o
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..4fb18d3
--- /dev/null
+++ b/README.md
@@ -0,0 +1,6 @@
+# LS Ring-Buffer Queue
+
+Single-header queue implementation in ANSI C.
+
+- Header-only
+- ANSI C
diff --git a/compile_commands.json b/compile_commands.json
new file mode 100644
index 0000000..e0ef177
--- /dev/null
+++ b/compile_commands.json
@@ -0,0 +1,40 @@
+[
+ {
+ "arguments": [
+ "/usr/bin/cc",
+ "-c",
+ "-x",
+ "c",
+ "-Wall",
+ "-Wextra",
+ "-Wpedantic",
+ "-Werror",
+ "-ansi",
+ "-pedantic",
+ "-DLS_QUEUE_IMPLEMENTATION",
+ "-Wno-error=pragma-once-outside-header",
+ "-Wno-pragma-once-outside-header",
+ "-o",
+ "queue.o",
+ "queue.h"
+ ],
+ "directory": "/home/lion/src/ls/ls_queue",
+ "file": "/home/lion/src/ls/ls_queue/queue.h",
+ "output": "/home/lion/src/ls/ls_queue/queue.o"
+ },
+ {
+ "arguments": [
+ "/usr/bin/cc",
+ "-c",
+ "queue.o",
+ "-Itests",
+ "-I.",
+ "-o",
+ "tests/tests",
+ "tests/tests.c"
+ ],
+ "directory": "/home/lion/src/ls/ls_queue",
+ "file": "/home/lion/src/ls/ls_queue/tests/tests.c",
+ "output": "/home/lion/src/ls/ls_queue/tests/tests"
+ }
+]
diff --git a/queue.h b/queue.h
new file mode 100644
index 0000000..ad4230c
--- /dev/null
+++ b/queue.h
@@ -0,0 +1,64 @@
+#pragma once
+
+/* Lion's Standard (LS) ANSI C ring buffer queue.
+ *
+ * Version: 1.0
+ * Repo: https://github.com/lionkor/ls_queue
+ * SPDX-License-Identifier: MIT
+ *
+ * ==== TABLE OF CONTENTS ====
+ *
+ * 1. DESCRIPTION
+ * 2. HOW TO USE
+ * 3. LICENSE
+ *
+ * ==== 1. DESCRIPTION ====
+ *
+ * TODO
+ *
+ * ==== 2. HOW TO USE ====
+ *
+ * 1. Copy this file into your project and include it:
+ *
+ * #include "ls_queue.h"
+ *
+ * 2. In ONE C file, define LS_QUEUE_IMPLEMENTATION before including:
+ *
+ * #define LS_QUEUE_IMPLEMENTATION
+ * #include "ls_queue.h"
+ *
+ * 3. TODO
+ *
+ * TODO
+ *
+ * ==== 3. LICENSE ====
+ *
+ * This file is provided under the MIT license. For commercial support and
+ * maintenance, feel free to use the e-mail below to contact the author(s).
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2026 Lion Kortlepel <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the “Software”), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+typedef struct ls_queue {
+ int a;
+} ls_queue;
diff --git a/tests/ls_test.h b/tests/ls_test.h
new file mode 100644
index 0000000..a2a6f2a
--- /dev/null
+++ b/tests/ls_test.h
@@ -0,0 +1,288 @@
+#pragma once
+
+/* Lion's Standard (LS) test harness.
+ *
+ * Version: 1.0
+ * Repo: https://github.com/lionkor/ls_test
+ * SPDX-License-Identifier: MIT
+ *
+ * ==== TABLE OF CONTENTS ====
+ *
+ * 1. DESCRIPTION
+ * 2. HOW TO USE
+ * 3. LICENSE
+ *
+ * ==== 1. DESCRIPTION ====
+ *
+ * This is a super simple, minimal unit-test harness. It has auto-registering
+ * tests and some macros for easy usage.
+ *
+ * Compiles under ANSI C, the only special part is the extension __typeof__ if
+ * you use asserts other than `ASSERT` (e.g. ASSERT_EQ), and the constructor
+ * attribute __attribute__((destructor)) for automatic test registration.
+ *
+ * ==== 2. HOW TO USE ====
+ *
+ * 1. Copy this file into your project and include it:
+ *
+ * #include "ls_test.h"
+ *
+ * 2. In ONE C file, define LS_TEST_IMPLEMENTATION before including:
+ *
+ * #define LS_TEST_IMPLEMENTATION
+ * #include "ls_test.h"
+ *
+ * 3. Write tests as functions with no arguments/returns:
+ *
+ * TEST_CASE(test_add) {
+ * ASSERT_EQ(add(1, 2), 3, "%d");
+ * return 0; // needed for --failfast
+ * }
+ *
+ * Use unique names, avoid starting with ls_ or lst_.
+ *
+ * 4. Add a main:
+ *
+ * TEST_MAIN
+ *
+ * 5. Compile and run. Use --help for options.
+ *
+ * ==== 3. LICENSE ====
+ *
+ * This file is provided under the MIT license. For commercial support and
+ * maintenance, feel free to use the e-mail below to contact the author(s).
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2026 Lion Kortlepel <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the “Software”), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+
+#define TEST_MAIN \
+ int main(int argc, char** argv) { return ls_test_main(argc, argv); }
+
+#define TEST_CASE(name) \
+ static int LS_CAT(lst_t_, name)(void); \
+ static void LS_CAT(lst_init_, name)(void) LS_CONSTRUCTOR; \
+ static void LS_CAT(lst_init_, name)(void) { \
+ lst_reg(LS_CAT(lst_t_, name)); \
+ } \
+ static int LS_CAT(lst_t_, name)(void)
+
+#define ASSERT(cond) \
+ do { \
+ if (!(cond)) { \
+ const char* _func = __func__; \
+ if (strncmp(_func, "lst_t_", 6) == 0) \
+ _func += 6; \
+ fprintf(stderr, "%s: FAILED: %s (%s:%d)\n", _func, #cond, \
+ __FILE__, __LINE__); \
+ } \
+ } while (0)
+
+/* the following macros require __typeof__ */
+
+#define ASSERT_EQ(a, b, fmt) \
+ do { \
+ __typeof__(a) _a = (a); \
+ __typeof__(b) _b = (b); \
+ if (_a != _b) { \
+ const char* _func = __func__; \
+ if (strncmp(_func, "lst_t_", 6) == 0) \
+ _func += 6; \
+ fprintf(stderr, \
+ "%s: FAILED: %s == %s (actual: " fmt " != " fmt ") (%s:%d)\n", \
+ _func, #a, #b, _a, _b, __FILE__, __LINE__); \
+ ++lst_fail; \
+ return 1; \
+ } \
+ ++lst_ok; \
+ } while (0)
+
+#define ASSERT_NEQ(a, b, fmt) \
+ do { \
+ __typeof__(a) _a = (a); \
+ __typeof__(b) _b = (b); \
+ if (_a == _b) { \
+ const char* _func = __func__; \
+ if (strncmp(_func, "lst_t_", 6) == 0) \
+ _func += 6; \
+ fprintf(stderr, \
+ "%s: FAILED: %s != %s (actual: " fmt " == " fmt ") (%s:%d)\n", \
+ _func, #a, #b, _a, _b, __FILE__, __LINE__); \
+ ++lst_fail; \
+ return 1; \
+ } \
+ ++lst_ok; \
+ } while (0)
+
+#define ASSERT_LT(a, b, fmt) \
+ do { \
+ __typeof__(a) _a = (a); \
+ __typeof__(b) _b = (b); \
+ if (!(_a < _b)) { \
+ const char* _func = __func__; \
+ if (strncmp(_func, "lst_t_", 6) == 0) \
+ _func += 6; \
+ fprintf(stderr, \
+ "%s: FAILED: %s < %s (actual: " fmt " >= " fmt ") (%s:%d)\n", \
+ _func, #a, #b, _a, _b, __FILE__, __LINE__); \
+ ++lst_fail; \
+ return 1; \
+ } \
+ ++lst_ok; \
+ } while (0)
+
+#define ASSERT_LE(a, b, fmt) \
+ do { \
+ __typeof__(a) _a = (a); \
+ __typeof__(b) _b = (b); \
+ if (!(_a <= _b)) { \
+ const char* _func = __func__; \
+ if (strncmp(_func, "lst_t_", 6) == 0) \
+ _func += 6; \
+ fprintf(stderr, \
+ "%s: FAILED: %s <= %s (actual: " fmt " > " fmt ") (%s:%d)\n", \
+ _func, #a, #b, _a, _b, __FILE__, __LINE__); \
+ ++lst_fail; \
+ return 1; \
+ } \
+ ++lst_ok; \
+ } while (0)
+
+#define ASSERT_GT(a, b, fmt) \
+ do { \
+ __typeof__(a) _a = (a); \
+ __typeof__(b) _b = (b); \
+ if (!(_a > _b)) { \
+ const char* _func = __func__; \
+ if (strncmp(_func, "lst_t_", 6) == 0) \
+ _func += 6; \
+ fprintf(stderr, \
+ "%s: FAILED: %s > %s (actual: " fmt " <= " fmt ") (%s:%d)\n", \
+ _func, #a, #b, _a, _b, __FILE__, __LINE__); \
+ ++lst_fail; \
+ return 1; \
+ } \
+ ++lst_ok; \
+ } while (0)
+
+#define ASSERT_GE(a, b, fmt) \
+ do { \
+ __typeof__(a) _a = (a); \
+ __typeof__(b) _b = (b); \
+ if (!(_a >= _b)) { \
+ const char* _func = __func__; \
+ if (strncmp(_func, "lst_t_", 6) == 0) \
+ _func += 6; \
+ fprintf(stderr, \
+ "%s: FAILED: %s >= %s (actual: " fmt " < " fmt ") (%s:%d)\n", \
+ _func, #a, #b, _a, _b, __FILE__, __LINE__); \
+ ++lst_fail; \
+ return 1; \
+ } \
+ ++lst_ok; \
+ } while (0)
+#define LS_CAT2(a, b) a##b
+#define LS_CAT(a, b) LS_CAT2(a, b)
+
+#if defined(__GNUC__) || defined(__clang__)
+#define LS_CONSTRUCTOR __attribute__((constructor))
+#else
+#error "Requires __attribute__((constructor)) support"
+#endif
+
+typedef int (*lst_func)(void);
+
+extern lst_func* lst_funcs;
+extern int lst_n;
+extern int lst_cap;
+extern int lst_fail;
+extern int lst_ok;
+
+void lst_reg(lst_func f);
+
+#ifdef LS_TEST_IMPLEMENTATION
+#include <stdlib.h>
+#include <string.h>
+
+lst_func* lst_funcs;
+int lst_n;
+int lst_cap;
+int lst_fail = 0;
+int lst_ok = 0;
+
+void lst_reg(lst_func f) {
+ if (lst_n == lst_cap) {
+ if (lst_cap == 0) {
+ lst_cap = 8;
+ } else {
+ lst_cap *= 2;
+ }
+ lst_funcs = (lst_func*)realloc(lst_funcs, lst_cap * sizeof(*lst_funcs));
+ }
+ lst_funcs[lst_n++] = f;
+}
+
+#define HELP_STR \
+ "Usage: %s [options]\n" \
+ "Options:\n" \
+ " --failfast Stop after the first failed test\n" \
+ " --help Show this help message\n"
+
+static int ls_test_main(int argc, char** argv) {
+ (void)argc;
+ (void)argv;
+
+ int failfast = 0;
+ int i;
+
+ for (i = 1; i < argc; ++i) {
+ if (strcmp(argv[i], "--failfast") == 0) {
+ failfast = 1;
+ } else if (strcmp(argv[i], "--help") == 0) {
+ fprintf(stderr, HELP_STR, argv[0]);
+ return 0;
+ } else {
+ fprintf(
+ stderr, "unknown argument: %s\n\n" HELP_STR, argv[i], argv[0]);
+ return 1;
+ }
+ }
+
+ for (i = 0; i < lst_n; ++i) {
+ if (lst_funcs[i]() != 0 && failfast) {
+ goto end;
+ }
+ }
+
+end:
+ fprintf(stderr, "%d succeeded, %d failed, %d total\n", lst_ok, lst_fail,
+ lst_ok + lst_fail);
+
+ if (lst_fail > 0) {
+ return 1;
+ }
+
+ return 0;
+}
+#endif
diff --git a/tests/tests.c b/tests/tests.c
new file mode 100644
index 0000000..0d57df4
--- /dev/null
+++ b/tests/tests.c
@@ -0,0 +1,16 @@
+#define LS_TEST_IMPLEMENTATION
+#include "ls_test.h"
+
+int add(int a, int b) {
+ return a + b;
+}
+
+TEST_CASE(add) {
+ ASSERT_EQ(add(1, 2), 3, "%d");
+ ASSERT_EQ(add(2, 3), 5, "%d");
+ ASSERT_EQ(add(0, 0), 100000, "%d");
+
+ return 0;
+}
+
+TEST_MAIN