| 1 |
const TypedArrayByteOffset = getSelfHostedValue("TypedArrayByteOffset"); |
| 2 |
|
| 3 |
function testTypedArrayByteOffset() { |
| 4 |
let ab = new ArrayBuffer(100, {maxByteLength: 100}); |
| 5 |
let typedArrays = [ |
| 6 |
new Int8Array(ab), |
| 7 |
new Int8Array(ab, 1), |
| 8 |
new Int8Array(ab, 2), |
| 9 |
new Int8Array(ab, 3), |
| 10 |
]; |
| 11 |
|
| 12 |
for (let i = 0; i < 200; ++i) { |
| 13 |
let ta = typedArrays[i & 3]; |
| 14 |
assertEq(TypedArrayByteOffset(ta), i & 3); |
| 15 |
} |
| 16 |
} |
| 17 |
testTypedArrayByteOffset(); |
| 18 |
|
| 19 |
function testTypedArrayByteOffsetOutOfBounds() { |
| 20 |
let ab = new ArrayBuffer(100, {maxByteLength: 100}); |
| 21 |
let typedArrays = [ |
| 22 |
new Int8Array(ab, 0, 10), |
| 23 |
new Int8Array(ab, 1, 10), |
| 24 |
new Int8Array(ab, 2, 10), |
| 25 |
new Int8Array(ab, 3, 10), |
| 26 |
]; |
| 27 |
|
| 28 |
// Resize to zero to make all views out-of-bounds. |
| 29 |
ab.resize(0); |
| 30 |
|
| 31 |
for (let i = 0; i < 200; ++i) { |
| 32 |
let ta = typedArrays[i & 3]; |
| 33 |
assertEq(TypedArrayByteOffset(ta), i & 3); |
| 34 |
} |
| 35 |
} |
| 36 |
testTypedArrayByteOffsetOutOfBounds(); |
| 37 |
|
| 38 |
function testTypedArrayByteOffsetDetached() { |
| 39 |
let ab = new ArrayBuffer(100, {maxByteLength: 100}); |
| 40 |
let typedArrays = [ |
| 41 |
new Int8Array(ab, 0, 10), |
| 42 |
new Int8Array(ab, 1, 10), |
| 43 |
new Int8Array(ab, 2, 10), |
| 44 |
new Int8Array(ab, 3, 10), |
| 45 |
]; |
| 46 |
|
| 47 |
// Detach the buffer. |
| 48 |
ab.transfer(); |
| 49 |
|
| 50 |
for (let i = 0; i < 200; ++i) { |
| 51 |
let ta = typedArrays[i & 3]; |
| 52 |
assertEq(TypedArrayByteOffset(ta), 0); |
| 53 |
} |
| 54 |
} |
| 55 |
testTypedArrayByteOffsetDetached(); |
| 56 |
|
| 57 |
function testTypedArrayByteOffsetWithShared() { |
| 58 |
let ab = new SharedArrayBuffer(100, {maxByteLength: 100}); |
| 59 |
let typedArrays = [ |
| 60 |
new Int8Array(ab), |
| 61 |
new Int8Array(ab, 1), |
| 62 |
new Int8Array(ab, 2), |
| 63 |
new Int8Array(ab, 3), |
| 64 |
]; |
| 65 |
|
| 66 |
for (let i = 0; i < 200; ++i) { |
| 67 |
let ta = typedArrays[i & 3]; |
| 68 |
assertEq(TypedArrayByteOffset(ta), i & 3); |
| 69 |
} |
| 70 |
} |
| 71 |
testTypedArrayByteOffsetWithShared(); |