-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathreadSequenceItemsImplicit_test.js
186 lines (146 loc) · 6.75 KB
/
readSequenceItemsImplicit_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { expect } from 'chai';
import ByteStream from '../src/byteStream';
import littleEndianByteArrayParser from '../src/littleEndianByteArrayParser';
import readSequenceItemsImplicit from '../src/readSequenceElementImplicit';
describe('readSequenceItemsImplicit', () => {
function convertToByteArray(bytes) {
const byteArray = new Uint8Array(bytes.length);
let i = 0;
bytes.forEach((byte) => { byteArray[i++] = byte; });
return byteArray;
}
it('element that looks like SQ is properly parsed in an undefined-length item in an undefined-length sequence with provided callback', () => {
// Arrange
// (fffe,e000) (undefined length)
const bytes = [0xfe, 0xff, 0x00, 0xe0, 0xFF, 0xFF, 0xFF, 0xFF,
// (7fe0,0010) 8
0xe0, 0x7f, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
// Looks like an item tag, but isn't since it's within pixel data
0xfe, 0xff, 0x00, 0xe0, 0x0A, 0x00, 0x00, 0x00,
// (fffe,e00d) 0
0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00,
// (fffe,e0dd) 0
0xfe, 0xff, 0xdd, 0xe0, 0x00, 0x00, 0x00, 0x00,
];
const callback = (tag) => {
return (tag === 'x7fe00010') ? 'OW' : undefined;
};
const byteStream = new ByteStream(littleEndianByteArrayParser, convertToByteArray(bytes));
const element = { length: 0xFFFFFFFF };
// Act
readSequenceItemsImplicit(byteStream, element, callback);
// Assert
expect(element.items.length).to.equal(1);
const sequenceItem = element.items[0];
expect(sequenceItem).to.be.ok;
expect(sequenceItem.length).to.equal(24);
const pixelData = sequenceItem.dataSet.elements['x7fe00010'];
expect(pixelData).to.be.ok;
expect(pixelData.length).to.equal(8);
expect(pixelData.vr).to.equal('OW');
expect(sequenceItem.dataSet.elements['xfffee00d']).to.be.ok;
expect(byteStream.warnings.length).to.equal(0);
});
it('should generate warnings for missing item and sequence delimiters', () => {
// Arrange
// (fffe,e000) (undefined length)
const bytes = [0xfe, 0xff, 0x00, 0xe0, 0xFF, 0xFF, 0xFF, 0xFF,
// (7fe0,0010) 4
0xe0, 0x7f, 0x10, 0x00, 0x04, 0x00, 0x00, 0x00,
0x01, 0x23, 0x45, 0x67,
];
const byteStream = new ByteStream(littleEndianByteArrayParser, convertToByteArray(bytes));
const element = { length: 0xFFFFFFFF };
// Act
readSequenceItemsImplicit(byteStream, element);
// Assert
expect(element.items.length).to.equal(1);
const sequenceItem = element.items[0];
expect(sequenceItem).to.be.ok;
expect(sequenceItem.length).to.equal(12);
const pixelData = sequenceItem.dataSet.elements['x7fe00010'];
expect(pixelData).to.be.ok;
expect(pixelData.length).to.equal(4);
expect(byteStream.warnings.length).to.equal(2);
expect(byteStream.warnings.some((warning) => warning.indexOf('sequence item delimiter') > -1)).to.be.ok;
expect(byteStream.warnings.some((warning) => warning.indexOf('sequence delimiter') > -1)).to.be.ok;
});
it('element that looks like SQ is properly parsed in an item with defined length in an undefined-length sequence with provided callback', () => {
// Arrange
// (fffe,e000) 16
const bytes = [0xfe, 0xff, 0x00, 0xe0, 0x10, 0x00, 0x00, 0x00,
// (7fe0,0010) 8
0xe0, 0x7f, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
// Looks like an item tag, but isn't since it's within pixel data
0xfe, 0xff, 0x00, 0xe0, 0x0A, 0x00, 0x00, 0x00,
// (fffe,e0dd) 0
0xfe, 0xff, 0xdd, 0xe0, 0x00, 0x00, 0x00, 0x00,
];
const callback = (tag) => {
return (tag === 'x7fe00010') ? 'OW' : undefined;
};
const byteStream = new ByteStream(littleEndianByteArrayParser, convertToByteArray(bytes));
const element = { length: 0xFFFFFFFF };
// Act
readSequenceItemsImplicit(byteStream, element, callback);
// Assert
expect(element.items.length).to.equal(1);
const sequenceItem = element.items[0];
expect(sequenceItem).to.be.ok;
expect(sequenceItem.length).to.equal(16);
const pixelData = sequenceItem.dataSet.elements['x7fe00010'];
expect(pixelData).to.be.ok;
expect(pixelData.length).to.equal(8);
expect(pixelData.vr).to.equal('OW');
expect(byteStream.warnings.length).to.equal(0);
});
it('element that looks like SQ is properly parsed in an item with defined length in a defined-length sequence with provided callback', () => {
// Arrange
// (fffe,e000) 16
const bytes = [0xfe, 0xff, 0x00, 0xe0, 0x10, 0x00, 0x00, 0x00,
// (7fe0,0010) 8
0xe0, 0x7f, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
// Looks like an item tag, but isn't since it's within pixel data
0xfe, 0xff, 0x00, 0xe0, 0x0A, 0x00, 0x00, 0x00,
];
const callback = (tag) => {
return (tag === 'x7fe00010') ? 'OW' : undefined;
};
const byteStream = new ByteStream(littleEndianByteArrayParser, convertToByteArray(bytes));
const element = {dataOffset: 0, length: 24};
// Act
readSequenceItemsImplicit(byteStream, element, callback);
// Assert
expect(element.items.length).to.equal(1);
const sequenceItem = element.items[0];
expect(sequenceItem).to.be.ok;
expect(sequenceItem.length).to.equal(16);
const pixelData = sequenceItem.dataSet.elements['x7fe00010'];
expect(pixelData).to.be.ok;
expect(pixelData.length).to.equal(8);
expect(pixelData.vr).to.equal('OW');
expect(byteStream.warnings.length).to.equal(0);
});
it('should not crash because a malformed tag at end of stream', () => {
// Arrange
const bytes = new Uint8Array(1);
const element = {length: 0xFFFFFFFF};
const byteStream = new ByteStream(littleEndianByteArrayParser, bytes);
// Act
readSequenceItemsImplicit(byteStream, element);
// Assert
expect(byteStream.warnings.length).to.equal(1);
});
it('should throw an exception for undefined byteStream', () => {
const invoker = () => readSequenceItemsImplicit(undefined, { length: 0xFFFFFFFF });
// Assert
expect(invoker).to.throw();
});
it('should throw an exception for undefined element', () => {
// Arrange
const byteStream = new ByteStream(littleEndianByteArrayParser, new Uint8Array(1));
const invoker = () => readSequenceItemsImplicit(byteStream);
// Assert
expect(invoker).to.throw();
});
});