OLD | NEW |
| (Empty) |
1 <html> | |
2 <head> | |
3 <script src="../../../resources/js-test.js"></script> | |
4 </head> | |
5 <body> | |
6 <div id="description"></div> | |
7 <div id="console"></div> | |
8 | |
9 <script> | |
10 description("Test DataView."); | |
11 | |
12 var intArray1 = [0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252, 253, 2
54, 255]; | |
13 var intArray2 = [31, 32, 33, 0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131,
252, 253, 254, 255]; | |
14 var emptyArray = [204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 20
4, 204, 204, 204]; | |
15 | |
16 var arayBuffer = null; | |
17 var view = null; | |
18 var viewStart = 0; | |
19 var viewLength = 0; | |
20 | |
21 function getElementSize(func) | |
22 { | |
23 switch (func) { | |
24 case "Int8": | |
25 case "Uint8": | |
26 return 1; | |
27 case "Int16": | |
28 case "Uint16": | |
29 return 2; | |
30 case "Int32": | |
31 case "Uint32": | |
32 case "Float32": | |
33 return 4; | |
34 case "Float64": | |
35 return 8; | |
36 default: | |
37 debug("Should not reached"); | |
38 } | |
39 } | |
40 | |
41 function checkGet(func, index, expected, littleEndian) | |
42 { | |
43 var expr = "view.get" + func + "(" + index; | |
44 if (littleEndian != undefined) { | |
45 expr += ", "; | |
46 expr += littleEndian ? "true" : "false"; | |
47 } | |
48 expr += ")"; | |
49 if (index >= 0 && index + getElementSize(func) - 1 < view.byteLength) | |
50 shouldBe(expr, expected); | |
51 else | |
52 shouldThrow(expr); | |
53 } | |
54 | |
55 function checkSet(func, index, value, littleEndian) | |
56 { | |
57 var expr = "view.set" + func + "(" + index + ", " + value; | |
58 if (littleEndian != undefined) { | |
59 expr += ", "; | |
60 expr += littleEndian ? "true" : "false"; | |
61 } | |
62 expr += ")"; | |
63 if (index >= 0 && index + getElementSize(func) - 1 < view.byteLength) { | |
64 shouldBeUndefined(expr); | |
65 checkGet(func, index, value, littleEndian); | |
66 } else | |
67 shouldThrow(expr); | |
68 } | |
69 | |
70 function test(isTestingGet, func, index, value, littleEndian) | |
71 { | |
72 if (isTestingGet) | |
73 checkGet(func, index, value, littleEndian); | |
74 else | |
75 checkSet(func, index, value, littleEndian); | |
76 } | |
77 | |
78 function createDataView(array, frontPaddingNum, littleEndian, start, length) | |
79 { | |
80 if (!littleEndian) | |
81 array.reverse(); | |
82 var paddingArray = new Array(frontPaddingNum); | |
83 arrayBuffer = (new Uint8Array(paddingArray.concat(array))).buffer; | |
84 viewStart = (start != undefined) ? start : 0; | |
85 viewLength = (length != undefined) ? length : arrayBuffer.byteLength - viewS
tart; | |
86 view = new DataView(arrayBuffer, viewStart, viewLength); | |
87 if (!littleEndian) | |
88 array.reverse(); // restore the array. | |
89 } | |
90 | |
91 function runIntegerTestCases(isTestingGet, array, start, length) | |
92 { | |
93 createDataView(array, 0, true, start, length); | |
94 | |
95 test(isTestingGet, "Int8", 0, "0"); | |
96 test(isTestingGet, "Int8", 8, "-128"); | |
97 test(isTestingGet, "Int8", 15, "-1"); | |
98 | |
99 test(isTestingGet, "Uint8", 0, "0"); | |
100 test(isTestingGet, "Uint8", 8, "128"); | |
101 test(isTestingGet, "Uint8", 15, "255"); | |
102 | |
103 // Little endian. | |
104 test(isTestingGet, "Int16", 0, "256", true); | |
105 test(isTestingGet, "Int16", 5, "26213", true); | |
106 test(isTestingGet, "Int16", 9, "-32127", true); | |
107 test(isTestingGet, "Int16", 14, "-2", true); | |
108 | |
109 // Big endian. | |
110 test(isTestingGet, "Int16", 0, "1"); | |
111 test(isTestingGet, "Int16", 5, "25958"); | |
112 test(isTestingGet, "Int16", 9, "-32382"); | |
113 test(isTestingGet, "Int16", 14, "-257"); | |
114 | |
115 // Little endian. | |
116 test(isTestingGet, "Uint16", 0, "256", true); | |
117 test(isTestingGet, "Uint16", 5, "26213", true); | |
118 test(isTestingGet, "Uint16", 9, "33409", true); | |
119 test(isTestingGet, "Uint16", 14, "65534", true); | |
120 | |
121 // Big endian. | |
122 test(isTestingGet, "Uint16", 0, "1"); | |
123 test(isTestingGet, "Uint16", 5, "25958"); | |
124 test(isTestingGet, "Uint16", 9, "33154"); | |
125 test(isTestingGet, "Uint16", 14, "65279"); | |
126 | |
127 // Little endian. | |
128 test(isTestingGet, "Int32", 0, "50462976", true); | |
129 test(isTestingGet, "Int32", 3, "1717920771", true); | |
130 test(isTestingGet, "Int32", 6, "-2122291354", true); | |
131 test(isTestingGet, "Int32", 9, "-58490239", true); | |
132 test(isTestingGet, "Int32", 12, "-66052", true); | |
133 | |
134 // Big endian. | |
135 test(isTestingGet, "Int32", 0, "66051"); | |
136 test(isTestingGet, "Int32", 3, "56911206"); | |
137 test(isTestingGet, "Int32", 6, "1718059137"); | |
138 test(isTestingGet, "Int32", 9, "-2122152964"); | |
139 test(isTestingGet, "Int32", 12, "-50462977"); | |
140 | |
141 // Little endian. | |
142 test(isTestingGet, "Uint32", 0, "50462976", true); | |
143 test(isTestingGet, "Uint32", 3, "1717920771", true); | |
144 test(isTestingGet, "Uint32", 6, "2172675942", true); | |
145 test(isTestingGet, "Uint32", 9, "4236477057", true); | |
146 test(isTestingGet, "Uint32", 12, "4294901244", true); | |
147 | |
148 // Big endian. | |
149 test(isTestingGet, "Uint32", 0, "66051"); | |
150 test(isTestingGet, "Uint32", 3, "56911206"); | |
151 test(isTestingGet, "Uint32", 6, "1718059137"); | |
152 test(isTestingGet, "Uint32", 9, "2172814332"); | |
153 test(isTestingGet, "Uint32", 12, "4244504319"); | |
154 } | |
155 | |
156 function testFloat(isTestingGet, func, array, start, expected) | |
157 { | |
158 // Little endian. | |
159 createDataView(array, 0, true, start); | |
160 test(isTestingGet, func, 0, expected, true); | |
161 createDataView(array, 3, true, start); | |
162 test(isTestingGet, func, 3, expected, true); | |
163 createDataView(array, 7, true, start); | |
164 test(isTestingGet, func, 7, expected, true); | |
165 createDataView(array, 10, true, start); | |
166 test(isTestingGet, func, 10, expected, true); | |
167 | |
168 // Big endian. | |
169 createDataView(array, 0, false); | |
170 test(isTestingGet, func, 0, expected, false); | |
171 createDataView(array, 3, false); | |
172 test(isTestingGet, func, 3, expected, false); | |
173 createDataView(array, 7, false); | |
174 test(isTestingGet, func, 7, expected, false); | |
175 createDataView(array, 10, false); | |
176 test(isTestingGet, func, 10, expected, false); | |
177 } | |
178 | |
179 function runFloatTestCases(isTestingGet, start) | |
180 { | |
181 testFloat(isTestingGet, "Float32", isTestingGet ? [0, 0, 32, 65] : emptyArra
y, start, "10"); | |
182 testFloat(isTestingGet, "Float32", isTestingGet ? [164, 112, 157, 63] : empt
yArray, start, "1.2300000190734863"); | |
183 testFloat(isTestingGet, "Float32", isTestingGet ? [95, 53, 50, 199] : emptyA
rray, start, "-45621.37109375"); | |
184 testFloat(isTestingGet, "Float32", isTestingGet ? [255, 255, 255, 127] : emp
tyArray, start, "NaN"); | |
185 testFloat(isTestingGet, "Float32", isTestingGet ? [255, 255, 255, 255] : emp
tyArray, start, "-NaN"); | |
186 | |
187 testFloat(isTestingGet, "Float64", isTestingGet ? [0, 0, 0, 0, 0, 0, 36, 64]
: emptyArray, start, "10"); | |
188 testFloat(isTestingGet, "Float64", isTestingGet ? [174, 71, 225, 122, 20, 17
4, 243, 63] : emptyArray, start, "1.23"); | |
189 testFloat(isTestingGet, "Float64", isTestingGet ? [181, 55, 248, 30, 242, 17
9, 87, 193] : emptyArray, start, "-6213576.4839"); | |
190 testFloat(isTestingGet, "Float64", isTestingGet ? [255, 255, 255, 255, 255,
255, 255, 127] : emptyArray, start, "NaN"); | |
191 testFloat(isTestingGet, "Float64", isTestingGet ? [255, 255, 255, 255, 255,
255, 255, 255] : emptyArray, start, "-NaN"); | |
192 } | |
193 | |
194 function runNegativeIndexTests(isTestingGet) | |
195 { | |
196 createDataView(intArray1, 0, true, 0, 16); | |
197 | |
198 test(isTestingGet, "Int8", -1, "0"); | |
199 test(isTestingGet, "Int8", -2, "0"); | |
200 | |
201 test(isTestingGet, "Uint8", -1, "0"); | |
202 test(isTestingGet, "Uint8", -2, "0"); | |
203 | |
204 test(isTestingGet, "Int16", -1, "0"); | |
205 test(isTestingGet, "Int16", -2, "0"); | |
206 test(isTestingGet, "Int16", -3, "0"); | |
207 | |
208 test(isTestingGet, "Uint16", -1, "0"); | |
209 test(isTestingGet, "Uint16", -2, "0"); | |
210 test(isTestingGet, "Uint16", -3, "0"); | |
211 | |
212 test(isTestingGet, "Int32", -1, "0"); | |
213 test(isTestingGet, "Int32", -3, "0"); | |
214 test(isTestingGet, "Int32", -5, "0"); | |
215 | |
216 test(isTestingGet, "Uint32", -1, "0"); | |
217 test(isTestingGet, "Uint32", -3, "0"); | |
218 test(isTestingGet, "Uint32", -5, "0"); | |
219 | |
220 createDataView([0, 0, 0, 0, 0, 0, 36, 64], 0, true, 0, 8); | |
221 | |
222 test(isTestingGet, "Float32", -1, "0"); | |
223 test(isTestingGet, "Float32", -3, "0"); | |
224 test(isTestingGet, "Float32", -5, "0"); | |
225 | |
226 test(isTestingGet, "Float64", -1, "0"); | |
227 test(isTestingGet, "Float64", -5, "0"); | |
228 test(isTestingGet, "Float64", -9, "0"); | |
229 } | |
230 | |
231 function runConstructorTests() | |
232 { | |
233 arayBuffer = (new Uint8Array([1, 2])).buffer; | |
234 | |
235 debug(""); | |
236 debug("Test for constructor not called as a function"); | |
237 var expr = "DataView(new ArrayBuffer)"; | |
238 // Use try/catch instead of calling shouldThrow to avoid different exception
message being reported from different platform. | |
239 try { | |
240 eval(expr); | |
241 testFailed(expr + " does not throw exception"); | |
242 } catch (e) { | |
243 testPassed(expr + " threw exception"); | |
244 } | |
245 | |
246 debug(""); | |
247 debug("Test for constructor taking 1 argument"); | |
248 shouldBeDefined("view = new DataView(arayBuffer)"); | |
249 shouldBe("view.byteOffset", "0"); | |
250 shouldBe("view.byteLength", "2"); | |
251 | |
252 debug(""); | |
253 debug("Test for constructor taking 2 arguments"); | |
254 shouldBeDefined("view = new DataView(arayBuffer, 1)"); | |
255 shouldBe("view.byteOffset", "1"); | |
256 shouldBe("view.byteLength", "1"); | |
257 | |
258 debug(""); | |
259 debug("Test for constructor taking 3 arguments"); | |
260 shouldBeDefined("view = new DataView(arayBuffer, 0, 1)"); | |
261 shouldBe("view.byteOffset", "0"); | |
262 shouldBe("view.byteLength", "1"); | |
263 | |
264 debug(""); | |
265 debug("Test for constructor throwing exception"); | |
266 shouldThrow("view = new DataView(arayBuffer, 0, 3)"); | |
267 shouldThrow("view = new DataView(arayBuffer, 1, 2)"); | |
268 shouldThrow("view = new DataView(arayBuffer, 2, 1)"); | |
269 } | |
270 | |
271 function runGetTests() | |
272 { | |
273 debug(""); | |
274 debug("Test for get methods that work"); | |
275 runIntegerTestCases(true, intArray1, 0, 16); | |
276 runFloatTestCases(true, 0); | |
277 | |
278 debug(""); | |
279 debug("Test for get methods that might read beyond range"); | |
280 runIntegerTestCases(true, intArray2, 3, 2); | |
281 runFloatTestCases(true, 3); | |
282 | |
283 debug(""); | |
284 debug("Test for get methods that read from negative index"); | |
285 runNegativeIndexTests(true); | |
286 | |
287 debug(""); | |
288 debug("Test for wrong arguments passed to get methods"); | |
289 view = new DataView((new Uint8Array([1, 2])).buffer); | |
290 shouldThrow("view.getInt8()"); | |
291 shouldThrow("view.getUint8()"); | |
292 shouldThrow("view.getInt16()"); | |
293 shouldThrow("view.getUint16()"); | |
294 shouldThrow("view.getInt32()"); | |
295 shouldThrow("view.getUint32()"); | |
296 shouldThrow("view.getFloat32()"); | |
297 shouldThrow("view.getFloat64()"); | |
298 } | |
299 | |
300 function runSetTests() | |
301 { | |
302 debug(""); | |
303 debug("Test for set methods that work"); | |
304 runIntegerTestCases(false, emptyArray, 0, 16); | |
305 runFloatTestCases(false); | |
306 | |
307 debug(""); | |
308 debug("Test for set methods that might write beyond the range"); | |
309 runIntegerTestCases(false, emptyArray, 3, 2); | |
310 runFloatTestCases(false, 7); | |
311 | |
312 debug(""); | |
313 debug("Test for set methods that write to negative index"); | |
314 runNegativeIndexTests(false); | |
315 | |
316 debug(""); | |
317 debug("Test for wrong arguments passed to set methods"); | |
318 view = new DataView((new Uint8Array([1, 2])).buffer); | |
319 shouldThrow("view.setInt8()"); | |
320 shouldThrow("view.setUint8()"); | |
321 shouldThrow("view.setInt16()"); | |
322 shouldThrow("view.setUint16()"); | |
323 shouldThrow("view.setInt32()"); | |
324 shouldThrow("view.setUint32()"); | |
325 shouldThrow("view.setFloat32()"); | |
326 shouldThrow("view.setFloat64()"); | |
327 shouldThrow("view.setInt8(1)"); | |
328 shouldThrow("view.setUint8(1)"); | |
329 shouldThrow("view.setInt16(1)"); | |
330 shouldThrow("view.setUint16(1)"); | |
331 shouldThrow("view.setInt32(1)"); | |
332 shouldThrow("view.setUint32(1)"); | |
333 shouldThrow("view.setFloat32(1)"); | |
334 shouldThrow("view.setFloat64(1)"); | |
335 } | |
336 | |
337 function runIndexingTests() | |
338 { | |
339 debug(""); | |
340 debug("Test for indexing that should not work"); | |
341 view = new DataView((new Uint8Array([1, 2])).buffer); | |
342 shouldBeUndefined("view[0]"); | |
343 shouldBeDefined("view[0] = 3"); | |
344 shouldBe("view.getUint8(0)", "1"); | |
345 } | |
346 | |
347 runConstructorTests(); | |
348 runGetTests(); | |
349 runSetTests(); | |
350 runIndexingTests(); | |
351 </script> | |
352 | |
353 </body> | |
354 </html> | |
OLD | NEW |