Debian Codegraph @

contrib/ms-sbvar/utilities_dw/test_routines/matrix_test/c++/matrix_test_main.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "matrix_test.hpp"
19 #include "matrix_test_instances.hpp"
20 #include "dw_error.h"
21 #include "dw_parse_cmd.h"
22 #include "dw_ascii.h"
23 #include "dw_std.h"
24
25 #include <fstream>
26
27 #include <string.h>
28 #include <time.h>
29 #include <sys/time.h>
30
31 //===============================================================================
32 //===============================================================================
33 //===============================================================================
34
35 void PrintMessage(ostream &out, TFunction *fnct, char *message)
36 {
37 out << fnct->GetName();
38 fnct->PrintArgList(out);
39 out << message << "\n";
40 out << flush;
41 }
42
43 int EvaluateErr(char *msg, PRECISION err)
44 {
45 if ((err < 0.0) || (err > SQRT_MACHINE_EPSILON))
46 {
47 sprintf(msg," - Not OK - Error : %le",err);
48 return 1;
49 }
50 else
51 {
52 if (err > MACHINE_EPSILON)
53 sprintf(msg," - OK - Correct to square root machine epsilon : %le",err);
54 else
55 sprintf(msg," - OK - Correct to machine epsilon : %le",err);
56 return 0;
57 }
58 }
59
60 char* GetErrorString(int arg_err, char *arg_string)
61 {
62 arg_string[0]='\0';
63 if (arg_err & ARG_ERR_SIZE) strcat(arg_string,"size");
64 if (arg_err & ARG_ERR_NULL) strcat(arg_string,arg_string[0] ? "/null" : "null");
65 if (arg_err & ARG_ERR_GENERAL) strcat(arg_string,arg_string[0] ? "/general" : "general");
66 return arg_string;
67 }
68
69 int Test(TFunction *fnct, int verbose, ostream &out)
70 {
71 PRECISION err;
72 int VerboseErrors, TerminalErrors, rtrn, arg_err, fnct_err, total_err=0;
73 char msg[512], err_string[128];
74
75 VerboseErrors=dw_SetVerboseErrors(0);
76 TerminalErrors=dw_SetTerminalErrors(0);
77
78 do
79 {
80 rtrn=fnct->CallFunction();
81 if (fnct->ArgChanged())
82 {
83 sprintf(msg," - Not OK - pointer argument improperly changed");
84 fnct_err=1;
85 }
86 else
87 if (rtrn)
88 fnct_err=EvaluateErr(msg,err=fnct->ResultOK());
89 else
90 if (arg_err=fnct->ArgumentsError())
91 {
92 sprintf(msg," - OK - Argument Error : %s : %s",GetErrorString(arg_err,err_string),dw_GetErrorMessage());
93 fnct_err=0;
94 }
95 else
96 {
97 sprintf(msg," - Not OK - function should have valid return : %s",dw_GetErrorMessage());
98 fnct_err=1;
99 }
100
101 if (verbose || fnct_err) PrintMessage(out,fnct,msg);
102
103 fnct->CleanCall();
104
105 total_err|=fnct_err;
106 }
107 while (fnct->Next());
108
109 dw_SetVerboseErrors(VerboseErrors);
110 dw_SetTerminalErrors(TerminalErrors);
111
112 return total_err;
113 }
114
115 int TestFunction(TFunction *fnct, int min, int max, int verbose, ostream &out)
116 {
117 int total_err=0;
118
119 // Basic Test
120 if (verbose) out << "=== Basic Test ====================\n";
121 if (fnct->TestBasic(min,max))
122 total_err|=Test(fnct,verbose,out);
123
124 // Equal Pointers Test
125 if (verbose) out << "=== Equal Pointers Test ====================\n";
126 if (fnct->TestEqualPointers(min,max))
127 total_err|=Test(fnct,verbose,out);
128
129 // Invalid Sizes Test
130 if (verbose) out << "=== Invalid Sizes Test ====================\n";
131 if (fnct->TestInvalidSizes(min,max))
132 total_err|=Test(fnct,verbose,out);
133
134 // Invalid Nulls Test
135 if (verbose) out << "=== Invalid Nulls Test ====================\n";
136 if (fnct->TestInvalidNulls(min,max))
137 total_err|=Test(fnct,verbose,out);
138
139 cout << fnct->GetName() << (total_err ? " - Not OK\n" : " - OK\n");
140
141 return total_err;
142 }
143
144 int TestFunctionAll(TFunction_Node *FunctionList, int min, int max, int verbose, ostream &out)
145 {
146 TFunction *fnct;
147 int total_err=0;
148 if (FunctionList)
149 do
150 {
151 fnct=FunctionList->GetNode();
152 total_err+=TestFunction(fnct,min,max,verbose,out);
153 }
154 while (FunctionList=FunctionList->Next());
155 return total_err;
156 }
157
158 int TestFunctionSingle(TFunction *fnct, int min, int max, int verbose, ostream &out)
159 {
160 return TestFunction(fnct,min,max,verbose,out);
161 }
162
163 int SpeedTest(TFunction *fnct, int size, int count, ostream &out)
164 {
165 int btime, etime, TerminalErrors;
166 timeval btimeval, etimeval;
167 if (size <= 0) size=10;
168 out << "Speed test " << count << " - " << fnct->GetName() << endl;
169 if (fnct->TestSpeed(size))
170 {
171 TerminalErrors=dw_SetTerminalErrors(0);
172 gettimeofday(&btimeval,NULL);
173 btime=clock();
174 for (int i=0; i < count; i++)
175 {
176 fnct->CallFunction();
177 fnct->CleanCall();
178 }
179 etime=clock();
180 gettimeofday(&etimeval,NULL);
181 dw_SetTerminalErrors(TerminalErrors);
182 out << "Elapsed CPU time: " << (double)(etime - btime)/(double)CLOCKS_PER_SEC << " seconds\n";
183 out << "Elapsed time: " << (etimeval.tv_sec+etimeval.tv_usec*1e-6)-(btimeval.tv_sec+btimeval.tv_usec*1e-6) << " seconds\n";
184 }
185 }
186
187 //===============================================================================
188 //===============================================================================
189 //===============================================================================
190
191 void PrintUntested(TFunction_Node *FunctionList, char *filename, ostream &out)
192 {
193 TFunction_Node *node;
194 TFunction *fnct;
195 FILE *f_in;
196 char *buffer1=(char*)NULL, *buffer2=(char*)NULL, *sbuffer, ch;
197 int n1=0, n2=0, sn, j, k;
198
199 f_in=dw_OpenTextFile(filename);
200
201 buffer2=dw_ReadLine(f_in,buffer2,&n2);
202
203 while (buffer1=dw_ReadLine(f_in,buffer1,&n1))
204 {
205 if (buffer1[0] == '{')
206 {
207 for (k=0; buffer2[k] && !isspace(buffer2[k]); k++);
208 for ( ; isspace(buffer2[k]); k++);
209 for (j=k; buffer2[j] && (buffer2[j] != '('); j++);
210 ch=buffer2[j];
211 buffer2[j]='\0';
212 for (node=FunctionList; node; node=node->Next())
213 {
214 fnct=node->GetNode();
215 if (!strcmp(buffer2+k,fnct->GetName())) break;
216 }
217 buffer2[j]=ch;
218 if (!node) out << buffer2;
219 }
220 sbuffer=buffer1;
221 buffer1=buffer2;
222 buffer2=sbuffer;
223 sn=n1;
224 n1=n2;
225 n2=sn;
226 }
227
228 fclose(f_in);
229 }
230
231 void PrintAll(TFunction_Node *FunctionList, char *filename, ostream &out)
232 {
233 FILE *f_in;
234 char *buffer1=(char*)NULL, *buffer2=(char*)NULL, *sbuffer;
235 int n1=0, n2=0, sn, k;
236
237 f_in=dw_OpenTextFile(filename);
238
239 buffer2=dw_ReadLine(f_in,buffer2,&n2);
240
241 while (buffer1=dw_ReadLine(f_in,buffer1,&n1))
242 {
243 if (buffer1[0] == '{') out << buffer2;
244 sbuffer=buffer1;
245 buffer1=buffer2;
246 buffer2=sbuffer;
247 sn=n1;
248 n1=n2;
249 n2=sn;
250 }
251
252 fclose(f_in);
253 }
254
255 //===============================================================================
256 //===============================================================================
257 //===============================================================================
258
259 int main(int nargs, char **args)
260 {
261 TFunction_Node *FunctionList, *Node;
262 TFunction *fnct;
263 ofstream out;
264 char ch[256], *filename, *fnct_name, *cfilename;
265 int min, max, verbose, i, count, total_err;
266
267 if (dw_FindArgument(nargs,args,'h') != -1)
268 {
269 printf("\nOptions"
270 "\n -b <size> minimum size to test (default: 1,2,3,7,10 or maximum)"
271 "\n -t <size> maximum size to test (default: 1,2,3,7,10 or minimum)"
272 "\n -f <function name> function to test (default: all functions)"
273 "\n -o <file name> output file (default: test.dat)"
274 "\n -v verbose mode (default: off)"
275 "\n -l make list of untested functions in file"
276 "\n -a make list of all functions in file"
277 "\n -s <count> speed test (default: 100 | must be used with -f | test size is minimum)"
278 "\n -h this help screen\n");
279 return 0;
280 }
281
282 filename=dw_ParseString(nargs,args,'o',"test.dat");
283 out.open(filename);
284 if (!out.is_open())
285 {
286 cerr << "Unable to open " << filename << endl;
287 dw_exit(0);
288 }
289
290 min=dw_ParseInteger(nargs,args,'b',-1);
291 if ((max=dw_ParseInteger(nargs,args,'t',min)) < min) max=min;
292
293 verbose=(dw_FindArgument(nargs,args,'v') != -1) ? 1 : 0;
294
295 dw_initialize_generator(30);
296
297 FunctionList=CreateFunctionList();
298
299 if (cfilename=dw_ParseString(nargs,args,'l',(char*)NULL))
300 PrintUntested(FunctionList,cfilename,out);
301 else if (cfilename=dw_ParseString(nargs,args,'a',(char*)NULL))
302 PrintAll(FunctionList,cfilename,out);
303 else if (fnct_name=dw_ParseString(nargs,args,'f',(char*)NULL))
304 {
305 for (Node=FunctionList; Node; Node=Node->Next())
306 {
307 fnct=Node->GetNode();
308 if (!strcmp(fnct_name,fnct->GetName()))
309 {
310 if (dw_FindArgument(nargs,args,'s') != -1)
311 SpeedTest(fnct,min,dw_ParseInteger(nargs,args,'s',100),cout);
312 else
313 do
314 {
315 cout << '\n';
316 TestFunctionSingle(fnct,min,max,verbose,out);
317 cout << "Enter 'q' to quit ";
318 cin.getline(ch,256);
319 }
320 while (toupper(ch[0]) != 'Q');
321 break;
322 }
323 }
324 if (!Node)
325 cout << "Function not found: " << fnct_name << endl;
326 }
327 else
328 do
329 {
330 cout << '\n';
331 if (total_err=TestFunctionAll(FunctionList,min,max,verbose,out))
332 cout << "\nAn error occured in " << total_err << " of the functions\n";
333 else
334 cout << "\nAll OK\n";
335 cout << "Enter 'q' to quit ";
336 cin.getline(ch,256);
337 }
338 while (toupper(ch[0]) != 'Q');
339
340 out.close();
341
342 delete FunctionList;
343
344 cout << "Done!\n";
345 return 0;
346 }