Source

neglect / neglect / tests / src / test_quaternion.cpp

Full commit
#include <neglect/unittest.hpp>
#include <neglect/vector.hpp>
#include <neglect/quaternion.hpp>
#include <neglect/quaternion_io.hpp>

using namespace neglect;


NUT_TESTGROUP(quaternion) {

    NUT_TESTCASE(constructors)
    {
        quat q1(1.0f, 2.0f, 3.0f, 4.0f);
        NUT_REQUIRE_EQUAL(q1.w, 1.0f);
        NUT_REQUIRE_EQUAL(q1.vec, vec3(2.0f, 3.0f, 4.0f));

        quat q2 = quat::zero();
        NUT_REQUIRE_EQUAL(q2.w, 0.0f);
        NUT_REQUIRE_EQUAL(q2.vec, vec3::zero());

        float *qptr = q1.ptr();
        NUT_REQUIRE_EQUAL(qptr[0], 1.0f);
        NUT_REQUIRE_EQUAL(qptr[1], 2.0f);
        NUT_REQUIRE_EQUAL(qptr[2], 3.0f);
        NUT_REQUIRE_EQUAL(qptr[3], 4.0f);
    }

    NUT_TESTCASE(multiplication)
    {
        quat q1(1.0f, 1.0f, 2.0f, 3.0f);
        quat q2(2.0f, 2.0f, 1.0f, 2.0f);
        NUT_REQUIRE_EQUAL(q1 * q2, quat(-8.0f, 5.0f, 9.0f, 5.0f));
    }

    NUT_TESTCASE(inverse)
    {
        quat q1(1.0f, 1.0f, 2.0f, 3.0f);
        NUT_REQUIRE_EQUAL(inverse(q1), quat(1.0f, -1.0f, -2.0f, -3.0f));
    }

    NUT_TESTCASE(dot_product)
    {
        quat q1(1.0f, 2.0f, 3.0f, 4.0f);
        quat q2(3.0f, 1.0f, 4.0f, 3.0f);
        NUT_REQUIRE_EQUAL(dot(q1, q2), 29.0f);
    }
}