| 1 |
load(libdir + "asserts.js"); |
| 2 |
|
| 3 |
var gb = 1 * 1024 * 1024 * 1024; |
| 4 |
var ta = new Uint8Array(4 * gb + 10); |
| 5 |
|
| 6 |
function testSetFromTyped() { |
| 7 |
var ta2 = new Int8Array(10); |
| 8 |
ta2[0] = 23; |
| 9 |
ta2[9] = -10; |
| 10 |
ta.set(ta2, 4 * gb); |
| 11 |
assertEq(ta[4 * gb + 0], 23); |
| 12 |
assertEq(ta[4 * gb + 9], 246); |
| 13 |
} |
| 14 |
testSetFromTyped(); |
| 15 |
|
| 16 |
function testSetFromOther() { |
| 17 |
ta.set([12, 34], 4 * gb + 4); |
| 18 |
assertEq(ta[4 * gb + 4], 12); |
| 19 |
assertEq(ta[4 * gb + 5], 34); |
| 20 |
} |
| 21 |
testSetFromOther(); |
| 22 |
|
| 23 |
function testCopyWithin() { |
| 24 |
// Large |start|. |
| 25 |
ta[ta.length - 1] = 3; |
| 26 |
ta.copyWithin(0, 4 * gb); |
| 27 |
assertEq(ta[9], 3); |
| 28 |
|
| 29 |
// Large relative |start|. |
| 30 |
ta[ta.length - 1] = 4; |
| 31 |
ta.copyWithin(0, -10); |
| 32 |
assertEq(ta[9], 4); |
| 33 |
|
| 34 |
// Large |start| and |end|. |
| 35 |
ta[ta.length - 3] = 5; |
| 36 |
ta[ta.length - 2] = 66; |
| 37 |
ta[1] = 1; |
| 38 |
ta.copyWithin(0, ta.length - 3, ta.length - 2); |
| 39 |
assertEq(ta[0], 5); |
| 40 |
assertEq(ta[1], 1); |
| 41 |
|
| 42 |
// Large |target| and |start|. |
| 43 |
ta.copyWithin(4 * gb + 5, 4 * gb + 7); |
| 44 |
assertEq(ta[4 * gb + 6], 66); |
| 45 |
|
| 46 |
// Large |target|. |
| 47 |
ta[6] = 117; |
| 48 |
ta.copyWithin(4 * gb); |
| 49 |
assertEq(ta[4 * gb + 6], 117); |
| 50 |
} |
| 51 |
testCopyWithin(); |
| 52 |
|
| 53 |
function testSlice() { |
| 54 |
// Large |start| and |end|. |
| 55 |
ta[4 * gb + 0] = 11; |
| 56 |
ta[4 * gb + 1] = 22; |
| 57 |
ta[4 * gb + 2] = 33; |
| 58 |
ta[4 * gb + 3] = 44; |
| 59 |
var ta2 = ta.slice(4 * gb, 4 * gb + 4); |
| 60 |
assertEq(ta2.toString(), "11,22,33,44"); |
| 61 |
|
| 62 |
// Large |start|. |
| 63 |
ta[ta.length - 3] = 99; |
| 64 |
ta[ta.length - 2] = 88; |
| 65 |
ta[ta.length - 1] = 77; |
| 66 |
ta2 = ta.slice(4 * gb + 8); |
| 67 |
assertEq(ta2.toString(), "88,77"); |
| 68 |
|
| 69 |
// Relative values. |
| 70 |
ta2 = ta.slice(-3, -1); |
| 71 |
assertEq(ta2.toString(), "99,88"); |
| 72 |
|
| 73 |
// Large relative values. |
| 74 |
ta[0] = 100; |
| 75 |
ta[1] = 101; |
| 76 |
ta[2] = 102; |
| 77 |
ta2 = ta.slice(-ta.length, -ta.length + 3); |
| 78 |
assertEq(ta2.toString(), "100,101,102"); |
| 79 |
} |
| 80 |
testSlice(); |
| 81 |
|
| 82 |
function testSubarray() { |
| 83 |
// Large |start| and |end|. |
| 84 |
ta[4 * gb + 0] = 11; |
| 85 |
ta[4 * gb + 1] = 22; |
| 86 |
ta[4 * gb + 2] = 33; |
| 87 |
ta[4 * gb + 3] = 44; |
| 88 |
var ta2 = ta.subarray(4 * gb, 4 * gb + 4); |
| 89 |
assertEq(ta2.toString(), "11,22,33,44"); |
| 90 |
|
| 91 |
// Large |start|. |
| 92 |
ta[ta.length - 3] = 99; |
| 93 |
ta[ta.length - 2] = 88; |
| 94 |
ta[ta.length - 1] = 77; |
| 95 |
ta2 = ta.subarray(4 * gb + 8); |
| 96 |
assertEq(ta2.toString(), "88,77"); |
| 97 |
|
| 98 |
// Relative values. |
| 99 |
ta2 = ta.subarray(-3, -1); |
| 100 |
assertEq(ta2.toString(), "99,88"); |
| 101 |
|
| 102 |
// Large relative values. |
| 103 |
ta[0] = 100; |
| 104 |
ta[1] = 101; |
| 105 |
ta[2] = 102; |
| 106 |
ta2 = ta.subarray(-ta.length, -ta.length + 3); |
| 107 |
assertEq(ta2.toString(), "100,101,102"); |
| 108 |
} |
| 109 |
testSubarray(); |
| 110 |
|
| 111 |
function testIterators() { |
| 112 |
var ex; |
| 113 |
|
| 114 |
ex = null; |
| 115 |
try { |
| 116 |
for (var p in ta) {} |
| 117 |
} catch (e) { |
| 118 |
ex = e; |
| 119 |
} |
| 120 |
assertEq(ex, "out of memory"); |
| 121 |
|
| 122 |
ex = null; |
| 123 |
try { |
| 124 |
Object.getOwnPropertyNames(ta); |
| 125 |
} catch (e) { |
| 126 |
ex = e; |
| 127 |
} |
| 128 |
const msg = ex + ""; |
| 129 |
assertEq(msg.includes("out of memory") || msg.includes("InternalError: allocation size overflow"), true); |
| 130 |
} |
| 131 |
testIterators(); |
| 132 |
|
| 133 |
function testArraySliceSparse() { |
| 134 |
// Length mustn't exceed UINT32_MAX. |
| 135 |
var len = 4 * gb - 1; |
| 136 |
var ta2 = new Int8Array(ta.buffer, 0, len); |
| 137 |
ta2[len - 1] = 1; |
| 138 |
|
| 139 |
// The SliceSparse optimisation is only used for native objects which have the |
| 140 |
// "indexed" flag set. |
| 141 |
var o = { |
| 142 |
length: len, |
| 143 |
100000: 0, // sparse + indexed |
| 144 |
__proto__: ta2, |
| 145 |
}; |
| 146 |
|
| 147 |
// Collect sufficient elements to trigger the SliceSparse optimisation. |
| 148 |
var r = Array.prototype.slice.call(o, -2000); |
| 149 |
assertEq(r.length, 2000); |
| 150 |
assertEq(r[r.length - 1], 1); |
| 151 |
} |
| 152 |
testArraySliceSparse(); |
| 153 |
|
| 154 |
function testArrayIterator() { |
| 155 |
for (var i = 0; i < 20; i++) { |
| 156 |
ta[i] = i; |
| 157 |
} |
| 158 |
var sum = 0; |
| 159 |
var i = 0; |
| 160 |
for (var x of ta) { |
| 161 |
if (i++ > 20) { |
| 162 |
break; |
| 163 |
} |
| 164 |
sum += x; |
| 165 |
} |
| 166 |
assertEq(sum, 190); |
| 167 |
} |
| 168 |
testArrayIterator(); |
| 169 |
|
| 170 |
function testArrayBufferSlice() { |
| 171 |
// Large |start| and |end|. |
| 172 |
ta[4 * gb + 0] = 11; |
| 173 |
ta[4 * gb + 1] = 22; |
| 174 |
ta[4 * gb + 2] = 33; |
| 175 |
ta[4 * gb + 3] = 44; |
| 176 |
var ta2 = new Uint8Array(ta.buffer.slice(4 * gb, 4 * gb + 4)); |
| 177 |
assertEq(ta2.toString(), "11,22,33,44"); |
| 178 |
|
| 179 |
// Large |start|. |
| 180 |
ta[ta.length - 3] = 99; |
| 181 |
ta[ta.length - 2] = 88; |
| 182 |
ta[ta.length - 1] = 77; |
| 183 |
ta2 = new Uint8Array(ta.buffer.slice(4 * gb + 8)); |
| 184 |
assertEq(ta2.toString(), "88,77"); |
| 185 |
|
| 186 |
// Relative values. |
| 187 |
ta2 = new Uint8Array(ta.buffer.slice(-3, -1)); |
| 188 |
assertEq(ta2.toString(), "99,88"); |
| 189 |
|
| 190 |
// Large relative values. |
| 191 |
ta[0] = 100; |
| 192 |
ta[1] = 101; |
| 193 |
ta[2] = 102; |
| 194 |
ta2 = new Uint8Array(ta.buffer.slice(-ta.length, -ta.length + 3)); |
| 195 |
assertEq(ta2.toString(), "100,101,102"); |
| 196 |
} |
| 197 |
testArrayBufferSlice(); |
| 198 |
|
| 199 |
function testFromObjectTooLargeLength() { |
| 200 |
assertThrowsInstanceOf(() => new Uint8Array({length: 9 * gb}), RangeError); |
| 201 |
assertThrowsInstanceOf(() => ta.set({length: 9 * gb}), RangeError); |
| 202 |
} |
| 203 |
testFromObjectTooLargeLength(); |
| 204 |
|
| 205 |
function testStringKeys() { |
| 206 |
for (var i = 0; i < 20; i++) { |
| 207 |
assertEq(4 * gb - 1 in ta, true); |
| 208 |
assertEq("4294967295" in ta, true); |
| 209 |
assertEq(4 * gb in ta, true); |
| 210 |
assertEq("4294967296" in ta, true); |
| 211 |
assertEq(4 * gb + 10 in ta, false); |
| 212 |
assertEq("4294967306" in ta, false); |
| 213 |
} |
| 214 |
} |
| 215 |
testStringKeys(); |
| 216 |
|
| 217 |
function testStringKeysFromProto() { |
| 218 |
var obj = {__proto__: ta}; |
| 219 |
for (var i = 0; i < 20; i++) { |
| 220 |
assertEq(4 * gb - 1 in obj, true); |
| 221 |
assertEq("4294967295" in obj, true); |
| 222 |
assertEq(4 * gb in obj, true); |
| 223 |
assertEq("4294967296" in obj, true); |
| 224 |
assertEq(4 * gb + 10 in obj, false); |
| 225 |
assertEq("4294967306" in obj, false); |
| 226 |
} |
| 227 |
} |
| 228 |
testStringKeysFromProto(); |