| 1 |
// Copyright (C) 2017 Mozilla Corporation. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
esid: sec-atomics.sub |
| 6 |
description: Test Atomics.sub on arrays that allow atomic operations |
| 7 |
includes: [testAtomics.js, testTypedArray.js] |
| 8 |
features: [SharedArrayBuffer, ArrayBuffer, DataView, Atomics, arrow-function, let, TypedArray, for-of] |
| 9 |
---*/ |
| 10 |
|
| 11 |
var sab = new SharedArrayBuffer(1024); |
| 12 |
var ab = new ArrayBuffer(16); |
| 13 |
|
| 14 |
var int_views = [Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array]; |
| 15 |
|
| 16 |
testWithTypedArrayConstructors(function(View) { |
| 17 |
// Make it interesting - use non-zero byteOffsets and non-zero indexes. |
| 18 |
|
| 19 |
var view = new View(sab, 32, 20); |
| 20 |
var control = new View(ab, 0, 2); |
| 21 |
|
| 22 |
view[8] = 100; |
| 23 |
assert.sameValue(Atomics.sub(view, 8, 10), 100, |
| 24 |
"Subtract positive number"); |
| 25 |
assert.sameValue(view[8], 90); |
| 26 |
|
| 27 |
assert.sameValue(Atomics.sub(view, 8, -5), 90, |
| 28 |
"Subtract negative number, though result remains positive"); |
| 29 |
assert.sameValue(view[8], 95); |
| 30 |
|
| 31 |
view[3] = -5; |
| 32 |
control[0] = -5; |
| 33 |
assert.sameValue(Atomics.sub(view, 3, 0), control[0], |
| 34 |
"Result is negative and subject to coercion"); |
| 35 |
|
| 36 |
control[0] = 12345; |
| 37 |
view[3] = 12345; |
| 38 |
assert.sameValue(Atomics.sub(view, 3, 0), control[0], |
| 39 |
"Result is subject to chopping"); |
| 40 |
|
| 41 |
control[0] = 123456789; |
| 42 |
view[3] = 123456789; |
| 43 |
assert.sameValue(Atomics.sub(view, 3, 0), control[0], |
| 44 |
"Result is subject to chopping"); |
| 45 |
|
| 46 |
// In-bounds boundary cases for indexing |
| 47 |
testWithAtomicsInBoundsIndices(function(IdxGen) { |
| 48 |
let Idx = IdxGen(view); |
| 49 |
view.fill(0); |
| 50 |
// Atomics.store() computes an index from Idx in the same way as other |
| 51 |
// Atomics operations, not quite like view[Idx]. |
| 52 |
Atomics.store(view, Idx, 37); |
| 53 |
assert.sameValue(Atomics.sub(view, Idx, 0), 37); |
| 54 |
}); |
| 55 |
}, int_views); |