1// =============================================================================
2// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3// Copyright (C) ????-2008 - INRIA
4// Copyright (C) 2008-2011 - DIGITEO
5//
6//  This file is distributed under the same license as the Scilab package.
7// =============================================================================
8//Checks syntactical aspects  related to functions
9funcprot(0);
10//
11//               Various syntaxes
12//               ----------------
13function y=foo(x)
14    y=x^2
15endfunction
16if foo(2)<>4  then bugmes();quit;end
17//
18function y=foo(a,b)
19    y=a+b
20endfunction
21if foo(2,3)<>5  then bugmes();quit;end
22//
23function [y,z]=foo(a)
24    y=a^2
25    z=a^3
26endfunction
27[y,z]=foo(2);if y<>4|z<>8 then bugmes();quit;end
28//
29function y=foo()
30    y=2
31endfunction
32if foo()<>2  then bugmes();quit;end
33//
34function y=foo
35    y=2
36endfunction
37if foo()<>2  then bugmes();quit;end
38//
39function []=foo
40    y=resume(2)
41endfunction
42foo();if y<>2  then bugmes();quit;end
43//
44function foo
45    y=resume(3)
46endfunction
47foo();if y<>3  then bugmes();quit;end
48//
49function result=foo(a,b)
50    result = [(a+b) (a-b) (a*a + b*b)];
51endfunction
52result=foo(32,64); if or(result <> [96. -32. 5120.]) then bugmes();quit;end
53//
54//               Various line splits
55//               -------------------
56function y=foo(x),y=x^2
57endfunction
58if foo(2)<>4  then bugmes();quit;end
59//
60function y=foo(x),y=3*x,endfunction
61if foo(2)<>6  then bugmes();quit;end
62//
63function y=foo(x)
64    z=x^2
65    function y=foo1(x)
66        y=x+1
67    endfunction
68    y=foo1(z)
69endfunction
70if foo(2)<>5  then bugmes();quit;end
71//
72//               Combined with deff
73//               -------------------
74function y=foo(x)
75    z=x^2
76    deff("y=foo1(x)","y=x+1")
77    y=foo1(z)
78endfunction
79if foo(2)<>5  then bugmes();quit;end
80//
81deff("y=foo(x)",[
82"z=x^2"
83"function y=foo1(x)"
84"y=x+1"
85"endfunction"
86"y=foo1(z)"])
87if foo(2)<>5  then bugmes();quit;end
88function y=foo(x),y=x+1
89endfunction;if foo(2)<>3  then bugmes();quit;end
90//
91//               Combined with exec
92//               -------------------
93function foo
94    z=9;
95    function y=foo1(x)
96        y=x+1;
97    endfunction
98    y=foo1(z);
99endfunction
100exec(foo); if y<>10  then bugmes();quit;end
101//
102//               Combined with control instructions
103//               ----------------------------------
104if %t then
105    function  y=foo(x)
106        y=sin(x)
107    endfunction
108else
109    function  y=foo(x)
110        y=1
111    endfunction
112end
113if foo(1)<>sin(1)  then bugmes();quit;end
114//
115if %t then
116    function  y=foo(x)
117        if x==0 then
118            y=1
119        else
120            y=sin(x)/x
121        end
122    endfunction
123else
124    function  y=foo(x)
125        y=1
126    endfunction
127end
128if foo(0)<>1   then bugmes();quit;end
129if foo(2)<>sin(2)/2   then bugmes();quit;end
130//
131z=0;
132for k=1:2
133    function y=foo()
134        y=k
135    endfunction
136    z=z+foo();
137end
138if z<>3 then bugmes();quit;end
139//
140z=0;for k=1:2
141    function y=foo(),y=k,endfunction
142    z=z+foo();
143end
144if z<>3 then bugmes();quit;end
145//
146z=0;for k=1:2, function y=foo(),y=k,endfunction
147    z=z+foo();
148end
149if z<>3 then bugmes();quit;end
150z=0;
151for k=1:2
152    function y=foo(k)//qsdsdf
153        y=k^2
154    endfunction
155    z=z+foo();
156end
157if z<>5 then bugmes();quit;end
158z=0;
159for k=1:2
160    function y=foo(k)//qsdsdf
161        y=k^2 //a comment
162    endfunction
163    z=z+foo();
164end
165if z<>5 then bugmes();quit;end
166z=0;
167for k=1:2
168    function y=foo(k), y=k^2, endfunction
169    z=z+foo();
170end
171if z<>5 then bugmes();quit;end
172z=0;
173for k=1:2
174    function y=foo(k), y=k^2, endfunction// a comment
175    z=z+foo();
176end
177if z<>5 then bugmes();quit;end
178//bug 1024 non regression test
179O=[];
180for n= 1:10;
181    Fx=rand(1,100);
182    Fy=1:100;
183    function [f,g,ind]=cout(x,ind)
184        f1=(1-exp(-x*Fx) - Fy);
185        f= (1/2)*f1*f1';
186        g= x*(Fx.*exp(-x*Fx))*f1';
187    endfunction
188    [fopt,lmopt]=optim(cout,0.4);
189    O=[O fopt];
190end
191if size(O,"*")<>10 then bugmes();quit;end
192// test line count in compiled macros
193//
194function a=foo(),a=1,endfunction
195L=macr2tree(foo);
196if L(6)<>1 then bugmes();quit;end
197clear foo
198function a=foo()
199    a=1,
200endfunction
201L=macr2tree(foo);
202if L(6)<>3 then bugmes();quit;end
203clear foo
204function a=foo()//xxcxcx
205    a=1,
206endfunction
207L=macr2tree(foo);
208if L(6)<>3 then bugmes();quit;end
209clear foo
210function a=foo(),
211    a=1,
212endfunction
213L=macr2tree(foo);
214if L(6)<>3 then bugmes();quit;end
215clear foo
216function a=foo(),a=1//xxcxcx
217    b=2
218endfunction
219L=macr2tree(foo);
220if L(6)<>3 then bugmes();quit;end
221clear foo
222function a=foo()
223    a=...
224    sin...
225    (...
226    1...
227    ),
228endfunction
229//continuation lines are replaced by a sequence of empty lines followed by the logical line
230//so the function above is  the same as
231function a=foo1()
232    a=sin(1),
233endfunction
234L=macr2tree(foo);
235t=L(6)<>7;
236if foo<>foo1 then bugmes();quit;end
237clear foo foo1
238function a=foo(),a=...
239    sin...
240    (...
241    1 ...
242    ),
243endfunction
244//continuation lines are replaced by a sequence of empty lines followed by the logical line
245//foo should be equal to foo1
246function a=foo1(),a=sin(1),
247endfunction
248L=macr2tree(foo);
249if L(6)<>6  then bugmes();quit;end
250if foo<>foo1 then bugmes();quit;end
251