function assert(cond, msg) { if (!cond) throw new Error("Assertion failed: " + msg); }
(function testWaitAsyncExists() {
assert(typeof Atomics.waitAsync === 'function', "Atomics.waitAsync should be a function");
assert(Atomics.waitAsync.length === 4, "Atomics.waitAsync.length should be 4");
console.log("PASS: Atomics.waitAsync exists with correct length");
})();
(function testWaitAsyncNotEqual() {
const sab = new SharedArrayBuffer(16);
const ia = new Int32Array(sab);
ia[0] = 42;
const result = Atomics.waitAsync(ia, 0, 0); assert(result.async === false, "Should be synchronous for not-equal, got async=" + result.async);
assert(result.value === "not-equal", "Should be 'not-equal', got: " + result.value);
console.log("PASS: Atomics.waitAsync returns not-equal synchronously");
})();
(function testWaitAsyncImmediateTimeout() {
const sab = new SharedArrayBuffer(16);
const ia = new Int32Array(sab);
ia[0] = 0;
const result = Atomics.waitAsync(ia, 0, 0, 0); assert(result.async === false, "Should be synchronous for timeout=0, got async=" + result.async);
assert(result.value === "timed-out", "Should be 'timed-out', got: " + result.value);
console.log("PASS: Atomics.waitAsync returns timed-out for zero timeout");
})();
(function testWaitAsyncPromise() {
const sab = new SharedArrayBuffer(16);
const ia = new Int32Array(sab);
ia[0] = 0;
const result = Atomics.waitAsync(ia, 0, 0, 100); assert(result.async === true, "Should be async for matching value with timeout, got: " + result.async);
assert(typeof result.value === 'object', "value should be a Promise object");
console.log("PASS: Atomics.waitAsync returns {async: true, value: Promise}");
})();
(function testWaitAsyncValidation() {
try {
const sab = new SharedArrayBuffer(16);
const ua = new Uint8Array(sab);
Atomics.waitAsync(ua, 0, 0);
assert(false, "Should throw for Uint8Array");
} catch (e) {
assert(e instanceof TypeError, "Should be TypeError");
console.log("PASS: Atomics.waitAsync rejects non-Int32Array");
}
try {
const ab = new ArrayBuffer(16);
const ia = new Int32Array(ab);
Atomics.waitAsync(ia, 0, 0);
assert(false, "Should throw for non-shared buffer");
} catch (e) {
assert(e instanceof TypeError, "Should be TypeError");
console.log("PASS: Atomics.waitAsync rejects non-SharedArrayBuffer");
}
})();
console.log("All Atomics.waitAsync tests passed!");