Add some simple tests

This commit is contained in:
2026-08-26 21:09:07 +02:00
parent 321284dec6
commit dbfb54fc7a
5 changed files with 89 additions and 1 deletions
+3
View File
@@ -1 +1,4 @@
.DS_Store
tests/build/
build/
+15
View File
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.16)
project(tests LANGUAGES C)
enable_testing()
add_executable(test_vector
tests/test_vector.c
)
add_executable(test_set
tests/test_set.c
)
add_test(NAME test_vector COMMAND test_vector)
add_test(NAME test_set COMMAND test_set)
+2
View File
@@ -17,6 +17,8 @@ Literally just copy the header file you want and let your favourite semi-usable
## Contributing
If you want to contribute, please make a pull request and I'll review it. If you want to contribute a new type or function, please make sure it is useful and not just a "cool" thing.
If you change an existing type, please run **make** in *build/* directory and **ctest** to ensure compatibility hasn't been broken.
If it's new, please write a **test** for it in *tests/test_<name>.c* so others can see the guidelines on how the type is used.
## Legal Mumbo Jumbo
Licensed under the MIT License (because GPL doesn't make sense for this (sorry Stallman))
+34
View File
@@ -0,0 +1,34 @@
#include <stdio.h>
#include "../set.h"
int main(void) {
set_t* set = set_create(sizeof(int));
if (!set) {
fprintf(stderr, "Failed to create set\n");
return 1;
}
for (int i = 0; i < 10; ++i) {
if (set_insert(set, &i) != 0) {
fprintf(stderr, "Failed to insert %d\n", i);
set_destroy(&set);
return 1;
}
}
for (size_t i = 0; i < set_size(set); ++i) {
int* value = (int*)set_get_const(set, i);
if (value) {
printf("set[%zu] = %d\n", i, *value);
} else {
fprintf(stderr, "Failed to get value at index %zu\n", i);
}
}
set_destroy(&set);
if (set != NULL) {
fprintf(stderr, "Set pointer was not set to NULL after destruction\n");
return 1;
}
return 0;
}
+34
View File
@@ -0,0 +1,34 @@
#include <stdio.h>
#include "../vector.h"
int main(void) {
vector_t* vec = vector_create(sizeof(int));
if (!vec) {
fprintf(stderr, "Failed to create vector\n");
return 1;
}
for (int i = 0; i < 10; ++i) {
if (vector_push_back(vec, &i) != 0) {
fprintf(stderr, "Failed to push back %d\n", i);
vector_destroy(&vec);
return 1;
}
}
for (size_t i = 0; i < vector_size(vec); ++i) {
int* value = (int*)vector_get(vec, i);
if (value) {
printf("vec[%zu] = %d\n", i, *value);
} else {
fprintf(stderr, "Failed to get value at index %zu\n", i);
}
}
vector_destroy(&vec);
if (vec != NULL) {
fprintf(stderr, "Vector pointer was not set to NULL after destruction\n");
return 1;
}
return 0;
}