Debian Codegraph @

tests/auto/qml/ecmascripttests/test262/test/built-ins/Atomics/exchange/good-views.js

raw
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.exchange
6 description: Test Atomics.exchange 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] = 0;
23 assert.sameValue(Atomics.exchange(view, 8, 10), 0,
24 "Exchange returns the value previously in the array");
25 assert.sameValue(view[8], 10);
26
27 assert.sameValue(Atomics.exchange(view, 8, -5), 10,
28 "Exchange returns the value previously in the array");
29 control[0] = -5;
30 assert.sameValue(view[8], control[0]);
31
32 view[3] = -5;
33 control[0] = -5;
34 assert.sameValue(Atomics.exchange(view, 3, 0), control[0],
35 "Result is subject to coercion");
36
37 control[0] = 12345;
38 view[3] = 12345;
39 assert.sameValue(Atomics.exchange(view, 3, 0), control[0],
40 "Result is subject to chopping");
41
42 control[0] = 123456789;
43 view[3] = 123456789;
44 assert.sameValue(Atomics.exchange(view, 3, 0), control[0],
45 "Result is subject to chopping");
46
47 // In-bounds boundary cases for indexing
48 testWithAtomicsInBoundsIndices(function(IdxGen) {
49 let Idx = IdxGen(view);
50 view.fill(0);
51 // Atomics.store() computes an index from Idx in the same way as other
52 // Atomics operations, not quite like view[Idx].
53 Atomics.store(view, Idx, 37);
54 assert.sameValue(Atomics.exchange(view, Idx, 0), 37);
55 });
56 }, int_views);