| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 return 4; | 55 return 4; |
| 56 case "Float64": | 56 case "Float64": |
| 57 return 8; | 57 return 8; |
| 58 default: | 58 default: |
| 59 assertUnreachable(func); | 59 assertUnreachable(func); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 | 62 |
| 63 function checkGet(func, index, expected, littleEndian) { | 63 function checkGet(func, index, expected, littleEndian) { |
| 64 function doGet() { | 64 function doGet() { |
| 65 return view["get" + func](index, littleEndian); | 65 if (littleEndian != undefined) |
| 66 return view["get" + func](index, littleEndian); |
| 67 else |
| 68 return view["get" + func](index); |
| 66 } | 69 } |
| 67 if (index >=0 && index + getElementSize(func) - 1 < view.byteLength) | 70 if (index >=0 && index + getElementSize(func) - 1 < view.byteLength) |
| 68 assertSame(expected, doGet()); | 71 assertSame(expected, doGet()); |
| 69 else | 72 else |
| 70 assertThrows(doGet, RangeError); | 73 assertThrows(doGet, RangeError); |
| 71 } | 74 } |
| 72 | 75 |
| 73 function checkSet(func, index, value, littleEndian) { | 76 function checkSet(func, index, value, littleEndian) { |
| 74 function doSet() { | 77 function doSet() { |
| 75 view["set" + func](index, value, littleEndian); | 78 if (littleEndian != undefined) |
| 79 view["set" + func](index, value, littleEndian); |
| 80 else |
| 81 view["set" + func](index, value); |
| 76 } | 82 } |
| 77 if (index >= 0 && | 83 if (index >= 0 && |
| 78 index + getElementSize(func) - 1 < view.byteLength) { | 84 index + getElementSize(func) - 1 < view.byteLength) { |
| 79 assertSame(undefined, doSet()); | 85 assertSame(undefined, doSet()); |
| 80 checkGet(func, index, value, littleEndian); | 86 checkGet(func, index, value, littleEndian); |
| 81 } else { | 87 } else { |
| 82 assertThrows(doSet, RangeError); | 88 assertThrows(doSet, RangeError); |
| 83 } | 89 } |
| 84 } | 90 } |
| 85 | 91 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 98 arrayBuffer = (new Uint8Array(paddingArray.concat(array))).buffer; | 104 arrayBuffer = (new Uint8Array(paddingArray.concat(array))).buffer; |
| 99 view = new DataView(arrayBuffer, viewStart, viewLength); | 105 view = new DataView(arrayBuffer, viewStart, viewLength); |
| 100 if (!littleEndian) | 106 if (!littleEndian) |
| 101 array.reverse(); // restore the array. | 107 array.reverse(); // restore the array. |
| 102 } | 108 } |
| 103 | 109 |
| 104 function runIntegerTestCases(isTestingGet, array, start, length) { | 110 function runIntegerTestCases(isTestingGet, array, start, length) { |
| 105 createDataView(array, 0, true, start, length); | 111 createDataView(array, 0, true, start, length); |
| 106 | 112 |
| 107 test(isTestingGet, "Int8", 0, 0); | 113 test(isTestingGet, "Int8", 0, 0); |
| 114 test(isTestingGet, "Int8", undefined, 0); |
| 108 test(isTestingGet, "Int8", 8, -128); | 115 test(isTestingGet, "Int8", 8, -128); |
| 109 test(isTestingGet, "Int8", 15, -1); | 116 test(isTestingGet, "Int8", 15, -1); |
| 110 | 117 |
| 111 test(isTestingGet, "Uint8", 0, 0); | 118 test(isTestingGet, "Uint8", 0, 0); |
| 119 test(isTestingGet, "Uint8", undefined, 0); |
| 112 test(isTestingGet, "Uint8", 8, 128); | 120 test(isTestingGet, "Uint8", 8, 128); |
| 113 test(isTestingGet, "Uint8", 15, 255); | 121 test(isTestingGet, "Uint8", 15, 255); |
| 114 | 122 |
| 115 // Little endian. | 123 // Little endian. |
| 116 test(isTestingGet, "Int16", 0, 256, true); | 124 test(isTestingGet, "Int16", 0, 256, true); |
| 125 test(isTestingGet, "Int16", undefined, 256, true); |
| 117 test(isTestingGet, "Int16", 5, 26213, true); | 126 test(isTestingGet, "Int16", 5, 26213, true); |
| 118 test(isTestingGet, "Int16", 9, -32127, true); | 127 test(isTestingGet, "Int16", 9, -32127, true); |
| 119 test(isTestingGet, "Int16", 14, -2, true); | 128 test(isTestingGet, "Int16", 14, -2, true); |
| 120 | 129 |
| 121 // Big endian. | 130 // Big endian. |
| 122 test(isTestingGet, "Int16", 0, 1); | 131 test(isTestingGet, "Int16", 0, 1); |
| 132 test(isTestingGet, "Int16", undefined, 1); |
| 123 test(isTestingGet, "Int16", 5, 25958); | 133 test(isTestingGet, "Int16", 5, 25958); |
| 124 test(isTestingGet, "Int16", 9, -32382); | 134 test(isTestingGet, "Int16", 9, -32382); |
| 125 test(isTestingGet, "Int16", 14, -257); | 135 test(isTestingGet, "Int16", 14, -257); |
| 126 | 136 |
| 127 // Little endian. | 137 // Little endian. |
| 128 test(isTestingGet, "Uint16", 0, 256, true); | 138 test(isTestingGet, "Uint16", 0, 256, true); |
| 139 test(isTestingGet, "Uint16", undefined, 256, true); |
| 129 test(isTestingGet, "Uint16", 5, 26213, true); | 140 test(isTestingGet, "Uint16", 5, 26213, true); |
| 130 test(isTestingGet, "Uint16", 9, 33409, true); | 141 test(isTestingGet, "Uint16", 9, 33409, true); |
| 131 test(isTestingGet, "Uint16", 14, 65534, true); | 142 test(isTestingGet, "Uint16", 14, 65534, true); |
| 132 | 143 |
| 133 // Big endian. | 144 // Big endian. |
| 134 test(isTestingGet, "Uint16", 0, 1); | 145 test(isTestingGet, "Uint16", 0, 1); |
| 146 test(isTestingGet, "Uint16", undefined, 1); |
| 135 test(isTestingGet, "Uint16", 5, 25958); | 147 test(isTestingGet, "Uint16", 5, 25958); |
| 136 test(isTestingGet, "Uint16", 9, 33154); | 148 test(isTestingGet, "Uint16", 9, 33154); |
| 137 test(isTestingGet, "Uint16", 14, 65279); | 149 test(isTestingGet, "Uint16", 14, 65279); |
| 138 | 150 |
| 139 // Little endian. | 151 // Little endian. |
| 140 test(isTestingGet, "Int32", 0, 50462976, true); | 152 test(isTestingGet, "Int32", 0, 50462976, true); |
| 153 test(isTestingGet, "Int32", undefined, 50462976, true); |
| 141 test(isTestingGet, "Int32", 3, 1717920771, true); | 154 test(isTestingGet, "Int32", 3, 1717920771, true); |
| 142 test(isTestingGet, "Int32", 6, -2122291354, true); | 155 test(isTestingGet, "Int32", 6, -2122291354, true); |
| 143 test(isTestingGet, "Int32", 9, -58490239, true); | 156 test(isTestingGet, "Int32", 9, -58490239, true); |
| 144 test(isTestingGet, "Int32", 12,-66052, true); | 157 test(isTestingGet, "Int32", 12,-66052, true); |
| 145 | 158 |
| 146 // Big endian. | 159 // Big endian. |
| 147 test(isTestingGet, "Int32", 0, 66051); | 160 test(isTestingGet, "Int32", 0, 66051); |
| 161 test(isTestingGet, "Int32", undefined, 66051); |
| 148 test(isTestingGet, "Int32", 3, 56911206); | 162 test(isTestingGet, "Int32", 3, 56911206); |
| 149 test(isTestingGet, "Int32", 6, 1718059137); | 163 test(isTestingGet, "Int32", 6, 1718059137); |
| 150 test(isTestingGet, "Int32", 9, -2122152964); | 164 test(isTestingGet, "Int32", 9, -2122152964); |
| 151 test(isTestingGet, "Int32", 12, -50462977); | 165 test(isTestingGet, "Int32", 12, -50462977); |
| 152 | 166 |
| 153 // Little endian. | 167 // Little endian. |
| 154 test(isTestingGet, "Uint32", 0, 50462976, true); | 168 test(isTestingGet, "Uint32", 0, 50462976, true); |
| 169 test(isTestingGet, "Uint32", undefined, 50462976, true); |
| 155 test(isTestingGet, "Uint32", 3, 1717920771, true); | 170 test(isTestingGet, "Uint32", 3, 1717920771, true); |
| 156 test(isTestingGet, "Uint32", 6, 2172675942, true); | 171 test(isTestingGet, "Uint32", 6, 2172675942, true); |
| 157 test(isTestingGet, "Uint32", 9, 4236477057, true); | 172 test(isTestingGet, "Uint32", 9, 4236477057, true); |
| 158 test(isTestingGet, "Uint32", 12,4294901244, true); | 173 test(isTestingGet, "Uint32", 12,4294901244, true); |
| 159 | 174 |
| 160 // Big endian. | 175 // Big endian. |
| 161 test(isTestingGet, "Uint32", 0, 66051); | 176 test(isTestingGet, "Uint32", 0, 66051); |
| 177 test(isTestingGet, "Uint32", undefined, 66051); |
| 162 test(isTestingGet, "Uint32", 3, 56911206); | 178 test(isTestingGet, "Uint32", 3, 56911206); |
| 163 test(isTestingGet, "Uint32", 6, 1718059137); | 179 test(isTestingGet, "Uint32", 6, 1718059137); |
| 164 test(isTestingGet, "Uint32", 9, 2172814332); | 180 test(isTestingGet, "Uint32", 9, 2172814332); |
| 165 test(isTestingGet, "Uint32", 12, 4244504319); | 181 test(isTestingGet, "Uint32", 12, 4244504319); |
| 166 } | 182 } |
| 167 | 183 |
| 168 function testFloat(isTestingGet, func, array, start, expected) { | 184 function testFloat(isTestingGet, func, array, start, expected) { |
| 169 // Little endian. | 185 // Little endian. |
| 170 createDataView(array, 0, true, start); | 186 createDataView(array, 0, true, start); |
| 171 test(isTestingGet, func, 0, expected, true); | 187 test(isTestingGet, func, 0, expected, true); |
| 188 test(isTestingGet, func, undefined, expected, true); |
| 172 createDataView(array, 3, true, start); | 189 createDataView(array, 3, true, start); |
| 173 test(isTestingGet, func, 3, expected, true); | 190 test(isTestingGet, func, 3, expected, true); |
| 174 createDataView(array, 7, true, start); | 191 createDataView(array, 7, true, start); |
| 175 test(isTestingGet, func, 7, expected, true); | 192 test(isTestingGet, func, 7, expected, true); |
| 176 createDataView(array, 10, true, start); | 193 createDataView(array, 10, true, start); |
| 177 test(isTestingGet, func, 10, expected, true); | 194 test(isTestingGet, func, 10, expected, true); |
| 178 | 195 |
| 179 // Big endian. | 196 // Big endian. |
| 180 createDataView(array, 0, false); | 197 createDataView(array, 0, false); |
| 181 test(isTestingGet, func, 0, expected, false); | 198 test(isTestingGet, func, 0, expected, false); |
| 199 test(isTestingGet, func, undefined, expected, false); |
| 182 createDataView(array, 3, false); | 200 createDataView(array, 3, false); |
| 183 test(isTestingGet, func, 3, expected, false); | 201 test(isTestingGet, func, 3, expected, false); |
| 184 createDataView(array, 7, false); | 202 createDataView(array, 7, false); |
| 185 test(isTestingGet, func, 7, expected, false); | 203 test(isTestingGet, func, 7, expected, false); |
| 186 createDataView(array, 10, false); | 204 createDataView(array, 10, false); |
| 187 test(isTestingGet, func, 10, expected, false); | 205 test(isTestingGet, func, 10, expected, false); |
| 188 } | 206 } |
| 189 | 207 |
| 190 function runFloatTestCases(isTestingGet, start) { | 208 function runFloatTestCases(isTestingGet, start) { |
| 191 testFloat(isTestingGet, "Float32", | 209 testFloat(isTestingGet, "Float32", |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 287 } |
| 270 | 288 |
| 271 function TestSetters() { | 289 function TestSetters() { |
| 272 runIntegerTestCases(false, initialArray, 0, 16); | 290 runIntegerTestCases(false, initialArray, 0, 16); |
| 273 runFloatTestCases(false); | 291 runFloatTestCases(false); |
| 274 | 292 |
| 275 runIntegerTestCases(false, initialArray, 3, 2); | 293 runIntegerTestCases(false, initialArray, 3, 2); |
| 276 runFloatTestCases(false, 7); | 294 runFloatTestCases(false, 7); |
| 277 | 295 |
| 278 runNegativeIndexTests(false); | 296 runNegativeIndexTests(false); |
| 279 | |
| 280 } | 297 } |
| 281 | 298 |
| 282 TestGetters(); | 299 TestGetters(); |
| 283 TestSetters(); | 300 TestSetters(); |
| 284 | 301 |
| 302 function CheckOutOfRangeInt8(value, expected) { |
| 303 var view = new DataView(new ArrayBuffer(100)); |
| 304 assertSame(undefined, view.setInt8(0, value)); |
| 305 assertSame(expected, view.getInt8(0)); |
| 306 assertSame(undefined, view.setInt8(0, value, true)); |
| 307 assertSame(expected, view.getInt8(0, true)); |
| 308 } |
| 309 |
| 310 function CheckOutOfRangeUint8(value, expected) { |
| 311 var view = new DataView(new ArrayBuffer(100)); |
| 312 assertSame(undefined, view.setUint8(0, value)); |
| 313 assertSame(expected, view.getUint8(0)); |
| 314 assertSame(undefined, view.setUint8(0, value, true)); |
| 315 assertSame(expected, view.getUint8(0, true)); |
| 316 } |
| 317 |
| 318 function CheckOutOfRangeInt16(value, expected) { |
| 319 var view = new DataView(new ArrayBuffer(100)); |
| 320 assertSame(undefined, view.setInt16(0, value)); |
| 321 assertSame(expected, view.getInt16(0)); |
| 322 assertSame(undefined, view.setInt16(0, value, true)); |
| 323 assertSame(expected, view.getInt16(0, true)); |
| 324 } |
| 325 |
| 326 function CheckOutOfRangeUint16(value, expected) { |
| 327 var view = new DataView(new ArrayBuffer(100)); |
| 328 assertSame(undefined, view.setUint16(0, value)); |
| 329 assertSame(expected, view.getUint16(0)); |
| 330 assertSame(undefined, view.setUint16(0, value, true)); |
| 331 assertSame(expected, view.getUint16(0, true)); |
| 332 } |
| 333 |
| 334 function CheckOutOfRangeInt32(value, expected) { |
| 335 var view = new DataView(new ArrayBuffer(100)); |
| 336 assertSame(undefined, view.setInt32(0, value)); |
| 337 assertSame(expected, view.getInt32(0)); |
| 338 assertSame(undefined, view.setInt32(0, value, true)); |
| 339 assertSame(expected, view.getInt32(0, true)); |
| 340 } |
| 341 |
| 342 function CheckOutOfRangeUint32(value, expected) { |
| 343 var view = new DataView(new ArrayBuffer(100)); |
| 344 assertSame(undefined, view.setUint32(0, value)); |
| 345 assertSame(expected, view.getUint32(0)); |
| 346 assertSame(undefined, view.setUint32(0, value, true)); |
| 347 assertSame(expected, view.getUint32(0, true)); |
| 348 } |
| 349 |
| 350 function TestOutOfRange() { |
| 351 CheckOutOfRangeInt8(0x80, -0x80); |
| 352 CheckOutOfRangeInt8(0x1000, 0); |
| 353 CheckOutOfRangeInt8(-0x81, 0x7F); |
| 354 |
| 355 CheckOutOfRangeUint8(0x100, 0); |
| 356 CheckOutOfRangeUint8(0x1000, 0); |
| 357 CheckOutOfRangeUint8(-0x80, 0x80); |
| 358 CheckOutOfRangeUint8(-1, 0xFF); |
| 359 CheckOutOfRangeUint8(-0xFF, 1); |
| 360 |
| 361 CheckOutOfRangeInt16(0x8000, -0x8000); |
| 362 CheckOutOfRangeInt16(0x10000, 0); |
| 363 CheckOutOfRangeInt16(-0x8001, 0x7FFF); |
| 364 |
| 365 CheckOutOfRangeUint16(0x10000, 0); |
| 366 CheckOutOfRangeUint16(0x100000, 0); |
| 367 CheckOutOfRangeUint16(-0x8000, 0x8000); |
| 368 CheckOutOfRangeUint16(-1, 0xFFFF); |
| 369 CheckOutOfRangeUint16(-0xFFFF, 1); |
| 370 |
| 371 CheckOutOfRangeInt32(0x80000000, -0x80000000); |
| 372 CheckOutOfRangeInt32(0x100000000, 0); |
| 373 CheckOutOfRangeInt32(-0x80000001, 0x7FFFFFFF); |
| 374 |
| 375 CheckOutOfRangeUint32(0x100000000, 0); |
| 376 CheckOutOfRangeUint32(0x1000000000, 0); |
| 377 CheckOutOfRangeUint32(-0x80000000, 0x80000000); |
| 378 CheckOutOfRangeUint32(-1, 0xFFFFFFFF); |
| 379 CheckOutOfRangeUint32(-0xFFFFFFFF, 1); |
| 380 } |
| 381 |
| 382 TestOutOfRange(); |
| 383 |
| 285 function TestGeneralAccessors() { | 384 function TestGeneralAccessors() { |
| 286 var a = new DataView(new ArrayBuffer(256)); | 385 var a = new DataView(new ArrayBuffer(256)); |
| 287 function CheckAccessor(name) { | 386 function CheckAccessor(name) { |
| 288 var f = a[name]; | 387 var f = a[name]; |
| 388 assertThrows(function() { f(); }, TypeError); |
| 289 f.call(a, 0, 0); // should not throw | 389 f.call(a, 0, 0); // should not throw |
| 290 assertThrows(function() { f.call({}, 0, 0); }, TypeError); | 390 assertThrows(function() { f.call({}, 0, 0); }, TypeError); |
| 391 assertThrows(function() { f.call(a); }, TypeError); |
| 291 } | 392 } |
| 292 CheckAccessor("getUint8"); | 393 CheckAccessor("getUint8"); |
| 293 CheckAccessor("setUint8"); | 394 CheckAccessor("setUint8"); |
| 294 CheckAccessor("getInt8"); | 395 CheckAccessor("getInt8"); |
| 295 CheckAccessor("setInt8"); | 396 CheckAccessor("setInt8"); |
| 296 CheckAccessor("getUint16"); | 397 CheckAccessor("getUint16"); |
| 297 CheckAccessor("setUint16"); | 398 CheckAccessor("setUint16"); |
| 298 CheckAccessor("getInt16"); | 399 CheckAccessor("getInt16"); |
| 299 CheckAccessor("setInt16"); | 400 CheckAccessor("setInt16"); |
| 300 CheckAccessor("getUint32"); | 401 CheckAccessor("getUint32"); |
| 301 CheckAccessor("setUint32"); | 402 CheckAccessor("setUint32"); |
| 302 CheckAccessor("getInt32"); | 403 CheckAccessor("getInt32"); |
| 303 CheckAccessor("setInt32"); | 404 CheckAccessor("setInt32"); |
| 304 CheckAccessor("getFloat32"); | 405 CheckAccessor("getFloat32"); |
| 305 CheckAccessor("setFloat32"); | 406 CheckAccessor("setFloat32"); |
| 306 CheckAccessor("getFloat64"); | 407 CheckAccessor("getFloat64"); |
| 307 CheckAccessor("setFloat64"); | 408 CheckAccessor("setFloat64"); |
| 308 } | 409 } |
| 309 | 410 |
| 310 TestGeneralAccessors(); | 411 TestGeneralAccessors(); |
| OLD | NEW |