From a9c37d2ad50e022f9d40331548d090e31fa2d307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Mon, 22 Apr 2013 14:53:16 +0000 Subject: [PATCH] paysages: More unit testing git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@557 b1fd45b6-86a6-48da-8261-f70d1f35bdcc --- config.vim | 27 +++++++++++---------------- src/rendering/tools/euclid.c | 15 ++++++--------- src/testing/common.h | 21 ++++++++++++++++++++- src/testing/euclid.c | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 69 insertions(+), 27 deletions(-) diff --git a/config.vim b/config.vim index cf2b39e..9544134 100644 --- a/config.vim +++ b/config.vim @@ -1,23 +1,18 @@ +function! UpdateTags() + !ctags --recurse=yes ./src/ +endfunction + function! RunDebug() - silent !ctags src/*.c src/shared/*.h - silent !cmake -D CMAKE_BUILD_TYPE:STRING=Debug . - !make && ./paysages + UpdateTags() + !make BUILDMODE=debug all run_qt endfunction function! RunRelease() - silent !ctags src/*.c src/shared/*.h - silent !cmake -D CMAKE_BUILD_TYPE:STRING=Release . - !make && ./paysages + UpdateTags() + !make BUILDMODE=debug all run_qt endfunction -function! RunProfile() - silent !ctags src/*.c src/shared/*.h - silent !cmake -D CMAKE_BUILD_TYPE:STRING=Profile . - !make && ./paysages && gprof -endfunction - -map :call RunDebug() -map :call RunProfile() -map :call RunRelease() -map :!eog output/resultaa.png +map :call RunDebug() +map :call RunRelease() +map :!make tests diff --git a/src/rendering/tools/euclid.c b/src/rendering/tools/euclid.c index 4ff94a1..7dc3f2a 100644 --- a/src/rendering/tools/euclid.c +++ b/src/rendering/tools/euclid.c @@ -421,16 +421,13 @@ double euclidGet2DAngle(double x, double y) { return 0.0; } + else if (y < 0.0) + { + return 3.0 * M_PI_2; + } else { - if (y < 0.0) - { - return 3.0 * M_PI_2; - } - else - { - return M_PI_2; - } + return M_PI_2; } } @@ -443,7 +440,7 @@ double euclidGet2DAngle(double x, double y) { ret = M_PI - ret; } - return fmod(ret, 2.0 * M_PI); + return ret < 0.0 ? ret + 2.0 * M_PI : ret; } Vector3 euclidGetNormalFromTriangle(Vector3 center, Vector3 bottom, Vector3 right) diff --git a/src/testing/common.h b/src/testing/common.h index cd205bd..3aabbf5 100644 --- a/src/testing/common.h +++ b/src/testing/common.h @@ -1,9 +1,12 @@ #ifndef _PAYSAGES_TESTING_COMMON_H_ #define _PAYSAGES_TESTING_COMMON_H_ +#include +#include #include #include #include +#include static inline void _add_methods_to_case(TCase* tc, ...) { @@ -33,8 +36,24 @@ static inline int _double_not_equals(double x, double y) return fabs(x - y) >= 0.00000000001; } -#define _ck_assert_double(F, X, O, Y) ck_assert_msg(F(X, Y), "Assertion '"#X#O#Y"' failed: "#X"==%f, "#Y"==%f", X, Y) +#define _ck_assert_double(F, X, O, Y) ck_assert_msg(F(X, Y), "Assertion '"#X#O#Y"' failed: "#X"=%f, "#Y"=%f", X, Y) #define ck_assert_double_eq(X, Y) _ck_assert_double(_double_equals, X, ==, Y) #define ck_assert_double_ne(X, Y) _ck_assert_double(_double_not_equals, X, !=, Y) + +/***** Generic comparison assertions *****/ +#define DEFINE_COMPARE_ASSERT(_type_, _cmpfunc_, _strfunc_) \ +static inline int _ck_gen_##_type_##_cmp(_type_ X, _type_ Y) { \ + return _cmpfunc_(X, Y); \ +} \ +static inline char* _ck_gen_##_type_##_str(char* buffer, _type_ X) { \ + _strfunc_(X, buffer, 99); \ + buffer[100] = '\0'; \ + return buffer; \ +} +#define ck_assert_generic_eq(_type_, X, Y) ck_assert_msg(_ck_gen_##_type_##_cmp(X, Y) == 0, "Assertion '"#X"=="#Y"' failed : "#X"=%s, "#Y"=%s", _ck_gen_##_type_##_str(_ck_gen_strbuf1, X), _ck_gen_##_type_##_str(_ck_gen_strbuf2, Y)) + +static char _ck_gen_strbuf1[101]; +static char _ck_gen_strbuf2[101]; + #endif diff --git a/src/testing/euclid.c b/src/testing/euclid.c index 0da69d6..7cd29dc 100644 --- a/src/testing/euclid.c +++ b/src/testing/euclid.c @@ -1,6 +1,16 @@ #include "testing/common.h" #include "rendering/tools/euclid.h" +static inline int _Vector3_cmp(Vector3 v1, Vector3 v2) +{ + return fabs(v1.x - v2.z) >= 0.00000000001 && fabs(v1.y - v2.y) >= 0.00000000001 && fabs(v1.z - v2.z) >= 0.00000000001; +} +static inline void _Vector3_str(Vector3 v, char* buffer, int length) +{ + snprintf(buffer, length, "(%f,%f,%f)", v.x, v.y, v.z); +} +DEFINE_COMPARE_ASSERT(Vector3, _Vector3_cmp, _Vector3_str); + START_TEST(test_euclid_angles) { ck_assert_double_eq(euclidGet2DAngle(0.0, 0.0), 0.0); @@ -28,4 +38,25 @@ START_TEST(test_euclid_angles) } END_TEST -TEST_CASE(euclid, test_euclid_angles) +START_TEST(test_vectors) +{ + Vector3 v1, v2, v3; + double d1, d2; + + /* Test scaling */ + v1.x = 0.0; v1.y = -4.3; v1.z = 8.2; + v2.x = 0.0; v2.y = -8.6; v2.z = 16.4; + v3.x = 0.0; v3.y = 2.15; v3.z = -4.1; + ck_assert_generic_eq(Vector3, v3Scale(v1, 2.0), v2); + ck_assert_generic_eq(Vector3, v3Scale(v1, -0.5), v3); + + /* Test euler angles */ + v1.x = v1.y = v1.z = 0.5; + v3ToEuler(v1, &d1, &d2); + ck_assert_double_eq(d1, M_PI_4); + ck_assert_double_eq(d2, M_PI_2 - 0.955316618125); +} +END_TEST + +TEST_CASE(euclid, test_euclid_angles, test_vectors) +