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