Mercurial > hg > octave-nkf
annotate test/args.tst @ 20114:52d2bbf49c92 rc-4-0-0-1
maint: Bump version number for 4.0.0-rc1.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 05 Mar 2015 10:12:26 -0500 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 2006-2015 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); | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
26 %!endfunction |
5590 | 27 %!test |
28 %! f; | |
29 | |
7775 | 30 ## one input with two possible inputs |
5590 | 31 %!function f (x, y) |
7775 | 32 %! assert (nargin, 1); |
33 %! assert (nargout, 0); | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
34 %!endfunction |
5590 | 35 %!test |
36 %! f (1); | |
37 | |
7775 | 38 ## no inputs, one of multiple outputs |
5590 | 39 %!function [x, y] = f () |
7775 | 40 %! assert (nargin, 0); |
41 %! assert (nargout, 1); | |
5590 | 42 %! x = 2; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
43 %!endfunction |
5590 | 44 %!test |
7775 | 45 %! assert (f (), 2); |
5590 | 46 |
7775 | 47 ## one of multiple inputs, one of multiple outputs |
5590 | 48 %!function [x, y] = f (a, b) |
7775 | 49 %! assert (nargin, 1); |
50 %! assert (nargout, 1); | |
5590 | 51 %! x = a; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
52 %!endfunction |
5590 | 53 %!test |
7775 | 54 %! assert (f (1), 1); |
55 | |
56 ######################################## | |
57 ## Varargin, varargout | |
5590 | 58 |
7775 | 59 ## varargin and varargout with no inputs or outputs |
5590 | 60 %!function [varargout] = f (varargin) |
7775 | 61 %! assert (nargin, 0); |
62 %! assert (nargout, 0); | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
63 %!endfunction |
5590 | 64 %!test |
65 %! f; | |
66 | |
7775 | 67 ## varargin and varargout with one input |
5590 | 68 %!function [varargout] = f (x, varargin) |
7775 | 69 %! assert (nargin, 1); |
70 %! assert (nargout, 0); | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
71 %!endfunction |
5590 | 72 %!test |
73 %! f (1); | |
74 | |
7775 | 75 ## varargin and varargout with one output |
5590 | 76 %!function [x, varargout] = f (varargin) |
7775 | 77 %! assert (nargin, 0); |
78 %! assert (nargout, 1); | |
5590 | 79 %! x = 2; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
80 %!endfunction |
5590 | 81 %!test |
7775 | 82 %! assert (f (), 2); |
5590 | 83 |
7775 | 84 ## varargin and varargout with one input and output |
5590 | 85 %!function [varargout] = f (varargin) |
7775 | 86 %! assert (nargin, 1); |
87 %! assert (nargout, 1); | |
88 %! varargout{1} = varargin{1}; | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
89 %!endfunction |
5590 | 90 %!test |
7775 | 91 %! assert (f (1), 1); |
5590 | 92 |
7775 | 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. | |
5590 | 97 %!function [x, y, z] = f (a, b, c, d, e) |
7775 | 98 %! assert (nargin, 4); |
99 %! assert (nargout, 2); | |
100 %! x = a; | |
101 %! y = b; | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
102 %!endfunction |
5590 | 103 %!test |
104 %! [s, t] = f (1, 2, 3, 4); | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
105 %! assert ([s t], [1 2]); |
5590 | 106 |
7775 | 107 ## Fully used varargin and varargout |
5590 | 108 %!function [varargout] = f (varargin) |
7775 | 109 %! assert (nargin, 3); |
110 %! assert (nargout, 4); | |
111 %! varargout{1} = varargin{1}; | |
112 %! varargout{2} = varargin{2}; | |
113 %! varargout{3} = varargin{3}; | |
5590 | 114 %! varargout{4} = 4; |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
115 %!endfunction |
5590 | 116 %!test |
117 %! [s, t, u, v] = f (1, 2, 3); | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
118 %! assert ([s t u v], [1 2 3 4]); |
7775 | 119 |
120 ## Test default arguments | |
121 ## numeric | |
122 %!function f (x = 0) | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
123 %! assert (x, 0); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
124 %!endfunction |
7775 | 125 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
126 %! f() |
7775 | 127 |
128 ## numeric vector (spaces) | |
129 %!function f (x = [0 1 2]) | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
130 %! assert (x, [0 1 2]); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
131 %!endfunction |
7775 | 132 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
133 %! f() |
7775 | 134 |
135 ## numeric vector (range) | |
136 %!function f (x = 1:3) | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
137 %! assert (x, 1:3); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
138 %!endfunction |
7775 | 139 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
140 %! f() |
7775 | 141 |
142 ## numeric vector (commas) | |
143 %!function f (x = [0,1,2]) | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
144 %! assert (x, [0 1 2]); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
145 %!endfunction |
7775 | 146 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
147 %! f() |
7775 | 148 |
149 ## numeric vector (commas and spaces) | |
150 %!function f (x = [0, 1, 2]) | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
151 %! assert (x, [0 1 2]); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
152 %!endfunction |
7775 | 153 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
154 %! f() |
7775 | 155 |
156 ## numeric matrix | |
157 %!function f (x = [0, 1, 2;3, 4, 5]) | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
158 %! assert (x, [0 1 2;3 4 5]); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
159 %!endfunction |
7775 | 160 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
161 %! f() |
7775 | 162 |
163 ## empty cell | |
164 %!function f (x = {}) | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
165 %! assert (x, {}); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
166 %!endfunction |
7775 | 167 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
168 %! f() |
5590 | 169 |
7775 | 170 ## full cell |
171 %!function f (x = {1}) | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
172 %! assert (x, {1}); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
173 %!endfunction |
7775 | 174 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
175 %! f() |
7775 | 176 |
177 ## many cells | |
178 %!function f (x = {1 'a' "b" 2.0 struct("a", 3)}) | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
179 %! assert (x, {1 'a' "b" 2.0 struct("a", 3)}); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
180 %!endfunction |
7775 | 181 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
182 %! f() |
7775 | 183 |
184 ## struct | |
185 %!function f (x = struct("a", 3)) | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
186 %! assert (x, struct ("a", 3)); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
187 %!endfunction |
7775 | 188 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
189 %! f() |
7775 | 190 |
191 ## char (double quotes) | |
192 %!function f (x = "a") | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
193 %! assert (x, "a"); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
194 %!endfunction |
7775 | 195 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
196 %! f() |
7775 | 197 |
198 ## char (single quotes) | |
199 %!function f (x = 'a') | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
200 %! assert (x, "a"); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
201 %!endfunction |
7775 | 202 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
203 %! f() |
7775 | 204 |
205 ## char (string, double quotes) | |
206 %!function f (x = "abc123") | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
207 %! assert (x, "abc123"); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
208 %!endfunction |
7775 | 209 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
210 %! f() |
7775 | 211 |
212 ## char (string, double quotes, punctuation) | |
213 %!function f (x = "abc123`1234567890-=~!@#$%^&*()_+[]{}|;':\",./<>?\\") | |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
214 %! assert (x, "abc123`1234567890-=~!@#$%^&*()_+[]{}|;':\",./<>?\\"); |
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
215 %!endfunction |
7775 | 216 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
217 %! f() |
7775 | 218 |
219 ## Function handle (builtin) | |
220 %!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
|
221 %! 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
|
222 %! 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
|
223 %! assert (isa (x, "function_handle") && strcmp (fname, "sin")); |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
224 %!endfunction |
8805
065a05eb148a
test_args.m: don't use assert to test for function handles
John W. Eaton <jwe@octave.org>
parents:
7775
diff
changeset
|
225 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
226 %! f() |
7775 | 227 |
228 ## Function handle (anonymous) | |
229 %!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
|
230 %! 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
|
231 %! 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
|
232 %! assert (isa (x, "function_handle") && strcmp (ftype, "anonymous")); |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
233 %!endfunction |
8805
065a05eb148a
test_args.m: don't use assert to test for function handles
John W. Eaton <jwe@octave.org>
parents:
7775
diff
changeset
|
234 %!test |
17314
7f27a3cbdb41
Use Octave coding convention of 1 space after '%!' test prefix.
Rik <rik@octave.org>
parents:
16030
diff
changeset
|
235 %! f() |
14131
c3309e1ec50d
test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
236 |