Chromium Code Reviews| 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 = | |
| 31 [0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252, 253, 254, 255]; | |
| 32 var intArray2 = | |
| 33 [31, 32, 33, 0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252, | |
| 34 253, 254, 255]; | |
| 35 var initialArray = | |
| 36 [204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, | |
| 37 204, 204, 204]; | |
| 38 | |
| 39 var arayBuffer = null; | |
| 40 var view = null; | |
| 41 var viewStart = 0; | |
| 42 var viewLength = 0; | |
| 43 | |
| 44 function getElementSize(func) { | |
| 45 switch (func) { | |
| 46 case "Int8": | |
| 47 case "Uint8": | |
| 48 return 1; | |
| 49 case "Int16": | |
| 50 case "Uint16": | |
| 51 return 2; | |
| 52 case "Int32": | |
| 53 case "Uint32": | |
| 54 case "Float32": | |
| 55 return 4; | |
| 56 case "Float64": | |
| 57 return 8; | |
| 58 default: | |
| 59 assertUnreachable(func); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 function checkGet(func, index, expected, littleEndian) { | |
| 64 function doGet() { | |
| 65 return view["get" + func](index, littleEndian); | |
| 66 } | |
| 67 if (index < 0) index = 0; | |
| 68 if (index + getElementSize(func) - 1 < view.byteLength) | |
| 69 assertSame(expected, doGet()); | |
| 70 else | |
| 71 assertThrows(doGet, RangeError); | |
| 72 } | |
| 73 | |
| 74 function checkSet(func, index, value, littleEndian) { | |
| 75 function doSet() { | |
| 76 view["set" + func](index, value, littleEndian); | |
| 77 } | |
| 78 actualIndex = index < 0 ? 0 : index; | |
| 79 if (actualIndex >= 0 && | |
| 80 actualIndex + getElementSize(func) - 1 < view.byteLength) { | |
| 81 assertSame(undefined, doSet()); | |
| 82 checkGet(func, index, value, littleEndian); | |
| 83 } else { | |
| 84 assertThrows(doSet, RangeError); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 function test(isTestingGet, func, index, value, littleEndian) { | |
| 89 if (isTestingGet) | |
| 90 checkGet(func, index, value, littleEndian); | |
| 91 else | |
| 92 checkSet(func, index, value, littleEndian); | |
| 93 } | |
| 94 | |
| 95 function createDataView(array, frontPaddingNum, littleEndian, start, length) | |
| 96 { | |
|
rossberg
2013/06/21 12:25:53
Nit: style
Dmitry Lomov (no reviews)
2013/06/21 12:39:20
Done.
| |
| 97 if (!littleEndian) | |
| 98 array.reverse(); | |
| 99 var paddingArray = new Array(frontPaddingNum); | |
| 100 arrayBuffer = (new Uint8Array(paddingArray.concat(array))).buffer; | |
| 101 view = new DataView(arrayBuffer, viewStart, viewLength); | |
| 102 if (!littleEndian) | |
| 103 array.reverse(); // restore the array. | |
| 104 } | |
| 105 | |
| 106 function runIntegerTestCases(isTestingGet, array, start, length) { | |
| 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 // Little endian. | |
| 172 createDataView(array, 0, true, start); | |
| 173 test(isTestingGet, func, 0, expected, true); | |
| 174 createDataView(array, 3, true, start); | |
| 175 test(isTestingGet, func, 3, expected, true); | |
| 176 createDataView(array, 7, true, start); | |
| 177 test(isTestingGet, func, 7, expected, true); | |
| 178 createDataView(array, 10, true, start); | |
| 179 test(isTestingGet, func, 10, expected, true); | |
| 180 | |
| 181 // Big endian. | |
| 182 createDataView(array, 0, false); | |
| 183 test(isTestingGet, func, 0, expected, false); | |
| 184 createDataView(array, 3, false); | |
| 185 test(isTestingGet, func, 3, expected, false); | |
| 186 createDataView(array, 7, false); | |
| 187 test(isTestingGet, func, 7, expected, false); | |
| 188 createDataView(array, 10, false); | |
| 189 test(isTestingGet, func, 10, expected, false); | |
| 190 } | |
| 191 | |
| 192 function runFloatTestCases(isTestingGet, start) { | |
| 193 testFloat(isTestingGet, "Float32", | |
| 194 isTestingGet ? [0, 0, 32, 65] : initialArray, start, 10); | |
| 195 | |
| 196 testFloat(isTestingGet, "Float32", | |
| 197 isTestingGet ? [164, 112, 157, 63] : initialArray, | |
| 198 start, 1.2300000190734863); | |
| 199 testFloat(isTestingGet, "Float32", | |
| 200 isTestingGet ? [95, 53, 50, 199] : initialArray, | |
| 201 start, -45621.37109375); | |
| 202 testFloat(isTestingGet, "Float32", | |
| 203 isTestingGet ? [255, 255, 255, 127] : initialArray, | |
| 204 start, NaN); | |
| 205 testFloat(isTestingGet, "Float32", | |
| 206 isTestingGet ? [255, 255, 255, 255] : initialArray, | |
| 207 start, -NaN); | |
| 208 | |
| 209 testFloat(isTestingGet, "Float64", | |
| 210 isTestingGet ? [0, 0, 0, 0, 0, 0, 36, 64] : initialArray, | |
| 211 start, 10); | |
| 212 testFloat(isTestingGet, "Float64", | |
| 213 isTestingGet ? [174, 71, 225, 122, 20, 174, 243, 63] : initialArray, | |
| 214 start, 1.23); | |
| 215 testFloat(isTestingGet, "Float64", | |
| 216 isTestingGet ? [181, 55, 248, 30, 242, 179, 87, 193] : initialArray, | |
| 217 start, -6213576.4839); | |
| 218 testFloat(isTestingGet, "Float64", | |
| 219 isTestingGet ? [255, 255, 255, 255, 255, 255, 255, 127] : initialArray, | |
| 220 start, NaN); | |
| 221 testFloat(isTestingGet, "Float64", | |
| 222 isTestingGet ? [255, 255, 255, 255, 255, 255, 255, 255] : initialArray, | |
| 223 start, -NaN); | |
| 224 } | |
| 225 | |
| 226 function runNegativeIndexTests(isTestingGet) { | |
| 227 createDataView(intArray1, 0, true, 0, 16); | |
| 228 | |
| 229 test(isTestingGet, "Int8", -1, 0); | |
| 230 test(isTestingGet, "Int8", -2, 0); | |
| 231 | |
| 232 test(isTestingGet, "Uint8", -1, 0); | |
| 233 test(isTestingGet, "Uint8", -2, 0); | |
| 234 | |
| 235 test(isTestingGet, "Int16", -1, 1); | |
| 236 test(isTestingGet, "Int16", -2, 1); | |
| 237 test(isTestingGet, "Int16", -3, 1); | |
| 238 | |
| 239 test(isTestingGet, "Uint16", -1, 1); | |
| 240 test(isTestingGet, "Uint16", -2, 1); | |
| 241 test(isTestingGet, "Uint16", -3, 1); | |
| 242 | |
| 243 test(isTestingGet, "Int32", -1, 66051); | |
| 244 test(isTestingGet, "Int32", -3, 66051); | |
| 245 test(isTestingGet, "Int32", -5, 66051); | |
| 246 | |
| 247 test(isTestingGet, "Uint32", -1, 66051); | |
| 248 test(isTestingGet, "Uint32", -3, 66051); | |
| 249 test(isTestingGet, "Uint32", -5, 66051); | |
| 250 | |
| 251 createDataView([0, 0, 0, 0, 0, 0, 0, 0], 0, true, 0, 8); | |
| 252 | |
| 253 test(isTestingGet, "Float32", -1, 0); | |
| 254 test(isTestingGet, "Float32", -3, 0); | |
| 255 test(isTestingGet, "Float32", -5, 0); | |
| 256 | |
| 257 test(isTestingGet, "Float64", -1, 0); | |
| 258 test(isTestingGet, "Float64", -5, 0); | |
| 259 test(isTestingGet, "Float64", -9, 0); | |
| 260 } | |
| 261 | |
| 262 | |
| 263 function TestGetters() { | |
| 264 runIntegerTestCases(true, intArray1, 0, 16); | |
| 265 runFloatTestCases(true, 0); | |
| 266 | |
| 267 runIntegerTestCases(true, intArray2, 3, 2); | |
| 268 runFloatTestCases(true, 3); | |
| 269 | |
| 270 runNegativeIndexTests(true); | |
| 271 } | |
| 272 | |
| 273 function TestSetters() { | |
| 274 runIntegerTestCases(false, initialArray, 0, 16); | |
| 275 runFloatTestCases(false); | |
| 276 | |
| 277 runIntegerTestCases(false, initialArray, 3, 2); | |
| 278 runFloatTestCases(false, 7); | |
| 279 | |
| 280 runNegativeIndexTests(false); | |
| 281 | |
| 282 } | |
| 283 | |
| 284 TestGetters(); | |
| 285 TestSetters(); | |
| 286 | |
| 287 function TestGeneralAccessors() { | |
| 288 var a = new DataView(new ArrayBuffer(256)); | |
| 289 function CheckAccessor(name) { | |
| 290 var f = a[name]; | |
| 291 f.call(a, 0, 0); // should not throw | |
| 292 assertThrows(function() { f.call({}, 0, 0); }, TypeError); | |
| 293 } | |
| 294 CheckAccessor("getUint8"); | |
| 295 CheckAccessor("setUint8"); | |
| 296 CheckAccessor("getInt8"); | |
| 297 CheckAccessor("setInt8"); | |
| 298 CheckAccessor("getUint16"); | |
| 299 CheckAccessor("setUint16"); | |
| 300 CheckAccessor("getInt16"); | |
| 301 CheckAccessor("setInt16"); | |
| 302 CheckAccessor("getUint32"); | |
| 303 CheckAccessor("setUint32"); | |
| 304 CheckAccessor("getInt32"); | |
| 305 CheckAccessor("setInt32"); | |
| 306 CheckAccessor("getFloat32"); | |
| 307 CheckAccessor("setFloat32"); | |
| 308 CheckAccessor("getFloat64"); | |
| 309 CheckAccessor("setFloat64"); | |
| 310 } | |
| 311 | |
| 312 TestGeneralAccessors(); | |
| OLD | NEW |