Quelques ajustements de codes.
[ProjetInfo.git] / testing / math / testSqMatrix3.cc
blobffe353a6e6e0b0664af1b4b0fcfec035495067a6
1 #include <iostream>
2 #include "../../math/inc/sqmatrix3.h"
4 using namespace std;
6 int main()
8 cout << "=== Testing SqMatrix3 ===" << endl;
9 cout << "Test 1: creating three matrices: " << endl;
11 // mat1 : identity in Mat3
12 cout << "Mat1: must be identity" << endl;
13 SqMatrix3 mat1;
14 mat1.show();
15 cout << endl;
17 // mat2: null matrix in Mat3
18 cout << "Mat2: must be null" << endl;
19 SqMatrix3 mat2(SqMatrix3::Mat_Null);
20 mat2.show();
21 cout << endl;
23 // mat3: [[1,2,3][4,5,6][7,8,9]]
24 cout << "Mat3: must be [[1,2,3][4,5,6][7,8,9]] (row notation)" << endl;
25 SqMatrix3 mat3(Vector3(1,4,7), Vector3(2,5,8), Vector3(3,6,9));
26 mat3.show();
27 cout << endl;
29 cout << "=>" << " End of test 1" << endl << endl;
31 //test 2 : operator<<
32 cout << "Test 2: operator<<" << endl;
33 cout << "Showing mat1 again: must be identity" << endl;
34 cout << mat1 << endl;
36 cout << "=>" << " End of test 2" << endl << endl;
38 // test 3 : copy constructor and operator=
39 cout << "Test 3: copy constructor and operator= " << endl;
40 cout << "Copy constructor: creating Mat4 by copying Mat3" << endl;
41 SqMatrix3 mat4(mat3);
42 cout << "Showing mat4, must be [[1,2,3][4,5,6][7,8,9]]" << endl;
43 cout << mat4 << endl;
44 cout << "operator=: Mat1 = Mat4" << endl;
45 mat1 = mat4;
46 cout << "Showing Mat1: must be [[1,2,3][4,5,6][7,8,9]]" << endl;
47 cout << mat1 << endl;
48 cout << "=>" << " End of test 3" << endl << endl;
50 // test 4 : sums, substractions
51 cout << "Test 4: additions, substractions" << endl;
52 cout << "Showing Mat1 + Mat2: must be [[1,2,3][4,5,6][7,8,9]]" << endl;
53 cout << mat1 + mat2 << endl;
55 cout << "Creating Mat5 := [[4.5,3,2.7][1,2,5.9][3,5.23,5]] " << endl;
56 SqMatrix3 mat5(Vector3(4.5,1.0,3.0), Vector3(3.0,2.0,5.23), Vector3(2.7,5.9,5.0));
57 cout << "Assigning Mat2 = Mat1 + Mat5" << endl;
58 mat2 = mat1 + mat5;
59 cout << "Showing mat2: must be [[5.5,5,5.7][5,7,11.9][10,13.23,14]]" << endl;
60 cout << mat2 << endl;
62 cout << "Creating Mat6 := null matrix" << endl;
63 SqMatrix3 mat6(SqMatrix3::Mat_Null);
64 cout << "Assigning Mat6 += Mat1" << endl;
65 mat6 += mat1;
66 cout << "Showing Mat6: must be [[1,2,3][4,5,6][7,8,9]]" << endl;
67 cout << mat6 << endl;
69 cout << "Assingning Mat6 += Mat5" << endl;
70 mat6 += mat5;
71 cout << "Showing Mat6: must be [[5.5,5,5.7][5,7,11.9][10,13.23,14]]" << endl;
72 cout << mat6 << endl;
74 cout << "Creating Mat7 := identity" << endl;
75 SqMatrix3 mat7;
76 cout << "Showing Mat1 - Mat7: must be [[0,2,3][4,4,6][7,8,8]]" << endl;
77 cout << mat1 - mat7 << endl;
79 cout << "Assigning Mat7 -= Mat2" << endl;
80 mat7 -= mat2;
81 cout << "Showing Mat7: must be [[-4.5,-5,-5.7][-5,-7,-11.9][-10,-13.23,-13]]" << endl;
82 cout << mat7 << endl;
84 cout << "=> End of test 4" << endl << endl;
86 // test 5: comparison operators
87 cout << "Test 5 : comparison operators" << endl;
88 cout << "Testing Mat6 == Mat2: must be true ->: " << boolalpha << (mat6 == mat2) << endl;
89 cout << "Testing Mat2 == Mat6: must be true ->: " << boolalpha << (mat2 == mat6) << endl;
90 cout << "Testing Mat1 == Mat7: must be false ->: " << boolalpha << (mat1 == mat7) << endl;
91 cout << "Testing Mat1 != Mat7: must be true ->: " << boolalpha << (mat1 != mat7) << endl;
92 cout << "Testing Mat7 != Mat1: must ne true ->: " << boolalpha << (mat7 != mat1) << endl;
94 cout << endl << "=> End of test 5" << endl << endl;
96 // test 6: multiplication operators
97 cout << "Test 6 : multiplication operators" << endl;
98 cout << "Creating Mat8: identity" << endl;
99 SqMatrix3 mat8;
100 cout << "Showing Mat4 * Mat8: must be [[1,2,3][4,5,6][7,8,9]]" << endl;
101 cout << mat4 * mat8 << endl;
102 cout << "Showing Mat8 * Mat4: must be [[1,2,3][4,5,6][7,8,9]]" << endl;
103 cout << mat8 * mat4 << endl;
105 cout << "Showing Mat3 * Mat4: must be [[30,36,42][66,81,96][102,126,150]]" << endl;
106 cout << mat3 * mat4 << endl;
108 cout << "Assigning Mat3 *= Mat7" << endl;
109 mat3 *= mat7;
110 cout << "Showing Mat3: must be [[-44.5,-56,69,-68.5][-103,-129.38,-160.3][-161.5,-202.07,-252.1]]" << endl;
111 cout << mat3 << endl;
113 cout << "Creating Vec1 := [[58][59][60]]" << endl;
114 Vector3 vec1(58,59,60);
115 cout << "Showing Mat1 * Vec1: must be [[356][887][1418]]" << endl;
116 (mat1 * vec1).show();
117 cout << endl;
119 cout << "Showing 2 * Mat1: must be [[2,4,6][8,10,12][14,16,18]]" << endl;
120 cout << (2 * mat1) << endl;
121 cout << "Showing Mat1 * 2: must be [[2,4,6][8,10,12][14,16,18]]" << endl;
122 cout << (mat1 * 2) << endl;
123 cout << "Assigning Mat1 *= 2" << endl;
124 mat1 *= 2;
125 cout << "Showing Mat1: must be [[2,4,6][8,10,12][14,16,18]]" << endl;
126 cout << mat1 << endl;
128 cout << "=> End of test 6" << endl << endl;
130 // test 7: transposition
131 cout << "Test 7: Transposition" << endl;
132 cout << "Showing T(Mat4): must be [[1,4,7][2,5,8][3,6,9]]" << endl;
133 cout << mat4.transpose() << endl;
134 cout << "Assigning Mat4 = T(Mat4)" << endl;
135 mat4.setToTransposed();
136 cout << "Showing Mat4: must be [[1,4,7][2,5,8][3,6,9]]" << endl;
137 cout << mat4 << endl;
139 cout << "=> End of test 7" << endl;
141 // test 8: determinant, inverse
142 cout << "Test 8: determinant, inverse" << endl;
143 cout << "det(Mat4): must be 0 ->: " << mat4.determinant() << endl;
144 cout << "det(Mat7): must be 52,4115 ->: " << mat7.determinant() << endl;
146 cout << "Trying to inverse Mat4: must throw std::domain_error exception" << endl;
147 try {
148 mat4.inverse();
150 catch (domain_error &err) {
151 cout << err.what() << endl;
153 cout << "End of catch block" << endl;
154 cout << "Showing Mat7 inverse: must be [[-1.51564,0.19864,0.482718][1.03031,0.0286197,-0.477949][-0.117341,-0.181926,0.0381596]]" << endl;
155 cout << mat7.inverse() << endl;
156 cout << "Assigning Mat7 = Mat7 inverse" << endl;
157 mat7.setToInverse();
158 cout << "Showing Mat7: must be [[-1.51564,0.19864,0.482718][1.03031,0.0286197,-0.477949][-0.117341,-0.181926,0.0381596]]" << endl;
159 cout << mat7 << endl;
161 cout << "=> End of test 8" << endl;
163 // automatic tests : trust them only if tests 1 to 8 have been successful
164 cout << "Automatic tests: should be trusted only if tests 1 to 8 have been successful" << endl;
165 cout << "Getter/Setter tests:" << endl;
166 cout << setfill(' ') << setw(5) << "Access: ";
167 if (mat1(1,2) == 4.0) {
168 cout << "SUCCESS" << endl;
170 else {
171 cout << "FAIL" << endl;
173 cout << setfill(' ') << setw(5) << "Modify: ";
174 mat1(1,1) = 5.76;
175 mat1(1,1) == 5.76 ? cout << "SUCCESS" << endl : cout << "FAIL" << endl;
176 cout << "Multiplication tests:" << endl;
177 SqMatrix3 mat9(Vector3(1,4,7), Vector3(2,5,8), Vector3(3,6,10));
178 cout << setfill(' ') << setw(5) << "Mat * Mat^(-1): ";
179 if (mat8 * mat8.inverse() == SqMatrix3()) {
180 cout << "SUCCESS" << endl;
182 else {
183 cout << "FAIL" << endl;
186 cout << endl << endl << "End of SqMatrix3 test" << endl;
187 return 0;