diff options
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | tests/matrix.scm | 63 |
2 files changed, 64 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index 4dc96a7..767e76d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -103,6 +103,7 @@ SOURCES = \ TESTS = \ tests/vector.scm \ + tests/matrix.scm \ tests/array-list.scm \ tests/heap.scm \ tests/quadtree.scm \ diff --git a/tests/matrix.scm b/tests/matrix.scm new file mode 100644 index 0000000..7f36280 --- /dev/null +++ b/tests/matrix.scm @@ -0,0 +1,63 @@ +;;; Chickadee Game Toolkit +;;; Copyright © 2022 David Thompson <dthompson2@worcester.edu> +;;; +;;; Chickadee is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published +;;; by the Free Software Foundation, either version 3 of the License, +;;; or (at your option) any later version. +;;; +;;; Chickadee is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;; General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see +;;; <http://www.gnu.org/licenses/>. + +(define-module (tests matrix) + #:use-module (tests utils) + #:use-module (srfi srfi-64) + #:use-module (chickadee math) + #:use-module (chickadee math matrix) + #:use-module (chickadee math quaternion) + #:use-module (chickadee math vector)) + +(with-tests "matrix" + (test-group "4x4 matrices" + (test-group "multiplication" + (test-assert "identity" + (let ((m (make-matrix4 0 1 2 3 + 4 5 6 7 + 8 9 0 1 + 2 3 4 5))) + (matrix4= m (matrix4* (make-identity-matrix4) m)))) + (test-assert "inverse" + ;; Using whole numbers to avoid floating point precision + ;; issues where the resulting identity matrix has values very + ;; close to 1.0 (like 0.9999999403953552) but not exactly, + ;; causing the test to fail. + (let ((m (matrix4-scale (vec3 2 3 4)))) + (matrix4= (make-identity-matrix4) + (matrix4* m (matrix4-inverse m)))))) + (test-group "transformation" + (test-assert "identity (vec2)" + (let ((v (vec2 1 2))) + (vec2= v (matrix4-transform-vec2 (make-identity-matrix4) v)))) + (test-assert "identity (vec3)" + (let ((v (vec3 1 2 3))) + (vec3= v (matrix4-transform-vec3 (make-identity-matrix4) v)))) + (test-assert "translate" + (vec3= (vec3 5 7 9) + (matrix4-transform-vec3 (matrix4-translate (vec3 4 5 6)) + (vec3 1 2 3)))) + (test-assert "scale" + (vec3= (vec3 6 12 20) + (matrix4-transform-vec3 (matrix4-scale (vec3 3 4 5)) + (vec3 2 3 4)))) + (test-assert "rotate" + (vec3= (vec3 0 1 0) + (matrix4-transform-vec3 (matrix4-rotate + (rotation->quaternion (vec3 0 0 -1) + (/ pi 2))) + (vec3 1 0 0))))))) |