1########################################################################
2##
3## Copyright (C) 2006-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26########################################
27## No inputs or no outputs
28
29## no input or output arguments
30%!function f ()
31%!  assert (nargin, 0);
32%!  assert (nargout, 0);
33%!endfunction
34%!test
35%! f;
36
37## one input with two possible inputs
38%!function f (x, y)
39%!  assert (nargin, 1);
40%!  assert (nargout, 0);
41%!endfunction
42%!test
43%! f (1);
44
45## no inputs, one of multiple outputs
46%!function [x, y] = f ()
47%!  assert (nargin, 0);
48%!  assert (nargout, 1);
49%!  x = 2;
50%!endfunction
51%!test
52%! assert (f (), 2);
53
54## one of multiple inputs, one of multiple outputs
55%!function [x, y] = f (a, b)
56%!  assert (nargin, 1);
57%!  assert (nargout, 1);
58%!  x = a;
59%!endfunction
60%!test
61%! assert (f (1), 1);
62
63########################################
64## Varargin, varargout
65
66## varargin and varargout with no inputs or outputs
67%!function [varargout] = f (varargin)
68%!  assert (nargin, 0);
69%!  assert (nargout, 0);
70%!endfunction
71%!test
72%! f;
73
74## varargin and varargout with one input
75%!function [varargout] = f (x, varargin)
76%!  assert (nargin, 1);
77%!  assert (nargout, 0);
78%!endfunction
79%!test
80%! f (1);
81
82## varargin and varargout with one output
83%!function [x, varargout] = f (varargin)
84%!  assert (nargin, 0);
85%!  assert (nargout, 1);
86%!  x = 2;
87%!endfunction
88%!test
89%! assert (f (), 2);
90
91## varargin and varargout with one input and output
92%!function [varargout] = f (varargin)
93%!  assert (nargin, 1);
94%!  assert (nargout, 1);
95%!  varargout{1} = varargin{1};
96%!endfunction
97%!test
98%! assert (f (1), 1);
99
100## multiple inputs, multiple outputs, but not all of either
101## WARNING: The original test did not assign the outputs, it just
102## requested them, and I think that is supposed to be an error.  It also
103## still has a non-assigned output argument.
104%!function [x, y, z] = f (a, b, c, d, e)
105%!  assert (nargin, 4);
106%!  assert (nargout, 2);
107%!  x = a;
108%!  y = b;
109%!endfunction
110%!test
111%! [s, t] = f (1, 2, 3, 4);
112%! assert ([s t], [1 2]);
113
114## Fully used varargin and varargout
115%!function [varargout] = f (varargin)
116%!  assert (nargin, 3);
117%!  assert (nargout, 4);
118%!  varargout{1} = varargin{1};
119%!  varargout{2} = varargin{2};
120%!  varargout{3} = varargin{3};
121%!  varargout{4} = 4;
122%!endfunction
123%!test
124%! [s, t, u, v] = f (1, 2, 3);
125%! assert ([s t u v], [1 2 3 4]);
126
127## Wrapper functions
128%!function [x, y, z] = f (varargin)
129%!  assert (nargin, 0);
130%!  assert (nargout, 0);
131%!  x = 3;
132%!  y = 2;
133%!  z = 1;
134%!endfunction
135%!function varargout = wrapper_1 (varargin)
136%!  assert (nargout, 0);
137%!  [varargout{1:nargout}] = f ();
138%!endfunction
139%!function varargout = wrapper_2 (varargin)
140%!  assert (nargout, 0);
141%!  varargout = cell (1, nargout);
142%!  [varargout{1:nargout}] = f ();
143%!endfunction
144%!function varargout = wrapper_3 (varargin)
145%!  assert (nargout, 0);
146%!  varargout = cell (1, nargout);
147%!  [varargout{:}] = f ();
148%!endfunction
149%!test
150%! wrapper_1 ();
151%! assert (ans, 3);
152%! wrapper_2 ();
153%! assert (ans, 3);
154%! wrapper_3 ();
155%! assert (ans, 3);
156
157## Test default arguments
158## numeric
159%!function f (x = 0)
160%!  assert (x, 0);
161%!endfunction
162%!test
163%! f()
164
165## numeric vector (spaces)
166%!function f (x = [0 1 2])
167%!  assert (x, [0 1 2]);
168%!endfunction
169%!test
170%! f()
171
172## numeric vector (range)
173%!function f (x = 1:3)
174%!  assert (x, 1:3);
175%!endfunction
176%!test
177%! f()
178
179## numeric vector (commas)
180%!function f (x = [0,1,2])
181%!  assert (x, [0 1 2]);
182%!endfunction
183%!test
184%! f()
185
186## numeric vector (commas and spaces)
187%!function f (x = [0, 1, 2])
188%!  assert (x, [0 1 2]);
189%!endfunction
190%!test
191%! f()
192
193## numeric matrix
194%!function f (x = [0, 1, 2;3, 4, 5])
195%!  assert (x, [0 1 2;3 4 5]);
196%!endfunction
197%!test
198%! f()
199
200## empty cell
201%!function f (x = {})
202%!  assert (x, {});
203%!endfunction
204%!test
205%! f()
206
207## full cell
208%!function f (x = {1})
209%!  assert (x, {1});
210%!endfunction
211%!test
212%! f()
213
214## many cells
215%!function f (x = {1 'a' "b" 2.0 struct("a", 3)})
216%!  assert (x, {1 'a' "b" 2.0 struct("a", 3)});
217%!endfunction
218%!test
219%! f()
220
221## struct
222%!function f (x = struct("a", 3))
223%!  assert (x, struct ("a", 3));
224%!endfunction
225%!test
226%! f()
227
228## char (double quotes)
229%!function f (x = "a")
230%!  assert (x, "a");
231%!endfunction
232%!test
233%! f()
234
235## char (single quotes)
236%!function f (x = 'a')
237%!  assert (x, "a");
238%!endfunction
239%!test
240%! f()
241
242## char (string, double quotes)
243%!function f (x = "abc123")
244%!  assert (x, "abc123");
245%!endfunction
246%!test
247%! f()
248
249## char (string, double quotes, punctuation)
250%!function f (x = "abc123`1234567890-=~!@#$%^&*()_+[]{}|;':\",./<>?\\")
251%!  assert (x, "abc123`1234567890-=~!@#$%^&*()_+[]{}|;':\",./<>?\\");
252%!endfunction
253%!test
254%! f()
255
256## Function handle (builtin)
257%!function f (x = @sin)
258%!  finfo = functions (x);
259%!  fname = finfo.function;
260%!  assert (is_function_handle (x) && strcmp (fname, "sin"));
261%!endfunction
262%!test
263%! f()
264
265## Function handle (anonymous)
266%!function f (x = @(x) x.^2)
267%!  finfo = functions (x);
268%!  ftype = finfo.type;
269%!  assert (is_function_handle (x) && strcmp (ftype, "anonymous"));
270%!endfunction
271%!test
272%! f()
273