classe 'Vector' : ajout de deux constructeurs et surcharge de l'operateur 'ostream...
[ProjetInfo.git] / testing / math / testVector.cc
blobeae9dad27aecc73012818ef5a9ae83a18f4e38c7
1 #include "vector.h"
2 #include <iomanip>
4 using namespace std;
6 bool test (const Vector &testVal, const Vector &refVal) {return (testVal == refVal);}
7 bool test (const double testVal, const double refVal) {return (testVal == refVal);}
9 int main ()
11 Vector vec1 (vector<double> (5,1));
12 Vector vec2 (vector<double> (5, -2.25));
13 Vector vec3 (4);
14 Vector vec4 (vec3);
15 Vector e3 (8, Vector::ei, 3);
18 cout << "**** Beginnning of preliminary testing ****" << endl << endl;
20 cout << "Testing constructors. Output should be (1 1 1 1 1), \n(-2.25 -2.25 -2.25 -2.25 -2.25), 2x(0 0 0 0) and e3 in R8" << endl;
21 vec1.show();
22 vec2.show();
23 vec3.show();
24 vec4.show();
25 e3.show();
27 cout << "Testing overload of operator << (implementation only for testing purpose)" << endl << "Output should be (0 0 0 0)" << endl;
28 cout << vec4 << endl;
30 cout << "Testing operator = : Output should be (-2.25 -2.25 -2.25 -2.25-2.25)" << endl;
31 vec3 = vec2;
32 cout << vec3 << endl;
33 vec3 = vec4;
35 cout << "Testing operator == : output should be FALSE TRUE" << endl;
36 cout << boolalpha << (vec1 == vec2) << '\t' << (vec3 == vec2) << endl << endl;
38 cout << "**** End of preliminary testing ****" << endl
39 << "/!\\ If any of the above tests failed, discard rest of tests /!\\" << endl << endl;
41 cout << "**** Beginning of methods testing ****" << endl << endl;
43 cout << "at() const test : ";
44 cout << boolalpha << ( test(vec2.at(2), -2.25) and test(e3.at(3), 0) ) << endl;
46 cout << "at() non const test : "; {
47 Vector e4 (4, Vector::ei, 1);
48 e4.at(0) = 1;
49 e4.at(3) = 3.1415;
50 cout << boolalpha << ( test(e4.at(0), 1) and test(e4.at(3), 3.1415) ) << endl;
53 cout << "'addComponent()' test : "; {
54 vec3.addComponent(4);
55 Vector refVal (5);
56 refVal.at(4) = 4;
57 cout << boolalpha << test (vec3, refVal) << endl;
61 cout << "magnitude() test : ";
62 Vector vec5 (vector<double>(25, 5));
63 cout << boolalpha << test(vec5.magnitude(), 25) << endl;
64 cout << "magnitude2() test : ";
65 cout << boolalpha << test (vec1.magnitude2(), 5) << endl;
67 cout << "show() test : Output should be (0 0 1 0 0 0 0 0)" << endl;
68 e3.show();
70 cout << "**** End of methods testing ****" << endl << endl;
72 cout << "**** Beginning of operators testing ****" << endl << endl;
74 cout << "Operator + test : "; {
75 vec3 = vec1 + vec2;
76 Vector refVal (vector<double> (5,-1.25));
77 cout << boolalpha << test (vec3, refVal) << endl;
80 cout << "Operator += test : "; {
81 vec1 += vec3;
82 Vector refVal (vector<double> (5,-0.25));
83 cout << boolalpha << test (vec1, refVal) << endl;
86 cout << "Operator - test : "; {
87 vec3 = vec1 - vec2;
88 Vector refVal (vector<double> (5,2));
89 cout << boolalpha << test (vec3, refVal) << endl;
92 cout << "Operator -= test : "; {
93 vec2 -= vec3;
94 Vector refVal (vector<double> (5,-4.25));
95 cout << boolalpha << test (vec2, refVal) << endl;
98 cout << "Operator * (scalar) test : "; {
99 vec3 = vec1 * -3;
100 Vector refVal (vector<double> (5,0.75));
101 cout << boolalpha << test (vec3, refVal) << endl;
104 cout << "Operator *= (scalar) test : "; {
105 vec1 *= 4;
106 Vector refVal (vector<double> (5,-1));
107 cout << boolalpha << test (vec1, refVal) << endl;
110 cout << "Operator * (dotProduct) test : "; {
111 cout << boolalpha << test ((vec2 * vec3), -15.9375) << endl;
114 cout << "Operator / test : "; {
115 Vector a (vector<double> (5, -6));
116 vec3 = a / -3;
117 Vector refVal (vector<double> (5,2));
118 cout << boolalpha << test (vec3, refVal) << endl;
121 cout << "Operator /= test : "; {
122 Vector a (vector<double> (5, 1.75));
123 a /= 1.25;
124 Vector refVal (vector<double> (5,1.4));
125 cout << boolalpha << test (a, refVal) << endl;
128 cout << "Operator ^ test : "; {
129 Vector e2 (3, Vector::ei, 2);
130 Vector a;
131 a.addComponent(5);
132 a.addComponent(3);
133 a.addComponent(2.27);
134 Vector refVal;
135 refVal.addComponent(2.27);
136 refVal.addComponent(0);
137 refVal.addComponent(-5.0);
138 cout << boolalpha << test ((e2^a), refVal) << endl;
141 cout << "Operator != test : ";
142 cout << boolalpha << ((vec1 != vec2) == true) << endl;
144 cout << "Operator [] test : "; {
145 cout << boolalpha << ( test(vec1[5], -1) and test(vec3[2], 2) ) << endl;
148 cout << endl;
149 cout << "**** End of testing operators ****" << endl << endl;
151 cout << "**** Beginning of exception handling testing ****" << endl << endl;
153 try {
154 cout << "Operator +" << endl;
155 vec1 = e3 + vec2;
157 catch (logic_error &e) {
158 cout << e.what() << endl;
161 try {
162 cout << "Operator +=" << endl;
163 vec1 += e3;
165 catch (logic_error &e) {
166 cout << e.what() << endl;
169 try {
170 cout << "Operator -" << endl;
171 vec1 = e3 - vec2;
173 catch (logic_error &e) {
174 cout << e.what() << endl;
177 try {
178 cout << "Operator -=" << endl;
179 vec1 -= e3;
181 catch (logic_error &e) {
182 cout << e.what() << endl;
185 try {
186 cout << "Operator *" << endl;
187 vec1 = e3 * vec2;
189 catch (logic_error &e) {
190 cout << e.what() << endl;
193 try {
194 cout << "Operator /" << endl;
195 vec1 = e3 / 0;
197 catch (domain_error &e) {
198 cout << e.what() << endl;
201 try {
202 cout << "Operator /=" << endl;
203 vec1 /= 0;
205 catch (domain_error &e) {
206 cout << e.what() << endl;
209 try {
210 cout << "Operator ^" << endl;
211 vec3 = vec1 ^ vec2;
213 catch (logic_error &e) {
214 cout << e.what() << endl;
217 try {
218 cout << "at() const" << endl;
219 vec1.at(100);
221 catch (out_of_range &e) {
222 cout << e.what() << endl;
225 try {
226 cout << "at() non-const" << endl;
227 vec1.at(6) = 2;
229 catch (out_of_range &e) {
230 cout << e.what() << endl;
233 try {
234 cout << "[] const" << endl;
235 vec1[100];
237 catch (out_of_range &e) {
238 cout << e.what() << endl;
241 try {
242 cout << "[] non-const" << endl;
243 vec1[7] = 2;
245 catch (out_of_range &e) {
246 cout << e.what() << endl;
249 cout << endl << endl << "**** End of testing ****" << endl << endl;
251 return 0;