Merge remote-tracking branch 'origin/master' into YaSp
[scilab.git] / scilab / modules / development_tools / macros / scitest.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2008 - INRIA
3 //
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
9
10 function scitest(tstfile,force,error_check,keep_prompt,postscript_graphics)
11
12     // if error_check is %t then execution errors are reported
13     // if error_check is %f then only test checked error are detected
14     // (see examples in tests directory)
15     // if keep_prompt is %t the prompt is kept in the dia file
16     // this can be useful for producing demos
17
18     [lhs,rhs]=argn(0);
19
20     if exists("force","local")              ==0 then force               = %f ; end
21     if exists("error_check","local")        ==0 then error_check         = %f ; end
22     if exists("keep_prompt","local")        ==0 then keep_prompt         = %f ; end
23     if exists("postscript_graphics","local")==0 then postscript_graphics = %f ; end
24
25     ind = strindex(tstfile,".");
26
27     if ind==[] then
28         diafile = tstfile+".tst";
29     else
30         suf = part(tstfile,ind($):length(tstfile));
31         if suf == ".dia" then
32             error(msprintf(gettext("%s: Wrong first input argument: File with .dia suffix expected."),"scitest"));
33             return
34         end
35         diafile=strsubst(tstfile,suf,".dia");
36     end
37
38     if newest(tstfile,diafile)==2 & force == %f  then return,end
39
40     mydisp("------------------- File "+tstfile+" --------------------");
41
42     //Reset standard globals
43     rand("seed",0);rand("uniform");
44
45     if getos() == "Windows" then
46         tmpfiles=strsubst(TMPDIR,"/","\")+"\tmp.";
47     else
48         tmpfiles=TMPDIR+"/tmp.";
49     end
50
51     // Do some modification in  tst file
52     // ---------------------------------
53     txt = mgetl(tstfile);
54     txt = strsubst(txt,"pause,end","bugmes();quit;end");
55     txt = strsubst(txt,"-->","@#>");
56     //to avoid suppression of input --> with prompts
57     txt=strsubst(txt,"halt()","");
58
59     if postscript_graphics then
60         pg1="driver(''Pos'');xinit(''"+tmpfiles+"gr"+"'');";
61         pg2="xend();";
62     else
63         pg1="";
64         pg2="";
65     end
66
67     head="mode(3);clear;lines(28,72);lines(0);"..
68     +"deff(''[]=bugmes()'',''write(%io(2),''''error on test'''')'');"..
69     +"predef(''all'');"..
70     +"tmpdirToPrint = msprintf(''TMPDIR1=''''%s''''\n'',TMPDIR);"..
71     +"diary(''"+tmpfiles+"dia"+"'');"..
72     +"write(%io(2),tmpdirToPrint);"..
73     +pg1;
74
75     tail = "diary(0);"+pg2+"exit;";
76
77     txt = [head;
78     txt;
79     tail];
80
81     // and save it in a temporary file
82     mputl(txt,tmpfiles+"tst");
83
84     myexec()
85
86     //  Do some modification in  dia file
87     // ----------------------------------
88     dia=mgetl(tmpfiles+"dia")
89     dia(grep(dia,"exec("))=[];
90     TMP=dia(1);
91     dia(1)=[];
92     dia(grep(dia,"diary(0)"))=[];
93
94     execstr(TMP)
95
96     dia = strsubst(dia,TMPDIR,"TMPDIR");
97     dia = strsubst(dia,TMPDIR1,"TMPDIR");
98     dia = strsubst(dia,TMPDIR1,"TMPDIR");
99     dia = strsubst(dia,SCI,"SCI");
100
101     //suppress the prompts
102     if keep_prompt == %f then
103         dia=strsubst(dia,"-->","");
104     end
105
106     dia = strsubst(dia,"@#>","-->");
107     dia = strsubst(dia,"-1->","");
108
109     //standardise  number display
110     dia=strsubst(strsubst(strsubst(strsubst(dia," .","0."),..
111     "E+","D+"),"E-","D-"),"-.","-0.");
112
113     //not to change the ref files
114     dia=strsubst(dia,"bugmes();return","bugmes();quit");
115
116     // write down the resulting dia file
117     mputl(dia,diafile)
118
119     //Check for execution errors
120     // -------------------------
121
122     if  error_check == %t then
123         if grep(dia,"!--error")<>[] then
124             mydisp(msprintf(gettext("Test failed ERROR DETECTED while executing %s"),tstfile));
125             return;
126         end
127     end
128
129     if grep(dia,"error on test")<>[] then
130         mydisp(msprintf(gettext("Test failed ERROR DETECTED while executing %s"),tstfile));
131         return;
132     end
133
134     //Check for diff with the .ref file
135     // --------------------------------
136     [u,ierr]=mopen(diafile+".ref","r")
137     if ierr== 0 then //ref file exists
138         ref=mgetl(u);mclose(u)
139         // suppress blank (diff -nw)
140         dia=strsubst(dia," ","")
141         ref=strsubst(ref," ","")
142
143         if or(ref<>dia) then
144             if getos() == "Windows" then
145                 mydisp(msprintf(gettext("Test Failed. See : fc /L /N  %s"),diafile+" "+diafile+".ref "));
146             else
147                 mydisp(msprintf(gettext("Test Failed. See : diff -wu  %s"),diafile+" "+diafile+".ref "));
148             end
149         else
150             mydisp(gettext("Test passed"));
151         end
152     end
153     mydisp("----------------------------------------------------------");
154
155 endfunction
156
157 function mydisp(str)
158     write(%io(2),str,"(a)")
159 endfunction
160
161 function myexec()
162     if getos() == "Windows" then
163         if fileinfo(tmpfiles+"dia")<>[] then
164             deletefile(tmpfiles+"dia")
165         end
166         dos(""""+SCI+"\bin\scilex.exe"+""""+" -nwni -args -nouserstartup -f "+tmpfiles+"tst 1>NUL");
167     else
168         if fileinfo(tmpfiles+"dia")<>[] then
169             deletefile(tmpfiles+"dia")
170         end
171         unix_s("( "+SCI+"/bin/scilab -nw -args -nouserstartup -f "+tmpfiles+"tst > "+tmpfiles+"res ) 2> "+tmpfiles+"err")
172     end
173 endfunction