testDomain.m 7.34 KB
Newer Older
1
2
3
4
function [tests] = testDomain()
	tests = functiontests(localfunctions);
end

Jakob Gabriel's avatar
Jakob Gabriel committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function testReplace(tc)
z = linspace(0, pi, 3);
a = quantity.Domain("a", z);
b = quantity.Domain("b", z);
c = quantity.Domain("c", z);
d = [a b c];

d = d.replace(quantity.Domain("b", linspace(0, 1, 2)));
tc.verifyEqual([d.name], ["a"; "b"; "c"].');
tc.verifyEqual(d(1).grid, linspace(0, pi, 3).');
tc.verifyEqual(d(3).grid, linspace(0, pi, 3).');
tc.verifyEqual(d(2).grid, linspace(0, 1, 2).');

d = d.replace(quantity.Domain("e", linspace(2, 3, 4)));
tc.verifyEqual([d.name], ["a"; "b"; "c"].');
tc.verifyEqual(d(1).grid, linspace(0, pi, 3).');
tc.verifyEqual(d(3).grid, linspace(0, pi, 3).');
tc.verifyEqual(d(2).grid, linspace(0, 1, 2).');
end % testReplace

25
function setupOnce(tc)
26
27
28
29
30
31
32

z = linspace(0, pi, 3);
a = quantity.Domain('a', z);
b = quantity.Domain('b', z);
c = quantity.Domain('c', z);
d = [a b c];

33
34
35
36
37
tc.TestData.z = z;
tc.TestData.a = a;
tc.TestData.b = b;
tc.TestData.c = c;
tc.TestData.d = d;
38

39
40
end

41
42
function testDiscrete(tc)
d = quantity.Domain("a", linspace(0, 1, 3));
43
tc.verifyEqual(d.Discrete.on(), d.grid);
44
45
46
47
end % testDiscrete

function testSymbolic(tc)
d = quantity.Domain("a", linspace(0, 1, 3));
48
tc.verifyEqual(d.Symbolic.on(), d.grid);
49
50
end % testDiscrete

Jakob Gabriel's avatar
Jakob Gabriel committed
51
52
53
54
55
56
57
58
59
60
61
62
function testRename(tc)
d = [quantity.Domain("a", linspace(0, 1, 3)), ...
	quantity.Domain("b", linspace(0, 2, 5)), ...
	quantity.Domain("ab", linspace(0, pi, 4))];
xyz = d.rename(["x", "y", "z"]);
xyz2 = d.rename(["z", "y", "x"], ["ab", "b", "a"]);
tc.verifyEqual([xyz.name], ["x", "y", "z"]);
tc.verifyEqual([xyz.name], [xyz2.name]);

cba = d.rename(["c", "b", "a"], ["a", "b", "ab"]);
tc.verifyEqual([cba.name], ["c", "b", "a"]);
tc.verifyEqual([cba.n], [d.n]);
63
64
65

zZetaDomain = [quantity.Domain("z", linspace(0, 1, 3)), quantity.Domain("zeta", linspace(0, 1, 3))];
tc.verifyEqual([zZetaDomain.rename("a", "z").name], ["a", "zeta"])
Jakob Gabriel's avatar
Jakob Gabriel committed
66
end % testRename()
67

Jakob Gabriel's avatar
Jakob Gabriel committed
68
function testParser(tc)%profile on
69
70
71
72
d = quantity.Domain.parser('blub', 1, 'grid', 1:3, 'gridName', 'test');
a = quantity.Domain.parser('domain', d);
b = quantity.Domain.parser('blabla', 0, 'domain', a);
b = quantity.Domain.parser('blabla', 0, 'domain', a, 'blub', 'a');
Jakob Gabriel's avatar
Jakob Gabriel committed
73
74
%profile off
%profile viewer
75
76


77
78
tc.verifyEqual(d, a);
tc.verifyEqual(b, a);
79
80
81

end

82
function testSplit(tc)
83
84
85
86
87

a = quantity.Domain('a', -5:15 );

b = a.split([-1, 0, 4]);

88
89
90
91
tc.verifyEqual(b(1).grid', -5:-1)
tc.verifyEqual(b(2).grid', -1:0)
tc.verifyEqual(b(3).grid', 0:4)
tc.verifyEqual(b(4).grid', 4:15)
92

93
94
tc.verifyWarning(@() a.split([0.1, 2.6]), 'DOMAIN:split')
tc.verifyWarningFree(@() a.split([0.1, 2.6], 'AbsTol', 0.4), 'DOMAIN:split')
95
96

c = a.split([0.1, 2.6], 'warning', false);
97

98
99
100
tc.verifyEqual(c(1).grid', -5:0)
tc.verifyEqual(c(2).grid', 0:3)
tc.verifyEqual(c(3).grid', 3:15)
101
102
103

end

104
function testContains(tc)
105
106
107
108

a = quantity.Domain('a', 0:10);
b = quantity.Domain('b', 3:5);

109
110
111
112
tc.verifyTrue( a.contains(b) )
tc.verifyTrue( a.contains(1) )
tc.verifyFalse( a.contains(11) )
tc.verifyFalse( a.contains(1.5) )
113
114
115

A = [quantity.Domain('a1', 0:10), quantity.Domain('a2', 0:10)];

116
tc.verifyTrue( A.contains(5) )
117

118
119
end

120
121
122
123
124
125
126
function testIsequidistant(tc)
tc.verifyTrue(tc.TestData.a.isequidistant);
tc.verifyTrue(all(isequidistant(tc.TestData.d)));
tc.verifyFalse(isequidistant(quantity.Domain("z", [0, 0.2, 0.4, 0.8, 1.6])));
tc.verifyFalse(all(isequidistant(...
	[tc.TestData.a, quantity.Domain("z", [0, 0.2, 0.4, 0.8, 1.6]), tc.TestData.b])));
end % testIsequidistant
127

128
129
130
131
132
133
134
135
136
137
138
139
140
141
function testFind(tc)

z = tc.TestData.z;
a = tc.TestData.a;
b = tc.TestData.b;
c = tc.TestData.c;
d = tc.TestData.d;

tc.verifyEqual( d.find('a'), a)
tc.verifyEqual( d.find('b', 'c'), [b c])
tc.verifyEqual( d.find({'b', 'c'}), [b c])
tc.verifyEqual( d.find(b.name), b);
tc.verifyEqual( d.find({a.name c.name}), [a c])
tc.verifyEqual( d.find([a.name b.name]), [a b])
142

143
144
end

Jakob Gabriel's avatar
Jakob Gabriel committed
145
146
147
148
149
150
151
152
153
function testRemove(tc)
d = tc.TestData.d;
dWithoutA = d.remove("a");
tc.verifyEqual([dWithoutA.name], ["b" "c"]);

a = d.remove(dWithoutA);
tc.verifyEqual(a.name, "a");
end

154
function testGet(tc)
155
156
157
158
159
160
161
162

z = linspace(0, pi, 3);
a = quantity.Domain('a', z);
b = quantity.Domain('b', z);
c = quantity.Domain('c', z);

d = [a b c];

163
164
165
tc.verifyEqual(d.find('a'), a);
tc.verifyEqual(d.find(b.name), b);
tc.verifyEqual(d.find('c'), c);
166
167
168

end

169
function testEquality(tc)
170

171
d = quantity.Domain('d', 0);
172
173
e = quantity.Domain.empty();

174
175
176
dd = d;
ee = e;

177
178
179
180
tc.verifyNotEqual( d, e );
tc.verifyEqual( dd, d );
tc.verifyEqual( ee, e );
tc.verifyNotEqual( e, d );
181

182
183
184
185
186
187
d1 = quantity.Domain('d', 1:3);
d2 = quantity.Domain('d', 1:3);
d3 = quantity.Domain('d', 2:4);
tc.verifyEqual(d1, d2);
tc.verifyNotEqual(d1, d3);

188
189
end

190
function testDomainInit(tc)
191
192

Z = linspace(0,pi, 3);
193
d = quantity.Domain('z', Z);
194
195
D = [d d];

196
197
tc.verifyEqual( ndims(D), 2);
tc.verifyEqual( ndims(d), 1);
198
199
end

200
function testSort(tc)
Ferdinand Fischer's avatar
tmp    
Ferdinand Fischer committed
201
202

z = linspace(0, pi, 3);
203
204
205
a = quantity.Domain('a', z);
b = quantity.Domain('b', z);
c = quantity.Domain('c', z);
Ferdinand Fischer's avatar
tmp    
Ferdinand Fischer committed
206
207
208
209
210
211
212

d = [a b c];

[d_, I_] = d.sort('descend');
[d__, I__] = d.sort('ascend');
d___ = d.sort();

213
214
215
216
tc.verifyEqual([d_.name], ["c", "b", "a"]);
tc.verifyEqual({d__.name}, {d.name});
tc.verifyEqual({d__.name}, {d___.name});
tc.verifyEqual( I_, flip(I__) );
Ferdinand Fischer's avatar
tmp    
Ferdinand Fischer committed
217
218
219


end
220

221
function testDomainGridLength(tc)
222
223

Z = linspace(0,pi, 3);
224
d = quantity.Domain('z', Z);
225
226
D = [d d];

227
tc.verifyEqual( cellfun(@(v) numel(v), {Z, Z}), D.gridLength)
228
229
230

end

231
function testDomainNumGridElements(tc)
232
233

Z = linspace(0,pi, 3);
234
d = quantity.Domain('z', Z);
235
236
D = [d d];

237
tc.verifyEqual( D.numGridElements, prod([length(Z), length(Z)]));
238
239
240

end

241
function testEmpty(tc)
242
243

d = quantity.Domain();
244
e = quantity.Domain.empty();
245
o = quantity.Domain('', 1);
246

247
248
249
250
251
252
253
tc.verifyTrue( isempty(d) )
tc.verifyTrue( isempty(e) )
tc.verifyTrue( isempty([d, e]))
tc.verifyTrue( isempty([d, d]))
tc.verifyFalse( isempty(o) )
tc.verifyFalse( isempty([o e]) )
tc.verifyTrue( isempty([o d]) )
254

255
256
257
end


258
259
260
261
262
263
264
265
266
function testDefaultGrid(testCase)
	g = quantity.Domain.defaultDomain([100, 42]);
	testCase.verifyEqual(g(1).grid, linspace(0, 1, 100).');
	testCase.verifyEqual(g(2).grid, linspace(0, 1, 42).');
    
    g = quantity.Domain.defaultDomain();
    testCase.verifyEqual(g.grid, linspace(0, 1).');
    testCase.verifyEqual(g.name, "x_1")
    
267
268
end

269
function testJoin(tc)
270
271
272
273
s = linspace(0, 1);
z = linspace(0, 1, 71)';
t = linspace(0, 1, 51);

274
275
276
277
278
279
S = quantity.Domain('s', s);
Z = quantity.Domain('z', z);
T = quantity.Domain('t', t);
Ps = quantity.Domain('p', s);
St = quantity.Domain('s', t);
Pt = quantity.Domain('p', t);
280
281
282
283
284

a = [Z, T, S];
b = [Ps, St];
c = Pt;

285
286
287
288
joinedDomainAB = sort( join(a, b) );
joinedDomainCC = sort( join(c, c) );
joinedDomainBC = sort( join(b, c) );
joinedDomain = sort( join(Z, [Z, T]) );
289

290
291
292
293
294
295
296
tc.verifyEqual([joinedDomainAB.name], ["p", "s", "t", "z"]);
tc.verifyEqual({joinedDomainAB.grid}, {s(:), s(:), t(:), z(:)});
tc.verifyEqual(joinedDomainCC.name, "p");
tc.verifyEqual({joinedDomainCC.grid}, {t(:)});
tc.verifyEqual([joinedDomainBC.name], ["p", "s"]);
tc.verifyEqual({joinedDomainBC.grid}, {s(:), t(:)});
tc.verifyEqual({joinedDomain.grid}, {t(:), z(:)});
297
298
299
300

d1 = [Z Z T];
d2 = quantity.Domain.empty;

301
tc.verifyEqual( d1.join( d2 ), [Z T] )
302

303
end % join()
304

305
function testFind_Index(tc)
306
307
308
309
z = linspace(0, 2*pi, 71)';
t = linspace(0, 3*pi, 51);
s = linspace(0, 1);

310
311
312
S = quantity.Domain('s', s);
Z = quantity.Domain('z', z);
T = quantity.Domain('t', t);
313
314
315

a = [Z, T, S];

316
idx = a.index('z');
317

318
tc.verifyEqual(idx, 1);
319
320


321
idx = a.index({'z', 't'});
322
tc.verifyEqual(idx, [1 2]);
323

324
idx = a.index({'z', 's'});
325
tc.verifyEqual(idx, [1 3]);
326

327
idx = a.index('t');
328
tc.verifyEqual(idx, 2);
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

end



















350

351