From 5a02d6537df42f370ad9224c4b4fa94dad99ba92 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sat, 28 Dec 2024 21:48:32 +0100 Subject: [PATCH 1/4] Tests --- Makefile | 23 ++++++++++++------- test/test.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 test/test.c diff --git a/Makefile b/Makefile index 861b543..4c5b92b 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ # Compiler -CC := gcc - -GITHASH := $(shell git rev-parse --short HEAD) +CC ?= gcc +# GITHASH := $(shell git rev-parse --short HEAD | echo "") +# # Compiler flags -CFLAGS := -Wall -Wextra -Wpedantic -std=c2x +CFLAGS := -Wall -Wextra -Wpedantic -Wunused -std=c99 -Isrc # Directories SRC_DIR := src @@ -25,6 +25,14 @@ TARGET := $(BUILD_DIR)/CTree # Default target all: $(TARGET) +test/test.o: test/test.c + +test/test.elf: test/test.o src/tree.o + $(CC) $(CFLAGS) $^ -o $@ + +test: test/test.elf + ./test/test.elf + # Rule to build the target executable $(TARGET): $(OBJS) $(CC) $(CFLAGS) $^ -o $@ @@ -41,18 +49,17 @@ run: $(TARGET) fmt: clang-format -i $(SRCS) $(HEADERS) -# Clean rule clean: - rm -rf $(BUILD_DIR) $(TARGET) + rm -rf $(BUILD_DIR) $(TARGET) test/test.o tester docs docs: PROJECT_NUMBER=git-$(GITHASH) doxygen Doxyfile cppcheck: - cppcheck --enable=all --inconclusive --std=c11 --language=c --platform=unix64 --suppress=missingIncludeSystem $(SRCS) + cppcheck --enable=all --inconclusive --std=c99 --language=c --platform=unix64 --suppress=missingIncludeSystem $(SRCS) test/test.c valgrind: $(TARGET) valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./$(TARGET) # Mark rules as phony -.PHONY: all run clean docs docs +.PHONY: all run clean docs docs test diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..907571e --- /dev/null +++ b/test/test.c @@ -0,0 +1,66 @@ +#include "tree.h" + +// Function prototypes for helper functions +void test_insert(Tree *tree); +void test_remove(Tree *tree); +void test_contains(Tree *tree); +void test_inorder(Tree *tree); + +int main() { + Tree tree = { .root = NULL }; + + printf("\n-- Testing Insert --\n"); + test_insert(&tree); + + printf("\n-- Testing Contains --\n"); + test_contains(&tree); + + printf("\n-- Testing Inorder Traversal --\n"); + test_inorder(&tree); + + printf("\n-- Testing Remove --\n"); + test_remove(&tree); + + printf("\n-- Clearing Tree --\n"); + tree_clear(&tree); + printf("Tree cleared.\n"); + + return 0; +} + +void test_insert(Tree *tree) { + int values[] = {10, 5, 15, 3, 7, 12, 18}; + for (int i = 0; i < 7; i++) { + printf("Inserting %d\n", values[i]); + tree_insert(tree, values[i]); + } +} + +void test_remove(Tree *tree) { + int values_to_remove[] = {15, 5, 10}; + for (int i = 0; i < 3; i++) { + printf("Removing %d\n", values_to_remove[i]); + if (tree_remove(&tree->root, values_to_remove[i]) == 0) { + printf("Successfully removed %d\n", values_to_remove[i]); + } else { + printf("Failed to remove %d\n", values_to_remove[i]); + } + } + printf("Remaining elements (in-order traversal):\n"); + tree_inorder(tree->root); + printf("\n"); +} + +void test_contains(Tree *tree) { + int values_to_check[] = {7, 15, 20}; + for (int i = 0; i < 3; i++) { + printf("Checking if tree contains %d: %s\n", values_to_check[i], + tree_contains(tree->root, values_to_check[i]) ? "Yes" : "No"); + } +} + +void test_inorder(Tree *tree) { + printf("Tree elements (in-order traversal):\n"); + tree_inorder(tree->root); + printf("\n"); +} From 8c0322456d93d102c568043415650970ee37c9fe Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sat, 28 Dec 2024 21:48:39 +0100 Subject: [PATCH 2/4] Ignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 6f50b8a..12d0fb0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ docs zig-out zig-cache .zig-cache +*.o +*.elf From f7e946afa91558e9c0668290410288252c8ee934 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sat, 28 Dec 2024 21:48:56 +0100 Subject: [PATCH 3/4] Formatting --- src/tree.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tree.h b/src/tree.h index c89a820..db2e282 100644 --- a/src/tree.h +++ b/src/tree.h @@ -56,4 +56,4 @@ bool tree_contains(Node *node, int data); // Print the tree in order void tree_inorder(Node *node); -#endif // TREE_H \ No newline at end of file +#endif // TREE_H From 2010faadd6c6134a6229beb84ca566d373ae0f68 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sat, 28 Dec 2024 22:02:21 +0100 Subject: [PATCH 4/4] Woodpecker testing --- .woodpecker/test.yaml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .woodpecker/test.yaml diff --git a/.woodpecker/test.yaml b/.woodpecker/test.yaml new file mode 100644 index 0000000..9eece92 --- /dev/null +++ b/.woodpecker/test.yaml @@ -0,0 +1,20 @@ +when: + - event: push + branch: master + +steps: + - name: build + image: debian + commands: + - apt update + - apt install libcmocka-dev -y + - apt install gcc -y + - apt install make -y + - make -j$(nproc) test/test.elf + + - name: run-tests + image: debian + commands: + - apt update + - apt install libcmocka0 -y + - ./test/test.elf