First implementation (not final at all)
[scilab.git] / scilab / modules / development_tools / macros / unit_test_run.sci
1 //-----------------------------------------------------------------------------
2 // Pierre MARECHAL
3 // Scilab team
4 // Copyright INRIA
5 // Date : 25/10/2007
6 //
7 // Launch unitary tests
8 //-----------------------------------------------------------------------------
9
10 function unit_test_run(varargin)
11         
12         lhs = argn(1);
13         rhs = argn(2);
14         
15         global test_list;
16         global test_count;
17
18         test_count = 0;
19         
20         select rhs
21                 case 0
22                         // No input argument
23                         // unit_test_run()
24                         // => Launch each test of each module
25                         
26                         module_list = getmodules();
27                         for k=1:size(module_list,'*')
28                                 unit_test_add_module(module_list(k));
29                         end
30                         
31                         break
32                 
33                 case 1
34                         
35                         // One input argument
36                         // unit_test_run(<module_name>)
37                         // unit_test_run([<module_name_1>,<module_name_2>])
38                         
39                         // varargin(1) = [<module_name_1>,<module_name_2>]
40                         
41                         module_mat = varargin(1);
42                         
43                         [nl,nc] = size(module_mat);
44                         
45                         for i=1:nl
46                                 for j=1:nc
47                                         if( with_module(module_mat(i,j)) ) then
48                                                 unit_test_add_module(module_mat(i,j));
49                                         else
50                                                 error(sprintf(gettext("%s is not an installed module"),module_mat(i,j)));
51                                         end
52                                 end
53                         end
54                         
55                         break
56                         
57                 case 2
58                         
59                         // Two input arguments
60                         // unit_test_run(<module_name>,<test_name>)
61                         // unit_test_run(<module_name>,[<test_name_1>,<test_name_2>] )
62                         
63                         // varargin(1) = <module_name> ==> string 1x1
64                         // varargin(2) = <test_name_1> ==> mat nl x nc
65                         
66                         module   = varargin(1);
67                         test_mat = varargin(2);
68                         
69                         if (size(module) <> [1,1]) & (size(module) <> [1,1]) then
70                                 error(gettext("Input argument sizes are not valid"));
71                         end
72                         
73                         [nl,nc] = size(test_mat);
74                         
75                         for i=1:nl
76                                 for j=1:nc
77                                         if( fileinfo(SCI+"/modules/"+module+"/unit_tests/"+test_mat(i,j)+".tst") <> [] ) then
78                                                 unit_test_add_onetest(module,test_mat(i,j));
79                                         else
80                                                 error(sprintf(gettext("The test ""%s"" is not available from the ""%s"" module"),test_mat(i,j),module));
81                                         end
82                                 end
83                         end
84                         
85                         break
86                         
87                 else
88                         error(gettext('Number of parameters incorrect.'));
89         end
90         
91         for i=1:test_count
92                 
93                 printf("\t %02d/%02d - ",i,test_count);
94                 printf("%s [%s]",test_list(i,2),test_list(i,1));
95                 for j = length(test_list(i,2) + test_list(i,1)):50
96                         printf(".");
97                 end
98                 status = unit_test_run_onetest(test_list(i,1),test_list(i,2));
99                 printf("%d\n",status);
100         end
101         
102 endfunction
103
104 //-----------------------------------------------------------------------------
105 // Pierre MARECHAL
106 // Scilab team
107 // Copyright INRIA
108 // Date : 28 oct. 2007
109 //
110 // => List all test files in the module <module_mat>
111 // => Add them to the test_mat matrix
112 //-----------------------------------------------------------------------------
113
114 function unit_test_add_module(module_mat)
115         
116         module_test_dir = SCI+"/modules/"+module_mat+"/unit_tests";
117         test_mat        = basename(listfiles(module_test_dir+"/*.tst"));
118         
119         nl = size(test_mat,"*");
120         for i=1:nl
121                 unit_test_add_onetest(module_mat,test_mat(i));
122         end
123         
124 endfunction
125
126 //-----------------------------------------------------------------------------
127 // Pierre MARECHAL
128 // Scilab team
129 // Copyright INRIA
130 // Date : 28 oct. 2007
131 //
132 // => Add the test <test> to the test_mat matrix
133 //-----------------------------------------------------------------------------
134
135 function unit_test_add_onetest(module,test)
136         
137         global test_list;
138         global test_count;
139         
140         test_count = test_count + 1;
141         test_list( test_count , 1 ) = module;
142         test_list( test_count , 2 ) = test;
143         
144 endfunction
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 //-----------------------------------------------------------------------------
162 // Pierre MARECHAL
163 // Scilab team
164 // Copyright INRIA
165 // Date : 28 oct. 2007
166 //
167 // => Run one test
168 //-----------------------------------------------------------------------------
169
170 function status = unit_test_run_onetest(module,test)
171         
172         status = 0 ;
173         
174         // Some definitions
175         
176         tstfile     = pathconvert(SCI+"/modules/"+module+"/unit_tests/"+test+".tst",%f,%f);
177         diafile     = pathconvert(SCI+"/modules/"+module+"/unit_tests/"+test+".dia",%f,%f);
178         reffile     = pathconvert(SCI+"/modules/"+module+"/unit_tests/"+test+".dia.ref",%f,%f);
179         
180         tmp_tstfile = pathconvert(TMPDIR+"/"+test+".tst",%f,%f);
181         tmp_diafile = pathconvert(TMPDIR+"/"+test+".dia",%f,%f);
182         tmp_resfile = pathconvert(TMPDIR+"/"+test+".res",%f,%f);
183         tmp_errfile = pathconvert(TMPDIR+"/"+test+".err",%f,%f);
184
185         debug_diafile = pathconvert("/tmp/"+test+".dia",%f,%f);
186         
187         // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
188         // printf("tstfile = %s\n",tstfile);
189         // printf("diafile = %s\n",diafile);
190         // printf("tmp_tstfile = %s\n",tmp_tstfile);
191         // printf("tmp_diafile = %s\n",tmp_diafile);
192         // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
193         
194         //Reset standard globals
195         rand('seed',0);
196         rand('uniform');
197
198         // Do some modification in  tst file
199         
200         txt = mgetl(tstfile);
201         txt = strsubst(txt,'pause,end','bugmes();quit;end');
202         txt = strsubst(txt,'-->','@#>'); //to avoid suppression of input --> with prompts
203         txt = strsubst(txt,'halt()','');
204         
205         // Header : the same for each test
206         
207         head = [        "// <-- HEADER START -->";
208                                 "mode(3);" ;
209                                 "clear;" ;
210                                 "lines(28,72);";
211                                 "lines(0);" ;
212                                 "deff(''[]=bugmes()'',''write(%io(2),''''error on test'''')'');" ;
213                                 "predef(''all'');" ;
214                                 "tmpdirToPrint = msprintf(''TMPDIR1=''''%s''''\n'',TMPDIR);" ;
215                                 "try";
216                                 "diary(''"+tmp_diafile+"'');";
217                                 "write(%io(2),tmpdirToPrint);";
218                                 "// <-- HEADER END -->"];
219                                 
220         tail = [        "// <-- FOOTER START -->";
221                                 "catch";
222                                 "       printf(""Error on the test script file"")";
223                                 "end";
224                                 "diary(0);";
225                                 "exit;";
226                                 "// <-- FOOTER END -->"]
227         
228         txt = [head;
229                 txt;
230                 tail];
231         
232         // and save it in a temporary file
233         mputl(txt,tmp_tstfile);
234         
235         // Delete previous dia file
236         if fileinfo(diafile) <> [] then
237                 deletefile(diafile)
238         end
239         
240         // Build the command to launch
241         if MSDOS then
242                 unit_test_cmd = """"+SCI+"\bin\scilex.exe"+""""+" -nw -args -nouserstartup -f "+tmp_tstfile+" 1>NUL";
243         else
244                 unit_test_cmd = "( "+SCI+"/bin/scilab -nw -nb -args -nouserstartup -f "+tmp_tstfile+" > "+tmp_resfile+" ) 2> "+tmp_errfile;
245         end
246         
247         // Launch the test exec
248         
249         //printf("%s\n",unit_test_cmd);
250         host(unit_test_cmd);
251
252         // First Check
253         tmp_errfile_info = fileinfo(tmp_errfile);
254         if ( (tmp_errfile_info <> []) & (tmp_errfile_info(1)<>0) ) then
255                 status = 5;
256                 return;
257         end
258         
259         //  Do some modification in  dia file
260         dia = mgetl(tmp_diafile);
261         
262         // To get TMPDIR line
263         tmpdir1_line = grep(dia,"TMPDIR1");
264         execstr(dia(tmpdir1_line));
265         // printf("-- %s -- ",TMPDIR1);
266         
267         // Remove Header and Footer
268         
269         body_start = grep(dia,"// <-- HEADER END -->");
270         
271         if body_start<>[] then
272                 dia(1:body_start(1)) = [];
273         end
274         
275         body_end   = grep(dia,"// <-- FOOTER START -->");
276         
277         if body_end<>[] then
278                 [dia_nl,dia_nc] = size(dia);
279                 dia(body_end(1):dia_nl) = [];
280         end
281         
282         mputl(dia,debug_diafile);
283         
284         //Check for execution errors
285         
286         if grep(dia,"!--error")<>[] then
287                 status = 1;
288                 return;
289         end
290         
291         if grep(dia,"error on test")<>[] then
292                 status = 2;
293                 return;
294         end
295         
296         if grep(dia,"Error on the test script file")<>[] then
297                 status = 3;
298                 return;
299         end
300         
301         //  Do some modification in  dia file
302         dia(grep(dia,"exec("))                     = [];
303         dia(grep(dia,"write(%io(2),tmpdirToPrint"))= [];
304         dia(grep(dia,"TMPDIR1"))                   = [];
305         dia(grep(dia,"diary(0)"))                  = [];
306         
307         dia = strsubst(dia,TMPDIR,'TMPDIR');
308         dia = strsubst(dia,TMPDIR1,'TMPDIR');
309         dia = strsubst(dia,TMPDIR1,'TMPDIR');
310         dia = strsubst(dia,SCI,'SCI');
311         
312         //suppress the prompts
313         dia = strsubst(dia,'-->','');
314         dia = strsubst(dia,'@#>','-->');
315         dia = strsubst(dia,'-1->','');
316         
317         //standardise  number display
318         dia = strsubst(strsubst(strsubst(strsubst(dia,' .','0.'),'E+','D+'),'E-','D-'),'-.','-0.');
319
320         //not to change the ref files
321         dia=strsubst(dia,'bugmes();return','bugmes();quit');
322         
323         // write down the resulting dia file
324         mputl(dia,diafile)
325         
326         //Check for diff with the .ref file
327         
328         [u,ierr] = mopen(reffile,'r');
329         if ierr== 0 then //ref file exists
330                 
331                 ref=mgetl(u);
332                 mclose(u)
333                 
334                 // suppress blank (diff -nw)
335                 dia=strsubst(dia,' ','')
336                 ref=strsubst(ref,' ','')
337                 
338                 if or(ref<>dia) then
339                         if MSDOS then
340                                 status = 4;
341                         else
342                                 status = 4;
343                         end
344                 end
345         end
346         
347         return;
348         
349 endfunction