Skip to content
Snippets Groups Projects
Commit 8cc55d80 authored by tanguy.cavagna's avatar tanguy.cavagna :desktop:
Browse files

Added vector test file

parent de04dd0f
Branches
Tags
No related merge requests found
......@@ -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)
// 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment