diff --git a/makefile b/makefile index b29a955348819c27d786df82e7ca24aad3f4faf2..28ef4a191d96b834a9398f3585e4a8f695fc7fea 100644 --- a/makefile +++ b/makefile @@ -38,9 +38,16 @@ PHONY += clean clean: rm -rf $(BIN) +PHONY += test-draw test-draw: draw/test.c $(BIN)/draw/draw.o $(BIN)/gfx/gfx.o $(CC) $(CFLAGS) -o $(BIN)/draw/$@.o -c $< $(LDFLAGS) - $(CC) $(CFLAGS) -o $(BIN)/draw/$@ $(BIN)/draw/$@.o $(BIN)/draw/draw.o $(BIN)/gfx/gfx.o $(LIBS) $(LDFLAGS) - ./$(BIN)/draw/$@ + $(CC) $(CFLAGS) -o $(BIN)/draw/test $(BIN)/draw/$@.o $(BIN)/draw/draw.o $(BIN)/gfx/gfx.o $(LIBS) $(LDFLAGS) + ./$(BIN)/draw/test + +PHONY += test-vector +test-vector: vector/test.c $(BIN)/vector/vector.o + $(CC) $(CFLAGS) -o $(BIN)/vector/test.o -c $< $(LDFLAGS) + $(CC) $(CFLAGS) -o $(BIN)/vector/test $(BIN)/vector/test.o $(BIN)/vector/vector.o -lcmocka $(LDFLAGS) + ./$(BIN)/vector/test .PHONY: $(PHONY) diff --git a/vector/test.c b/vector/test.c new file mode 100644 index 0000000000000000000000000000000000000000..0d675ffad7d66e5b3aa4441d1c5067ab8a8c5684 --- /dev/null +++ b/vector/test.c @@ -0,0 +1,96 @@ +// cmocka includes +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <stdint.h> +#include <cmocka.h> + +// vector include +#include <stdio.h> +#include <stdbool.h> +#include "vector.h" + +//* ==================== +//* Tests descriptions +//* ==================== +static void vector_initialization() { + vector v = {2, 3}; + + assert_non_null(&v); +} + +static void vector_addition() { + vector v1 = {2, 5}; + vector v2 = {3, 7}; + vector v3; + + vector target = {5, 12}; + + v3 = add_vector(v1, v2); + + assert_memory_equal(&v3, &target, sizeof(target)); +} + +static void vector_substraction() { + vector v1 = {2, 5}; + vector v2 = {3, 7}; + vector v3; + + vector target = {-1, -2}; + + v3 = sub_vector(v1, v2); + + assert_memory_equal(&v3, &target, sizeof(target)); +} + +static void vector_multiplication_scalar() { + vector v1 = {2, 5}; + int scal = 2; + vector v2; + + vector target = {4, 10}; + + v2 = mult_by_scalar(v1, scal); + + assert_memory_equal(&v2, &target, sizeof(target)); +} + +static void vector_to_polar_convertion() { + vector c_v = {84.85, -84.85}; + polarVector p_v; + + polarVector target = {119.996, -45}; + + p_v = to_polar(c_v); + + assert_float_equal(p_v.a, target.a, 1e-3); + assert_float_equal(p_v.f, target.f, 1e-3); +} + +static void vector_to_cartesian_convertion() { + polarVector p_v = {200, 60}; + vector c_v; + + vector target = {100, 173.205}; + + c_v = to_cartesian(p_v); + + assert_float_equal(c_v.x, target.x, 1e-3); + assert_float_equal(c_v.y, target.y, 1e-3); +} + +//* ==================== +//* Run test +//* ==================== +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(vector_initialization), + cmocka_unit_test(vector_addition), + cmocka_unit_test(vector_substraction), + cmocka_unit_test(vector_multiplication_scalar), + cmocka_unit_test(vector_to_polar_convertion), + cmocka_unit_test(vector_to_cartesian_convertion), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} \ No newline at end of file