classe 'Vector' : fin de la phase de test.
[ProjetInfo.git] / testVector.cc
blob96205100b6ac46336cdf107d930c40052fe86162
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 ostream& operator << (ostream& os, const Vector &aVector)
11 const unsigned int SIZE (aVector.dimension());
13 for (unsigned int i(0) ; i < SIZE ; ++i) {
14 os << aVector.at(i) << ' ';
16 return os;
19 int main ()
21 Vector vec1 (vector<double> (5,1));
22 Vector vec2 (vector<double> (5, -2.25));
23 Vector vec3 (4);
24 Vector vec4 (vec3);
25 Vector e3 (8, Vector::ei, 3);
28 cout << "**** Beginnning of preliminary testing ****" << endl << endl;
30 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;
31 vec1.show();
32 vec2.show();
33 vec3.show();
34 vec4.show();
35 e3.show();
37 cout << "Testing overload of operator << (implementation only for testing purpose)" << endl << "Output should be (0 0 0 0)" << endl;
38 cout << vec4 << endl;
40 cout << "Testing operator = : Output should be (-2.25 -2.25 -2.25 -2.25-2.25)" << endl;
41 vec3 = vec2;
42 cout << vec3 << endl;
43 vec3 = vec4;
45 cout << "Testing operator == : output should be FALSE TRUE" << endl;
46 cout << boolalpha << (vec1 == vec2) << '\t' << (vec3 == vec2) << endl << endl;
48 cout << "**** End of preliminary testing ****" << endl
49 << "/!\\ If any of the above tests failed, discard rest of tests /!\\" << endl << endl;
51 cout << "**** Beginning of methods testing ****" << endl << endl;
53 cout << "at() const test : ";
54 cout << boolalpha << ( test(vec2.at(2), -2.25) and test(e3.at(3), 0) ) << endl;
56 cout << "at() non const test : "; {
57 Vector e4 (4, Vector::ei, 1);
58 e4.at(0) = 1;
59 e4.at(3) = 3.1415;
60 cout << boolalpha << ( test(e4.at(0), 1) and test(e4.at(3), 3.1415) ) << endl;
63 cout << "'addComponent()' test : "; {
64 vec3.addComponent(4);
65 Vector refVal (5);
66 refVal.at(4) = 4;
67 cout << boolalpha << test (vec3, refVal) << endl;
71 cout << "magnitude() test : ";
72 Vector vec5 (vector<double>(25, 5));
73 cout << boolalpha << test(vec5.magnitude(), 25) << endl;
74 cout << "magnitude2() test : ";
75 cout << boolalpha << test (vec1.magnitude2(), 5) << endl;
77 cout << "show() test : Output should be (0 0 1 0 0 0 0 0)" << endl;
78 e3.show();
80 cout << "**** End of methods testing ****" << endl << endl;
82 cout << "**** Beginning of operators testing ****" << endl << endl;
84 cout << "Operator + test : "; {
85 vec3 = vec1 + vec2;
86 Vector refVal (vector<double> (5,-1.25));
87 cout << boolalpha << test (vec3, refVal) << endl;
90 cout << "Operator += test : "; {
91 vec1 += vec3;
92 Vector refVal (vector<double> (5,-0.25));
93 cout << boolalpha << test (vec1, refVal) << endl;
96 cout << "Operator - test : "; {
97 vec3 = vec1 - vec2;
98 Vector refVal (vector<double> (5,2));
99 cout << boolalpha << test (vec3, refVal) << endl;
102 cout << "Operator -= test : "; {
103 vec2 -= vec3;
104 Vector refVal (vector<double> (5,-4.25));
105 cout << boolalpha << test (vec2, refVal) << endl;
108 cout << "Operator * (scalar) test : "; {
109 vec3 = vec1 * -3;
110 Vector refVal (vector<double> (5,0.75));
111 cout << boolalpha << test (vec3, refVal) << endl;
114 cout << "Operator *= (scalar) test : "; {
115 vec1 *= 4;
116 Vector refVal (vector<double> (5,-1));
117 cout << boolalpha << test (vec1, refVal) << endl;
120 cout << "Operator * (dotProduct) test : "; {
121 cout << boolalpha << test ((vec2 * vec3), -15.9375) << endl;
124 cout << "Operator / test : "; {
125 Vector a (vector<double> (5, -6));
126 vec3 = a / -3;
127 Vector refVal (vector<double> (5,2));
128 cout << boolalpha << test (vec3, refVal) << endl;
131 cout << "Operator /= test : "; {
132 Vector a (vector<double> (5, 1.75));
133 a /= 1.25;
134 Vector refVal (vector<double> (5,1.4));
135 cout << boolalpha << test (a, refVal) << endl;
138 cout << "Operator ^ test : "; {
139 Vector e2 (3, Vector::ei, 2);
140 Vector a;
141 a.addComponent(5);
142 a.addComponent(3);
143 a.addComponent(2.27);
144 Vector refVal;
145 refVal.addComponent(2.27);
146 refVal.addComponent(0);
147 refVal.addComponent(-5.0);
148 cout << boolalpha << test ((e2^a), refVal) << endl;
151 cout << "Operator != test : ";
152 cout << boolalpha << ((vec1 != vec2) == true) << endl;
154 cout << "Operator [] test : "; {
155 cout << boolalpha << ( test(vec1[5], -1) and test(vec3[2], 2) ) << endl;
158 cout << endl;
159 cout << "**** End of testing operators ****" << endl << endl;
161 cout << "**** Beginning of exception handling testing ****" << endl << endl;
163 try {
164 cout << "Operator +" << endl;
165 vec1 = e3 + vec2;
167 catch (logic_error &e) {
168 cout << e.what() << endl;
171 try {
172 cout << "Operator +=" << endl;
173 vec1 += e3;
175 catch (logic_error &e) {
176 cout << e.what() << endl;
179 try {
180 cout << "Operator -" << endl;
181 vec1 = e3 - vec2;
183 catch (logic_error &e) {
184 cout << e.what() << endl;
187 try {
188 cout << "Operator -=" << endl;
189 vec1 -= e3;
191 catch (logic_error &e) {
192 cout << e.what() << endl;
195 try {
196 cout << "Operator *" << endl;
197 vec1 = e3 * vec2;
199 catch (logic_error &e) {
200 cout << e.what() << endl;
203 try {
204 cout << "Operator /" << endl;
205 vec1 = e3 / 0;
207 catch (domain_error &e) {
208 cout << e.what() << endl;
211 try {
212 cout << "Operator /=" << endl;
213 vec1 /= 0;
215 catch (domain_error &e) {
216 cout << e.what() << endl;
219 try {
220 cout << "Operator ^" << endl;
221 vec3 = vec1 ^ vec2;
223 catch (logic_error &e) {
224 cout << e.what() << endl;
227 try {
228 cout << "at() const" << endl;
229 vec1.at(100);
231 catch (out_of_range &e) {
232 cout << e.what() << endl;
235 try {
236 cout << "at() non-const" << endl;
237 vec1.at(6) = 2;
239 catch (out_of_range &e) {
240 cout << e.what() << endl;
243 try {
244 cout << "[] const" << endl;
245 vec1[100];
247 catch (out_of_range &e) {
248 cout << e.what() << endl;
251 try {
252 cout << "[] non-const" << endl;
253 vec1[7] = 2;
255 catch (out_of_range &e) {
256 cout << e.what() << endl;
259 cout << endl << endl << "**** End of testing ****" << endl << endl;
261 return 0;