Mercurial > hg > octave-lyh
annotate scripts/general/isa.m @ 11449:93b8c7ca211f
isa.m: Add tests against logical types
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 05 Jan 2011 21:21:37 -0800 |
parents | 0d9640d755b1 |
children | fd0a3ac60b0e |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2004, 2005, 2007, 2008, 2009 John W. Eaton |
4950 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
4950 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
4950 | 18 |
19 ## -*- texinfo -*- | |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} isa (@var{obj}, @var{class}) |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## Return true if @var{obj} is an object from the class @var{class}. |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
22 ## @seealso{class, typeinfo} |
4950 | 23 ## @end deftypefn |
24 | |
25 ## Author: Paul Kienzle <pkienzle@users.sf.net> | |
26 ## Adapted-by: jwe | |
27 | |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
28 function retval = isa (obj, cname) |
7125 | 29 |
30 if (nargin != 2) | |
31 print_usage (); | |
32 endif | |
33 | |
7773 | 34 persistent float_classes = {"double", "single"}; |
35 | |
36 persistent fnum_classes = {"double", "single", ... | |
10549 | 37 "uint8", "uint16", "uint32", "uint64", ... |
38 "int8", "int16", "int32", "int64"}; | |
7773 | 39 |
7772
ff717f2f9feb
Treat numeric and float argument in the isa function.
David Bateman <dbateman@free.fr>
parents:
7125
diff
changeset
|
40 if (strcmp (cname, "float")) |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
41 retval = any (strcmp (class (obj), float_classes)); |
7778
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
42 elseif (strcmp (cname, "numeric")) |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
43 retval = any (strcmp (class (obj), fnum_classes)); |
7772
ff717f2f9feb
Treat numeric and float argument in the isa function.
David Bateman <dbateman@free.fr>
parents:
7125
diff
changeset
|
44 else |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
45 class_of_x = class (obj); |
9010
f914834836e7
Partial implementation of derived classes using the old form with "@" files.
rtshort@smoketree.phaselocked.com
parents:
8920
diff
changeset
|
46 retval = strcmp (class_of_x, cname); |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
47 if (! retval && isobject (obj)) |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
48 retval = __isa_parent__ (obj, cname); |
9010
f914834836e7
Partial implementation of derived classes using the old form with "@" files.
rtshort@smoketree.phaselocked.com
parents:
8920
diff
changeset
|
49 endif |
7772
ff717f2f9feb
Treat numeric and float argument in the isa function.
David Bateman <dbateman@free.fr>
parents:
7125
diff
changeset
|
50 endif |
7773 | 51 |
4950 | 52 endfunction |
7773 | 53 |
7777 | 54 %!assert (isa ("char", "float"), false) |
11449
93b8c7ca211f
isa.m: Add tests against logical types
Rik <octave@nomad.inbox5.com>
parents:
11431
diff
changeset
|
55 %!assert (isa (logical (1), "float"), false) |
7777 | 56 %!assert (isa (double (13), "float"), true) |
57 %!assert (isa (single (13), "float"), true) | |
58 %!assert (isa (int8 (13), "float"), false) | |
59 %!assert (isa (int16 (13), "float"), false) | |
60 %!assert (isa (int32 (13), "float"), false) | |
61 %!assert (isa (int64 (13), "float"), false) | |
62 %!assert (isa (uint8 (13), "float"), false) | |
63 %!assert (isa (uint16 (13), "float"), false) | |
64 %!assert (isa (uint32 (13), "float"), false) | |
65 %!assert (isa (uint64 (13), "float"), false) | |
7778
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
66 %!assert (isa ("char", "numeric"), false) |
11449
93b8c7ca211f
isa.m: Add tests against logical types
Rik <octave@nomad.inbox5.com>
parents:
11431
diff
changeset
|
67 %!assert (isa (logical (1), "numeric"), false) |
7778
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
68 %!assert (isa (double (13), "numeric"), true) |
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
69 %!assert (isa (single (13), "numeric"), true) |
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
70 %!assert (isa (int8 (13), "numeric"), true) |
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
71 %!assert (isa (int16 (13), "numeric"), true) |
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
72 %!assert (isa (int32 (13), "numeric"), true) |
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
73 %!assert (isa (int64 (13), "numeric"), true) |
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
74 %!assert (isa (uint8 (13), "numeric"), true) |
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
75 %!assert (isa (uint16 (13), "numeric"), true) |
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
76 %!assert (isa (uint32 (13), "numeric"), true) |
97b7ba81e1c3
isa.m: fnumeric -> numeric
David Bateman <dbateman@free.fr>
parents:
7777
diff
changeset
|
77 %!assert (isa (uint64 (13), "numeric"), true) |