3 <script src=
"../../../resources/js-test.js"></script>
6 <div id=
"description"></div>
7 <div id=
"console"></div>
10 description("Test DataView.");
12 var intArray1
= [0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252, 253, 254, 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, 204, 204, 204, 204];
16 var arayBuffer
= null;
21 function getElementSize(func
)
37 debug("Should not reached");
41 function checkGet(func
, index
, expected
, littleEndian
)
43 var expr
= "view.get" + func
+ "(" + index
;
44 if (littleEndian
!= undefined) {
46 expr
+= littleEndian
? "true" : "false";
49 if (index
>= 0 && index
+ getElementSize(func
) - 1 < view
.byteLength
)
50 shouldBe(expr
, expected
);
55 function checkSet(func
, index
, value
, littleEndian
)
57 var expr
= "view.set" + func
+ "(" + index
+ ", " + value
;
58 if (littleEndian
!= undefined) {
60 expr
+= littleEndian
? "true" : "false";
63 if (index
>= 0 && index
+ getElementSize(func
) - 1 < view
.byteLength
) {
64 shouldBeUndefined(expr
);
65 checkGet(func
, index
, value
, littleEndian
);
70 function test(isTestingGet
, func
, index
, value
, littleEndian
)
73 checkGet(func
, index
, value
, littleEndian
);
75 checkSet(func
, index
, value
, littleEndian
);
78 function createDataView(array
, frontPaddingNum
, littleEndian
, start
, length
)
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
- viewStart
;
86 view
= new DataView(arrayBuffer
, viewStart
, viewLength
);
88 array
.reverse(); // restore the array.
91 function runIntegerTestCases(isTestingGet
, array
, start
, length
)
93 createDataView(array
, 0, true, start
, length
);
95 test(isTestingGet
, "Int8", 0, "0");
96 test(isTestingGet
, "Int8", 8, "-128");
97 test(isTestingGet
, "Int8", 15, "-1");
99 test(isTestingGet
, "Uint8", 0, "0");
100 test(isTestingGet
, "Uint8", 8, "128");
101 test(isTestingGet
, "Uint8", 15, "255");
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);
110 test(isTestingGet
, "Int16", 0, "1");
111 test(isTestingGet
, "Int16", 5, "25958");
112 test(isTestingGet
, "Int16", 9, "-32382");
113 test(isTestingGet
, "Int16", 14, "-257");
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);
122 test(isTestingGet
, "Uint16", 0, "1");
123 test(isTestingGet
, "Uint16", 5, "25958");
124 test(isTestingGet
, "Uint16", 9, "33154");
125 test(isTestingGet
, "Uint16", 14, "65279");
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);
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");
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);
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");
156 function testFloat(isTestingGet
, func
, array
, start
, expected
)
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);
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);
179 function runFloatTestCases(isTestingGet
, start
)
181 testFloat(isTestingGet
, "Float32", isTestingGet
? [0, 0, 32, 65] : emptyArray
, start
, "10");
182 testFloat(isTestingGet
, "Float32", isTestingGet
? [164, 112, 157, 63] : emptyArray
, start
, "1.2300000190734863");
183 testFloat(isTestingGet
, "Float32", isTestingGet
? [95, 53, 50, 199] : emptyArray
, start
, "-45621.37109375");
184 testFloat(isTestingGet
, "Float32", isTestingGet
? [255, 255, 255, 127] : emptyArray
, start
, "NaN");
185 testFloat(isTestingGet
, "Float32", isTestingGet
? [255, 255, 255, 255] : emptyArray
, start
, "-NaN");
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, 174, 243, 63] : emptyArray
, start
, "1.23");
189 testFloat(isTestingGet
, "Float64", isTestingGet
? [181, 55, 248, 30, 242, 179, 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");
194 function runNegativeIndexTests(isTestingGet
)
196 createDataView(intArray1
, 0, true, 0, 16);
198 test(isTestingGet
, "Int8", -1, "0");
199 test(isTestingGet
, "Int8", -2, "0");
201 test(isTestingGet
, "Uint8", -1, "0");
202 test(isTestingGet
, "Uint8", -2, "0");
204 test(isTestingGet
, "Int16", -1, "0");
205 test(isTestingGet
, "Int16", -2, "0");
206 test(isTestingGet
, "Int16", -3, "0");
208 test(isTestingGet
, "Uint16", -1, "0");
209 test(isTestingGet
, "Uint16", -2, "0");
210 test(isTestingGet
, "Uint16", -3, "0");
212 test(isTestingGet
, "Int32", -1, "0");
213 test(isTestingGet
, "Int32", -3, "0");
214 test(isTestingGet
, "Int32", -5, "0");
216 test(isTestingGet
, "Uint32", -1, "0");
217 test(isTestingGet
, "Uint32", -3, "0");
218 test(isTestingGet
, "Uint32", -5, "0");
220 createDataView([0, 0, 0, 0, 0, 0, 36, 64], 0, true, 0, 8);
222 test(isTestingGet
, "Float32", -1, "0");
223 test(isTestingGet
, "Float32", -3, "0");
224 test(isTestingGet
, "Float32", -5, "0");
226 test(isTestingGet
, "Float64", -1, "0");
227 test(isTestingGet
, "Float64", -5, "0");
228 test(isTestingGet
, "Float64", -9, "0");
231 function runConstructorTests()
233 arayBuffer
= (new Uint8Array([1, 2])).buffer
;
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.
241 testFailed(expr
+ " does not throw exception");
243 testPassed(expr
+ " threw exception");
247 debug("Test for constructor taking 1 argument");
248 shouldBeDefined("view = new DataView(arayBuffer)");
249 shouldBe("view.byteOffset", "0");
250 shouldBe("view.byteLength", "2");
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");
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");
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)");
271 function runGetTests()
274 debug("Test for get methods that work");
275 runIntegerTestCases(true, intArray1
, 0, 16);
276 runFloatTestCases(true, 0);
279 debug("Test for get methods that might read beyond range");
280 runIntegerTestCases(true, intArray2
, 3, 2);
281 runFloatTestCases(true, 3);
284 debug("Test for get methods that read from negative index");
285 runNegativeIndexTests(true);
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()");
300 function runSetTests()
303 debug("Test for set methods that work");
304 runIntegerTestCases(false, emptyArray
, 0, 16);
305 runFloatTestCases(false);
308 debug("Test for set methods that might write beyond the range");
309 runIntegerTestCases(false, emptyArray
, 3, 2);
310 runFloatTestCases(false, 7);
313 debug("Test for set methods that write to negative index");
314 runNegativeIndexTests(false);
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)");
337 function runIndexingTests()
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");
347 runConstructorTests();