Skip to content

Commit 56e04ea

Browse files
jmannaufloryst
authored andcommitted
test(DataArray): Update DataArray.getRanges tests to remove need for compareFloat
added test for getRanges(false)
1 parent 1cb7040 commit 56e04ea

File tree

1 file changed

+64
-13
lines changed

1 file changed

+64
-13
lines changed

Sources/Common/Core/DataArray/test/testDataArray.js

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,41 +237,92 @@ test('Test vtkDataArray getRange function with multi-channel data.', (t) => {
237237

238238
test('Test vtkDataArray getRanges function with multi-channel data.', (t) => {
239239
// create a data array with 3 channel data.
240-
const newArray = new Uint16Array(256 * 3);
240+
const numberOfPixels = 10;
241+
const numberOfComponents = 4;
242+
const newArray = new Uint16Array(numberOfPixels * numberOfComponents);
241243

242244
// fill the new array with the pattern 1,2,3, 1,2,3
243245
// such that each channel has 1,1,1 2,2,2 3,3,3 respectively.
244-
for (let i = 0; i < 256; ++i) {
245-
newArray[i * 3] = i;
246-
newArray[i * 3 + 1] = i * 2;
247-
newArray[i * 3 + 2] = i * 3;
246+
for (let i = 0; i < numberOfPixels; ++i) {
247+
newArray[i * numberOfComponents] = i;
248+
newArray[i * numberOfComponents + 1] = i * 2;
249+
newArray[i * numberOfComponents + 2] = i * 3;
250+
newArray[i * numberOfComponents + 3] = i * 4;
248251
}
249252

250253
const da = vtkDataArray.newInstance({
251-
numberOfComponents: 3,
254+
numberOfComponents,
252255
values: newArray,
253256
});
254257

255258
const ranges = da.getRanges();
256259

257260
t.ok(
258-
ranges.length === 4,
259-
'getRanges should return an array of 4 vtkRange objects'
261+
ranges.length === numberOfComponents + 1,
262+
'getRanges should return an array of 5 vtkRange objects'
260263
);
261264
t.ok(ranges[0].min === 0, 'component:0 minimum value should be 0');
262-
t.ok(ranges[0].max === 255, 'component:0 maximum value should be 255');
265+
t.ok(ranges[0].max === 9, 'component:0 maximum value should be 9');
263266
t.ok(ranges[1].min === 0, 'component:1 minimum value should be 0');
264-
t.ok(ranges[1].max === 510, 'component:1 maximum value should be 510');
267+
t.ok(ranges[1].max === 18, 'component:1 maximum value should be 18');
265268
t.ok(ranges[2].min === 0, 'component:2 minimum value should be 0');
266-
t.ok(ranges[2].max === 765, 'component:2 maximum value should be 765');
269+
t.ok(ranges[2].max === 27, 'component:2 maximum value should be 27 ');
267270
t.ok(
268271
ranges[2].min === 0,
269272
'component:-1 vector magnitude minimum should be 0'
270273
);
271274
t.ok(
272-
compareFloat(ranges[3].max, 954.1226336273551),
273-
'component:-1 vector magnitude maximum should be 954.1226336273551'
275+
ranges[3].max === 36,
276+
'component:-1 vector magnitude maximum should be 36'
277+
);
278+
279+
t.end();
280+
});
281+
282+
test('Test vtkDataArray getRanges(false) (`computeRanges=false`) function with multi-channel data', (t) => {
283+
// create a data array with 3 channel data.
284+
const numberOfPixels = 10;
285+
const numberOfComponents = 4;
286+
const newArray = new Uint16Array(numberOfPixels * numberOfComponents);
287+
288+
// fill the new array with the pattern 1,2,3, 1,2,3
289+
// such that each channel has 1,1,1 2,2,2 3,3,3 respectively.
290+
for (let i = 0; i < numberOfPixels; ++i) {
291+
newArray[i * numberOfComponents] = i;
292+
newArray[i * numberOfComponents + 1] = i * 2;
293+
newArray[i * numberOfComponents + 2] = i * 3;
294+
newArray[i * numberOfComponents + 3] = i * 4;
295+
}
296+
297+
const da = vtkDataArray.newInstance({
298+
numberOfComponents,
299+
values: newArray,
300+
});
301+
302+
// set `computeRanges` to false. This will prevent the ranges from being
303+
// computed and will return only the ranges previously computer (if any).
304+
const ranges = da.getRanges(false);
305+
306+
t.ok(ranges === undefined, `getRanges should return undefined`);
307+
308+
// now fetch the range for component 0.
309+
da.getRange(0);
310+
311+
// now fetch the ranges again with `computeRanges` set to false.
312+
const updatedRanges = da.getRanges(false);
313+
314+
// `updatedRanges` should now be only the range for component 0. because if
315+
// was computed in `da.getRange(0)`
316+
t.ok(
317+
updatedRanges.length === numberOfComponents + 1,
318+
'getRanges should return an array of 5 vtkRange objects'
274319
);
320+
t.ok(updatedRanges[0].min === 0, 'component:0 minimum value should be 0');
321+
t.ok(updatedRanges[0].max === 9, 'component:0 maximum value should be 9');
322+
t.ok(updatedRanges[1] === null, 'component:1 should be null');
323+
t.ok(updatedRanges[2] === null, 'component:2 should be null');
324+
t.ok(updatedRanges[3] === null, 'component:3 should be null');
325+
t.ok(updatedRanges[4] === null, 'component:-1 should be null');
275326

276327
t.end();
277328
});

0 commit comments

Comments
 (0)