diff --git a/LICENSE b/LICENSE index bd3e7eb..2e5bae4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Kyle Laker +Copyright (c) 2024 Laurel May Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Makefile b/Makefile index b51173b..582e287 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC=clang -override CFLAGS := -Wall -pedantic -std=c17 $(CFLAGS) +override CFLAGS := -Wall -pedantic -std=c23 $(CFLAGS) override LDFLAGS := $(LDFLAGS) SRCDIR = src diff --git a/README.md b/README.md index 853445e..2224deb 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,11 @@ HEAD->link ^= TAIL->link ## Installing +### Dependencies + +- `subunit` +- `libcheck` + A typical `make && sudo make install` will work on most Linux distros (though be careful! `/usr/` will be modified). For ArchLinux users, a `PKGBUILD` is provided. You can build with `makepkg -si` to install the latest released @@ -87,4 +92,4 @@ version. This package is not currently available in the Arch repos or the AUR. ## Notes Pointers are not integers. This very heavily treats pointers as if they were -in fact integers. That's a horrible idea. \ No newline at end of file +in fact integers. That's a horrible idea. diff --git a/compile_flags.txt b/compile_flags.txt index f1fcad3..6ee65ef 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,4 +1,4 @@ -Iinclude -Wall ---std=c17 +--std=c23 -pedantic diff --git a/include/list.h b/include/list.h index 78b4922..2736e34 100644 --- a/include/list.h +++ b/include/list.h @@ -1,6 +1,6 @@ /** * @file list.h - * @author Kyle Laker (kyle@laker.email) + * @author Laurel May (laurel@laurelmay.me) * * @copyright Copyright (c) 2022 * @@ -66,7 +66,7 @@ typedef struct typedef void (*element_destructor)(list_val_t); /* Exported list functions */ -list_t *list_create(); +list_t *list_create(void); void list_destroy(list_t *, element_destructor); int list_insert(list_t *, size_t, list_val_t); int list_append(list_t *, list_val_t); diff --git a/src/xorlist.c b/src/xorlist.c index e5bda44..de6829d 100644 --- a/src/xorlist.c +++ b/src/xorlist.c @@ -1,7 +1,7 @@ /** * @internal * @file xorlist.c - * @author Kyle Laker (kyle@laker.email) + * @author Laurel May (laurel@laurelmay.me) * * @copyright Copyright (c) 2022 * @@ -13,8 +13,8 @@ #include "list.h" #include +#include #include -#include #include #include @@ -55,17 +55,17 @@ static node_pair_t traverse_to_idx(list_t *, size_t); * returned list should not be passed to free directly and should be passed * to list_destroy(list_t *, element_destructor). */ -list_t *list_create() { +list_t *list_create(void) { list_t *list = malloc(sizeof(list_t)); node_t *head = malloc(sizeof(node_t)); node_t *tail = malloc(sizeof(node_t)); if (!list || !head || !tail) { - return NULL; + return nullptr; } - head->link = calc_new_ptr(NULL, NULL, tail); - tail->link = calc_new_ptr(head, NULL, NULL); + head->link = calc_new_ptr(nullptr, nullptr, tail); + tail->link = calc_new_ptr(head, nullptr, nullptr); list->head = head; list->tail = tail; @@ -85,7 +85,7 @@ list_t *list_create() { * \ref list_val_t as an argument (for example, `free(void *)`). * * @param list The list to tear down - * @param destroy A function to properly free list elements (or `NULL`) + * @param destroy A function to properly free list elements (or `nullptr`) */ void list_destroy(list_t *list, element_destructor destroy) { /* Destroy all remaining items in the list. */ @@ -99,8 +99,8 @@ void list_destroy(list_t *list, element_destructor destroy) { /* Free memory for list struct members. */ free(list->head); free(list->tail); - list->head = NULL; - list->tail = NULL; + list->head = nullptr; + list->tail = nullptr; free(list); } @@ -138,7 +138,7 @@ int list_insert(list_t *list, size_t idx, list_val_t value) { * @return int A non-zero value on failure */ int list_append(list_t *list, list_val_t value) { - return add_at_node(list, value, list_prev(list->tail, NULL), list->tail); + return add_at_node(list, value, list_prev(list->tail, nullptr), list->tail); } /** @@ -162,7 +162,7 @@ int list_enqueue(list_t *list, list_val_t value) { * @return int A non-zero value on failure */ int list_prepend(list_t *list, list_val_t value) { - return add_at_node(list, value, list->head, list_next(list->head, NULL)); + return add_at_node(list, value, list->head, list_next(list->head, nullptr)); } /** @@ -196,12 +196,12 @@ bool list_is_empty(list_t list) { * * @param list The list to remove from * @param idx The index to remove at - * @return list_val_t The item at that index (or NULL for an invalid index) + * @return list_val_t The item at that index (or nullptr for an invalid index) */ list_val_t list_delete(list_t *list, size_t idx) { node_pair_t nodes = traverse_to_idx(list, idx); if (!nodes.prev || !nodes.curr) { - return NULL; + return nullptr; } node_t *prev = nodes.prev; @@ -216,7 +216,7 @@ list_val_t list_delete(list_t *list, size_t idx) { list_val_t val = curr->value; free(curr); - curr = NULL; + curr = nullptr; list->size -= 1; return val; @@ -228,7 +228,7 @@ list_val_t list_delete(list_t *list, size_t idx) { * This is equivalent to list_delete(list_t *, size_t) with an index of 0. * * @param list The list to remove from - * @return list_val_t The item at the top of the stack (or NULL if empty) + * @return list_val_t The item at the top of the stack (or nullptr if empty) */ list_val_t list_pop(list_t *list) { return list_delete(list, 0); @@ -240,7 +240,7 @@ list_val_t list_pop(list_t *list) { * This is equivalent to list_delete(list_t *, size_t) with an index of 0. * * @param list The queue to remove from - * @return list_val_t The first item in the queue (or NULL if empty) + * @return list_val_t The first item in the queue (or nullptr if empty) */ list_val_t list_dequeue(list_t *list) { return list_delete(list, 0); @@ -255,12 +255,12 @@ list_val_t list_dequeue(list_t *list) { */ list_val_t list_get(list_t list, size_t idx) { if (idx >= list.size) { - return NULL; + return nullptr; } node_pair_t nodes = traverse_to_idx(&list, idx); if (!nodes.prev || !nodes.curr) { - return NULL; + return nullptr; } return nodes.curr->value; @@ -271,7 +271,7 @@ list_val_t list_get(list_t list, size_t idx) { * * @param list The stack/queue to peek at * @return list_val_t The item at the front of queue (or top of the stack) or - * NULL if empty + * nullptr if empty */ list_val_t list_peek(list_t list) { return list_get(list, 0); @@ -286,13 +286,13 @@ list_val_t list_peek(list_t list) { * @param list The list to modify * @param idx The index to modify * @param value The new value to set - * @return list_val_t The previous at the given index (or NULL if the index + * @return list_val_t The previous at the given index (or nullptr if the index * is invalid) */ list_val_t list_set(list_t *list, size_t idx, list_val_t value) { node_pair_t nodes = traverse_to_idx(list, idx); if (!nodes.curr) { - return NULL; + return nullptr; } list_val_t prior_value = nodes.curr->value; @@ -362,7 +362,7 @@ void list_reverse(list_t *list) { * @return ssize_t The index of `value` or -1 if not found */ ssize_t list_find(list_t list, list_val_t value) { - node_t *curr = list_next(list.head, NULL); + node_t *curr = list_next(list.head, nullptr); node_t *prev = list.head; size_t idx = 0; @@ -394,7 +394,7 @@ ssize_t list_find(list_t list, list_val_t value) { */ bool list_contains(list_t list, list_val_t value) { node_t *curr = list.head; - node_t *prev = NULL; + node_t *prev = nullptr; while (curr != list.tail) { node_t *next_node = list_next(curr, prev); @@ -424,14 +424,14 @@ static node_t *calc_new_ptr(void *a, void *b, void *c) { * Returns the next item in the list after curr. */ static node_t *list_next(node_t *curr, node_t *prev) { - return calc_new_ptr(prev, curr->link, NULL); + return calc_new_ptr(prev, curr->link, nullptr); } /** * Returns the previous item in the list before curr. */ static node_t *list_prev(node_t *curr, node_t *next) { - return calc_new_ptr(NULL, curr->link, next); + return calc_new_ptr(nullptr, curr->link, next); } /** @@ -447,7 +447,7 @@ static int add_at_node(list_t *list, list_val_t value, node_t *before, node_t *a new_node->value = value; /* Set the pointers to surrounding nodes. */ - new_node->link = calc_new_ptr(before, NULL, after); + new_node->link = calc_new_ptr(before, nullptr, after); after->link = calc_new_ptr(before, new_node, after->link); before->link = calc_new_ptr(before->link, new_node, after); @@ -468,7 +468,7 @@ static node_pair_t traverse_to_idx(list_t *list, size_t idx) { * If the index isn't valid, don't bother searching at all. */ if (idx > list->size) { - node_pair_t all_null = {.prev = NULL, .curr = NULL}; + node_pair_t all_null = {.prev = nullptr, .curr = nullptr}; return all_null; } @@ -489,7 +489,7 @@ static node_pair_t traverse_to_idx(list_t *list, size_t idx) { } /* Start traversing from the end until the needed index. */ - node_t *curr = list_next(starting_end, NULL); + node_t *curr = list_next(starting_end, nullptr); node_t *prev = starting_end; for (int i = 0; i < num_iter; i++) { diff --git a/tests/Makefile b/tests/Makefile index b89f61f..c242563 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -2,7 +2,7 @@ CC=clang -override CFLAGS := -g -O0 -Wall -pedantic -std=c17 $(CFLAGS) +override CFLAGS := -g -O0 -Wall -pedantic -std=c23 $(CFLAGS) override LDFLAGS := -g -O0 $(LDFLAGS) SRCDIR=../src diff --git a/tests/tests.c b/tests/tests.c index 305e693..8904784 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -1,6 +1,6 @@ #include +#include #include -#include #include @@ -22,7 +22,7 @@ START_TEST(LIST_CREATE) ck_assert(list->size == 0); - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -32,7 +32,7 @@ START_TEST(LIST_DESTROY) ck_assert(list); size_t items = 10; - for (size_t i = 0; i < items; i++) list_append(list, NULL); + for (size_t i = 0; i < items; i++) list_append(list, nullptr); destroy_count = 0; list_destroy(list, destroy_counter); @@ -68,8 +68,8 @@ START_TEST(LIST_INSERT) ck_assert_msg(!list_insert(list, 0, value), "Adding at a valid index (0) succeeds"); ck_assert(list_get(*list, 0) == value); - - list_destroy(list, NULL); + + list_destroy(list, nullptr); } END_TEST @@ -103,7 +103,7 @@ START_TEST(LIST_INSERT_INDEX) ck_assert(list_get(*list, i) == values + (i - 2)); } - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -117,7 +117,7 @@ START_TEST(LIST_APPEND) ck_assert(!list_append(list, &one)); ck_assert(list_get(*list, 0) == &one); - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -130,7 +130,7 @@ START_TEST(LIST_ENQUEUE) ck_assert(!list_enqueue(list, &one)); ck_assert(list_get(*list, 0) == &one); - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -145,7 +145,7 @@ START_TEST(LIST_PREPEND) ck_assert(!list_prepend(list, &one)); ck_assert(list_get(*list, 0) == &one); - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -160,7 +160,7 @@ START_TEST(LIST_PUSH) ck_assert(!list_push(list, &one)); ck_assert(list_get(*list, 0) == &one); - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -180,7 +180,7 @@ START_TEST(LIST_EMPTY) ck_assert(list_is_empty(*list)); - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -195,7 +195,7 @@ START_TEST(LIST_DELETE) ck_assert(list->size == 1); list_delete(list, 0); ck_assert(list->size == 0); - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -213,7 +213,7 @@ START_TEST(LIST_DEQUEUE) for (int i = 0; i < 5; i++) { ck_assert(list_dequeue(list) == values + i); } - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -231,7 +231,7 @@ START_TEST(LIST_POP) for (int i = 0; i < 5; i++) { ck_assert(list_pop(list) == values + (4 - i)); } - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -239,7 +239,7 @@ END_TEST START_TEST(LIST_GET) { list_t *list = list_create(); - ck_assert(list_get(*list, 0) == NULL); + ck_assert(list_get(*list, 0) == nullptr); data_t values[10]; for (int i = 0; i < 10; i++) { @@ -251,7 +251,7 @@ START_TEST(LIST_GET) ck_assert(list_get(*list, i) == values + i); } - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -259,7 +259,7 @@ END_TEST START_TEST(LIST_PEEK) { list_t *list = list_create(); - ck_assert(list_peek(*list) == NULL); + ck_assert(list_peek(*list) == nullptr); for (int i = 0; i < 10; i++) { list_append(list, &(data_t) { .val = i }); @@ -269,7 +269,7 @@ START_TEST(LIST_PEEK) ck_assert(list_peek(*list) == list_pop(list)); } - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -292,7 +292,7 @@ START_TEST(LIST_SET) ck_assert(list_get(*list, i) == values + 10 - i); } - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -316,7 +316,7 @@ START_TEST(LIST_SIZE) { ck_assert(list_size(*list) == items); - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -344,7 +344,7 @@ START_TEST(LIST_REMOVE) ck_assert(list_size(*list) == 0); - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -368,7 +368,7 @@ START_TEST(LIST_FIND) ck_assert(idx == i); } - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST @@ -391,7 +391,7 @@ START_TEST(LIST_CONTAINS) ck_assert(list_contains(*list, val)); } - list_destroy(list, NULL); + list_destroy(list, nullptr); } END_TEST diff --git a/tests/testsuite.c b/tests/testsuite.c index 2bdc941..4cdc528 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include @@ -15,16 +15,16 @@ Suite * test_suite (void) { return s; } -void run_testsuite() { - Suite *s = test_suite (); - SRunner *sr = srunner_create (s); - srunner_run_all (sr, CK_NORMAL); - srunner_free (sr); +void run_testsuite(void) { + Suite *s = test_suite(); + SRunner *sr = srunner_create(s); + srunner_run_all(sr, CK_NORMAL); + srunner_free(sr); } int main(void) { - srand((unsigned)time(NULL)); - run_testsuite (); + srand((unsigned) time(nullptr)); + run_testsuite(); return EXIT_SUCCESS; }