diff --git a/.gitignore b/.gitignore index a61007e2..0fbbc7b2 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,8 @@ _codeql_detected_source_root /tests/bitset/build /tests/list/build /tests/pool/build -doc +/tests/stack/build tests/util/.vscode tests/util/build audio/*.bak +doc diff --git a/include/data_structures/stack.h b/include/data_structures/stack.h new file mode 100644 index 00000000..46767a08 --- /dev/null +++ b/include/data_structures/stack.h @@ -0,0 +1,26 @@ +/** + * @file stack.h + * + * @brief Stack implementation, no frills + */ +#ifndef STACK_H +#define STACK_H + +#define STACK_DEFINE(name, size) \ + static void* name##_data_array[size]; \ + static Stack name = { .data_array = name##_data_array, .top = -1, .max = size }; + +typedef struct +{ + void** data_array; + int top; + int max; +} Stack; + +void stack_reset(Stack* stack); +bool stack_empty(Stack* stack); +int stack_len(Stack* stack); +void stack_push(Stack* stack, void* data); +void* stack_pop(Stack* stack); + +#endif // STACK_H diff --git a/source/data_structures/stack.c b/source/data_structures/stack.c new file mode 100644 index 00000000..73f6adfe --- /dev/null +++ b/source/data_structures/stack.c @@ -0,0 +1,29 @@ +#include "stack.h" + +#include "stddef.h" + +void stack_reset(Stack* stack) +{ + stack->top = -1; +} + +bool stack_empty(Stack* stack) +{ + return stack->top < 0; +} + +int stack_len(Stack* stack) +{ + +} + +void stack_push(Stack* stack, void* data) +{ + if (stack->top < stack->max) + stack->data_array[++stack->top] = data; +} + +void* stack_pop(Stack* stack) +{ + return (stack->top < 0) ? NULL : stack->data_array[stack->top--]; +} diff --git a/tests/list/Makefile b/tests/list/Makefile index 4ecd5123..9fddc9af 100644 --- a/tests/list/Makefile +++ b/tests/list/Makefile @@ -1,4 +1,3 @@ - CC := gcc CFLAGS := -I../../include -I../../include/data_structures -I. \ -g -O3 -std=gnu23 -Wall -Werror -DPOOLS_TEST_ENV=yes diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 93ba3ecb..6adc73c5 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -17,6 +17,7 @@ run_test() { } run_test bitset -run_test pool run_test list +run_test pool +run_test stack run_test util diff --git a/tests/stack/Makefile b/tests/stack/Makefile new file mode 100644 index 00000000..c166c048 --- /dev/null +++ b/tests/stack/Makefile @@ -0,0 +1,16 @@ + +CC := gcc +CFLAGS := -I../../include -I../../include/data_structures -I. \ + -g -O3 -std=gnu23 -Wall -Werror +SRC := stack_test.c \ + ../../source/data_structures/stack.c +OUT := build/stack_test + +$(OUT): $(SRC) | build + $(CC) $(CFLAGS) -o $@ $^ + +build: + mkdir -p build + +clean: + rm -f $(OUT) diff --git a/tests/stack/stack_test.c b/tests/stack/stack_test.c new file mode 100644 index 00000000..22ddd1b8 --- /dev/null +++ b/tests/stack/stack_test.c @@ -0,0 +1,41 @@ +#include "stack.h" + +#include +#include +#include + +#define STACK_SIZE 64 + +STACK_DEFINE(coolstack, STACK_SIZE) + +void test_stack(void) +{ + int int_arr[STACK_SIZE]; + + for(int i = 0; i < STACK_SIZE; i++) + { + int_arr[i] = i; + } + + for(int i = 0; i < STACK_SIZE; i++) + { + stack_push(&coolstack, &int_arr[i]); + } + + for(int i = STACK_SIZE - 1; i >= 0; i--) + { + assert(*(int*)stack_pop(&coolstack) == i); + } +} + +int main(void) +{ + printf("Testing Stack.\n"); + test_stack(); + + printf("-------------------------------------------------------------------------------\n"); + printf("Stack Tests Passed :)\n"); + printf("-------------------------------------------------------------------------------\n"); + + return 0; +}