From 8cc55d80027c607f3655058ec37f307fbc7f29d4 Mon Sep 17 00:00:00 2001 From: "tanguy.cavagna" <tanguy.cavagna@etu.hesge.ch> Date: Mon, 12 Jul 2021 18:44:23 +0200 Subject: [PATCH] Added vector test file --- makefile | 11 ++++-- vector/test.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 vector/test.c diff --git a/makefile b/makefile index b29a955..28ef4a1 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 0000000..0d675ff --- /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 -- GitLab