comparison CellTest.m @ 592:4422c4476650 feature/utux2D

Merge with feature/grids
author Martin Almquist <martin.almquist@it.uu.se>
date Mon, 11 Sep 2017 14:17:15 +0200
parents 2ce903f28193
children
comparison
equal deleted inserted replaced
591:39554f2de783 592:4422c4476650
1 function tests = CellTest()
2 tests = functiontests(localfunctions);
3 end
4
5 function testSize(testCase)
6 cases = {
7 {{}, [0, 0]},
8 {{1}, [1, 1]},
9 {{1, 2}, [1, 2]},
10 {{1; 2}, [2, 1]},
11 {{1, 2; 3, 4}, [2,2]},
12 };
13
14 for i = 1:length(cases)
15 A = Cell(cases{i}{1});
16 expected = cases{i}{2};
17
18 testCase.verifyEqual(size(A),expected);
19 end
20 end
21
22 function testLength(testCase)
23 cases = {
24 {{}, 0},
25 {{1}, 1},
26 {{1, 2}, 2},
27 {{1; 2}, 2},
28 {{1, 2; 3, 4}, 2},
29 };
30
31 for i = 1:length(cases)
32 A = Cell(cases{i}{1});
33 expected = cases{i}{2};
34
35 testCase.verifyEqual(length(A),expected);
36 end
37 end
38
39 function testIsEmpty(testCase)
40 cases = {
41 {cell(0,0), true},
42 {cell(1,0), true},
43 {cell(0,1), true},
44 {cell(1,1), false},
45 };
46
47 for i = 1:length(cases)
48 A = Cell(cases{i}{1});
49 expected = cases{i}{2};
50 testCase.verifyEqual(isempty(A),expected);
51 end
52 end
53
54 function testTranspose(testCase)
55 testCase.verifyEqual(Cell({1i, 2}).', Cell({1i; 2}));
56 testCase.verifyEqual(Cell({1i; 2}).', Cell({1i, 2}));
57 end
58
59 function testCtranspose(testCase)
60 testCase.verifyEqual(Cell({1i, 2})', Cell({1i; 2}));
61 testCase.verifyEqual(Cell({1i; 2})', Cell({1i, 2}));
62 end
63
64 function testRoundIndexWithProperty(testCase)
65 A = Cell({3,2,1});
66
67 result = A([1,3]).data;
68 testCase.verifyEqual(result, {3, 1});
69 end
70
71 function testSubAssignmentRound(testCase)
72 cases = {
73 % {
74 % lArray,
75 % index,
76 % rhs,
77 % expectedResult
78 % },
79 {
80 {},
81 1,
82 {'a'},
83 {'a'},
84 },
85 {
86 {1},
87 1,
88 {'a'},
89 {'a'},
90 },
91 {
92 {1,2,3},
93 2,
94 {'a'},
95 {1,'a',3},
96 },
97 {
98 {1,2,3},
99 2,
100 [],
101 {1,3},
102 },
103 };
104
105 for i = 1:length(cases)
106 lArray = Cell(cases{i}{1});
107 index = cases{i}{2};
108 rhs = cases{i}{3};
109 expectedResult = cases{i}{4};
110
111 lArray(index) = rhs;
112
113 testCase.verifyEqual(lArray.data, expectedResult)
114 end
115 end
116
117 function testSubAssignmentCurly(testCase)
118 cases = {
119 % {
120 % lArray,
121 % index,
122 % rhs,
123 % expectedResult
124 % },
125 {
126 {},
127 1,
128 'a',
129 {'a'},
130 },
131 {
132 {1},
133 1,
134 'a',
135 {'a'},
136 },
137 {
138 {1,2,3},
139 2,
140 'a',
141 {1,'a',3},
142 },
143 };
144
145 for i = 1:length(cases)
146 lArray = Cell(cases{i}{1});
147 index = cases{i}{2};
148 rhs = cases{i}{3};
149 expectedResult = cases{i}{4};
150
151 lArray{index} = rhs;
152
153 testCase.verifyEqual(lArray.data, expectedResult)
154 end
155 end
156
157 function testIndexreferenceRound(testCase)
158 cases = {
159 % {
160 % array,
161 % index,
162 % roundResult
163 % },
164 {
165 {1,2,3},
166 1,
167 {1},
168 },
169 {
170 {1,3,2},
171 2,
172 {3},
173 },
174 {
175 {1,3,2},
176 [1 3],
177 {1, 2},
178 },
179 };
180
181
182 for i = 1:length(cases)
183 array = Cell(cases{i}{1});
184 index = cases{i}{2};
185 expected = cases{i}{3};
186
187 result = array(index);
188
189 testCase.verifyTrue(isa(result, 'Cell'));
190 testCase.verifyEqual(result.data, expected);
191 end
192 end
193
194 function testEndIndexing(testCase)
195 C = Cell({1,2,3});
196
197 testCase.verifyEqual(C(end), Cell({3}));
198 testCase.verifyEqual(C{end}, 3);
199 end
200
201 function testColonIndexing(testCase)
202 C = Cell({1, 2, 3});
203 D = Cell({1; 2; 3});
204
205 testCase.verifyEqual(C(:), D);
206 testCase.verifyEqual(D(:), D);
207 end
208
209 function testIndexreferenceCurly(testCase)
210 cases = {
211 % {
212 % array,
213 % index,
214 % curlyResult
215 % },
216 {
217 {1,2,3},
218 1,
219 1
220 },
221 {
222 {1,3,2},
223 2,
224 3
225 },
226 };
227
228
229 for i = 1:length(cases)
230 array = Cell(cases{i}{1});
231 index = cases{i}{2};
232 expected = cases{i}{3};
233
234 result = array{index};
235
236 testCase.verifyEqual(result, expected);
237 end
238 end
239
240 function testConcat(testCase)
241 cases = {
242 {{},{}},
243 {{1},{}},
244 {{},{1}},
245 {{1},{2}},
246 {{1, 2},{3, 4}},
247 {{1; 2},{3; 4}},
248 };
249
250 horzCat = {
251 {},
252 {1},
253 {1},
254 {1,2},
255 {1, 2, 3, 4},
256 {1, 3; 2, 4},
257 };
258
259 vertCat = {
260 {},
261 {1},
262 {1},
263 {1; 2},
264 {1, 2; 3, 4},
265 {1; 2; 3; 4},
266 };
267
268 for i = 1:length(cases)
269 A = Cell(cases{i}{1});
270 B = Cell(cases{i}{2});
271
272 C_horz = [A, B];
273 C_vert = [A; B];
274
275 testCase.verifyEqual(C_horz.data, horzCat{i});
276 testCase.verifyEqual(C_vert.data, vertCat{i});
277
278 end
279 end