|
| 1 | +function shouldBe(actual, expected) { |
| 2 | + if (actual !== expected) |
| 3 | + throw new Error(`expected ${expected} but got ${actual}`); |
| 4 | +} |
| 5 | + |
| 6 | +function shouldThrowTypeError(func) { |
| 7 | + let error; |
| 8 | + try { |
| 9 | + func(); |
| 10 | + } catch (e) { |
| 11 | + error = e; |
| 12 | + } |
| 13 | + if (!(error instanceof TypeError)) |
| 14 | + throw new Error('Expected TypeError'); |
| 15 | +} |
| 16 | + |
| 17 | +const separators = [undefined, '', ',', '--', '\u3042', 'ab\u3042cd']; |
| 18 | + |
| 19 | +function reference(array, separator) { |
| 20 | + return Array.prototype.map.call(array, String).join(separator); |
| 21 | +} |
| 22 | + |
| 23 | +function check(array) { |
| 24 | + for (const separator of separators) |
| 25 | + shouldBe(array.join(separator), reference(array, separator)); |
| 26 | +} |
| 27 | + |
| 28 | +const numberTypes = [ |
| 29 | + [Int8Array, -128, 127], |
| 30 | + [Uint8Array, 0, 255], |
| 31 | + [Uint8ClampedArray, 0, 255], |
| 32 | + [Int16Array, -32768, 32767], |
| 33 | + [Uint16Array, 0, 65535], |
| 34 | + [Int32Array, -2147483648, 2147483647], |
| 35 | + [Uint32Array, 0, 4294967295], |
| 36 | +]; |
| 37 | + |
| 38 | +const bigIntTypes = [ |
| 39 | + [BigInt64Array, -(2n ** 63n), 2n ** 63n - 1n], |
| 40 | + [BigUint64Array, 0n, 2n ** 64n - 1n], |
| 41 | +]; |
| 42 | + |
| 43 | +for (const [constructor, min, max] of numberTypes) { |
| 44 | + check(new constructor(0)); |
| 45 | + check(new constructor([0])); |
| 46 | + check(new constructor([max])); |
| 47 | + check(new constructor([min, max, 0, 1, 9, 10, 99, 100, 101, max - 1, min + 1])); |
| 48 | + const big = new constructor(1000); |
| 49 | + for (let i = 0; i < big.length; ++i) |
| 50 | + big[i] = (i * 2654435761) % (max - min + 1) + min; |
| 51 | + check(big); |
| 52 | +} |
| 53 | + |
| 54 | +for (const [constructor, min, max] of bigIntTypes) { |
| 55 | + check(new constructor(0)); |
| 56 | + check(new constructor([0n])); |
| 57 | + check(new constructor([max])); |
| 58 | + check(new constructor([min, max, 0n, 1n, 9n, 10n, 99n, 100n, max - 1n, min + 1n])); |
| 59 | + const big = new constructor(1000); |
| 60 | + for (let i = 0; i < big.length; ++i) |
| 61 | + big[i] = BigInt(i) * 0x9e3779b97f4a7c15n; |
| 62 | + check(big); |
| 63 | +} |
| 64 | + |
| 65 | +for (const [constructor] of [...numberTypes, ...bigIntTypes]) { |
| 66 | + const one = constructor === BigInt64Array || constructor === BigUint64Array ? 1n : 1; |
| 67 | + |
| 68 | + { |
| 69 | + const array = new constructor(4).fill(one); |
| 70 | + shouldBe(array.join({ toString() { transferArrayBuffer(array.buffer); return '-'; } }), '---'); |
| 71 | + shouldThrowTypeError(() => array.join()); |
| 72 | + } |
| 73 | + |
| 74 | + { |
| 75 | + const buffer = new ArrayBuffer(4 * constructor.BYTES_PER_ELEMENT, { maxByteLength: 8 * constructor.BYTES_PER_ELEMENT }); |
| 76 | + const array = new constructor(buffer, 0, 4).fill(one); |
| 77 | + shouldBe(array.join({ toString() { buffer.resize(2 * constructor.BYTES_PER_ELEMENT); return ','; } }), ',,,'); |
| 78 | + shouldThrowTypeError(() => array.join()); |
| 79 | + } |
| 80 | + |
| 81 | + { |
| 82 | + const buffer = new ArrayBuffer(4 * constructor.BYTES_PER_ELEMENT, { maxByteLength: 8 * constructor.BYTES_PER_ELEMENT }); |
| 83 | + const array = new constructor(buffer).fill(one); |
| 84 | + shouldBe(array.join({ toString() { buffer.resize(2 * constructor.BYTES_PER_ELEMENT); return '\u3042'; } }), '1\u30421\u3042\u3042'); |
| 85 | + shouldBe(array.join(), '1,1'); |
| 86 | + buffer.resize(8 * constructor.BYTES_PER_ELEMENT); |
| 87 | + shouldBe(array.join(''), '11000000'); |
| 88 | + } |
| 89 | + |
| 90 | + { |
| 91 | + const buffer = new ArrayBuffer(3 * constructor.BYTES_PER_ELEMENT, { maxByteLength: 3 * constructor.BYTES_PER_ELEMENT }); |
| 92 | + const array = new constructor(buffer).fill(one); |
| 93 | + shouldBe(array.join({ toString() { buffer.resize(0); return ''; } }), ''); |
| 94 | + } |
| 95 | + |
| 96 | + if (typeof SharedArrayBuffer !== 'undefined') { |
| 97 | + const array = new constructor(new SharedArrayBuffer(3 * constructor.BYTES_PER_ELEMENT)).fill(one); |
| 98 | + shouldBe(array.join(), '1,1,1'); |
| 99 | + check(array); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +for (const [constructor, min, max] of numberTypes) { |
| 104 | + const boundaries = []; |
| 105 | + for (let power = 1; power <= max; power *= 10) { |
| 106 | + for (const value of [power - 1, power, -(power - 1), -power]) { |
| 107 | + if (value >= min && value <= max) |
| 108 | + boundaries.push(value); |
| 109 | + } |
| 110 | + } |
| 111 | + check(new constructor(boundaries)); |
| 112 | +} |
| 113 | + |
| 114 | +for (const [constructor, min, max] of bigIntTypes) { |
| 115 | + const boundaries = []; |
| 116 | + for (let power = 1n; power <= max; power *= 10n) { |
| 117 | + for (const value of [power - 1n, power, -(power - 1n), -power]) { |
| 118 | + if (value >= min && value <= max) |
| 119 | + boundaries.push(value); |
| 120 | + } |
| 121 | + } |
| 122 | + check(new constructor(boundaries)); |
| 123 | +} |
0 commit comments