| 1 |
const types = [ |
| 2 |
"Int8", |
| 3 |
"Int16", |
| 4 |
"Int32", |
| 5 |
"Uint8", |
| 6 |
"Uint16", |
| 7 |
"Uint32", |
| 8 |
]; |
| 9 |
|
| 10 |
function convert(type, value) { |
| 11 |
let num = Number(value); |
| 12 |
switch (type) { |
| 13 |
case "Int8": |
| 14 |
return ((num | 0) << 24) >> 24; |
| 15 |
case "Int16": |
| 16 |
return ((num | 0) << 16) >> 16; |
| 17 |
case "Int32": |
| 18 |
return (num | 0); |
| 19 |
case "Uint8": |
| 20 |
return (num >>> 0) & 0xff; |
| 21 |
case "Uint16": |
| 22 |
return (num >>> 0) & 0xffff; |
| 23 |
case "Uint32": |
| 24 |
return (num >>> 0); |
| 25 |
} |
| 26 |
throw new Error(); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
function runTest(type, initial, values) { |
| 31 |
let expected = values.map(v => convert(type, v)); |
| 32 |
assertEq( |
| 33 |
expected.some(e => Object.is(e, initial)), |
| 34 |
false, |
| 35 |
"initial must be different from the expected values" |
| 36 |
); |
| 37 |
|
| 38 |
// Create a fresh function to ensure ICs are specific to a single TypedArray kind. |
| 39 |
let test = Function("initial, values, expected", ` |
| 40 |
let ta = new ${type}Array(1); |
| 41 |
for (let i = 0; i < 200; ++i) { |
| 42 |
ta[0] = initial; |
| 43 |
Atomics.store(ta, 0, values[i % values.length]); |
| 44 |
assertEq(ta[0], expected[i % expected.length]); |
| 45 |
} |
| 46 |
`); |
| 47 |
test(initial, values, expected); |
| 48 |
} |
| 49 |
|
| 50 |
const tests = [ |
| 51 |
// |null| is coerced to zero. |
| 52 |
{ |
| 53 |
initial: 1, |
| 54 |
values: [null], |
| 55 |
}, |
| 56 |
|
| 57 |
// |undefined| is coerced to zero or NaN. |
| 58 |
{ |
| 59 |
initial: 1, |
| 60 |
values: [undefined], |
| 61 |
}, |
| 62 |
|
| 63 |
// |false| is coerced to zero and |true| is coerced to one. |
| 64 |
{ |
| 65 |
initial: 2, |
| 66 |
values: [false, true], |
| 67 |
}, |
| 68 |
|
| 69 |
// Strings without a fractional part. |
| 70 |
{ |
| 71 |
initial: 42, |
| 72 |
values: [ |
| 73 |
"0", "1", "10", "111", "128", "256", "0x7fffffff", "0xffffffff", |
| 74 |
], |
| 75 |
}, |
| 76 |
|
| 77 |
// Strings without a fractional part, but a leading minus sign. |
| 78 |
{ |
| 79 |
initial: 42, |
| 80 |
values: [ |
| 81 |
"-0", "-1", "-10", "-111", "-128", "-256", "-2147483647", "-4294967295", |
| 82 |
], |
| 83 |
}, |
| 84 |
|
| 85 |
// Strings with a fractional number part. |
| 86 |
{ |
| 87 |
initial: 42, |
| 88 |
values: [ |
| 89 |
"0.1", "1.2", "10.8", "111.9", |
| 90 |
"-0.1", "-1.2", "-10.8", "-111.9", |
| 91 |
], |
| 92 |
}, |
| 93 |
|
| 94 |
// Special values and strings not parseable as a number. |
| 95 |
{ |
| 96 |
initial: 42, |
| 97 |
values: ["Infinity", "-Infinity", "NaN", "foobar"], |
| 98 |
}, |
| 99 |
]; |
| 100 |
|
| 101 |
for (let type of types) { |
| 102 |
for (let {initial, values} of tests) { |
| 103 |
runTest(type, initial, values); |
| 104 |
} |
| 105 |
} |