1########################################################################
2##
3## Copyright (C) 2013-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%%  Test script for classdef OOP.
27%%  Requires the path to contain the test classes.
28%%
29%%  Note: This script and all classes are also intended to run
30%%        in MATLAB to test compatibility.  Don't break that!
31%%
32%%  To Do:  This script tests to ensure that things done correctly work
33%%          correctly.  It should also check that things done incorrectly
34%%          error properly.
35%%
36%%  The classes used for the tests reside in the test/classdef with others
37%%  in the test directory.
38%%
39%%  The classes provide the ability to test most of the major features
40%%  of the classdef OOP facilities.  There are a number of classes, mostly
41%%  kind of the same, that create a hierarchy.
42
43%%  Basic classdef tests for value class
44%!shared p, q, i, amt
45%! q = foo_value_class ();
46%! p = foo_value_class (4, 4*12, 50e3);
47%! i = p.rate / (12 * 100);
48%! amt = (p.principle * i) / (1 - (1 + i)^(-p.term));
49%!assert (isempty (q.rate))
50%!assert (isempty (q.principle))
51%!assert (isempty (q.term))
52%!assert (strcmp (class (p), 'foo_value_class'))
53%!assert (p.term == 48)
54%!assert (p.rate == 4.0)
55%!assert (p.principle == 50e3)
56%!assert (p.amount, amt, eps ())
57%!assert (amount (p), amt, eps ())
58%!xtest <55961>
59%! assert (properties (p), {'rate'; 'term'; 'principle'});
60%!xtest <*55858>
61%! assert (methods (p), {'amount'; 'foo_value_class'});
62%!assert (isempty (foo_value_class().rate))
63%!error <property 'rate' is not constant> foo_value_class.rate
64%!shared  # clear all shared variables for remainder of tests
65
66%%  Static method and Constant Property
67%!assert (foo_static_method_constant_property.radians_per_cycle == 2*pi)
68%!assert (foo_static_method_constant_property().radians_per_cycle == 2*pi)
69%!assert (foo_static_method_constant_property().pie == pi)
70%!error <property 'frequency' is not constant> foo_static_method_constant_property.frequency
71%!error <method 'cosine' is not static> foo_static_method_constant_property.cosine
72%!test
73%! obj = foo_static_method_constant_property;
74%! obj.frequency = 10;
75%! assert (obj.cosine (0.1), cos (2 * pi * 10 * 0.1), eps ());
76%! assert (obj.sine (0.1), sin (2 * pi * 10 * 0.1), eps ());
77
78%!test
79%! obj = foo_method_changes_property_size (3);
80%! obj = obj.move_element_to_end (2);
81%! assert (isequal (obj.element, [1 3 2]));
82
83%!error <parse error> plist_t1
84%!assert (strcmp (class (plist_t2), 'plist_t2'))
85%!assert (strcmp (class (plist_t3), 'plist_t3'))
86
87%!test
88%! obj = struct_wrapper ();
89%! obj{'a'} = 1;
90%! assert (obj{'a'} == 1);
91%! obj{'bc'} = 2;
92%! assert (obj{'bc'} == 2);
93%! assert (isequal (obj{'a', 'bc'}, [1 2]));
94
95%% Test for meta.class.fromName
96%!test <*51935>
97%! meta.class.fromName ("inputParser");
98
99%% Do not change this to "containers.Map()".  This test is intended to
100%% ensure that calling a function in a +package directory will work
101%% properly.
102%!test <*51715>
103%! x = containers.Map;
104%! assert (isobject (x));
105
106%!assert <*52096> (isempty (meta.class.fromName ("__nonexi$tent_cl@$$__")))
107%!assert <*52096> (isempty (meta.package.fromName ("__nonexi$tent_p@ck@ge__")))
108
109%% Test overloaded subsref and subsasgn functions.
110%% (bug #54783, bug #54966, and bug #55223)
111%!test <*54783>
112%! obj = foo_subsref_subsasgn (1);
113%! obj(2) = 3;
114%! assert (obj(2) == 3)
115%! assert (obj{2} == 3)
116%! assert (isequal (obj.x, [1 3 3 4]))
117%! obj{2} = 4;
118%! assert (obj(2) == 4)
119%! assert (obj{2} == 4)
120%! assert (isequal (obj.x, [1 4 3 4]))
121%! obj(end) = 6;
122%! assert (obj(end) == 6)
123%! assert (obj{end} == 6)
124%! assert (isequal (obj.x, [1 4 3 6]))
125%! obj{end} = 8;
126%! assert (obj(end) == 8)
127%! assert (obj{end} == 8)
128%! assert (isequal (obj.x, [1 4 3 8]))
129%! obj.x = 1:4;
130%! assert (isequal (obj.x, 1:4))
131%! obj(1:3) = 7:9;
132%! assert (isequal (obj(1:3), 7:9))
133%! assert (isequal (obj.x, [7 8 9 4]))
134%! obj(2:end) = 5:7;
135%! assert (isequal (obj(2:end), 5:7))
136%! assert (isequal (obj.x, [7 5 6 7]))
137
138%!xtest <54966>
139%! obj = foo_subsref_subsasgn (1);
140%! obj{1:3} = 5:7;
141%! assert (isequal ([obj{1:3}], 5:7))
142%! assert (isequal (obj.x, [5 6 7 4]))
143%! obj{2:end} = 7:9;
144%! assert (isequal ([obj{2:end}], 7:9))
145%! assert (isequal (obj.x, [5 7 8 9]))
146
147%!test <*54783>
148%! obj = foo_subsref_subsasgn (1);
149%! obj.x(2) = 3;
150%! assert (obj.x(2) == 3)
151%! assert (obj.x{2} == 3)
152%! assert (isequal (obj.x, [1 3 3 4]))
153%! obj.x{2} = 4;
154%! assert (obj.x(2) == 4)
155%! assert (obj.x{2} == 4)
156%! assert (isequal (obj.x, [1 4 3 4]))
157%! obj.x(end) = 6;
158%! assert (obj.x(end) == 6)
159%! assert (obj.x{end} == 6)
160%! assert (isequal (obj.x, [1 4 3 6]))
161%! obj.x{end} = 8;
162%! assert (obj.x(end) == 8)
163%! assert (obj.x{end} == 8)
164%! assert (isequal (obj.x, [1 4 3 8]))
165%! obj.x = 1:4;
166%! assert (isequal (obj.x, 1:4))
167%! obj.x(1:3) = 7:9;
168%! assert (isequal (obj.x(1:3), 7:9))
169%! assert (isequal (obj.x, [7 8 9 4]))
170%! obj.x(2:end) = 5:7;
171%! assert (isequal (obj.x(2:end), 5:7))
172%! assert (isequal (obj.x, [7 5 6 7]))
173
174%!xtest <54966>
175%! obj = foo_subsref_subsasgn (1);
176%! obj.x{1:3} = 5:7;
177%! assert (isequal ([obj.x{1:3}], 5:7))
178%! assert (isequal (obj.x, [5 6 7 4]))
179%! obj.x{2:end} = 7:9;
180%! assert (isequal ([obj.x{2:end}], 7:9))
181%! assert (isequal (obj.x, [5 7 8 9]))
182
183%!test <*55223>
184%! obj = foo_subsref_subsasgn (2);
185%! obj{2}(2) = 3;
186%! assert (obj{2}(2) == 3);
187%! obj{2}{2} = 4;
188%! assert (obj{2}{2} == 4);
189
190%!xtest <54966>
191%! obj = foo_subsref_subsasgn (2);
192%! obj{1:2}(1:2) = ones (2);
193%! assert (isequal (obj{1:2}(1:2), ones (2)));
194%! obj{3:4}(3:4) = 4 * ones (2);
195%! assert (isequal (obj{3:4}(3:4), 4 * ones (2)));
196
197%!test <*52614>
198%! A = class_bug52614A ();
199%! assert (A.a, 1);
200%! B = class_bug52614B ();
201%! assert (B.a, 1);
202%! assert (B.b, 2);
203
204%!test <*55766>
205%! x = class_bug55766 ();
206%! props = {"notahiddentestprop"; "publictestprop"; "testprop"};
207%! assert (properties (x), props);
208