Make use of CLOS for vector and matrix types Rewrite matrix implementation as column major Verify the computations in rewritten unit tests
100 lines
3.8 KiB
Common Lisp
100 lines
3.8 KiB
Common Lisp
#|
|
|
This file is a part of stoe project.
|
|
Copyright (c) 2015 Renaud Casenave-Péré (renaud@casenave-pere.fr)
|
|
|#
|
|
|
|
(in-package :cl-user)
|
|
(defpackage stoe-test.maths
|
|
(:use :cl
|
|
:stoe.maths.types
|
|
:stoe.maths.vector
|
|
:stoe.maths.matrix
|
|
:prove))
|
|
(in-package :stoe-test.maths)
|
|
|
|
(defvar *v2* (vec 1 2))
|
|
(defvar *v3* (vec 3 4 5))
|
|
(defvar *v4* (vec 6 7 8 9))
|
|
(defvar *v5* (vec 2.0 3.0))
|
|
(defvar *v6* (vec 4.0 5.0 6.0))
|
|
|
|
(defvar *m22* (mat 1 2
|
|
3 4))
|
|
(defvar *m33* (mat 1 2 3
|
|
4 5 6
|
|
7 8 9))
|
|
(defvar *m44* (mat 1 2 3 4
|
|
5 6 7 8
|
|
9 10 11 12
|
|
13 14 15 16))
|
|
(defvar *m23* (make-matrix '(2 3) 'fixnum 1 3 5 2 4 6))
|
|
(defvar *m32* (make-matrix '(3 2) 'fixnum 1 4 2 5 3 6))
|
|
|
|
(plan 23)
|
|
|
|
(diag "Vector constructor tests")
|
|
(is (slot-value (vec 1 2 3) 'array) #(1 2 3)
|
|
"Integer vector constructor" :test #'equalp)
|
|
|
|
(is (slot-value (vec 1.0 2.0 3.0) 'array) #(1.0 2.0 3.0)
|
|
"Float vector constructor" :test #'equalp)
|
|
|
|
(is (vec2 2 3) (vec 2 3) "float2 constructor" :test (lambda (v1 v2)
|
|
(eq (type-of v1)
|
|
(type-of v2))))
|
|
|
|
(is (vec3 2 3 4) (vec 2 3 4) "float3 constructor" :test (lambda (v1 v2)
|
|
(eq (type-of v1)
|
|
(type-of v2))))
|
|
|
|
(is (vec4 2 3 4 5) (vec 2 3 4 5) "float4 constructor" :test (lambda (v1 v2)
|
|
(eq (type-of v1)
|
|
(type-of v2))))
|
|
|
|
(diag "Swizzle tests")
|
|
(is (slot-value (xy *v4*) 'array) #(6 7) "f4.xy" :test #'equalp)
|
|
(is (slot-value (xyz *v2*) 'array) #(1 2 0) "f2.xyz" :test #'equalp)
|
|
(is (slot-value (xyz *v3*) 'array) #(3 4 5) "f3.xyz (identity)" :test #'equalp)
|
|
(is (slot-value (wzyx *v4*) 'array) #(9 8 7 6) "f4.wzyx (reverse)" :test #'equalp)
|
|
(is (slot-value (xyxy *v2*) 'array) #(1 2 1 2)
|
|
"f2.xyxy (multiple attributes)" :test #'equalp)
|
|
|
|
(diag "Simple vector operations")
|
|
(is (slot-value (v+ *v2* (xy *v4*)) 'array) #(7 9) "Add f2" :test #'equalp)
|
|
(is (slot-value (v- *v3* *v3*) 'array) #(0 0 0)
|
|
"Substract f3 to itself" :test #'equalp)
|
|
(is (slot-value (v* *v4* (xyxy *v2*)) 'array) #(6 14 8 18)
|
|
"Multiply f4" :test #'equalp)
|
|
(is (slot-value (v/ *v5* (xz *v6*)) 'array) #(0.5 0.5) "Divide f2" :test #'equalp)
|
|
|
|
(diag "Simple vector / scalar operations")
|
|
(is (slot-value (v+ *v2* 3) 'array) #(4 5) "Add f2" :test #'equalp)
|
|
(is (slot-value (v- *v3* 1) 'array) #(2 3 4) "Substract f3" :test #'equalp)
|
|
(is (slot-value (v* *v4* 2) 'array) #(12 14 16 18) "Multiply f4" :test #'equalp)
|
|
(is (slot-value (v/ *v5* 5) 'array) #(0.4 0.6) "Divide f2" :test #'equalp)
|
|
|
|
(diag "Matrix constructor tests")
|
|
(is (slot-value (mat 1 2 3 4 5 6 7 8 9 10 11 12) 'array)
|
|
#(1 2 3 4 5 6 7 8 9 10 11 12) "Matrix constructor (array)" :test #'equalp)
|
|
(is (slot-value (mat 1 2 3 4 5 6 7 8 9 10 11 12) 'dimensions) '(3 4)
|
|
"Matrix constructor (dimensions)" :test #'equalp)
|
|
|
|
(diag "Simple matrix operations")
|
|
(is (slot-value (m+ *m22* (mat-id 2 'fixnum)) 'array)
|
|
#(2 2
|
|
3 5) "Add f22" :test #'equalp)
|
|
(is (slot-value (m- *m33* (mat 9 8 7
|
|
6 5 4
|
|
3 2 1)) 'array)
|
|
#(-8 -6 -4
|
|
-2 0 2
|
|
4 6 8) "Substract f33" :test #'equalp)
|
|
(is (slot-value (m* *m44* (mat 1 0 0 0
|
|
0 1 0 0
|
|
0 0 1 0
|
|
0 0 0 1)) 'array)
|
|
#(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16) "Multiply f44 by id" :test #'equalp)
|
|
(is (slot-value (m* *m23* *m32) 'array)
|
|
#(9 19 29 12 26 40 15 33 51) "Multiply f23 by f32" :test #'equalp)
|
|
|
|
(finalize)
|