Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}

7 changes: 1 addition & 6 deletions include/flex2d/vector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#ifndef __VECTOR_H__
#define __VECTOR_H_

typedef struct Vector_t Vector;
typedef struct Vector Point;

Expand All @@ -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 *);
44 changes: 44 additions & 0 deletions test/unit/flex2d/test_vector_new.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <flex2d/vector.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdarg.h>
#include <cmocka.h>
#include <math.h>
#include <stdlib.h>

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;
}