OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 // These tests are adapted from Khronos DataView tests | |
29 | |
30 var intArray1 = [0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252, 253, 2 54, 255]; | |
rossberg
2013/06/21 08:44:01
Nit: line length
Dmitry Lomov (no reviews)
2013/06/21 11:32:10
Done.
| |
31 var intArray2 = [31, 32, 33, 0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252, 253, 254, 255]; | |
32 var initialArray = [204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204]; | |
33 | |
34 var arayBuffer = null; | |
35 var view = null; | |
36 var viewStart = 0; | |
37 var viewLength = 0; | |
38 | |
39 function getElementSize(func) | |
40 { | |
rossberg
2013/06/21 08:44:01
Nit: style violation (here and below)
Dmitry Lomov (no reviews)
2013/06/21 11:32:10
Done.
| |
41 switch (func) { | |
rossberg
2013/06/21 08:44:01
Nit: indentation style
Dmitry Lomov (no reviews)
2013/06/21 11:32:10
Done.
| |
42 case "Int8": | |
43 case "Uint8": | |
44 return 1; | |
45 case "Int16": | |
46 case "Uint16": | |
47 return 2; | |
48 case "Int32": | |
49 case "Uint32": | |
50 case "Float32": | |
51 return 4; | |
52 case "Float64": | |
53 return 8; | |
54 default: | |
55 debug("Should not reached"); | |
rossberg
2013/06/21 08:44:01
I think we have assertUnreachable()
Dmitry Lomov (no reviews)
2013/06/21 11:32:10
Done.
| |
56 } | |
57 } | |
58 | |
59 function checkGet(func, index, expected, littleEndian) | |
60 { | |
61 function doGet() { | |
62 return view["get" + func](index, littleEndian); | |
63 } | |
64 if (index < 0) index = 0; | |
rossberg
2013/06/21 08:44:01
I don't understand. What use is runNegativeIndexTe
Dmitry Lomov (no reviews)
2013/06/21 11:32:10
Done.
rossberg
2013/06/21 12:25:53
This doesn't seem to be fixed.
Dmitry Lomov (no reviews)
2013/06/21 12:39:20
No it is fixed - see the latest version of this fi
| |
65 if (index + getElementSize(func) - 1 < view.byteLength) | |
66 assertSame(expected, doGet()); | |
67 else | |
68 assertThrows(doGet, RangeError); | |
69 } | |
70 | |
71 function checkSet(func, index, value, littleEndian) | |
72 { | |
73 function doSet() { | |
74 view["set" + func](index, value, littleEndian); | |
75 } | |
76 if (index < 0) index = 0; | |
77 if (index >= 0 && index + getElementSize(func) - 1 < view.byteLength) { | |
78 assertSame(undefined, doSet()); | |
79 checkGet(func, index, value, littleEndian); | |
80 } else | |
rossberg
2013/06/21 08:44:01
Nit: style guide wants braces consistently on bran
Dmitry Lomov (no reviews)
2013/06/21 11:32:10
Done.
| |
81 assertThrows(doSet, RangeError); | |
82 } | |
83 | |
84 function test(isTestingGet, func, index, value, littleEndian) | |
85 { | |
86 if (isTestingGet) | |
87 checkGet(func, index, value, littleEndian); | |
88 else | |
rossberg
2013/06/21 08:44:01
Nit: odd indentation
Dmitry Lomov (no reviews)
2013/06/21 11:32:10
Done.
| |
89 checkSet(func, index, value, littleEndian); | |
90 } | |
91 | |
92 function createDataView(array, frontPaddingNum, littleEndian, start, length) | |
93 { | |
94 if (!littleEndian) | |
95 array.reverse(); | |
96 var paddingArray = new Array(frontPaddingNum); | |
97 arrayBuffer = (new Uint8Array(paddingArray.concat(array))).buffer; | |
98 viewStart = (start != undefined) ? start : 0; | |
rossberg
2013/06/21 08:44:01
Shouldn't the DataView constructor do all the defa
Dmitry Lomov (no reviews)
2013/06/21 11:32:10
Done.
| |
99 viewLength = (length != undefined) ? length : arrayBuffer.byteLength - viewS tart; | |
rossberg
2013/06/21 08:44:01
Nit: line length
Dmitry Lomov (no reviews)
2013/06/21 11:32:10
Done by removing line
| |
100 view = new DataView(arrayBuffer, viewStart, viewLength); | |
101 if (!littleEndian) | |
102 array.reverse(); // restore the array. | |
103 } | |
104 | |
105 function runIntegerTestCases(isTestingGet, array, start, length) | |
106 { | |
107 createDataView(array, 0, true, start, length); | |
108 | |
109 test(isTestingGet, "Int8", 0, 0); | |
110 test(isTestingGet, "Int8", 8, -128); | |
111 test(isTestingGet, "Int8", 15, -1); | |
112 | |
113 test(isTestingGet, "Uint8", 0, 0); | |
114 test(isTestingGet, "Uint8", 8, 128); | |
115 test(isTestingGet, "Uint8", 15, 255); | |
116 | |
117 // Little endian. | |
118 test(isTestingGet, "Int16", 0, 256, true); | |
119 test(isTestingGet, "Int16", 5, 26213, true); | |
120 test(isTestingGet, "Int16", 9, -32127, true); | |
121 test(isTestingGet, "Int16", 14, -2, true); | |
122 | |
123 // Big endian. | |
124 test(isTestingGet, "Int16", 0, 1); | |
125 test(isTestingGet, "Int16", 5, 25958); | |
126 test(isTestingGet, "Int16", 9, -32382); | |
127 test(isTestingGet, "Int16", 14, -257); | |
128 | |
129 // Little endian. | |
130 test(isTestingGet, "Uint16", 0, 256, true); | |
131 test(isTestingGet, "Uint16", 5, 26213, true); | |
132 test(isTestingGet, "Uint16", 9, 33409, true); | |
133 test(isTestingGet, "Uint16", 14, 65534, true); | |
134 | |
135 // Big endian. | |
136 test(isTestingGet, "Uint16", 0, 1); | |
137 test(isTestingGet, "Uint16", 5, 25958); | |
138 test(isTestingGet, "Uint16", 9, 33154); | |
139 test(isTestingGet, "Uint16", 14, 65279); | |
140 | |
141 // Little endian. | |
142 test(isTestingGet, "Int32", 0, 50462976, true); | |
143 test(isTestingGet, "Int32", 3, 1717920771, true); | |
144 test(isTestingGet, "Int32", 6, -2122291354, true); | |
145 test(isTestingGet, "Int32", 9, -58490239, true); | |
146 test(isTestingGet, "Int32", 12,-66052, true); | |
147 | |
148 // Big endian. | |
149 test(isTestingGet, "Int32", 0, 66051); | |
150 test(isTestingGet, "Int32", 3, 56911206); | |
151 test(isTestingGet, "Int32", 6, 1718059137); | |
152 test(isTestingGet, "Int32", 9, -2122152964); | |
153 test(isTestingGet, "Int32", 12, -50462977); | |
154 | |
155 // Little endian. | |
156 test(isTestingGet, "Uint32", 0, 50462976, true); | |
157 test(isTestingGet, "Uint32", 3, 1717920771, true); | |
158 test(isTestingGet, "Uint32", 6, 2172675942, true); | |
159 test(isTestingGet, "Uint32", 9, 4236477057, true); | |
160 test(isTestingGet, "Uint32", 12,4294901244, true); | |
161 | |
162 // Big endian. | |
163 test(isTestingGet, "Uint32", 0, 66051); | |
164 test(isTestingGet, "Uint32", 3, 56911206); | |
165 test(isTestingGet, "Uint32", 6, 1718059137); | |
166 test(isTestingGet, "Uint32", 9, 2172814332); | |
167 test(isTestingGet, "Uint32", 12, 4244504319); | |
168 } | |
169 | |
170 function testFloat(isTestingGet, func, array, start, expected) | |
171 { | |
172 // Little endian. | |
173 createDataView(array, 0, true, start); | |
174 test(isTestingGet, func, 0, expected, true); | |
175 createDataView(array, 3, true, start); | |
176 test(isTestingGet, func, 3, expected, true); | |
177 createDataView(array, 7, true, start); | |
178 test(isTestingGet, func, 7, expected, true); | |
179 createDataView(array, 10, true, start); | |
180 test(isTestingGet, func, 10, expected, true); | |
181 | |
182 // Big endian. | |
183 createDataView(array, 0, false); | |
184 test(isTestingGet, func, 0, expected, false); | |
185 createDataView(array, 3, false); | |
186 test(isTestingGet, func, 3, expected, false); | |
187 createDataView(array, 7, false); | |
188 test(isTestingGet, func, 7, expected, false); | |
189 createDataView(array, 10, false); | |
190 test(isTestingGet, func, 10, expected, false); | |
191 } | |
192 | |
193 function runFloatTestCases(isTestingGet, start) | |
194 { | |
195 testFloat(isTestingGet, "Float32", | |
196 isTestingGet ? [0, 0, 32, 65] : initialArray, start, 10); | |
197 | |
198 testFloat(isTestingGet, "Float32", | |
199 isTestingGet ? [164, 112, 157, 63] : initialArray, | |
200 start, 1.2300000190734863); | |
201 testFloat(isTestingGet, "Float32", | |
202 isTestingGet ? [95, 53, 50, 199] : initialArray, | |
203 start, -45621.37109375); | |
204 testFloat(isTestingGet, "Float32", | |
205 isTestingGet ? [255, 255, 255, 127] : initialArray, | |
206 start, NaN); | |
207 testFloat(isTestingGet, "Float32", | |
208 isTestingGet ? [255, 255, 255, 255] : initialArray, | |
209 start, -NaN); | |
210 | |
211 testFloat(isTestingGet, "Float64", | |
212 isTestingGet ? [0, 0, 0, 0, 0, 0, 36, 64] : initialArray, | |
213 start, 10); | |
214 testFloat(isTestingGet, "Float64", | |
215 isTestingGet ? [174, 71, 225, 122, 20, 174, 243, 63] : initialArray, | |
216 start, 1.23); | |
217 testFloat(isTestingGet, "Float64", | |
218 isTestingGet ? [181, 55, 248, 30, 242, 179, 87, 193] : initialArray, | |
219 start, -6213576.4839); | |
220 testFloat(isTestingGet, "Float64", | |
221 isTestingGet ? [255, 255, 255, 255, 255, 255, 255, 127] : initialArray, | |
222 start, NaN); | |
223 testFloat(isTestingGet, "Float64", | |
224 isTestingGet ? [255, 255, 255, 255, 255, 255, 255, 255] : initialArray, | |
225 start, -NaN); | |
226 } | |
227 | |
228 function runNegativeIndexTests(isTestingGet) | |
229 { | |
230 createDataView(intArray1, 0, true, 0, 16); | |
231 | |
232 test(isTestingGet, "Int8", -1, 0); | |
233 test(isTestingGet, "Int8", -2, 0); | |
234 | |
235 test(isTestingGet, "Uint8", -1, 0); | |
236 test(isTestingGet, "Uint8", -2, 0); | |
237 | |
238 test(isTestingGet, "Int16", -1, 1); | |
239 test(isTestingGet, "Int16", -2, 1); | |
240 test(isTestingGet, "Int16", -3, 1); | |
241 | |
242 test(isTestingGet, "Uint16", -1, 1); | |
243 test(isTestingGet, "Uint16", -2, 1); | |
244 test(isTestingGet, "Uint16", -3, 1); | |
245 | |
246 test(isTestingGet, "Int32", -1, 66051); | |
247 test(isTestingGet, "Int32", -3, 66051); | |
248 test(isTestingGet, "Int32", -5, 66051); | |
249 | |
250 test(isTestingGet, "Uint32", -1, 66051); | |
251 test(isTestingGet, "Uint32", -3, 66051); | |
252 test(isTestingGet, "Uint32", -5, 66051); | |
253 | |
254 createDataView([0, 0, 0, 0, 0, 0, 0, 0], 0, true, 0, 8); | |
255 | |
256 test(isTestingGet, "Float32", -1, 0); | |
257 test(isTestingGet, "Float32", -3, 0); | |
258 test(isTestingGet, "Float32", -5, 0); | |
259 | |
260 test(isTestingGet, "Float64", -1, 0); | |
261 test(isTestingGet, "Float64", -5, 0); | |
262 test(isTestingGet, "Float64", -9, 0); | |
263 } | |
264 | |
265 | |
266 function TestGetters() | |
267 { | |
268 runIntegerTestCases(true, intArray1, 0, 16); | |
269 runFloatTestCases(true, 0); | |
270 | |
271 runIntegerTestCases(true, intArray2, 3, 2); | |
272 runFloatTestCases(true, 3); | |
273 | |
274 runNegativeIndexTests(true); | |
275 } | |
276 | |
277 function TestSetters() | |
278 { | |
279 runIntegerTestCases(false, initialArray, 0, 16); | |
280 runFloatTestCases(false); | |
281 | |
282 runIntegerTestCases(false, initialArray, 3, 2); | |
283 runFloatTestCases(false, 7); | |
284 | |
285 runNegativeIndexTests(false); | |
286 | |
287 } | |
288 | |
289 TestGetters(); | |
290 TestSetters(); | |
291 | |
292 function TestGeneralAccessors() { | |
293 var a = new DataView(new ArrayBuffer(256)); | |
294 function CheckAccessor(name) { | |
295 var f = a[name]; | |
296 f.call(a, 0, 0); // should not throw | |
297 assertThrows(function() { f.call({}, 0, 0); }, TypeError); | |
298 } | |
299 CheckAccessor("getUint8"); | |
300 CheckAccessor("setUint8"); | |
301 CheckAccessor("getInt8"); | |
302 CheckAccessor("setInt8"); | |
303 CheckAccessor("getUint16"); | |
304 CheckAccessor("setUint16"); | |
305 CheckAccessor("getInt16"); | |
306 CheckAccessor("setInt16"); | |
307 CheckAccessor("getUint32"); | |
308 CheckAccessor("setUint32"); | |
309 CheckAccessor("getInt32"); | |
310 CheckAccessor("setInt32"); | |
311 CheckAccessor("getFloat32"); | |
312 CheckAccessor("setFloat32"); | |
313 CheckAccessor("getFloat64"); | |
314 CheckAccessor("setFloat64"); | |
315 } | |
316 | |
317 TestGeneralAccessors(); | |
OLD | NEW |