From d43075327699725f8704edbddf1e7d6407f08b7f Mon Sep 17 00:00:00 2001 From: Edson Michaque Date: Wed, 24 Jun 2020 05:58:52 +0200 Subject: [PATCH] Add tests for vector_new, vector_distance and vector_distance functions --- Makefile | 28 ++++++++++++++++--- include/flex2d/vector.h | 7 +---- test/unit/flex2d/test_vector_new.c | 44 ++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 test/unit/flex2d/test_vector_new.c diff --git a/Makefile b/Makefile index 656f91d..a1306fe 100644 --- a/Makefile +++ b/Makefile @@ -2,15 +2,35 @@ CC=gcc INCLUDE_DIR=include BUILD_DIR=build SRC_DIR=src/flex2d - +INCLUDE_TEST=build/include +CMOCKA_LIBRARY_DIR=build/lib64 +VECTOR_LIBRARY_DIR=build +LD_LIBRARY_PATH=${VECTOR_LIBRARY_DIR}:${CMOCKA_LIBRARY_DIR} all: compile compile: vector contrib - ${CC} -shared -o ${BUILD_DIR}/vector.so -lm + ${CC} -shared -o ${BUILD_DIR}/libvector.so ${BUILD_DIR}/vector.o -lm vector: - ${CC} -c -o ${BUILD_DIR}/vector.o ${SRC_DIR}/vector.c -I ${INCLUDE_DIR} -lm + ${CC} -c -fPIC -o ${BUILD_DIR}/vector.o ${SRC_DIR}/vector.c -I ${INCLUDE_DIR} -lm contrib: cd contrib && $(MAKE) - + +compile-test: test/unit/flex2d/test_vector_new.c + ${CC} -o ${BUILD_DIR}/test \ + test/unit/flex2d/test_vector_new.c \ + -I ${INCLUDE_TEST} \ + -I ${INCLUDE_DIR} \ + -L ${CMOCKA_LIBRARY_DIR} \ + -lcmocka \ + -L ${VECTOR_LIBRARY_DIR} \ + -lvector \ + -lm + +test: compile-test + LD_LIBRARY_PATH=${LD_LIBRARY_PATH} ${BUILD_DIR}/test + +clean: + rm -rf build/*.{o,so} + diff --git a/include/flex2d/vector.h b/include/flex2d/vector.h index 6c3bc6b..70e73db 100644 --- a/include/flex2d/vector.h +++ b/include/flex2d/vector.h @@ -1,6 +1,3 @@ -#ifndef __VECTOR_H__ -#define __VECTOR_H_ - typedef struct Vector_t Vector; typedef struct Vector Point; @@ -11,6 +8,4 @@ struct Vector_t Vector *vector_new(double, double); double vector_distance_squared(Vector *); -double distance(Vector *); - -#endif /* __VECTOR_H__ */ +double vector_distance(Vector *); diff --git a/test/unit/flex2d/test_vector_new.c b/test/unit/flex2d/test_vector_new.c new file mode 100644 index 0000000..bb2c0c7 --- /dev/null +++ b/test/unit/flex2d/test_vector_new.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include + +void test_vector_non_null(void **state) +{ + Vector *v = vector_new(0, 0); + assert_non_null(v); +} + +void test_vector_distance_squared(void **state) +{ + double x = 10.0, y = 10.0; + Vector *v = vector_new(x, y); + double distance = x*x + y*y; + double epsilon = 1.0e-9; + assert_float_equal(vector_distance_squared(v), distance, epsilon); +} + +void test_vector_distance(void **state) +{ + double x = 10.0, y = 10.0; + Vector *v = vector_new(x, y); + double distance = sqrt(x*x + y*y); + double epsilon = 1.0e-9; + assert_float_equal(vector_distance(v), distance, epsilon); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_vector_non_null), + cmocka_unit_test(test_vector_distance), + cmocka_unit_test(test_vector_distance_squared) + }; + + int failed_tests = cmocka_run_group_tests(tests, NULL, NULL); + + return failed_tests; +}