Debian Codegraph @

tests/auto/qml/ecmascripttests/test262/test/built-ins/Atomics/add/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.add
6 description: Test Atomics.add 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 // Add positive number
23 view[8] = 0;
24 assert.sameValue(Atomics.add(view, 8, 10), 0);
25 assert.sameValue(view[8], 10);
26
27 // Add negative number
28 assert.sameValue(Atomics.add(view, 8, -5), 10);
29 assert.sameValue(view[8], 5);
30
31 view[3] = -5;
32 control[0] = -5;
33 assert.sameValue(Atomics.add(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.add(view, 3, 0), control[0],
39 "Result is subject to chopping");
40
41 control[0] = 123456789;
42 view[3] = 123456789;
43 assert.sameValue(Atomics.add(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.add(view, Idx, 0), 37);
54 });
55 }, int_views);