Mercurial > octave-nkf
comparison test/args.tst @ 16030:1af8d21608b7
rename all test files in the test directory from test_X.m to X.tst
* Use - instead of _ for .tst file names. Fix all file lists in
module.mk and Makefile.am files.
* __run_test_suite__.m: Adapt to new naming convention.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 09 Feb 2013 21:35:55 -0500 |
parents | test/test_args.m@72c96de7a403 |
children | 7f27a3cbdb41 |
comparison
equal
deleted
inserted
replaced
16029:b8157404614f | 16030:1af8d21608b7 |
---|---|
1 ## Copyright (C) 2006-2012 John W. Eaton | |
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 | |
19 ######################################## | |
20 ## No inputs or no outputs | |
21 | |
22 ## no input or output arguments | |
23 %!function f () | |
24 %! assert (nargin, 0); | |
25 %! assert (nargout, 0); | |
26 %!endfunction | |
27 %!test | |
28 %! f; | |
29 | |
30 ## one input with two possible inputs | |
31 %!function f (x, y) | |
32 %! assert (nargin, 1); | |
33 %! assert (nargout, 0); | |
34 %!endfunction | |
35 %!test | |
36 %! f (1); | |
37 | |
38 ## no inputs, one of multiple outputs | |
39 %!function [x, y] = f () | |
40 %! assert (nargin, 0); | |
41 %! assert (nargout, 1); | |
42 %! x = 2; | |
43 %!endfunction | |
44 %!test | |
45 %! assert (f (), 2); | |
46 | |
47 ## one of multiple inputs, one of multiple outputs | |
48 %!function [x, y] = f (a, b) | |
49 %! assert (nargin, 1); | |
50 %! assert (nargout, 1); | |
51 %! x = a; | |
52 %!endfunction | |
53 %!test | |
54 %! assert (f (1), 1); | |
55 | |
56 ######################################## | |
57 ## Varargin, varargout | |
58 | |
59 ## varargin and varargout with no inputs or outputs | |
60 %!function [varargout] = f (varargin) | |
61 %! assert (nargin, 0); | |
62 %! assert (nargout, 0); | |
63 %!endfunction | |
64 %!test | |
65 %! f; | |
66 | |
67 ## varargin and varargout with one input | |
68 %!function [varargout] = f (x, varargin) | |
69 %! assert (nargin, 1); | |
70 %! assert (nargout, 0); | |
71 %!endfunction | |
72 %!test | |
73 %! f (1); | |
74 | |
75 ## varargin and varargout with one output | |
76 %!function [x, varargout] = f (varargin) | |
77 %! assert (nargin, 0); | |
78 %! assert (nargout, 1); | |
79 %! x = 2; | |
80 %!endfunction | |
81 %!test | |
82 %! assert (f (), 2); | |
83 | |
84 ## varargin and varargout with one input and output | |
85 %!function [varargout] = f (varargin) | |
86 %! assert (nargin, 1); | |
87 %! assert (nargout, 1); | |
88 %! varargout{1} = varargin{1}; | |
89 %!endfunction | |
90 %!test | |
91 %! assert (f (1), 1); | |
92 | |
93 ## multiple inputs, multiple outputs, but not all of either | |
94 ## WARNING: The original test did not assign the outputs, it just | |
95 ## requested them, and I think that is supposed to be an error. It also | |
96 ## still has a non-assigned output argument. | |
97 %!function [x, y, z] = f (a, b, c, d, e) | |
98 %! assert (nargin, 4); | |
99 %! assert (nargout, 2); | |
100 %! x = a; | |
101 %! y = b; | |
102 %!endfunction | |
103 %!test | |
104 %! [s, t] = f (1, 2, 3, 4); | |
105 %! assert ([s t], [1 2]); | |
106 | |
107 ## Fully used varargin and varargout | |
108 %!function [varargout] = f (varargin) | |
109 %! assert (nargin, 3); | |
110 %! assert (nargout, 4); | |
111 %! varargout{1} = varargin{1}; | |
112 %! varargout{2} = varargin{2}; | |
113 %! varargout{3} = varargin{3}; | |
114 %! varargout{4} = 4; | |
115 %!endfunction | |
116 %!test | |
117 %! [s, t, u, v] = f (1, 2, 3); | |
118 %! assert ([s t u v], [1 2 3 4]); | |
119 | |
120 ## Test default arguments | |
121 ## numeric | |
122 %!function f (x = 0) | |
123 %! assert (x, 0); | |
124 %!endfunction | |
125 %!test | |
126 %! f() | |
127 | |
128 ## numeric vector (spaces) | |
129 %!function f (x = [0 1 2]) | |
130 %! assert (x, [0 1 2]); | |
131 %!endfunction | |
132 %!test | |
133 %! f() | |
134 | |
135 ## numeric vector (range) | |
136 %!function f (x = 1:3) | |
137 %! assert (x, 1:3); | |
138 %!endfunction | |
139 %!test | |
140 %! f() | |
141 | |
142 ## numeric vector (commas) | |
143 %!function f (x = [0,1,2]) | |
144 %! assert (x, [0 1 2]); | |
145 %!endfunction | |
146 %!test | |
147 %! f() | |
148 | |
149 ## numeric vector (commas and spaces) | |
150 %!function f (x = [0, 1, 2]) | |
151 %! assert (x, [0 1 2]); | |
152 %!endfunction | |
153 %!test | |
154 %! f() | |
155 | |
156 ## numeric matrix | |
157 %!function f (x = [0, 1, 2;3, 4, 5]) | |
158 %! assert (x, [0 1 2;3 4 5]); | |
159 %!endfunction | |
160 %!test | |
161 %! f() | |
162 | |
163 ## empty cell | |
164 %!function f (x = {}) | |
165 %! assert (x, {}); | |
166 %!endfunction | |
167 %!test | |
168 %! f() | |
169 | |
170 ## full cell | |
171 %!function f (x = {1}) | |
172 %! assert (x, {1}); | |
173 %!endfunction | |
174 %!test | |
175 %! f() | |
176 | |
177 ## many cells | |
178 %!function f (x = {1 'a' "b" 2.0 struct("a", 3)}) | |
179 %! assert (x, {1 'a' "b" 2.0 struct("a", 3)}); | |
180 %!endfunction | |
181 %!test | |
182 %! f() | |
183 | |
184 ## struct | |
185 %!function f (x = struct("a", 3)) | |
186 %! assert (x, struct ("a", 3)); | |
187 %!endfunction | |
188 %!test | |
189 %! f() | |
190 | |
191 ## char (double quotes) | |
192 %!function f (x = "a") | |
193 %! assert (x, "a"); | |
194 %!endfunction | |
195 %!test | |
196 %! f() | |
197 | |
198 ## char (single quotes) | |
199 %!function f (x = 'a') | |
200 %! assert (x, "a"); | |
201 %!endfunction | |
202 %!test | |
203 %! f() | |
204 | |
205 ## char (string, double quotes) | |
206 %!function f (x = "abc123") | |
207 %! assert (x, "abc123"); | |
208 %!endfunction | |
209 %!test | |
210 %! f() | |
211 | |
212 ## char (string, double quotes, punctuation) | |
213 %!function f (x = "abc123`1234567890-=~!@#$%^&*()_+[]{}|;':\",./<>?\\") | |
214 %! assert (x, "abc123`1234567890-=~!@#$%^&*()_+[]{}|;':\",./<>?\\"); | |
215 %!endfunction | |
216 %!test | |
217 %! f() | |
218 | |
219 ## Function handle (builtin) | |
220 %!function f (x = @sin) | |
221 %! finfo = functions (x); | |
222 %! fname = finfo.function; | |
223 %! assert (isa (x, "function_handle") && strcmp (fname, "sin")); | |
224 %!endfunction | |
225 %!test | |
226 %! f() | |
227 | |
228 ## Function handle (anonymous) | |
229 %!function f (x = @(x) x.^2) | |
230 %! finfo = functions (x); | |
231 %! ftype = finfo.type; | |
232 %! assert (isa (x, "function_handle") && strcmp (ftype, "anonymous")); | |
233 %!endfunction | |
234 %!test | |
235 %! f() | |
236 |