| 1 |
// |reftest| skip-if(!this.hasOwnProperty('Atomics')||!this.hasOwnProperty('SharedArrayBuffer')||(this.hasOwnProperty('getBuildConfiguration')&&getBuildConfiguration('arm64-simulator'))) -- Atomics,SharedArrayBuffer is not enabled unconditionally, ARM64 Simulator cannot emulate atomics |
| 2 |
// Copyright (C) 2017 Mozilla Corporation. All rights reserved. |
| 3 |
// This code is governed by the BSD license found in the LICENSE file. |
| 4 |
|
| 5 |
/*--- |
| 6 |
esid: sec-atomics.compareexchange |
| 7 |
description: Test Atomics.compareExchange on arrays that allow atomic operations. |
| 8 |
includes: [testAtomics.js, testTypedArray.js] |
| 9 |
features: [ArrayBuffer, Atomics, DataView, SharedArrayBuffer, Symbol, TypedArray] |
| 10 |
---*/ |
| 11 |
|
| 12 |
const sab = new SharedArrayBuffer(1024); |
| 13 |
const ab = new ArrayBuffer(16); |
| 14 |
const views = nonClampedIntArrayConstructors.slice(); |
| 15 |
|
| 16 |
testWithTypedArrayConstructors(function(TA) { |
| 17 |
// Make it interesting - use non-zero byteOffsets and non-zero indexes. |
| 18 |
|
| 19 |
const view = new TA(sab, 32, 20); |
| 20 |
const control = new TA(ab, 0, 2); |
| 21 |
|
| 22 |
// Performs the exchange |
| 23 |
view[8] = 0; |
| 24 |
assert.sameValue( |
| 25 |
Atomics.compareExchange(view, 8, 0, 10), |
| 26 |
0, |
| 27 |
'Atomics.compareExchange(view, 8, 0, 10) returns 0' |
| 28 |
); |
| 29 |
assert.sameValue(view[8], 10, 'The value of view[8] is 10'); |
| 30 |
|
| 31 |
view[8] = 0; |
| 32 |
assert.sameValue(Atomics.compareExchange(view, 8, 1, 10), 0, |
| 33 |
'Atomics.compareExchange(view, 8, 1, 10) returns 0'); |
| 34 |
assert.sameValue(view[8], 0, 'The value of view[8] is 0'); |
| 35 |
|
| 36 |
view[8] = 0; |
| 37 |
assert.sameValue(Atomics.compareExchange(view, 8, 0, -5), 0, |
| 38 |
'Atomics.compareExchange(view, 8, 0, -5) returns 0'); |
| 39 |
control[0] = -5; |
| 40 |
assert.sameValue(view[8], control[0], 'The value of view[8] equals the value of `control[0]` (-5)'); |
| 41 |
|
| 42 |
|
| 43 |
view[3] = -5; |
| 44 |
control[0] = -5; |
| 45 |
assert.sameValue(Atomics.compareExchange(view, 3, -5, 0), control[0], |
| 46 |
'Atomics.compareExchange(view, 3, -5, 0) returns the value of `control[0]` (-5)'); |
| 47 |
assert.sameValue(view[3], 0, 'The value of view[3] is 0'); |
| 48 |
|
| 49 |
|
| 50 |
control[0] = 12345; |
| 51 |
view[3] = 12345; |
| 52 |
assert.sameValue(Atomics.compareExchange(view, 3, 12345, 0), control[0], |
| 53 |
'Atomics.compareExchange(view, 3, 12345, 0) returns the value of `control[0]` (12345)'); |
| 54 |
assert.sameValue(view[3], 0, 'The value of view[3] is 0'); |
| 55 |
|
| 56 |
control[0] = 123456789; |
| 57 |
view[3] = 123456789; |
| 58 |
assert.sameValue(Atomics.compareExchange(view, 3, 123456789, 0), control[0], |
| 59 |
'Atomics.compareExchange(view, 3, 123456789, 0) returns the value of `control[0]` (123456789)'); |
| 60 |
assert.sameValue(view[3], 0, 'The value of view[3] is 0'); |
| 61 |
|
| 62 |
// In-bounds boundary cases for indexing |
| 63 |
testWithAtomicsInBoundsIndices(function(IdxGen) { |
| 64 |
let Idx = IdxGen(view); |
| 65 |
view.fill(0); |
| 66 |
// Atomics.store() computes an index from Idx in the same way as other |
| 67 |
// Atomics operations, not quite like view[Idx]. |
| 68 |
Atomics.store(view, Idx, 37); |
| 69 |
assert.sameValue( |
| 70 |
Atomics.compareExchange(view, Idx, 37, 0), |
| 71 |
37, |
| 72 |
'Atomics.compareExchange(view, Idx, 37, 0) returns 37' |
| 73 |
); |
| 74 |
}); |
| 75 |
}, views); |
| 76 |
|
| 77 |
reportCompare(0, 0); |