Commit 33eec0f72e01767cf7aff56523b6f48127f2cea5

Authored by Jean-Baptiste CAYROU
1 parent e3723baf

Adapt test_GVector with unit tests tool

test/test_GVector.cpp
1 1 /***************************************************************************
2 2 * test_GVector.cpp - test vector class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2006-2011 by Jurgen Knodlseder *
  4 + * copyright (C) 2006-2012 by Jurgen Knodlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -21,328 +21,424 @@
21 21  
22 22 /* __ Includes ___________________________________________________________ */
23 23 #include "GammaLib.hpp"
  24 +#include "test_GVector.hpp"
24 25 #include <cmath>
25   -#include <iostream> // cout, cerr
  26 +#include <iostream>
26 27 #include <stdexcept> // std::exception
27 28  
28   -using namespace std;
  29 +void TestGVector::set(void){
  30 + // Test name
  31 + name("GVector");
29 32  
30   -int main(void)
31   -{
32   - // Dump header
33   - cout << endl;
34   - cout << "*************************" << endl;
35   - cout << "* GVector class testing *" << endl;
36   - cout << "*************************" << endl;
37   -
38   - // Set parameters
39   - int num = 5;
40   -
41   - // Define vectors
42   - GVector test(num);
43   - GVector result(num);
44   - GVector smaller(num+1);
45   - GVector bigger(num+1);
46   - for (int i = 0; i < num; ++i)
47   - test[i] = (i+1) * 1.1;
48   - for (int i = 0; i < num-1; ++i)
49   - smaller[i] = (i+1) * 1.1;
50   - for (int i = 0; i < num+1; ++i)
51   - bigger[i] = (i+1) * 1.1;
52   -
53   - // Test 1: Allocate zero vector
54   - try {
55   - cout << "Test 1: Allocate zero vector: " << endl;
  33 + // Set parameters
  34 + m_num=5;
  35 +
  36 + //add tests
  37 + add_test(static_cast<pfunction>(&TestGVector::define_vectors),"Define vectors");
  38 + add_test(static_cast<pfunction>(&TestGVector::test1),"Test 1: Allocate zero vector");
  39 + add_test(static_cast<pfunction>(&TestGVector::test2),"Test 2: Allocate too large vector");
  40 + add_test(static_cast<pfunction>(&TestGVector::test3),"Test 3: Assign values");
  41 + add_test(static_cast<pfunction>(&TestGVector::test4),"Test 4: Define vector using copy constructor");
  42 + add_test(static_cast<pfunction>(&TestGVector::test5),"Test 5: Vector assignment");
  43 + add_test(static_cast<pfunction>(&TestGVector::test6),"Test 6: Assignment and arithmetics");
  44 + add_test(static_cast<pfunction>(&TestGVector::test7),"Test 7: Comparison");
  45 +
  46 + return;
  47 +}
  48 +
  49 +// Define vectors
  50 +void TestGVector::define_vectors(void){
  51 + m_test= GVector(m_num);
  52 + m_result=GVector(m_num);
  53 + m_smaller=GVector(m_num+1);
  54 + m_bigger=GVector(m_num+1);
  55 + for (int i = 0; i < m_num; ++i)
  56 + m_test[i] = (i+1) * 1.1;
  57 + for (int i = 0; i < m_num-1; ++i)
  58 + m_smaller[i] = (i+1) * 1.1;
  59 + for (int i = 0; i < m_num+1; ++i)
  60 + m_bigger[i] = (i+1) * 1.1;
  61 +}
  62 +
  63 +//Test 1: Allocate zero vector
  64 +void TestGVector::test1(void){
56 65 GVector test1(0);
57   - cout << " Size = " << test1.size() << endl;
58   - }
59   - catch (exception &e) {
60   - cout << e.what() << endl;
61   - }
62   -
63   - // Test 2: Allocate too large vector
64   - /*
65   - try {
66   - cout << "Test 2: Allocate too large vector: " << endl;
  66 +}
  67 +
  68 +//Test 2: Allocate too large vector
  69 +void TestGVector::test2(void){
  70 + /*
67 71 GVector test2(1000000000);
68   - cout << " Size = " << test2.size() << endl;
69   - }
70   - catch (exception &e) {
71   - cout << e.what() << endl;
72   - }
73   - */
74   -
75   - // Test 3: Assign values
76   - try {
77   - cout << "Test 3: Assign values:" << endl;
78   - //
79   - cout << " test3(1) = pi: ";
  72 + */
  73 +}
  74 +
  75 +//Test 3: Assign values
  76 +void TestGVector::test3(void){
80 77 GVector test3(3);
81   - test3[1] = acos(-1.0);
82   - cout << test3 << endl;
83   - cout << " Size = " << test3.size() << endl;
84   - //
85   - cout << " GVector out of range access: ";
  78 + test3[1] = acos(-1.0);
  79 +
  80 + test_assert(test3[0]==0&&test3[1]==acos(-1.0)&&test3[2]==0,"test3 == (0, pi, 0)");
  81 + test_assert(test3.size()==3,"test3.size()==3");
  82 +
86 83 #if defined(G_RANGE_CHECK)
87   - cout << test3(3) << endl;
88   - #endif
89   - }
90   - catch (exception &e) {
91   - cout << e.what() << endl;
92   - }
93   -
94   - // Test 4: Vector copy constructor
95   - try {
96   - cout << "Test 4: Define vector using copy constructor:" << endl;
97   - //
98   - cout << " GVector: ";
99   - cout << test << endl;
100   - //
101   - cout << " GVector test = GVector: ";
102   - GVector test4 = test;
103   - cout << test4 << endl;
104   - }
105   - catch (exception &e) {
106   - cout << e.what() << endl;
107   - }
108   -
109   - // Test 5: Vector assignment
110   - try {
111   - cout << "Test 5: Vector assignment:" << endl;
112   - //
113   - cout << " GVector: ";
114   - cout << test << endl;
115   - //
116   - cout << " GVector = GVector: ";
117   - result = test;
118   - cout << result << endl;
119   - //
120   - cout << " GVector = GVector (bigger vector): ";
121   - result = bigger;
122   - cout << result << endl;
123   - }
124   - catch (exception &e) {
125   - cout << e.what() << endl;
126   - }
127   -
128   - // Test 6: Assignment and arithmetics
129   - try {
130   - cout << "Test 6: Assignment and arithmetics:" << endl;
131   - //
132   - cout << " GVector: ";
133   - cout << test << endl;
134   - //
135   - cout << " GVector += GVector: ";
136   - result = test;
137   - result += test;
138   - cout << result << endl;
139   - //
140   - cout << " GVector += 2.0: ";
141   - result = test;
142   - result += 2.0;
143   - cout << result << endl;
144   - //
145   - cout << " GVector -= GVector: ";
146   - result = test;
147   - result -= test;
148   - cout << result << endl;
149   - //
150   - cout << " GVector -= 2.0: ";
151   - result = test;
152   - result -= 2.0;
153   - cout << result << endl;
154   - //
155   - cout << " GVector *= 2.0: ";
156   - result = test;
157   - result *= 2.0;
158   - cout << result << endl;
159   - //
160   - cout << " GVector /= 2.0: ";
161   - result = test;
162   - result /= 2.0;
163   - cout << result << endl;
164   - //
165   - cout << " GVector = -GVector: ";
166   - result = -test;
167   - cout << result << endl;
168   - //
169   - cout << " Devide by zero: ";
170   - result = test;
171   - result /= 0.0;
172   - cout << result << endl;
173   - //
174   - cout << " GVector + GVector: ";
175   - result = test + test;
176   - cout << result << endl;
177   - //
178   - cout << " GVector + 2.0: ";
179   - result = test + 2.0;
180   - cout << result << endl;
181   - //
182   - cout << " GVector + 2: ";
183   - result = test + 2;
184   - cout << result << endl;
185   - //
186   - cout << " 2.0 + GVector: ";
187   - result = 2.0 + test;
188   - cout << result << endl;
189   - //
190   - cout << " 2 + GVector: ";
191   - result = 2 + test;
192   - cout << result << endl;
193   - //
194   - cout << " GVector - GVector: ";
195   - result = test - test;
196   - cout << result << endl;
197   - //
198   - cout << " GVector - 2.0: ";
199   - result = test - 2.0;
200   - cout << result << endl;
201   - //
202   - cout << " 2.0 - GVector: ";
203   - result = 2.0 - test;
204   - cout << result << endl;
205   - //
206   - cout << " Scalar (or dot) product GVector * GVector: ";
207   - cout << (test * test) << endl;
208   - //
209   - cout << " GVector * 2.0: ";
210   - result = test * 2.0;
211   - cout << result << endl;
212   - //
213   - cout << " 2.0 * GVector: ";
214   - result = 2.0 * test;
215   - cout << result << endl;
216   - //
217   - cout << " |GVector| (vector norm): ";
218   - cout << norm(test) << endl;
219   - //
220   - cout << " min(GVector): ";
221   - cout << min(test) << endl;
222   - //
223   - cout << " max(GVector): ";
224   - cout << max(test) << endl;
225   - //
226   - cout << " sum(GVector): ";
227   - cout << sum(test) << endl;
228   - //
229   - cout << " GVector: ";
230   - cout << test << endl;
231   - //
232   - cout << " acos(GVector/10.0): ";
233   - cout << acos(test/10.0) << endl;
234   - //
235   - cout << " acosh(GVector): ";
236   - cout << acosh(test) << endl;
237   - //
238   - cout << " asin(GVector/10.0): ";
239   - cout << asin(test/10.0) << endl;
240   - //
241   - cout << " asinh(GVector/10.0): ";
242   - cout << asinh(test/10.0) << endl;
243   - //
244   - cout << " atan(GVector/10.0): ";
245   - cout << atan(test/10.0) << endl;
246   - //
247   - cout << " atanh(GVector/10.0): ";
248   - cout << atanh(test/10.0) << endl;
249   - //
250   - cout << " cos(GVector): ";
251   - cout << cos(test) << endl;
252   - //
253   - cout << " cosh(GVector): ";
254   - cout << cosh(test) << endl;
255   - //
256   - cout << " exp(GVector): ";
257   - cout << exp(test) << endl;
258   - //
259   - cout << " abs(cos(GVector)): ";
260   - cout << abs(cos(test)) << endl;
261   - //
262   - cout << " log(GVector): ";
263   - cout << log(test) << endl;
264   - //
265   - cout << " log10(GVector): ";
266   - cout << log10(test) << endl;
267   - //
268   - cout << " sin(GVector): ";
269   - cout << sin(test) << endl;
270   - //
271   - cout << " sinh(GVector): ";
272   - cout << sinh(test) << endl;
273   - //
274   - cout << " sqrt(GVector): ";
275   - cout << sqrt(test) << endl;
276   - //
277   - cout << " tan(GVector): ";
278   - cout << tan(test) << endl;
  84 + test_try("Test out of range access");
  85 + try {
  86 + test3[3];
  87 +
  88 + test_try_failure();
  89 + }
  90 + catch (GException::out_of_range &e) {
  91 + test_try_success();
  92 + }
  93 + catch (std::exception &e) {
  94 + test_try_failure(e);
  95 + }
  96 + #endif
  97 +}
  98 +
  99 +//Test 4: Vector copy constructor
  100 +void TestGVector::test4(void){
  101 +
  102 + GVector test4 = m_test;
  103 + test_assert(test4==m_test,"test4 == m_test");
  104 +}
  105 +
  106 +//Test 5: Vector assignment
  107 +void TestGVector::test5(void){
  108 + m_result = m_test;
  109 +
  110 + test_assert(m_result==m_test,"m_result == m_test");
  111 +
  112 + m_result = m_bigger;
  113 + test_assert(m_result==m_bigger,"m_result == m_bigger");
  114 + test_assert(m_result.size()==m_bigger.size(),"m_result.size() == m_bigger.size()");
  115 +}
  116 +
  117 +//Test 6: Assignment and arithmetics
  118 +void TestGVector::test6(void){
  119 + m_result = m_test;
  120 + m_result += m_test;
  121 +
  122 + // GVector += GVector
  123 + test_assert(m_result[0]==m_test[0]*2&&
  124 + m_result[1]==m_test[1]*2&&
  125 + m_result[2]==m_test[2]*2&&
  126 + m_result[3]==m_test[3]*2&&
  127 + m_result[4]==m_test[4]*2,"GVector += GVector");
  128 +
  129 + // GVector += 2.0
  130 + m_result = m_test;
  131 + m_result += 2.0;
  132 + test_assert(m_result[0]==m_test[0]+2&&
  133 + m_result[1]==m_test[1]+2&&
  134 + m_result[2]==m_test[2]+2&&
  135 + m_result[3]==m_test[3]+2&&
  136 + m_result[4]==m_test[4]+2,"GVector += 2.0");
  137 +
  138 + // GVector -= GVector
  139 + m_result = m_test;
  140 + m_result -= m_test;
  141 + test_assert(m_result[0]==0&&
  142 + m_result[1]==0&&
  143 + m_result[2]==0&&
  144 + m_result[3]==0&&
  145 + m_result[4]==0,"GVector -= GVector");
  146 +
  147 + // GVector -= 2.0
  148 + m_result = m_test;
  149 + m_result -= 2.0;
  150 + test_assert(m_result[0]==m_test[0]-2&&
  151 + m_result[1]==m_test[1]-2&&
  152 + m_result[2]==m_test[2]-2&&
  153 + m_result[3]==m_test[3]-2&&
  154 + m_result[4]==m_test[4]-2,"GVector -= 2.0");
  155 +
  156 + // GVector *= 2.0
  157 + m_result = m_test;
  158 + m_result *= 2.0;
  159 + test_assert(m_result[0]==m_test[0]*2&&
  160 + m_result[1]==m_test[1]*2&&
  161 + m_result[2]==m_test[2]*2&&
  162 + m_result[3]==m_test[3]*2&&
  163 + m_result[4]==m_test[4]*2,"GVector *= 2.0");
  164 +
  165 + // GVector /= 2.0
  166 + m_result = m_test;
  167 + m_result /= 2.0;
  168 + test_assert(m_result[0]==m_test[0]/2&&
  169 + m_result[1]==m_test[1]/2&&
  170 + m_result[2]==m_test[2]/2&&
  171 + m_result[3]==m_test[3]/2&&
  172 + m_result[4]==m_test[4]/2,"GVector /= 2.0");
  173 +
  174 + // GVector = -GVector
  175 + m_result = -m_test;
  176 + test_assert(m_result[0]== -m_test[0]&&
  177 + m_result[1]==-m_test[1]&&
  178 + m_result[2]==-m_test[2]&&
  179 + m_result[3]==-m_test[3]&&
  180 + m_result[4]==-m_test[4],"GVector = -GVector");
  181 +
  182 + // Devide by zero
  183 + m_result = m_test;
  184 + m_result /= 0.0;
  185 + test_assert(m_result.print()=="(inf, inf, inf, inf, inf)","Devide by zero");
  186 +
  187 + // GVector + GVector
  188 + m_result = m_test + m_test;
  189 + test_assert(m_result[0]==m_test[0]*2&&
  190 + m_result[1]==m_test[1]*2&&
  191 + m_result[2]==m_test[2]*2&&
  192 + m_result[3]==m_test[3]*2&&
  193 + m_result[4]==m_test[4]*2,"GVector + GVector");
  194 +
  195 + // GVector + 2.0
  196 + m_result = m_test + 2.0;
  197 + test_assert(m_result[0]==m_test[0]+2&&
  198 + m_result[1]==m_test[1]+2&&
  199 + m_result[2]==m_test[2]+2&&
  200 + m_result[3]==m_test[3]+2&&
  201 + m_result[4]==m_test[4]+2,"GVector + 2.0");
  202 +
  203 + // GVector + 2
  204 + m_result = m_test + 2;
  205 + test_assert(m_result[0]==m_test[0]+2&&
  206 + m_result[1]==m_test[1]+2&&
  207 + m_result[2]==m_test[2]+2&&
  208 + m_result[3]==m_test[3]+2&&
  209 + m_result[4]==m_test[4]+2,"GVector + 2");
  210 + //
  211 + m_result = 2.0 + m_test;
  212 + test_assert(m_result[0]==m_test[0]+2&&
  213 + m_result[1]==m_test[1]+2&&
  214 + m_result[2]==m_test[2]+2&&
  215 + m_result[3]==m_test[3]+2&&
  216 + m_result[4]==m_test[4]+2,"2.0 + GVector");
  217 +
  218 + // 2 + GVector
  219 + m_result = 2 + m_test;
  220 + test_assert(m_result[0]==m_test[0]+2&&
  221 + m_result[1]==m_test[1]+2&&
  222 + m_result[2]==m_test[2]+2&&
  223 + m_result[3]==m_test[3]+2&&
  224 + m_result[4]==m_test[4]+2,"2 + GVector");
  225 +
  226 + // GVector - GVector
  227 + m_result = m_test - m_test;
  228 + test_assert(m_result[0]==0&&
  229 + m_result[1]==0&&
  230 + m_result[2]==0&&
  231 + m_result[3]==0&&
  232 + m_result[4]==0,"GVector - GVector");
  233 +
  234 + // GVector - 2.0
  235 + m_result = m_test - 2.0;
  236 + test_assert(m_result[0]==m_test[0]-2&&
  237 + m_result[1]==m_test[1]-2&&
  238 + m_result[2]==m_test[2]-2&&
  239 + m_result[3]==m_test[3]-2&&
  240 + m_result[4]==m_test[4]-2,"GVector - 2.0");
  241 +
  242 + // 2.0 - GVector
  243 + m_result = 2.0 - m_test;
  244 + test_assert(m_result[0]==2-m_test[0]&&
  245 + m_result[1]==2-m_test[1]&&
  246 + m_result[2]==2-m_test[2]&&
  247 + m_result[3]==2-m_test[3]&&
  248 + m_result[4]==2-m_test[4],"2.0 - GVector");
  249 +
  250 + // Scalar (or dot) product GVector * GVector
  251 + test_assert(m_test[0]*m_test[0]+
  252 + m_test[1]*m_test[1]+
  253 + m_test[2]*m_test[2]+
  254 + m_test[3]*m_test[3]+
  255 + m_test[4]*m_test[4]== m_test * m_test,"Scalar (or dot) product GVector * GVector");
  256 +
  257 + // GVector * 2.0
  258 + m_result = m_test * 2.0;
  259 + test_assert(m_result[0]==m_test[0]*2&&
  260 + m_result[1]==m_test[1]*2&&
  261 + m_result[2]==m_test[2]*2&&
  262 + m_result[3]==m_test[3]*2&&
  263 + m_result[4]==m_test[4]*2,"GVector * 2.0");
  264 +
  265 + // 2.0 * GVector
  266 + m_result = 2.0 * m_test;
  267 + test_assert(m_result[0]==m_test[0]*2&&
  268 + m_result[1]==m_test[1]*2&&
  269 + m_result[2]==m_test[2]*2&&
  270 + m_result[3]==m_test[3]*2&&
  271 + m_result[4]==m_test[4]*2,"2.0 * GVector");
  272 +
  273 + // |GVector| (vector norm))
  274 + test_assert(sqrt( m_test[0]*m_test[0]+
  275 + m_test[1]*m_test[1]+
  276 + m_test[2]*m_test[2]+
  277 + m_test[3]*m_test[3]+
  278 + m_test[4]*m_test[4])==norm(m_test),"|GVector| (vector norm))");
  279 +
  280 + // min(GVector)
  281 + test_assert(min(m_test)==1.1,"min(GVector)");
  282 +
  283 + // max(GVector)
  284 + test_assert(max(m_test)==5.5,"max(GVector)");
  285 +
  286 + // sum(GVector)
  287 + test_assert(m_test[0]+m_test[1]+m_test[2]+m_test[3]+m_test[4]==sum(m_test),"sum(GVector)");
  288 +
  289 + // acos(GVector/10.0)
  290 + test_assert(acos(m_test/10.0).print()=="(1.46057, 1.34898, 1.23449, 1.1152, 0.988432)","acos(GVector/10.0)");
  291 +
  292 + // acosh(GVector)
  293 + test_assert(acosh(m_test).print()=="(0.443568, 1.42542, 1.86328, 2.16158, 2.38953)","acosh(GVector)");
  294 +
  295 + // asin(GVector/10.0)
  296 + test_assert(asin(m_test/10.0).print()=="(0.110223, 0.221814, 0.336304, 0.455599, 0.582364)","asin(GVector/10.0)");
  297 +
  298 + // asinh(GVector/10.0)
  299 + test_assert(asinh(m_test/10.0).print()=="(0.109779, 0.218263, 0.324286, 0.426913, 0.52548)","asinh(GVector/10.0)");
  300 +
  301 + // atan(GVector/10.0)
  302 + test_assert(atan(m_test/10.0).print()=="(0.10956, 0.21655, 0.318748, 0.414507, 0.502843)","atan(GVector/10.0)");
  303 +
  304 + // atanh(GVector/10.0)
  305 + test_assert(atanh(m_test/10.0).print()=="(0.110447, 0.223656, 0.342828, 0.472231, 0.618381)","atanh(GVector/10.0)");
  306 +
  307 + // cos(GVector)
  308 + test_assert(cos(m_test).print()=="(0.453596, -0.588501, -0.98748, -0.307333, 0.70867)","cos(GVector)");
  309 +
  310 + // cosh(GVector)
  311 + test_assert(cosh(m_test).print()=="(1.66852, 4.56791, 13.5748, 40.7316, 122.348)","cosh(GVector)");
  312 +
  313 + // exp(GVector)
  314 + test_assert(exp(m_test).print()=="(3.00417, 9.02501, 27.1126, 81.4509, 244.692)","exp(GVector)");
  315 +
  316 + // abs(cos(m_test))
  317 + test_assert(abs(cos(m_test)).print()=="(0.453596, 0.588501, 0.98748, 0.307333, 0.70867)","abs(cos(m_test))");
  318 +
  319 + // log(GVector)
  320 + test_assert(log(m_test).print()=="(0.0953102, 0.788457, 1.19392, 1.4816, 1.70475)","log(GVector)");
  321 +
  322 + // log10(GVector)
  323 + test_assert(log10(m_test).print()=="(0.0413927, 0.342423, 0.518514, 0.643453, 0.740363)","log10(GVector)");
  324 +
  325 + // sin(GVector)
  326 + test_assert(sin(m_test).print()=="(0.891207, 0.808496, -0.157746, -0.951602, -0.70554)","sin(GVector)");
  327 +
  328 + // sinh(GVector)
  329 + test_assert(sinh(m_test).print()=="(1.33565, 4.45711, 13.5379, 40.7193, 122.344)","sinh(GVector)");
  330 +
  331 + // sqrt(GVector)
  332 + test_assert(sqrt(m_test).print()=="(1.04881, 1.48324, 1.81659, 2.09762, 2.34521)","sqrt(GVector)");
  333 +
  334 + // tan(GVector)
  335 + test_assert(tan(m_test).print()=="(1.96476, -1.37382, 0.159746, 3.09632, -0.995584)","tan(GVector)");
  336 +
  337 + // tanh(GVector)
  338 + test_assert(tanh(m_test).print()=="(0.800499, 0.975743, 0.997283, 0.999699, 0.999967)","tanh(GVector)");
  339 +
279 340 //
280   - cout << " tanh(GVector): ";
281   - cout << tanh(test) << endl;
282   - //
283   - cout << " Incompatible size GVector + GVector: ";
284   - result = test + bigger;
285   - cout << result << endl;
286   - }
287   - catch (exception &e) {
288   - cout << e.what() << endl;
289   - }
290   - try {
291   - cout << " cross(a,b) (using 5-dim vectors): ";
292   - cout << cross(test,test) << endl;
293   - }
294   - catch (exception &e) {
295   - cout << e.what() << endl;
296   - }
297   - try {
298   - cout << " cross(a,b) (using vectors with different dimension): ";
299   - cout << cross(test,bigger) << endl;
300   - }
301   - catch (exception &e) {
302   - cout << e.what() << endl;
303   - }
304   - try {
305   - cout << " cross(a,b) (using 3-dim vectors): " << endl;
306   - GVector test_cross_a(3);
307   - GVector test_cross_b(3);
308   - test_cross_a[0] = 1.0;
309   - test_cross_b[1] = 1.0;
310   - cout << " a: " << test_cross_a << endl;
311   - cout << " b: " << test_cross_b << endl;
312   - cout << " cross: " << cross(test_cross_a, test_cross_b) << endl;
313   - }
314   - catch (exception &e) {
315   - cout << e.what() << endl;
316   - }
317   -
318   - // Test 7: Vector comparison
319   - try {
320   - cout << "Test 7: Comparison:" << endl;
321   - //
322   - cout << " GVector == GVector: ";
323   - cout << (test == test) << endl;
324   - //
325   - cout << " GVector == GVector(0): ";
326   - GVector test7(num);
327   - cout << (test == test7) << endl;
328   - //
329   - cout << " GVector == GVector (bigger): ";
330   - cout << (test == bigger) << endl;
331   - //
332   - cout << " GVector != GVector: ";
333   - cout << (test != test) << endl;
334   - //
335   - cout << " GVector != GVector(0): ";
336   - cout << (test != test7) << endl;
337   - //
338   - cout << " GVector != GVector (bigger): ";
339   - cout << (test != bigger) << endl;
340   - }
341   - catch (exception &e) {
342   - cout << e.what() << endl;
343   - }
344   -
345   -
346   - // Return
347   - return 0;
  341 +
  342 + // Incompatible size GVector + GVector
  343 + test_try("Incompatible size GVector + GVector:");
  344 + try {
  345 + m_result = m_test + m_bigger;
  346 +
  347 + test_try_failure();
  348 + }
  349 + catch (GException::vector_mismatch &e) {
  350 + test_try_success();
  351 + }
  352 + catch (std::exception &e) {
  353 + test_try_failure(e);
  354 + }
  355 +
  356 + // cross(a,b) (using 5-dim vectors)
  357 + test_try("cross(a,b) (using 5-dim vectors)");
  358 + try {
  359 + cross(m_test,m_test);
  360 +
  361 + test_try_failure();
  362 + }
  363 + catch (GException::vector_bad_cross_dim &e) {
  364 + test_try_success();
  365 + }
  366 + catch (std::exception &e) {
  367 + test_try_failure(e);
  368 + }
  369 +
  370 + //cross(a,b) (using vectors with different dimension)
  371 + test_try("cross(a,b) (using vectors with different dimension)");
  372 + try {
  373 + cross(m_test,m_bigger);
  374 +
  375 + test_try_failure();
  376 + }
  377 + catch (GException::vector_mismatch &e) {
  378 + test_try_success();
  379 + }
  380 + catch (std::exception &e) {
  381 + test_try_failure(e);
  382 + }
  383 +
  384 + //cross(a,b) (using 3-dim vectors)
  385 + test_try("cross(a,b) (using 3-dim vectors)");
  386 + try {
  387 + GVector test_cross_a(3);
  388 + GVector test_cross_b(3);
  389 + test_cross_a[0] = 1.0;
  390 + test_cross_b[1] = 1.0;
  391 +
  392 + //Test if cross == (0,0,1)
  393 + test_assert(cross(test_cross_a, test_cross_b)[0]==0&&cross(test_cross_a, test_cross_b)[1]==0&&cross(test_cross_a, test_cross_b)[2]==1,"Check cross(a,b) value");
  394 +
  395 + test_try_success();
  396 + }
  397 + catch (std::exception &e) {
  398 + test_try_failure(e);
  399 + }
  400 +}
  401 +
  402 +//Test 7: Comparison
  403 +void TestGVector::test7(void){
  404 +
  405 + // GVector == GVector
  406 + test_assert((m_test == m_test),"GVector == GVector");
  407 +
  408 + // GVector == GVector(0)
  409 + GVector test7(m_num);
  410 + test_assert(!(m_test == test7),"GVector == GVector(0)");
  411 +
  412 + //GVector == GVector (m_bigger)
  413 + test_assert(!(m_test == m_bigger),"GVector == GVector (m_bigger)");
  414 +
  415 + // GVector != GVector
  416 + test_assert(!(m_test != m_test),"GVector != GVector");
  417 +
  418 + // GVector != GVector(0)
  419 + test_assert((m_test != test7),"GVector != GVector(0)");
  420 +
  421 + // GVector != GVector (m_bigger)
  422 + test_assert((m_test != m_bigger),"GVector != GVector (m_bigger)");
  423 +}
  424 +int main(void)
  425 +{
  426 + GTestSuites testsuites("GVector");
  427 +
  428 + bool was_successful=true;
  429 +
  430 + //Create a test suite
  431 + TestGVector test;
  432 +
  433 + //Append to the container
  434 + testsuites.append(test);
  435 +
  436 + //Run
  437 + was_successful=testsuites.run();
  438 +
  439 + //save xml report
  440 + testsuites.save("reports/GVector.xml");
  441 +
  442 + // Return
  443 + return was_successful ? 0:1;
348 444 }
... ...
test/test_GVector.hpp 0 → 100644
  1 +/***************************************************************************
  2 + * test_GVector.hpp - test vector class *
  3 + * ----------------------------------------------------------------------- *
  4 + * copyright (C) 2012 by Jean-Baptiste Cayrou *
  5 + * ----------------------------------------------------------------------- *
  6 + * *
  7 + * This program is free software: you can redistribute it and/or modify *
  8 + * it under the terms of the GNU General Public License as published by *
  9 + * the Free Software Foundation, either version 3 of the License, or *
  10 + * (at your option) any later version. *
  11 + * *
  12 + * This program is distributed in the hope that it will be useful, *
  13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15 + * GNU General Public License for more details. *
  16 + * *
  17 + * You should have received a copy of the GNU General Public License *
  18 + * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  19 + * *
  20 + ***************************************************************************/
  21 +
  22 +#ifndef TEST_GVECTOR_HPP
  23 +#define TEST_GVECTOR_HPP
  24 +
  25 +/* __ Includes ___________________________________________________________ */
  26 +#include "GammaLib.hpp"
  27 +#include <iostream> // cout, cerr
  28 +
  29 +class TestGVector : public GTestSuite
  30 +{
  31 + public:
  32 + // Constructors and destructors
  33 + TestGVector(void) : GTestSuite(){ return; }
  34 + virtual ~TestGVector(void){ return; }
  35 +
  36 + // Methods
  37 + virtual void set(void);
  38 + void define_vectors(void);
  39 + void test1(void);
  40 + void test2(void);
  41 + void test3(void);
  42 + void test4(void);
  43 + void test5(void);
  44 + void test6(void);
  45 + void test7(void);
  46 +
  47 +
  48 + // Private members
  49 + private:
  50 + int m_num;
  51 + GVector m_test;
  52 + GVector m_result;
  53 + GVector m_smaller;
  54 + GVector m_bigger;
  55 +};
  56 +
  57 +#endif /* TEST_GVECTOR_HPP */
... ...