neglect / neglect / tests / src / test_matrix.cpp

#include <neglect/unittest.hpp>
#include <neglect/matrix.hpp>
#include <neglect/matrix_io.hpp>

using namespace neglect;

NUT_TESTGROUP(matrix4) {

    NUT_TESTCASE(construction)
    {
        mat4 m1 = mat4::identity();
        mat4 m2;
        m2.set_identity();

        NUT_CHECK_EQUAL(m1, m2);
        NUT_CHECK_NOT_EQUAL(m1, mat4::zero());

        float values[16] = { // think, transpose
             1,  2,  3,  4,
             5,  6,  7,  8,
             9, 10, 11, 12,
            13, 14, 15, 16
        };
        mat4 m3(values);
        NUT_CHECK_EQUAL(m3[0], vec4(1, 2, 3, 4));
        NUT_CHECK_EQUAL(m3[1], vec4(5, 6, 7, 8));
        NUT_CHECK_EQUAL(m3[2], vec4(9, 10, 11, 12));
        NUT_CHECK_EQUAL(m3[3], vec4(13, 14, 15, 16));
        for (int i = 0; i < 16; i++)
            NUT_CHECK_EQUAL(m3.ptr()[i], values[i]);

        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                NUT_CHECK_EQUAL(m3[i][j], m3(j, i));
    }

    NUT_TESTCASE(dimensions)
    {
        NUT_CHECK_EQUAL(mat4::columns, 4);
        NUT_CHECK_EQUAL(mat4::rows, 4);
        NUT_CHECK_EQUAL(mat4::dimensions, 16);
    }

    NUT_TESTCASE(matrix_functions)
    {
        matrix<float, 2, 3> mat;
        mat.set_zero();
        mat[0][1] = 1.0f;
        mat[2][0] = 3.0f;
        matrix<float, 3, 2> mat_t = transpose(mat);
        NUT_CHECK_EQUAL(mat_t[1][0], 1.0f);
        NUT_CHECK_EQUAL(mat_t[0][2], 3.0f);
        NUT_CHECK_EQUAL(mat_t[0][0], 0.0f);
    }

    NUT_TESTCASE(matrix_multiplication)
    {
        mat3 m1;
        m1[0] = vec3(1, 4, 7);
        m1[1] = vec3(2, 5, 8);
        m1[2] = vec3(3, 6, 9);
        matrix<float, 3, 2> m2;
        m2[0] = vec3(1, 3, 5);
        m2[1] = vec3(2, 4, 6);

        matrix<float, 3, 2> expected;
        expected[0] = vec3(22, 49, 76);
        expected[1] = vec3(28, 64, 100);

        matrix<float, 3, 2> m3 = m1 * m2;
        NUT_CHECK_EQUAL(m3, expected);
    }

    NUT_TESTCASE(matrix_inplace_multiply)
    {
        mat2 m1, m2, m3, expected;
        m1[0] = vec2(1, 0);
        m1[1] = vec2(1, 1);
        m2[0] = vec2(1, 1);
        m2[1] = vec2(0, 1);
        expected[0] = vec2(2, 1);
        expected[1] = vec2(1, 1);
        m3 = m1 * m2;
        m1 *= m2;
        NUT_CHECK_EQUAL(m1, expected);
        NUT_CHECK_EQUAL(m1, m3);
    }

    NUT_TESTCASE(matrix_exclude)
    {
        mat4 m1;
        m1[0] = vec4( 1,  2,  3,  4);
        m1[1] = vec4( 5,  6,  7,  8);
        m1[2] = vec4( 9, 10, 11, 12);
        m1[3] = vec4(13, 14, 15, 16);

        mat3 expected;
        expected[0] = vec3( 1,  3,  4);
        expected[1] = vec3( 5,  7,  8);
        expected[2] = vec3(13, 15, 16);

        mat3 m1x = m1.exclude(1, 2);
        NUT_CHECK_EQUAL(m1x, expected);
        m1x(2, 2) = 100.0f;
        NUT_CHECK_NOT_EQUAL(m1x, expected);
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.