| 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.compareexchange |
| 6 |
description: Test Atomics.compareExchange 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 |
var good_indices = [ (view) => 0/-1, // -0 |
| 17 |
(view) => '-0', |
| 18 |
(view) => view.length - 1, |
| 19 |
(view) => ({ valueOf: () => 0 }), |
| 20 |
(view) => ({ toString: () => '0', valueOf: false }) // non-callable valueOf triggers invocation of toString |
| 21 |
]; |
| 22 |
|
| 23 |
testWithTypedArrayConstructors(function(View) { |
| 24 |
// Make it interesting - use non-zero byteOffsets and non-zero indexes. |
| 25 |
|
| 26 |
var view = new View(sab, 32, 20); |
| 27 |
var control = new View(ab, 0, 2); |
| 28 |
|
| 29 |
// Performs the exchange |
| 30 |
view[8] = 0; |
| 31 |
assert.sameValue(Atomics.compareExchange(view, 8, 0, 10), 0); |
| 32 |
assert.sameValue(view[8], 10); |
| 33 |
|
| 34 |
view[8] = 0; |
| 35 |
assert.sameValue(Atomics.compareExchange(view, 8, 1, 10), 0, |
| 36 |
"Does not perform the exchange"); |
| 37 |
assert.sameValue(view[8], 0); |
| 38 |
|
| 39 |
view[8] = 0; |
| 40 |
assert.sameValue(Atomics.compareExchange(view, 8, 0, -5), 0, |
| 41 |
"Performs the exchange, coercing the value being stored"); |
| 42 |
control[0] = -5; |
| 43 |
assert.sameValue(view[8], control[0]); |
| 44 |
|
| 45 |
|
| 46 |
view[3] = -5; |
| 47 |
control[0] = -5; |
| 48 |
assert.sameValue(Atomics.compareExchange(view, 3, -5, 0), control[0], |
| 49 |
"Performs the exchange, coercing the value being tested"); |
| 50 |
assert.sameValue(view[3], 0); |
| 51 |
|
| 52 |
|
| 53 |
control[0] = 12345; |
| 54 |
view[3] = 12345; |
| 55 |
assert.sameValue(Atomics.compareExchange(view, 3, 12345, 0), control[0], |
| 56 |
"Performs the exchange, chopping the value being tested"); |
| 57 |
assert.sameValue(view[3], 0); |
| 58 |
|
| 59 |
control[0] = 123456789; |
| 60 |
view[3] = 123456789; |
| 61 |
assert.sameValue(Atomics.compareExchange(view, 3, 123456789, 0), control[0], |
| 62 |
"Performs the exchange, chopping the value being tested"); |
| 63 |
assert.sameValue(view[3], 0); |
| 64 |
|
| 65 |
// In-bounds boundary cases for indexing |
| 66 |
testWithAtomicsInBoundsIndices(function(IdxGen) { |
| 67 |
let Idx = IdxGen(view); |
| 68 |
view.fill(0); |
| 69 |
// Atomics.store() computes an index from Idx in the same way as other |
| 70 |
// Atomics operations, not quite like view[Idx]. |
| 71 |
Atomics.store(view, Idx, 37); |
| 72 |
assert.sameValue(Atomics.compareExchange(view, Idx, 37, 0), 37); |
| 73 |
}); |
| 74 |
}, int_views); |