From b4abea17d59416ce77580adfcb73aeb58a1e009b Mon Sep 17 00:00:00 2001 From: rdb Date: Sun, 19 Aug 2018 16:43:34 +0200 Subject: [PATCH] tests: add various matrix unit tests --- tests/linmath/test_lmatrix3.py | 62 ++++++++++++++++++++ tests/linmath/test_lmatrix4.py | 89 +++++++++++++++++++++++++++++ tests/linmath/test_matrix_invert.py | 20 ------- 3 files changed, 151 insertions(+), 20 deletions(-) create mode 100644 tests/linmath/test_lmatrix3.py create mode 100644 tests/linmath/test_lmatrix4.py delete mode 100644 tests/linmath/test_matrix_invert.py diff --git a/tests/linmath/test_lmatrix3.py b/tests/linmath/test_lmatrix3.py new file mode 100644 index 0000000000..b0ce53685a --- /dev/null +++ b/tests/linmath/test_lmatrix3.py @@ -0,0 +1,62 @@ +import pytest +from copy import copy +from panda3d import core + + +def test_mat3_aliases(): + assert core.LMatrix3 is core.Mat3 + assert core.LMatrix3f is core.Mat3F + assert core.LMatrix3d is core.Mat3D + + assert (core.LMatrix3f is core.Mat3) != (core.LMatrix3d is core.Mat3) + + +@pytest.mark.parametrize("type", (core.LMatrix3f, core.LMatrix3d)) +def test_mat3_constructor(type): + # Test that three ways of construction produce the same matrix. + mat1 = type((1, 2, 3), + (4, 5, 6), + (7, 8, 9)) + + mat2 = type(1, 2, 3, 4, 5, 6, 7, 8, 9) + + mat3 = type((1, 2, 3, 4, 5, 6, 7, 8, 9)) + + assert mat1 == mat2 + assert mat2 == mat3 + assert mat1 == mat3 + + +@pytest.mark.parametrize("type", (core.LMatrix3d, core.LMatrix3f)) +def test_mat3_copy_constuctor(type): + mat1 = type((1, 2, 3), + (4, 5, 6), + (7, 8, 9)) + + # Make a copy. Changing it should not change the original. + mat2 = type(mat1) + assert mat1 == mat2 + mat2[0][0] = 100 + assert mat1 != mat2 + + # Make a copy by unpacking. + mat2 = type(*mat1) + assert mat1 == mat2 + mat2[0][0] = 100 + assert mat1 != mat2 + + # Make a copy by calling copy.copy. + mat2 = copy(mat1) + assert mat1 == mat2 + mat2[0][0] = 100 + assert mat1 != mat2 + + +@pytest.mark.parametrize("type", (core.LMatrix3d, core.LMatrix3f)) +def test_mat3_invert_same_type(type): + mat = type((1, 0, 0, + 0, 1, 0, + 1, 2, 3)) + + inv = core.invert(mat) + assert mat.__class__ == inv.__class__ diff --git a/tests/linmath/test_lmatrix4.py b/tests/linmath/test_lmatrix4.py new file mode 100644 index 0000000000..23c1bcc5e9 --- /dev/null +++ b/tests/linmath/test_lmatrix4.py @@ -0,0 +1,89 @@ +import pytest +from copy import copy +from panda3d import core + + +def test_mat4_aliases(): + assert core.LMatrix4 is core.Mat4 + assert core.LMatrix4f is core.Mat4F + assert core.LMatrix4d is core.Mat4D + + assert (core.LMatrix4f is core.Mat4) != (core.LMatrix4d is core.Mat4) + + +@pytest.mark.parametrize("type", (core.LMatrix4d, core.LMatrix4f)) +def test_mat4_constructor(type): + # Test that three ways of construction produce the same matrix. + mat1 = type((1, 2, 3, 4), + (5, 6, 7, 8), + (9, 10, 11, 12), + (13, 14, 15, 16)) + + mat2 = type(1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16) + + mat3 = type((1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16)) + + assert mat1 == mat2 + assert mat2 == mat3 + assert mat1 == mat3 + + +@pytest.mark.parametrize("type", (core.LMatrix4d, core.LMatrix4f)) +def test_mat4_copy_constuctor(type): + mat1 = type((1, 2, 3, 4), + (5, 6, 7, 8), + (9, 10, 11, 12), + (13, 14, 15, 16)) + + # Make a copy. Changing it should not change the original. + mat2 = type(mat1) + assert mat1 == mat2 + mat2[0][0] = 100 + assert mat1 != mat2 + + # Make a copy by unpacking. + mat2 = type(*mat1) + assert mat1 == mat2 + mat2[0][0] = 100 + assert mat1 != mat2 + + # Make a copy by calling copy.copy. + mat2 = copy(mat1) + assert mat1 == mat2 + mat2[0][0] = 100 + assert mat1 != mat2 + + +@pytest.mark.parametrize("type", (core.LMatrix4d, core.LMatrix4f)) +def test_mat4_invert_same_type(type): + mat = type((1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 1, 2, 3, 1)) + + inv = core.invert(mat) + assert mat.__class__ == inv.__class__ + + +@pytest.mark.parametrize("type", (core.LMatrix4d, core.LMatrix4f)) +def test_mat4_invert_correct(type): + mat = type((1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 1, 2, 3, 1)) + inv = type() + assert inv.invert_from(mat) + + assert inv == type(( 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + -1, -2, -3, 1)) + + assert (mat * inv).is_identity() + assert (inv * mat).is_identity() diff --git a/tests/linmath/test_matrix_invert.py b/tests/linmath/test_matrix_invert.py deleted file mode 100644 index b2e6dd49bf..0000000000 --- a/tests/linmath/test_matrix_invert.py +++ /dev/null @@ -1,20 +0,0 @@ -import pytest -from panda3d import core - - -@pytest.mark.parametrize("type", (core.Mat4, core.Mat4D)) -def test_mat4_invert(type): - mat = type((1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 1, 2, 3, 1)) - inv = type() - assert inv.invert_from(mat) - - assert inv == type(( 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - -1, -2, -3, 1)) - - assert (mat * inv).is_identity() - assert (inv * mat).is_identity()