1%% Copyright (C) 2014-2017 Colin B. Macdonald
2%% Copyright (C) 2016 Lagu
3%%
4%% This file is part of OctSymPy.
5%%
6%% OctSymPy is free software; you can redistribute it and/or modify
7%% it under the terms of the GNU General Public License as published
8%% by the Free Software Foundation; either version 3 of the License,
9%% or (at your option) any later version.
10%%
11%% This software is distributed in the hope that it will be useful,
12%% but WITHOUT ANY WARRANTY; without even the implied warranty
13%% of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14%% the GNU General Public License for more details.
15%%
16%% You should have received a copy of the GNU General Public
17%% License along with this software; see the file COPYING.
18%% If not, see <http://www.gnu.org/licenses/>.
19
20%% Tests
21% tests listed here are for current or fixed bugs.  Could move
22% these to appropriate functions later if desired.
23
24%!test
25%! % Issue #5, scalar expansion
26%! a = sym(1);
27%! a(2) = 2;
28%! assert (isequal(a, [1 2]))
29%! a = sym([]);
30%! a([1 2]) = [1 2];
31%! assert (isa(a, 'sym'))
32%! assert (isequal(a, [1 2]))
33%! a = sym([]);
34%! a([1 2]) = sym([1 2]);
35%! assert (isa(a, 'sym'))
36%! assert (isequal(a, [1 2]))
37
38
39%!test
40%! % "any, all" not implemented
41%! D = [0 1; 2 3];
42%! A = sym(D);
43%! assert (isequal( size(any(A-D)), [1 2] ))
44%! assert (isequal( size(all(A-D,2)), [2 1] ))
45
46
47%!test
48%! % double wasn't implemented correctly for arrays
49%! D = [0 1; 2 3];
50%! A = sym(D);
51%! assert (isequal( size(double(A)), size(A) ))
52%! assert (isequal( double(A), D ))
53
54
55%!test
56%! % in the past, inf/nan in array ctor made wrong matrix
57%! a = sym([nan 1 2]);
58%! assert (isequaln (a, [nan 1 2]))
59%! a = sym([1 inf]);
60%! assert( isequaln (a, [1 inf]))
61
62%!test
63%! % Issue #103: rot90, fliplr, flipud, flip on scalars
64%! % (In Octave, we do not need to overload these)
65%! syms x
66%! assert (isequal (rot90(x), x))
67%! assert (isequal (rot90(x, 1), x))
68%! assert (isequal (rot90(x, 17), x))
69%! assert (isequal (fliplr(x), x))
70%! assert (isequal (flipud(x), x))
71
72%!test
73%! % Issue #103: rot90, fliplr, flipud, flip on vectors
74%! syms x
75%! h = [1 2 x];
76%! v = [1; 2; x];
77%! assert (isequal (rot90(h), flipud(v)))
78%! assert (isequal (rot90(h, 1), flipud(v)))
79%! assert (isequal (rot90(h, 2), fliplr(h)))
80%! assert (isequal (rot90(h, 3), v))
81%! assert (isequal (rot90(h, 4), h))
82%! assert (isequal (rot90(h, 17), rot90(h, 1)))
83%! assert (isequal (rot90(v), h))
84%! assert (isequal (flipud(h), h))
85%! assert (isequal (fliplr(v), v))
86%! assert (isequal (fliplr(h), [x 2 1]))
87%! assert (isequal (flipud(v), [x; 2; 1]))
88
89%!test
90%! % Issue #103: rot90, fliplr, flipud, flip on matrices
91%! syms x
92%! A = [1 x 3; x 5 6];
93%! assert (isequal (rot90(A), [sym(3) 6; x 5; 1 x]))
94%! assert (isequal (rot90(A, 2), [6 5 x; 3 x 1]))
95%! assert (isequal (rot90(A, 3), [x 1; 5 x; sym(6) 3]))
96%! assert (isequal (rot90(A, 4), A))
97%! assert (isequal (flipud(A), [x 5 6; 1 x 3]))
98%! assert (isequal (fliplr(A), [3 x 1; 6 5 x]))
99
100%!test
101%! syms x
102%! h = [1 2 x];
103%! v = [1; 2; x];
104%! A = [1 x 3; x 5 6];
105%! assert (isequal (flip(x), x))
106%! assert (isequal (flip(x, 1), x))
107%! assert (isequal (flip(x, 2), x))
108%! assert (isequal (flip(h, 1), h))
109%! assert (isequal (flip(h, 2), fliplr(h)))
110%! assert (isequal (flip(A, 1), flipud(A)))
111%! assert (isequal (flip(A, 2), fliplr(A)))
112
113
114%% Bugs still active
115% Change these from xtest to test and move them up as fixed.
116
117%%!test
118%%! % FIXME: in SMT, x - true goes to x - 1
119%%! syms x
120%%! y = x - (1==1)
121%%! assert( isequal (y, x - 1))
122
123%!xtest
124%! % Issue #8: array construction when row is only doubles
125%! % fails on: Octave 3.6.4, 3.8.1, 4.0.0, hg tip Dec 2015.
126%! % works on: Matlab
127%! try
128%!   A = [sym(0) 1; 2 3];
129%!   failed = false;
130%! catch
131%!   failed = true;
132%! end
133%! assert (~failed)
134%! assert (isequal(A, [1 2; 3 4]))
135
136
137%!test
138%! % boolean not converted to sym (part of Issue #58)
139%! y = sym(1==1);
140%! assert( isa (y, 'sym'))
141%! y = sym(1==0);
142%! assert( isa (y, 'sym'))
143
144
145%!test
146%! % Issue #9, nan == 1 should be bool false not "nan == 1" sym
147%! snan = sym(0)/0;
148%! y = snan == 1;
149%! assert (~logical(y))
150
151%!test
152%! % Issue #9, for arrays, passes currently, probably for wrong reason
153%! snan = sym(nan);
154%! A = [snan snan 1] == [10 12 1];
155%! assert (isequal (A, sym([false false true])))
156
157%!test
158%! % these seem to work
159%! e = sym(inf) == 1;
160%! assert (~logical(e))
161
162
163%!test
164%! % known failure, issue #55; an upstream issue
165%! snan = sym(nan);
166%! assert (~logical(snan == snan))
167
168
169
170
171%% x == x tests
172% Probably should move to eq.m when fixed
173
174%!test
175%! % in SMT x == x is a sym (not "true") and isAlways returns true
176%! syms x
177%! assert (isAlways(  x == x  ))
178
179%!xtest
180%! % fails to match SMT (although true here is certainly reasonable)
181%! syms x
182%! e = x == x;
183%! assert (strcmp (strtrim(disp(e, 'flat')), 'x == x'))
184
185%!xtest
186%! % "fails" (well goes to false, which is reasonable enough)
187%! syms x
188%! e = x - 5 == x - 3;
189%! assert (strcmp (strtrim(disp(e, 'flat')), 'x - 5 == x - 3'))
190
191%%!test
192%%! % using eq for == and "same obj" is strange, part 1
193%%! % this case passes
194%%! syms x
195%%! e = (x == 4) == (x == 4);
196%%! assert (isAlways( e ))
197%%! assert (logical( e ))
198
199%%!test
200%%! % using eq for == and "same obj" is strange, part 2
201%%! syms x
202%%! e = (x-5 == x-3) == (x == 4);
203%%! assert (~logical( e ))
204%%! % assert (~isAlways( e ))
205
206%%!xtest
207%%! % using eq for == and "same obj" is strange, part 3
208%%! % this fails too, but should be true, although perhaps
209%%! % only with a call to simplify (i.e., isAlways should
210%%! % get it right).
211%%! syms x
212%%! e = (2*x-5 == x-1) == (x == 4);
213%%! assert (isAlways( e ))
214%%! assert (islogical( e ))
215%%! assert (isa(e, 'logical'))
216%%! assert (e)
217
218%!xtest
219%! % SMT behaviour for arrays: if any x's it should not be logical
220%! % output but instead syms for the equality objects
221%! syms x
222%! assert (isequal ( [x x] == sym([1 2]), [x==1 x==2] ))
223%! assert (isequal ( [x x] == [1 2], [x==1 x==2] ))
224%! % FIXME: new bool means these don't test the right thing
225%! %assert (~islogical( [x 1] == 1 ))
226%! %assert (~islogical( [x 1] == x ))
227%! %assert (~islogical( [x x] == x ))  % not so clear
228
229%!xtest
230%! % FIXME: symbolic matrix size, Issue #159
231%! syms n m integer
232%! A = sym('A', [n m]);
233%! assert (isequal (size (A), [n m]))
234
235%!xtest
236%! % symbolic matrix, subs in for size, Issue #160
237%! syms n m integer
238%! A = sym('A', [n m]);
239%! B = subs(A, [n m], [5 6]);
240%! assert (isa (B, 'sym'))
241%! assert (isequal (size (B), [5 6]))
242%! % FIXME: not same as an freshly created 5 x 6.
243%! C = sym('B', [5 6]);
244%! assert (isequal(B, C))
245