added test, comparing operation results using gph-math as reference
authorJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 11 Oct 2018 10:57:11 +0000 (13:57 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 11 Oct 2018 10:57:11 +0000 (13:57 +0300)
src/cgmmat.inl
src/cgmvec3.inl
test/Makefile [new file with mode: 0644]
test/test.cc [new file with mode: 0644]

index 4ae803a..c1b44fa 100644 (file)
@@ -442,7 +442,7 @@ static inline void cgm_mlookat(float *m, const cgm_vec3 *pos, const cgm_vec3 *ta
                const cgm_vec3 *up)
 {
        float rot[16], trans[16];
                const cgm_vec3 *up)
 {
        float rot[16], trans[16];
-       cgm_vec3 dir = targ, right, vup;
+       cgm_vec3 dir = *targ, right, vup;
 
        cgm_vsub(&dir, pos);
        cgm_vnormalize(&dir);
 
        cgm_vsub(&dir, pos);
        cgm_vnormalize(&dir);
index 11a8849..429c34e 100644 (file)
@@ -155,7 +155,7 @@ static inline void cgm_vrotate_axis(cgm_vec3 *v, int axis, float angle)
 {
        float m[16];
        cgm_mrotation_axis(m, axis, angle);
 {
        float m[16];
        cgm_mrotation_axis(m, axis, angle);
-       cgm_vmul_m3x3(v, m);
+       cgm_vmul_m3v3(v, m);
 }
 
 static inline void cgm_vrotate(cgm_vec3 *v, float angle, float x, float y, float z)
 }
 
 static inline void cgm_vrotate(cgm_vec3 *v, float angle, float x, float y, float z)
diff --git a/test/Makefile b/test/Makefile
new file mode 100644 (file)
index 0000000..c23e3f1
--- /dev/null
@@ -0,0 +1,12 @@
+obj = test.o
+bin = test
+
+CXXFLAGS = -pedantic -Wall -g -I../src
+LDFLAGS = -lgmath -lm
+
+$(bin): $(obj)
+       $(CXX) -o $@ $(obj) $(LDFLAGS)
+
+.PHONY: clean
+clean:
+       rm -f $(obj) $(bin)
diff --git a/test/test.cc b/test/test.cc
new file mode 100644 (file)
index 0000000..640cd18
--- /dev/null
@@ -0,0 +1,94 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <assert.h>
+#include <gmath/gmath.h>
+#include "cgmath.h"
+
+bool test_vec3();
+bool test_mat();
+float frand(float low, float high);
+
+int main(void)
+{
+       srand(time(0));
+
+       if(!test_vec3()) {
+               fprintf(stderr, "test_vec3 failed\n");
+               return 1;
+       }
+       if(!test_mat()) {
+               fprintf(stderr, "test_mat failed\n");
+               return 1;
+       }
+       return 0;
+}
+
+#define ASSERT_EQ(a, b)        assert(memcmp(&a, &b, sizeof a) == 0)
+
+bool test_vec3()
+{
+       Vec3 rv3[2];
+       cgm_vec3 crv3[2];
+       Vec3 v3;
+       cgm_vec3 cv3;
+
+       for(int i=0; i<2; i++) {
+               rv3[i].x = crv3[i].x = frand(-100, 100);
+               rv3[i].y = crv3[i].y = frand(-100, 100);
+               rv3[i].z = crv3[i].z = frand(-100, 100);
+       }
+
+       cgm_vcross(&cv3, crv3, crv3 + 1);
+       v3 = cross(rv3[0], rv3[1]);
+       ASSERT_EQ(v3, cv3);
+
+       cv3 = crv3[0];
+       cgm_vreflect(&cv3, crv3 + 1);
+       v3 = reflect(rv3[0], rv3[1]);
+       ASSERT_EQ(v3, cv3);
+
+       float ior = frand(1, 2);
+       cv3 = crv3[0];
+       cgm_vrefract(&cv3, crv3 + 1, ior);
+       v3 = refract(rv3[0], rv3[1], ior);
+       ASSERT_EQ(v3, cv3);
+
+       return true;
+}
+
+bool test_mat()
+{
+       float ca[16], cb[16], cc[16];
+       Mat4 a, b, c;
+
+       float angle = frand(-100, 100);
+       Vec3 axis = Vec3(frand(-10, 10), frand(-10, 10), frand(-10, 10));
+       cgm_mrotation(ca, angle, axis.x, axis.y, axis.z);
+       a.rotation(angle, axis.x, axis.y, axis.z);
+       ASSERT_EQ(a, ca);
+
+       Vec3 trans = Vec3(frand(-100, 100), frand(-100, 100), frand(-100, 100));
+       Vec3 scale = Vec3(frand(0.1, 10), frand(0.1, 10), frand(0.1, 10));
+       cgm_mtranslate(ca, trans.x, trans.y, trans.z);
+       cgm_mscale(ca, scale.x, scale.y, scale.z);
+       a.translate(trans.x, trans.y, trans.z);
+       a.scale(scale.x, scale.y, scale.z);
+       ASSERT_EQ(a, ca);
+
+       cgm_mprerotate_y(ca, trans.x);
+       a.pre_rotate_y(trans.x);
+       ASSERT_EQ(a, ca);
+
+       cgm_minverse(ca);
+       a.inverse();
+       ASSERT_EQ(a, ca);
+
+       return true;
+}
+
+float frand(float low, float high)
+{
+       float range = high - low;
+       return ((float)rand() / (float)RAND_MAX) * range + low;
+}