* Bug 16529 fixed: deff() upgraded: output arg added, one-string def, etc
[scilab.git] / scilab / modules / functions / tests / unit_tests / deff.tst
1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2009 - DIGITEO - Allan CORNET
4 // Copyright (C) 2020 - Samuel GOUGEON
5 //
6 //  This file is distributed under the same license as the Scilab package.
7 // =============================================================================
8
9 // <-- CLI SHELL MODE -->
10 // <-- NO CHECK REF -->
11
12
13 deff("[x] = myplus(y,z)","x=y+z");
14 assert_checktrue(isdef("myplus"));
15 assert_checkequal(myplus(3,2), 5);
16 clear myplus;
17 assert_checkfalse(isdef("myplus"));
18
19 deff("[x] = mymacro(y,z)",["a=3*y+1"; "x=a*z+y"]);
20 assert_checktrue(isdef("mymacro"));
21 assert_checkequal(mymacro(5,2), 37);
22 clear mymacro;
23 assert_checkfalse(isdef("mymacro"));
24
25 deff("[a,b,c] = mymacro(varargin)",["a = varargin(1); b = varargin(2) * a; c = varargin(3) * b"]);
26 assert_checktrue(isdef("mymacro"));
27 [a,b,c] = mymacro(3,4,5);
28 assert_checkequal(a, 3);
29 assert_checkequal(b, 12);
30 assert_checkequal(c, 60);
31 clear mymacro;
32 assert_checkfalse(isdef("mymacro"));
33
34 deff("[varargout] = mymacro(varargin)",["for i = 1 : size(varargin)"; "    varargout(i) = varargin(i) * 2;";"end"]);
35 assert_checktrue(isdef("mymacro"));
36 [a,b,c] = mymacro(3,4,5);
37 assert_checkequal(a, 6);
38 assert_checkequal(b, 8);
39 assert_checkequal(c, 10);
40 clear mymacro;
41 assert_checkfalse(isdef("mymacro"));
42
43 deff("[varargout] = mymacro(varargin)", ["deff(""varargout = mymacro2(varargin)"", [""for i = 1 : size(varargin)""; ""    varargout(i) = varargin(i) * 2;"";""end""]);";"[a,b,c] = mymacro2(varargin(:));";"varargout(1) = a*2;";"varargout(2) = b*2;";"varargout(3) = c*2;"]);
44 assert_checktrue(isdef("mymacro"));
45 assert_checkfalse(isdef("mymacro2"));
46 [a,b,c] = mymacro(3,4,5);
47 assert_checkequal(a, 12);
48 assert_checkequal(b, 16);
49 assert_checkequal(c, 20);
50 assert_checkfalse(isdef("mymacro2"));
51 clear mymacro;
52 assert_checkfalse(isdef("mymacro"));
53
54 // Other syntaxes
55 clear Fun
56 deff(["r = Fun(x)" "r = x.^2 - 3"]);    // Only one deff argin, being a vector
57 assert_checktrue(isdef("Fun","l"));
58 assert_checkequal(Fun(4), 13);
59 clear Fun
60 deff "r = Fun(x) r = x.^2 - 3";         // Only one deff argin, being a scalar
61 assert_checktrue(isdef("Fun","l"));
62 assert_checkequal(Fun(4), 13);
63 clear Fun
64 deff("r = Fun(x) x.^2 - 3");    // Fun argout implicit assignment (missing in definition)
65 assert_checktrue(isdef("Fun","l"));
66 assert_checkequal(Fun(4), 13);
67
68 // output = deff(..)
69 // =================
70 clear Fun myFun
71 myFun = deff("res = Fun(a,b)",["res = a+b" "res = res.^2"]);
72 assert_checkequal(myFun(1,1), 4);
73 assert_checkfalse(isdef("Fun","l"));
74
75 clear myFun
76 e = execstr("myFun = deff(""res = Fun(a,b)"",[""res = a+"" ""res = res.^2""]);", "errcatch");
77 assert_checktrue(e <> 0);             // syntax error ^^^
78 assert_checkfalse(isdef("myFun","l"));
79 assert_checkfalse(isdef("Fun","l"));
80
81 in = list(["r = Fun(x)" "r = x.^2"], .. // Only one deff argin, being a vector
82            "r = Fun(x) r = x.^2", ..    // Only one deff scalar argin
83            "r = Fun(x) x.^2",..  // Fun argout implicit assignment (missing in definition):
84            "[a,b] = Fun(x) a=x.^2; b=x-3"); // Only one deff scalar argin. 2 outputs
85 for argin = in
86     clear myFun
87     myFun = deff(argin);
88     assert_checktrue(isdef("myFun","l"));
89     assert_checkfalse(isdef("Fun","l"));
90     if grep(argin(1), "[a,b]") <> []
91         [u,v] = myFun(4);
92         assert_checkequal([u v], [16 1]);
93     else
94         assert_checkequal(myFun(4), 16);
95     end
96
97     // Anonymous "@"
98     clear myFun
99     argin = strsubst(argin, "Fun", "@")
100     myFun = deff(argin);
101     assert_checktrue(isdef("myFun","l"));
102     assert_checkequal(myFun(4), 16);
103 end