-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
130 lines (106 loc) · 4.06 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var test = require('tape');
var nrrd = require('./nrrd.js');
var ndarray = require('ndarray');
var fs = require('fs');
test("binaryInlineFile", function (t) {
var file = nrrd.parse(fs.readFileSync('example1.nrrd'));
t.equal(file.type, 'uint8');
t.equal(file.dimension, 3);
t.equal(file.sizes.length, 3);
t.equal(file.sizes[0], 3);
t.equal(file.sizes[1], 512);
t.equal(file.sizes[2], 512);
t.equal(file.data.length, 3*512*512);
t.end();
});
test("textInlineFile", function (t) {
var file = nrrd.parse(fs.readFileSync('example2.nrrd')),
i, list = [1,2,3, 65000,64000,63000, 10000,11000,12000, 4,5,6];
t.equal(file.type, 'uint16');
t.equal(file.dimension, 2);
t.equal(file.sizes.length, 2);
t.equal(file.sizes[0], 3);
t.equal(file.sizes[1], 4);
t.equal(file.data.length, 3*4);
t.equal(file.data.byteLength, 3*4*2);
for(i=0; i<list.length; i++) {
t.equal(file.data[i], list[i]);
}
t.end();
});
test("roundtrip", function (t) {
var file = nrrd.parse(nrrd.serialize(nrrd.parse(fs.readFileSync('example2.nrrd')))),
i, list = [1,2,3, 65000,64000,63000, 10000,11000,12000, 4,5,6];
t.equal(file.type, 'uint16');
t.equal(file.dimension, 2);
t.equal(file.sizes.length, 2);
t.equal(file.sizes[0], 3);
t.equal(file.sizes[1], 4);
t.equal(file.data.length, 3*4);
t.equal(file.data.byteLength, 3*4*2);
for(i=0; i<list.length; i++) {
t.equal(file.data[i], list[i]);
}
var file = nrrd.parse(nrrd.serialize(nrrd.parse(fs.readFileSync('example4.nrrd')))),
i, list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
t.equal(file.type, 'uint8');
t.equal(file.dimension, 4);
t.equal(file.sizes.length, 4);
t.equal(file.sizes[0], 4);
t.equal(file.sizes[1], 3);
t.equal(file.sizes[2], 2);
t.equal(file.sizes[3], 2);
t.equal(file.space, "right-anterior-superior");
t.ok(arrayEqual(file.spaceOrigin, [1.0,2.0,3.0]), "spaceOrigin should be equal to [1.0,2.0,3.0]");
t.ok(arrayEqual(file.spaceDirections, [[1.0,0.0,0.0],[0,0.5,0],[0,0,0.3],null]), "spaceDirections should be equal to [[1.0,0,0],...]");
t.equal(file.data.length, 4*3*2*2);
t.equal(file.data.byteLength, 4*3*2*2);
for(i=0; i<list.length; i++) {
t.equal(file.data[i], list[i]);
t.equal(file.data[i+list.length], list[i]);
}
t.end();
});
test("ndarray", function (t) {
var file = nrrd.parse(fs.readFileSync('example2.nrrd')),
i, j, list = [[1,2,3], [65000,64000,63000], [10000,11000,12000], [4,5,6]],
arr = ndarray(file.data, file.sizes.slice().reverse());
t.equal(file.dimension, 2);
t.equal(file.sizes[0], arr.shape[1]);
t.equal(file.sizes[1], arr.shape[0]);
for(i=0; i<list.length; i++) {
for(j=0; j<list[i].length; j++) {
t.equal(arr.get(i,j), list[i][j]);
}
}
t.end();
});
test("ndarray serialization", function (t) {
var i, j, list = [[1,2,3], [65000,64000,63000], [10000,11000,12000], [4,5,6]],
arr1 = ndarray(new Uint16Array([1,2,3, 65000,64000,63000, 10000,11000,12000, 4,5,6]), [4,3]),
serialized = nrrd.serialize({data: arr1.data, sizes: arr1.shape.slice().reverse()}),
file = nrrd.parse(serialized),
arr2 = ndarray(file.data, file.sizes.slice().reverse());
t.equal(file.dimension, arr1.shape.length);
t.equal(file.sizes[0], arr1.shape[1]);
t.equal(file.sizes[1], arr1.shape[0]);
t.equal(file.type, "uint16");
for(i=0; i<list.length; i++) {
for(j=0; j<list[i].length; j++) {
t.equal(arr1.get(i,j), arr2.get(i,j));
}
}
//console.log(String.fromCharCode.apply(null, new Uint8Array(serialized)));
t.end();
});
function arrayEqual(a, b) {
if (a.length === undefined || a.length !== b.length) return false;
for(var i=0; i<a.length; i++) {
if (a[i] instanceof Array) {
if (!arrayEqual(a[i],b[i])) return false;
} else {
if (a[i]!==b[i]) return false;
}
}
return true;
}