Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Unified Diff: test/mjsunit/harmony/atomics.js

Issue 1422533009: Support SAB atomics for offset-TypedArray (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: add a test case Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/mjsunit/asm/atomics-xor.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/atomics.js
diff --git a/test/mjsunit/harmony/atomics.js b/test/mjsunit/harmony/atomics.js
index dbd372fa6d909297a78c469ab37cf97bc88fd592..4b9c9f6c663a3951c4dfaeefab6fb211a4ffc810 100644
--- a/test/mjsunit/harmony/atomics.js
+++ b/test/mjsunit/harmony/atomics.js
@@ -77,12 +77,13 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
(function TestBadIndex() {
var sab = new SharedArrayBuffer(8);
var si32a = new Int32Array(sab);
+ var si32a2 = new Int32Array(sab, 4);
// Non-integer indexes are converted to an integer first, so they should all
// operate on index 0.
[undefined, null, false, 'hi', {}].forEach(function(i) {
- var name = String(i);
+ var name = String(i);
testAtomicOp(Atomics.compareExchange, si32a, i, 0, name);
testAtomicOp(Atomics.load, si32a, i, 0, name);
testAtomicOp(Atomics.store, si32a, i, 0, name);
@@ -109,6 +110,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
assertEquals(undefined, Atomics.exchange(si32a, i, 0), name);
});
+ // Out-of-bounds indexes for offset-array
+ [-1, 1, 100].forEach(function(i) {
+ var name = String(i);
+ assertEquals(undefined, Atomics.compareExchange(si32a2, i, 0, 0), name);
+ assertEquals(undefined, Atomics.load(si32a2, i), name);
+ assertEquals(undefined, Atomics.store(si32a2, i, 0), name);
+ assertEquals(undefined, Atomics.add(si32a2, i, 0), name);
+ assertEquals(undefined, Atomics.sub(si32a2, i, 0), name);
+ assertEquals(undefined, Atomics.and(si32a2, i, 0), name);
+ assertEquals(undefined, Atomics.or(si32a2, i, 0), name);
+ assertEquals(undefined, Atomics.xor(si32a2, i, 0), name);
+ assertEquals(undefined, Atomics.exchange(si32a2, i, 0), name);
+ });
+
// Monkey-patch length and make sure these functions still return undefined.
Object.defineProperty(si32a, 'length', {get: function() { return 1000; }});
[2, 100].forEach(function(i) {
@@ -128,39 +143,53 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
(function TestGoodIndex() {
var sab = new SharedArrayBuffer(64);
var si32a = new Int32Array(sab);
+ var si32a2 = new Int32Array(sab, 32);
var valueOf = {valueOf: function(){ return 3;}};
var toString = {toString: function(){ return '3';}};
[3, 3.5, '3', '3.5', valueOf, toString].forEach(function(i) {
var name = String(i);
-
- testAtomicOp(Atomics.compareExchange, si32a, i, 3, name);
- testAtomicOp(Atomics.load, si32a, i, 3, name);
- testAtomicOp(Atomics.store, si32a, i, 3, name);
- testAtomicOp(Atomics.add, si32a, i, 3, name);
- testAtomicOp(Atomics.sub, si32a, i, 3, name);
- testAtomicOp(Atomics.and, si32a, i, 3, name);
- testAtomicOp(Atomics.or, si32a, i, 3, name);
- testAtomicOp(Atomics.xor, si32a, i, 3, name);
- testAtomicOp(Atomics.exchange, si32a, i, 3, name);
+ [si32a, si32a2].forEach(function(array) {
+ testAtomicOp(Atomics.compareExchange, array, i, 3, name);
+ testAtomicOp(Atomics.load, array, i, 3, name);
+ testAtomicOp(Atomics.store, array, i, 3, name);
+ testAtomicOp(Atomics.add, array, i, 3, name);
+ testAtomicOp(Atomics.sub, array, i, 3, name);
+ testAtomicOp(Atomics.and, array, i, 3, name);
+ testAtomicOp(Atomics.or, array, i, 3, name);
+ testAtomicOp(Atomics.xor, array, i, 3, name);
+ testAtomicOp(Atomics.exchange, array, i, 3, name);
+ })
});
})();
+function clearArray(sab) {
+ var ui8 = new Uint8Array(sab);
+ for (var i = 0; i < sab.byteLength; ++i) {
+ ui8[i] = 0;
+ }
+}
+
(function TestCompareExchange() {
IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab);
- var name = Object.prototype.toString.call(sta);
- for (var i = 0; i < 10; ++i) {
- // sta[i] == 0, CAS will store
- assertEquals(0, Atomics.compareExchange(sta, i, 0, 50), name);
- assertEquals(50, sta[i], name);
-
- // sta[i] == 50, CAS will not store
- assertEquals(50, Atomics.compareExchange(sta, i, 0, 100), name);
- assertEquals(50, sta[i], name);
- }
+ var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
+
+ [sta, sta2].forEach(function(array) {
+ clearArray(array.buffer);
+ var name = Object.prototype.toString.call(array);
+ for (var i = 0; i < array.length; ++i) {
+ // array[i] == 0, CAS will store
+ assertEquals(0, Atomics.compareExchange(array, i, 0, 50), name);
+ assertEquals(50, array[i], name);
+
+ // array[i] == 50, CAS will not store
+ assertEquals(50, Atomics.compareExchange(array, i, 0, 100), name);
+ assertEquals(50, array[i], name);
+ }
+ })
});
})();
@@ -168,13 +197,18 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab);
- var name = Object.prototype.toString.call(sta);
- for (var i = 0; i < 10; ++i) {
- sta[i] = 0;
- assertEquals(0, Atomics.load(sta, i), name);
- sta[i] = 50;
- assertEquals(50, Atomics.load(sta, i), name);
- }
+ var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
+
+ [sta, sta2].forEach(function(array) {
+ clearArray(array.buffer);
+ var name = Object.prototype.toString.call(array);
+ for (var i = 0; i < array.length; ++i) {
+ array[i] = 0;
+ assertEquals(0, Atomics.load(array, i), name);
+ array[i] = 50;
+ assertEquals(50, Atomics.load(array, i), name);
+ }
+ })
});
})();
@@ -182,14 +216,19 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab);
- var name = Object.prototype.toString.call(sta);
- for (var i = 0; i < 10; ++i) {
- assertEquals(50, Atomics.store(sta, i, 50), name);
- assertEquals(50, sta[i], name);
-
- assertEquals(100, Atomics.store(sta, i, 100), name);
- assertEquals(100, sta[i], name);
- }
+ var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
+
+ [sta, sta2].forEach(function(array) {
+ clearArray(array.buffer);
+ var name = Object.prototype.toString.call(array);
+ for (var i = 0; i < array.length; ++i) {
+ assertEquals(50, Atomics.store(array, i, 50), name);
+ assertEquals(50, array[i], name);
+
+ assertEquals(100, Atomics.store(array, i, 100), name);
+ assertEquals(100, array[i], name);
+ }
+ })
});
})();
@@ -197,14 +236,19 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab);
- var name = Object.prototype.toString.call(sta);
- for (var i = 0; i < 10; ++i) {
- assertEquals(0, Atomics.add(sta, i, 50), name);
- assertEquals(50, sta[i], name);
-
- assertEquals(50, Atomics.add(sta, i, 70), name);
- assertEquals(120, sta[i], name);
- }
+ var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
+
+ [sta, sta2].forEach(function(array) {
+ clearArray(array.buffer);
+ var name = Object.prototype.toString.call(array);
+ for (var i = 0; i < array.length; ++i) {
+ assertEquals(0, Atomics.add(array, i, 50), name);
+ assertEquals(50, array[i], name);
+
+ assertEquals(50, Atomics.add(array, i, 70), name);
+ assertEquals(120, array[i], name);
+ }
+ })
});
})();
@@ -212,15 +256,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab);
- var name = Object.prototype.toString.call(sta);
- for (var i = 0; i < 10; ++i) {
- sta[i] = 120;
- assertEquals(120, Atomics.sub(sta, i, 50), name);
- assertEquals(70, sta[i], name);
-
- assertEquals(70, Atomics.sub(sta, i, 70), name);
- assertEquals(0, sta[i], name);
- }
+ var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
+
+ [sta, sta2].forEach(function(array) {
+ clearArray(array.buffer);
+ var name = Object.prototype.toString.call(array);
+ for (var i = 0; i < array.length; ++i) {
+ array[i] = 120;
+ assertEquals(120, Atomics.sub(array, i, 50), name);
+ assertEquals(70, array[i], name);
+
+ assertEquals(70, Atomics.sub(array, i, 70), name);
+ assertEquals(0, array[i], name);
+ }
+ })
});
})();
@@ -228,15 +277,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab);
- var name = Object.prototype.toString.call(sta);
- for (var i = 0; i < 10; ++i) {
- sta[i] = 0x3f;
- assertEquals(0x3f, Atomics.and(sta, i, 0x30), name);
- assertEquals(0x30, sta[i], name);
-
- assertEquals(0x30, Atomics.and(sta, i, 0x20), name);
- assertEquals(0x20, sta[i], name);
- }
+ var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
+
+ [sta, sta2].forEach(function(array) {
+ clearArray(array.buffer);
+ var name = Object.prototype.toString.call(sta);
+ for (var i = 0; i < array.length; ++i) {
+ array[i] = 0x3f;
+ assertEquals(0x3f, Atomics.and(array, i, 0x30), name);
+ assertEquals(0x30, array[i], name);
+
+ assertEquals(0x30, Atomics.and(array, i, 0x20), name);
+ assertEquals(0x20, array[i], name);
+ }
+ })
});
})();
@@ -244,15 +298,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab);
- var name = Object.prototype.toString.call(sta);
- for (var i = 0; i < 10; ++i) {
- sta[i] = 0x30;
- assertEquals(0x30, Atomics.or(sta, i, 0x1c), name);
- assertEquals(0x3c, sta[i], name);
-
- assertEquals(0x3c, Atomics.or(sta, i, 0x09), name);
- assertEquals(0x3d, sta[i], name);
- }
+ var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
+
+ [sta, sta2].forEach(function(array) {
+ clearArray(array.buffer);
+ var name = Object.prototype.toString.call(array);
+ for (var i = 0; i < array.length; ++i) {
+ array[i] = 0x30;
+ assertEquals(0x30, Atomics.or(array, i, 0x1c), name);
+ assertEquals(0x3c, array[i], name);
+
+ assertEquals(0x3c, Atomics.or(array, i, 0x09), name);
+ assertEquals(0x3d, array[i], name);
+ }
+ })
});
})();
@@ -260,15 +319,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab);
- var name = Object.prototype.toString.call(sta);
- for (var i = 0; i < 10; ++i) {
- sta[i] = 0x30;
- assertEquals(0x30, Atomics.xor(sta, i, 0x1c), name);
- assertEquals(0x2c, sta[i], name);
-
- assertEquals(0x2c, Atomics.xor(sta, i, 0x09), name);
- assertEquals(0x25, sta[i], name);
- }
+ var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
+
+ [sta, sta2].forEach(function(array) {
+ clearArray(array.buffer);
+ var name = Object.prototype.toString.call(array);
+ for (var i = 0; i < array.length; ++i) {
+ array[i] = 0x30;
+ assertEquals(0x30, Atomics.xor(array, i, 0x1c), name);
+ assertEquals(0x2c, array[i], name);
+
+ assertEquals(0x2c, Atomics.xor(array, i, 0x09), name);
+ assertEquals(0x25, array[i], name);
+ }
+ })
});
})();
@@ -276,15 +340,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab);
- var name = Object.prototype.toString.call(sta);
- for (var i = 0; i < 10; ++i) {
- sta[i] = 0x30;
- assertEquals(0x30, Atomics.exchange(sta, i, 0x1c), name);
- assertEquals(0x1c, sta[i], name);
-
- assertEquals(0x1c, Atomics.exchange(sta, i, 0x09), name);
- assertEquals(0x09, sta[i], name);
- }
+ var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
+
+ [sta, sta2].forEach(function(array) {
+ clearArray(array.buffer);
+ var name = Object.prototype.toString.call(array);
+ for (var i = 0; i < array.length; ++i) {
+ array[i] = 0x30;
+ assertEquals(0x30, Atomics.exchange(array, i, 0x1c), name);
+ assertEquals(0x1c, array[i], name);
+
+ assertEquals(0x1c, Atomics.exchange(array, i, 0x09), name);
+ assertEquals(0x09, array[i], name);
+ }
+ })
});
})();
« no previous file with comments | « test/mjsunit/asm/atomics-xor.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698