Mercurial > hg > octave-nkf
annotate test/test_args.m @ 14026:3781981be535 ss-3-5-90
snapshot 3.5.90
* configure.ac (AC_INIT): Version is now 3.5.90.
(OCTAVE_API_VERSION_NUMBER): Now 46.
(OCTAVE_RELEASE_DATE): Now 2011-12-11.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 11 Dec 2011 23:18:31 -0500 |
parents | fd0a3ac60b0e |
children | c3309e1ec50d |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2006-2011 John W. Eaton |
7016 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
7775 | 19 ######################################## |
20 ## No inputs or no outputs | |
5590 | 21 |
7775 | 22 ## no input or output arguments |
5590 | 23 %!function f () |
7775 | 24 %! assert (nargin, 0); |
25 %! assert (nargout, 0); | |
5590 | 26 %!test |
27 %! f; | |
28 | |
7775 | 29 ## one input with two possible inputs |
5590 | 30 %!function f (x, y) |
7775 | 31 %! assert (nargin, 1); |
32 %! assert (nargout, 0); | |
5590 | 33 %!test |
34 %! f (1); | |
35 | |
7775 | 36 ## no inputs, one of multiple outputs |
5590 | 37 %!function [x, y] = f () |
7775 | 38 %! assert (nargin, 0); |
39 %! assert (nargout, 1); | |
5590 | 40 %! x = 2; |
41 %!test | |
7775 | 42 %! assert (f (), 2); |
5590 | 43 |
7775 | 44 ## one of multiple inputs, one of multiple outputs |
5590 | 45 %!function [x, y] = f (a, b) |
7775 | 46 %! assert (nargin, 1); |
47 %! assert (nargout, 1); | |
5590 | 48 %! x = a; |
49 %!test | |
7775 | 50 %! assert (f (1), 1); |
51 | |
52 ######################################## | |
53 ## Varargin, varargout | |
5590 | 54 |
7775 | 55 ## varargin and varargout with no inputs or outputs |
5590 | 56 %!function [varargout] = f (varargin) |
7775 | 57 %! assert (nargin, 0); |
58 %! assert (nargout, 0); | |
5590 | 59 %!test |
60 %! f; | |
61 | |
7775 | 62 ## varargin and varargout with one input |
5590 | 63 %!function [varargout] = f (x, varargin) |
7775 | 64 %! assert (nargin, 1); |
65 %! assert (nargout, 0); | |
5590 | 66 %!test |
67 %! f (1); | |
68 | |
7775 | 69 ## varargin and varargout with one output |
5590 | 70 %!function [x, varargout] = f (varargin) |
7775 | 71 %! assert (nargin, 0); |
72 %! assert (nargout, 1); | |
5590 | 73 %! x = 2; |
74 %!test | |
7775 | 75 %! assert (f (), 2); |
5590 | 76 |
7775 | 77 ## varargin and varargout with one input and output |
5590 | 78 %!function [varargout] = f (varargin) |
7775 | 79 %! assert (nargin, 1); |
80 %! assert (nargout, 1); | |
81 %! varargout{1} = varargin{1}; | |
5590 | 82 %!test |
7775 | 83 %! assert (f (1), 1); |
5590 | 84 |
7775 | 85 ## multiple inputs, multiple outputs, but not all of either |
86 ## WARNING: The original test did not assign the outputs, it just | |
87 ## requested them, and I think that is supposed to be an error. It also | |
88 ## still has a non-assigned output argument. | |
5590 | 89 %!function [x, y, z] = f (a, b, c, d, e) |
7775 | 90 %! assert (nargin, 4); |
91 %! assert (nargout, 2); | |
92 %! x = a; | |
93 %! y = b; | |
5590 | 94 %!test |
95 %! [s, t] = f (1, 2, 3, 4); | |
7775 | 96 %! assert([s t], [1 2]); |
5590 | 97 |
7775 | 98 ## Fully used varargin and varargout |
5590 | 99 %!function [varargout] = f (varargin) |
7775 | 100 %! assert (nargin, 3); |
101 %! assert (nargout, 4); | |
102 %! varargout{1} = varargin{1}; | |
103 %! varargout{2} = varargin{2}; | |
104 %! varargout{3} = varargin{3}; | |
5590 | 105 %! varargout{4} = 4; |
106 %!test | |
107 %! [s, t, u, v] = f (1, 2, 3); | |
7775 | 108 %! assert([s t u v], [1 2 3 4]); |
109 | |
110 ## Test default arguments | |
111 ## numeric | |
112 %!function f (x = 0) | |
113 %! assert (x, 0) | |
114 %!test | |
115 %! f() | |
116 | |
117 ## numeric vector (spaces) | |
118 %!function f (x = [0 1 2]) | |
119 %! assert (x, [0 1 2]) | |
120 %!test | |
121 %! f() | |
122 | |
123 ## numeric vector (range) | |
124 %!function f (x = 1:3) | |
125 %! assert (x, 1:3) | |
126 %!test | |
127 %! f() | |
128 | |
129 ## numeric vector (commas) | |
130 %!function f (x = [0,1,2]) | |
131 %! assert (x, [0 1 2]) | |
132 %!test | |
133 %! f() | |
134 | |
135 ## numeric vector (commas and spaces) | |
136 %!function f (x = [0, 1, 2]) | |
137 %! assert (x, [0 1 2]) | |
138 %!test | |
139 %! f() | |
140 | |
141 ## numeric matrix | |
142 %!function f (x = [0, 1, 2;3, 4, 5]) | |
143 %! assert (x, [0 1 2;3 4 5]) | |
144 %!test | |
145 %! f() | |
146 | |
147 ## empty cell | |
148 %!function f (x = {}) | |
149 %! assert (x, {}) | |
150 %!test | |
151 %! f() | |
5590 | 152 |
7775 | 153 ## full cell |
154 %!function f (x = {1}) | |
155 %! assert (x, {1}) | |
156 %!test | |
157 %! f() | |
158 | |
159 ## many cells | |
160 %!function f (x = {1 'a' "b" 2.0 struct("a", 3)}) | |
161 %! assert (x, {1 'a' "b" 2.0 struct("a", 3)}) | |
162 %!test | |
163 %! f() | |
164 | |
165 ## struct | |
166 %!function f (x = struct("a", 3)) | |
167 %! assert (x, struct ("a", 3)) | |
168 %!test | |
169 %! f() | |
170 | |
171 ## char (double quotes) | |
172 %!function f (x = "a") | |
173 %! assert (x, "a") | |
174 %!test | |
175 %! f() | |
176 | |
177 ## char (single quotes) | |
178 %!function f (x = 'a') | |
179 %! assert (x, "a") | |
180 %!test | |
181 %! f() | |
182 | |
183 ## char (string, double quotes) | |
184 %!function f (x = "abc123") | |
185 %! assert (x, "abc123") | |
186 %!test | |
187 %! f() | |
188 | |
189 ## char (string, double quotes, punctuation) | |
190 %!function f (x = "abc123`1234567890-=~!@#$%^&*()_+[]{}|;':\",./<>?\\") | |
191 %! assert (x, "abc123`1234567890-=~!@#$%^&*()_+[]{}|;':\",./<>?\\") | |
192 %!test | |
193 %! f() | |
194 | |
195 ## Function handle (builtin) | |
196 %!function f (x = @sin) | |
8805
065a05eb148a
test_args.m: don't use assert to test for function handles
John W. Eaton <jwe@octave.org>
parents:
7775
diff
changeset
|
197 %! finfo = functions (x); |
065a05eb148a
test_args.m: don't use assert to test for function handles
John W. Eaton <jwe@octave.org>
parents:
7775
diff
changeset
|
198 %! fname = finfo.function; |
065a05eb148a
test_args.m: don't use assert to test for function handles
John W. Eaton <jwe@octave.org>
parents:
7775
diff
changeset
|
199 %! assert (isa (x, "function_handle") && strcmp (fname, "sin")); |
065a05eb148a
test_args.m: don't use assert to test for function handles
John W. Eaton <jwe@octave.org>
parents:
7775
diff
changeset
|
200 %!test |
7775 | 201 %! f() |
202 | |
203 ## Function handle (anonymous) | |
204 %!function f (x = @(x) x.^2) | |
8805
065a05eb148a
test_args.m: don't use assert to test for function handles
John W. Eaton <jwe@octave.org>
parents:
7775
diff
changeset
|
205 %! finfo = functions (x); |
065a05eb148a
test_args.m: don't use assert to test for function handles
John W. Eaton <jwe@octave.org>
parents:
7775
diff
changeset
|
206 %! ftype = finfo.type; |
065a05eb148a
test_args.m: don't use assert to test for function handles
John W. Eaton <jwe@octave.org>
parents:
7775
diff
changeset
|
207 %! assert (isa (x, "function_handle") && strcmp (ftype, "anonymous")); |
065a05eb148a
test_args.m: don't use assert to test for function handles
John W. Eaton <jwe@octave.org>
parents:
7775
diff
changeset
|
208 %!test |
7775 | 209 %! f() |