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

Side by Side Diff: test/mjsunit/shared-typed-arrays.js

Issue 1069883002: WIP SharedArrayBuffer implementation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: merge master Created 5 years, 7 months 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 unified diff | Download patch
« no previous file with comments | « test/cctest/test-api.cc ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Flags: --harmony-tostring 28 // Flags: --harmony_shared_typed_arrays
29 29
30 // ArrayBuffer 30 // SharedArrayBuffer
31 31
32 function TestByteLength(param, expectedByteLength) { 32 function TestByteLength(param, expectedByteLength) {
33 var ab = new ArrayBuffer(param); 33 var sab = new SharedArrayBuffer(param);
34 assertSame(expectedByteLength, ab.byteLength); 34 assertSame(expectedByteLength, sab.byteLength);
35 } 35 }
36 36
37 function TestArrayBufferCreation() { 37 function TestArrayBufferCreation() {
38 TestByteLength(1, 1); 38 TestByteLength(1, 1);
39 TestByteLength(256, 256); 39 TestByteLength(256, 256);
40 TestByteLength(2.567, 2); 40 TestByteLength(2.567, 2);
41 41
42 TestByteLength("abc", 0); 42 TestByteLength("abc", 0);
43 43
44 TestByteLength(0, 0); 44 TestByteLength(0, 0);
45 45
46 assertThrows(function() { new ArrayBuffer(-10); }, RangeError); 46 assertThrows(function() { new SharedArrayBuffer(-10); }, RangeError);
47 assertThrows(function() { new ArrayBuffer(-2.567); }, RangeError); 47 assertThrows(function() { new SharedArrayBuffer(-2.567); }, RangeError);
48 48
49 /* TODO[dslomov]: Reenable the test 49 /* TODO[dslomov]: Reenable the test
50 assertThrows(function() { 50 assertThrows(function() {
51 var ab1 = new ArrayBuffer(0xFFFFFFFFFFFF) 51 var ab1 = new SharedArrayBuffer(0xFFFFFFFFFFFF)
52 }, RangeError); 52 }, RangeError);
53 */ 53 */
54 54
55 var ab = new ArrayBuffer(); 55 var sab = new SharedArrayBuffer();
56 assertSame(0, ab.byteLength); 56 assertSame(0, sab.byteLength);
57 assertEquals("[object ArrayBuffer]", 57 assertEquals("[object SharedArrayBuffer]",
58 Object.prototype.toString.call(ab)); 58 Object.prototype.toString.call(sab));
59 } 59 }
60 60
61 TestArrayBufferCreation(); 61 TestArrayBufferCreation();
62 62
63 function TestByteLengthNotWritable() { 63 function TestByteLengthNotWritable() {
64 var ab = new ArrayBuffer(1024); 64 var sab = new SharedArrayBuffer(1024);
65 assertSame(1024, ab.byteLength); 65 assertSame(1024, sab.byteLength);
66 66
67 assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError); 67 assertThrows(function() { "use strict"; sab.byteLength = 42; }, TypeError);
68 } 68 }
69 69
70 TestByteLengthNotWritable(); 70 TestByteLengthNotWritable();
71 71
72 function TestSlice(expectedResultLen, initialLen, start, end) { 72 function TestArrayBufferNoSlice() {
73 var ab = new ArrayBuffer(initialLen); 73 var sab = new SharedArrayBuffer(10);
74 var a1 = new Uint8Array(ab); 74 assertEquals(undefined, sab.slice);
75 for (var i = 0; i < a1.length; i++) {
76 a1[i] = 0xCA;
77 }
78 var slice = ab.slice(start, end);
79 assertSame(expectedResultLen, slice.byteLength);
80 var a2 = new Uint8Array(slice);
81 for (var i = 0; i < a2.length; i++) {
82 assertSame(0xCA, a2[i]);
83 }
84 } 75 }
85 76
86 function TestArrayBufferSlice() { 77 TestArrayBufferNoSlice();
87 var ab = new ArrayBuffer(1024);
88 var ab1 = ab.slice(512, 1024);
89 assertSame(512, ab1.byteLength);
90 78
91 TestSlice(512, 1024, 512, 1024); 79 // Shared Typed arrays
92 TestSlice(512, 1024, 512);
93 80
94 TestSlice(0, 0, 1, 20); 81 function TestTypedArray(constr, nonsharedConstr, elementSize, typicalElement) {
95 TestSlice(100, 100, 0, 100);
96 TestSlice(100, 100, 0, 1000);
97
98 TestSlice(0, 100, 5, 1);
99
100 TestSlice(1, 100, -11, -10);
101 TestSlice(9, 100, -10, 99);
102 TestSlice(0, 100, -10, 80);
103 TestSlice(10, 100, 80, -10);
104
105 TestSlice(10, 100, 90, "100");
106 TestSlice(10, 100, "90", "100");
107
108 TestSlice(0, 100, 90, "abc");
109 TestSlice(10, 100, "abc", 10);
110
111 TestSlice(10, 100, 0.96, 10.96);
112 TestSlice(10, 100, 0.96, 10.01);
113 TestSlice(10, 100, 0.01, 10.01);
114 TestSlice(10, 100, 0.01, 10.96);
115
116 TestSlice(10, 100, 90);
117 TestSlice(10, 100, -10);
118 }
119
120 TestArrayBufferSlice();
121
122 // Typed arrays
123
124 function TestTypedArray(constr, elementSize, typicalElement) {
125 assertSame(elementSize, constr.BYTES_PER_ELEMENT); 82 assertSame(elementSize, constr.BYTES_PER_ELEMENT);
126 83
84 var sab = new SharedArrayBuffer(256*elementSize);
85
86 // Constructing TypedArray from SharedArrayBuffer should throw
87 assertThrows(function() { new nonsharedConstr(sab); }, TypeError);
88
89 // Constructing SharedTypedArray from ArrayBuffer should throw
127 var ab = new ArrayBuffer(256*elementSize); 90 var ab = new ArrayBuffer(256*elementSize);
91 assertThrows(function() { new constr(ab); }, TypeError);
128 92
129 var a0 = new constr(30); 93 var a0 = new constr(30);
130 assertEquals("[object " + constr.name + "]", 94 assertEquals("[object " + constr.name + "]",
131 Object.prototype.toString.call(a0)); 95 Object.prototype.toString.call(a0));
132 96
133 assertTrue(ArrayBuffer.isView(a0)); 97 assertTrue(SharedArrayBuffer.isView(a0));
134 assertSame(elementSize, a0.BYTES_PER_ELEMENT); 98 assertSame(elementSize, a0.BYTES_PER_ELEMENT);
135 assertSame(30, a0.length); 99 assertSame(30, a0.length);
136 assertSame(30*elementSize, a0.byteLength); 100 assertSame(30*elementSize, a0.byteLength);
137 assertSame(0, a0.byteOffset); 101 assertSame(0, a0.byteOffset);
138 assertSame(30*elementSize, a0.buffer.byteLength); 102 assertSame(30*elementSize, a0.buffer.byteLength);
139 103
140 var aLen0 = new constr(0); 104 var aLen0 = new constr(0);
141 assertSame(elementSize, aLen0.BYTES_PER_ELEMENT); 105 assertSame(elementSize, aLen0.BYTES_PER_ELEMENT);
142 assertSame(0, aLen0.length); 106 assertSame(0, aLen0.length);
143 assertSame(0, aLen0.byteLength); 107 assertSame(0, aLen0.byteLength);
144 assertSame(0, aLen0.byteOffset); 108 assertSame(0, aLen0.byteOffset);
145 assertSame(0, aLen0.buffer.byteLength); 109 assertSame(0, aLen0.buffer.byteLength);
146 110
147 var aOverBufferLen0 = new constr(ab, 128*elementSize, 0); 111 var aOverBufferLen0 = new constr(sab, 128*elementSize, 0);
148 assertSame(ab, aOverBufferLen0.buffer); 112 assertSame(sab, aOverBufferLen0.buffer);
149 assertSame(elementSize, aOverBufferLen0.BYTES_PER_ELEMENT); 113 assertSame(elementSize, aOverBufferLen0.BYTES_PER_ELEMENT);
150 assertSame(0, aOverBufferLen0.length); 114 assertSame(0, aOverBufferLen0.length);
151 assertSame(0, aOverBufferLen0.byteLength); 115 assertSame(0, aOverBufferLen0.byteLength);
152 assertSame(128*elementSize, aOverBufferLen0.byteOffset); 116 assertSame(128*elementSize, aOverBufferLen0.byteOffset);
153 117
154 var a1 = new constr(ab, 128*elementSize, 128); 118 var a1 = new constr(sab, 128*elementSize, 128);
155 assertSame(ab, a1.buffer); 119 assertSame(sab, a1.buffer);
156 assertSame(elementSize, a1.BYTES_PER_ELEMENT); 120 assertSame(elementSize, a1.BYTES_PER_ELEMENT);
157 assertSame(128, a1.length); 121 assertSame(128, a1.length);
158 assertSame(128*elementSize, a1.byteLength); 122 assertSame(128*elementSize, a1.byteLength);
159 assertSame(128*elementSize, a1.byteOffset); 123 assertSame(128*elementSize, a1.byteOffset);
160 124
161 125
162 var a2 = new constr(ab, 64*elementSize, 128); 126 var a2 = new constr(sab, 64*elementSize, 128);
163 assertSame(ab, a2.buffer); 127 assertSame(sab, a2.buffer);
164 assertSame(elementSize, a2.BYTES_PER_ELEMENT); 128 assertSame(elementSize, a2.BYTES_PER_ELEMENT);
165 assertSame(128, a2.length); 129 assertSame(128, a2.length);
166 assertSame(128*elementSize, a2.byteLength); 130 assertSame(128*elementSize, a2.byteLength);
167 assertSame(64*elementSize, a2.byteOffset); 131 assertSame(64*elementSize, a2.byteOffset);
168 132
169 var a3 = new constr(ab, 192*elementSize); 133 var a3 = new constr(sab, 192*elementSize);
170 assertSame(ab, a3.buffer); 134 assertSame(sab, a3.buffer);
171 assertSame(64, a3.length); 135 assertSame(64, a3.length);
172 assertSame(64*elementSize, a3.byteLength); 136 assertSame(64*elementSize, a3.byteLength);
173 assertSame(192*elementSize, a3.byteOffset); 137 assertSame(192*elementSize, a3.byteOffset);
174 138
175 var a4 = new constr(ab); 139 var a4 = new constr(sab);
176 assertSame(ab, a4.buffer); 140 assertSame(sab, a4.buffer);
177 assertSame(256, a4.length); 141 assertSame(256, a4.length);
178 assertSame(256*elementSize, a4.byteLength); 142 assertSame(256*elementSize, a4.byteLength);
179 assertSame(0, a4.byteOffset); 143 assertSame(0, a4.byteOffset);
180 144
181 145
182 var i; 146 var i;
183 for (i = 0; i < 128; i++) { 147 for (i = 0; i < 128; i++) {
184 a1[i] = typicalElement; 148 a1[i] = typicalElement;
185 } 149 }
186 150
(...skipping 14 matching lines...) Expand all
201 } 165 }
202 166
203 for (i = 0; i < 128; i++) { 167 for (i = 0; i < 128; i++) {
204 assertSame(0, a4[i]); 168 assertSame(0, a4[i]);
205 } 169 }
206 170
207 for (i = 128; i < 256; i++) { 171 for (i = 128; i < 256; i++) {
208 assertSame(typicalElement, a4[i]); 172 assertSame(typicalElement, a4[i]);
209 } 173 }
210 174
211 var aAtTheEnd = new constr(ab, 256*elementSize); 175 var aAtTheEnd = new constr(sab, 256*elementSize);
212 assertSame(elementSize, aAtTheEnd.BYTES_PER_ELEMENT); 176 assertSame(elementSize, aAtTheEnd.BYTES_PER_ELEMENT);
213 assertSame(0, aAtTheEnd.length); 177 assertSame(0, aAtTheEnd.length);
214 assertSame(0, aAtTheEnd.byteLength); 178 assertSame(0, aAtTheEnd.byteLength);
215 assertSame(256*elementSize, aAtTheEnd.byteOffset); 179 assertSame(256*elementSize, aAtTheEnd.byteOffset);
216 180
217 assertThrows(function () { new constr(ab, 257*elementSize); }, RangeError); 181 assertThrows(function () { new constr(sab, 257*elementSize); }, RangeError);
218 assertThrows( 182 assertThrows(
219 function () { new constr(ab, 128*elementSize, 192); }, 183 function () { new constr(sab, 128*elementSize, 192); },
220 RangeError); 184 RangeError);
221 185
222 if (elementSize !== 1) { 186 if (elementSize !== 1) {
223 assertThrows(function() { new constr(ab, 128*elementSize - 1, 10); }, 187 assertThrows(function() { new constr(sab, 128*elementSize - 1, 10); },
224 RangeError); 188 RangeError);
225 var unalignedArrayBuffer = new ArrayBuffer(10*elementSize + 1); 189 var unalignedArrayBuffer = new SharedArrayBuffer(10*elementSize + 1);
226 var goodArray = new constr(unalignedArrayBuffer, 0, 10); 190 var goodArray = new constr(unalignedArrayBuffer, 0, 10);
227 assertSame(10, goodArray.length); 191 assertSame(10, goodArray.length);
228 assertSame(10*elementSize, goodArray.byteLength); 192 assertSame(10*elementSize, goodArray.byteLength);
229 assertThrows(function() { new constr(unalignedArrayBuffer)}, RangeError); 193 assertThrows(function() { new constr(unalignedArrayBuffer)}, RangeError);
230 assertThrows(function() { new constr(unalignedArrayBuffer, 5*elementSize)}, 194 assertThrows(function() { new constr(unalignedArrayBuffer, 5*elementSize)},
231 RangeError); 195 RangeError);
232 } 196 }
233 197
234 var aFromString = new constr("30"); 198 var aFromString = new constr("30");
235 assertSame(elementSize, aFromString.BYTES_PER_ELEMENT); 199 assertSame(elementSize, aFromString.BYTES_PER_ELEMENT);
236 assertSame(30, aFromString.length); 200 assertSame(30, aFromString.length);
237 assertSame(30*elementSize, aFromString.byteLength); 201 assertSame(30*elementSize, aFromString.byteLength);
238 assertSame(0, aFromString.byteOffset); 202 assertSame(0, aFromString.byteOffset);
239 assertSame(30*elementSize, aFromString.buffer.byteLength); 203 assertSame(30*elementSize, aFromString.buffer.byteLength);
240 204
205 // Difference from ArrayBuffer -- SharedArrayBuffer cannot be initialized
206 // from Array-like object.
241 var jsArray = []; 207 var jsArray = [];
242 for (i = 0; i < 30; i++) { 208 assertThrows(function() { new constr(jsArray); }, TypeError);
243 jsArray.push(typicalElement);
244 }
245 var aFromArray = new constr(jsArray);
246 assertSame(elementSize, aFromArray.BYTES_PER_ELEMENT);
247 assertSame(30, aFromArray.length);
248 assertSame(30*elementSize, aFromArray.byteLength);
249 assertSame(0, aFromArray.byteOffset);
250 assertSame(30*elementSize, aFromArray.buffer.byteLength);
251 for (i = 0; i < 30; i++) {
252 assertSame(typicalElement, aFromArray[i]);
253 }
254 209
255 var abLen0 = new ArrayBuffer(0); 210 var abLen0 = new SharedArrayBuffer(0);
256 var aOverAbLen0 = new constr(abLen0); 211 var aOverAbLen0 = new constr(abLen0);
257 assertSame(abLen0, aOverAbLen0.buffer); 212 assertSame(abLen0, aOverAbLen0.buffer);
258 assertSame(elementSize, aOverAbLen0.BYTES_PER_ELEMENT); 213 assertSame(elementSize, aOverAbLen0.BYTES_PER_ELEMENT);
259 assertSame(0, aOverAbLen0.length); 214 assertSame(0, aOverAbLen0.length);
260 assertSame(0, aOverAbLen0.byteLength); 215 assertSame(0, aOverAbLen0.byteLength);
261 assertSame(0, aOverAbLen0.byteOffset); 216 assertSame(0, aOverAbLen0.byteOffset);
262 217
263 var aNoParam = new constr(); 218 var aNoParam = new constr();
264 assertSame(elementSize, aNoParam.BYTES_PER_ELEMENT); 219 assertSame(elementSize, aNoParam.BYTES_PER_ELEMENT);
265 assertSame(0, aNoParam.length); 220 assertSame(0, aNoParam.length);
266 assertSame(0, aNoParam.byteLength); 221 assertSame(0, aNoParam.byteLength);
267 assertSame(0, aNoParam.byteOffset); 222 assertSame(0, aNoParam.byteOffset);
268 223
269 var a = new constr(ab, 64*elementSize, 128); 224 var a = new constr(sab, 64*elementSize, 128);
270 assertEquals("[object " + constr.name + "]", 225 assertEquals("[object " + constr.name + "]",
271 Object.prototype.toString.call(a)); 226 Object.prototype.toString.call(a));
272 var desc = Object.getOwnPropertyDescriptor( 227 var desc = Object.getOwnPropertyDescriptor(
273 constr.prototype, Symbol.toStringTag); 228 constr.prototype, Symbol.toStringTag);
274 assertTrue(desc.configurable); 229 assertTrue(desc.configurable);
275 assertFalse(desc.enumerable); 230 assertFalse(desc.enumerable);
276 assertFalse(!!desc.writable); 231 assertFalse(!!desc.writable);
277 assertFalse(!!desc.set); 232 assertFalse(!!desc.set);
278 assertEquals("function", typeof desc.get); 233 assertEquals("function", typeof desc.get);
279 } 234 }
280 235
281 TestTypedArray(Uint8Array, 1, 0xFF); 236 TestTypedArray(SharedUint8Array, Uint8Array, 1, 0xFF);
282 TestTypedArray(Int8Array, 1, -0x7F); 237 TestTypedArray(SharedInt8Array, Int8Array, 1, -0x7F);
283 TestTypedArray(Uint16Array, 2, 0xFFFF); 238 TestTypedArray(SharedUint16Array, Uint16Array, 2, 0xFFFF);
284 TestTypedArray(Int16Array, 2, -0x7FFF); 239 TestTypedArray(SharedInt16Array, Int16Array, 2, -0x7FFF);
285 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF); 240 TestTypedArray(SharedUint32Array, Uint32Array, 4, 0xFFFFFFFF);
286 TestTypedArray(Int32Array, 4, -0x7FFFFFFF); 241 TestTypedArray(SharedInt32Array, Int32Array, 4, -0x7FFFFFFF);
287 TestTypedArray(Float32Array, 4, 0.5); 242 TestTypedArray(SharedFloat32Array, Float32Array, 4, 0.5);
288 TestTypedArray(Float64Array, 8, 0.5); 243 TestTypedArray(SharedFloat64Array, Float64Array, 8, 0.5);
289 TestTypedArray(Uint8ClampedArray, 1, 0xFF); 244 TestTypedArray(SharedUint8ClampedArray, Uint8ClampedArray, 1, 0xFF);
290 245
291 function SubarrayTestCase(constructor, item, expectedResultLen, expectedStartInd ex, 246
292 initialLen, start, end) { 247 function SubarrayTestCase(constructor, item, expectedResultLen,
248 expectedStartIndex, initialLen, start, end) {
293 var a = new constructor(initialLen); 249 var a = new constructor(initialLen);
294 var s = a.subarray(start, end); 250 var s = a.subarray(start, end);
295 assertSame(constructor, s.constructor); 251 assertSame(constructor, s.constructor);
296 assertSame(expectedResultLen, s.length); 252 assertSame(expectedResultLen, s.length);
297 if (s.length > 0) { 253 if (s.length > 0) {
298 s[0] = item; 254 s[0] = item;
299 assertSame(item, a[expectedStartIndex]); 255 assertSame(item, a[expectedStartIndex]);
300 } 256 }
301 } 257 }
302 258
(...skipping 25 matching lines...) Expand all
328 284
329 SubarrayTestCase(constructor, item, 10,90, 100, 90); 285 SubarrayTestCase(constructor, item, 10,90, 100, 90);
330 SubarrayTestCase(constructor, item, 10,90, 100, -10); 286 SubarrayTestCase(constructor, item, 10,90, 100, -10);
331 287
332 var method = constructor.prototype.subarray; 288 var method = constructor.prototype.subarray;
333 method.call(new constructor(100), 0, 100); 289 method.call(new constructor(100), 0, 100);
334 var o = {}; 290 var o = {};
335 assertThrows(function() { method.call(o, 0, 100); }, TypeError); 291 assertThrows(function() { method.call(o, 0, 100); }, TypeError);
336 } 292 }
337 293
338 TestSubArray(Uint8Array, 0xFF); 294 TestSubArray(SharedUint8Array, 0xFF);
339 TestSubArray(Int8Array, -0x7F); 295 TestSubArray(SharedInt8Array, -0x7F);
340 TestSubArray(Uint16Array, 0xFFFF); 296 TestSubArray(SharedUint16Array, 0xFFFF);
341 TestSubArray(Int16Array, -0x7FFF); 297 TestSubArray(SharedInt16Array, -0x7FFF);
342 TestSubArray(Uint32Array, 0xFFFFFFFF); 298 TestSubArray(SharedUint32Array, 0xFFFFFFFF);
343 TestSubArray(Int32Array, -0x7FFFFFFF); 299 TestSubArray(SharedInt32Array, -0x7FFFFFFF);
344 TestSubArray(Float32Array, 0.5); 300 TestSubArray(SharedFloat32Array, 0.5);
345 TestSubArray(Float64Array, 0.5); 301 TestSubArray(SharedFloat64Array, 0.5);
346 TestSubArray(Uint8ClampedArray, 0xFF); 302 TestSubArray(SharedUint8ClampedArray, 0xFF);
347 303
348 function TestTypedArrayOutOfRange(constructor, value, result) { 304 function TestTypedArrayOutOfRange(constructor, value, result) {
349 var a = new constructor(1); 305 var a = new constructor(1);
350 a[0] = value; 306 a[0] = value;
351 assertSame(result, a[0]); 307 assertSame(result, a[0]);
352 } 308 }
353 309
354 TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA); 310 TestTypedArrayOutOfRange(SharedUint8Array, 0x1FA, 0xFA);
355 TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF); 311 TestTypedArrayOutOfRange(SharedUint8Array, -1, 0xFF);
356 312
357 TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80); 313 TestTypedArrayOutOfRange(SharedInt8Array, 0x1FA, 0x7A - 0x80);
358 314
359 TestTypedArrayOutOfRange(Uint16Array, 0x1FFFA, 0xFFFA); 315 TestTypedArrayOutOfRange(SharedUint16Array, 0x1FFFA, 0xFFFA);
360 TestTypedArrayOutOfRange(Uint16Array, -1, 0xFFFF); 316 TestTypedArrayOutOfRange(SharedUint16Array, -1, 0xFFFF);
361 TestTypedArrayOutOfRange(Int16Array, 0x1FFFA, 0x7FFA - 0x8000); 317 TestTypedArrayOutOfRange(SharedInt16Array, 0x1FFFA, 0x7FFA - 0x8000);
362 318
363 TestTypedArrayOutOfRange(Uint32Array, 0x1FFFFFFFA, 0xFFFFFFFA); 319 TestTypedArrayOutOfRange(SharedUint32Array, 0x1FFFFFFFA, 0xFFFFFFFA);
364 TestTypedArrayOutOfRange(Uint32Array, -1, 0xFFFFFFFF); 320 TestTypedArrayOutOfRange(SharedUint32Array, -1, 0xFFFFFFFF);
365 TestTypedArrayOutOfRange(Int32Array, 0x1FFFFFFFA, 0x7FFFFFFA - 0x80000000); 321 TestTypedArrayOutOfRange(SharedInt32Array, 0x1FFFFFFFA,
322 0x7FFFFFFA - 0x80000000);
366 323
367 TestTypedArrayOutOfRange(Uint8ClampedArray, 0x1FA, 0xFF); 324 TestTypedArrayOutOfRange(SharedUint8ClampedArray, 0x1FA, 0xFF);
368 TestTypedArrayOutOfRange(Uint8ClampedArray, -1, 0); 325 TestTypedArrayOutOfRange(SharedUint8ClampedArray, -1, 0);
369 326
370 var typedArrayConstructors = [ 327 var typedArrayConstructors = [
371 Uint8Array, 328 SharedUint8Array,
372 Int8Array, 329 SharedInt8Array,
373 Uint16Array, 330 SharedUint16Array,
374 Int16Array, 331 SharedInt16Array,
375 Uint32Array, 332 SharedUint32Array,
376 Int32Array, 333 SharedInt32Array,
377 Uint8ClampedArray, 334 SharedUint8ClampedArray,
378 Float32Array, 335 SharedFloat32Array,
379 Float64Array]; 336 SharedFloat64Array];
380 337
381 function TestPropertyTypeChecks(constructor) { 338 function TestPropertyTypeChecks(constructor) {
382 function CheckProperty(name) { 339 function CheckProperty(name) {
383 var d = Object.getOwnPropertyDescriptor(constructor.prototype, name); 340 var d = Object.getOwnPropertyDescriptor(constructor.prototype, name);
384 var o = {}; 341 var o = {};
385 assertThrows(function() {d.get.call(o);}, TypeError); 342 assertThrows(function() {d.get.call(o);}, TypeError);
386 for (var i = 0; i < typedArrayConstructors.length; i++) { 343 for (var i = 0; i < typedArrayConstructors.length; i++) {
387 var ctor = typedArrayConstructors[i]; 344 var ctor = typedArrayConstructors[i];
388 var a = new ctor(10); 345 var a = new ctor(10);
389 if (ctor === constructor) { 346 if (ctor === constructor) {
390 d.get.call(a); // shouldn't throw 347 d.get.call(a); // shouldn't throw
391 } else { 348 } else {
392 assertThrows(function() {d.get.call(a);}, TypeError); 349 assertThrows(function() {d.get.call(a);}, TypeError);
393 } 350 }
394 } 351 }
395 } 352 }
396 353
397 CheckProperty("buffer"); 354 CheckProperty("buffer");
398 CheckProperty("byteOffset"); 355 CheckProperty("byteOffset");
399 CheckProperty("byteLength"); 356 CheckProperty("byteLength");
400 CheckProperty("length"); 357 CheckProperty("length");
401 } 358 }
402 359
403 for(i = 0; i < typedArrayConstructors.length; i++) { 360 for(i = 0; i < typedArrayConstructors.length; i++) {
404 TestPropertyTypeChecks(typedArrayConstructors[i]); 361 TestPropertyTypeChecks(typedArrayConstructors[i]);
405 } 362 }
406 363
407
408 function TestTypedArraySet() { 364 function TestTypedArraySet() {
409 // Test array.set in different combinations. 365 // Test array.set in different combinations.
410 366
411 function assertArrayPrefix(expected, array) { 367 function assertArrayPrefix(expected, array) {
412 for (var i = 0; i < expected.length; ++i) { 368 for (var i = 0; i < expected.length; ++i) {
413 assertEquals(expected[i], array[i]); 369 assertEquals(expected[i], array[i]);
414 } 370 }
415 } 371 }
416 372
417 var a11 = new Int16Array([1, 2, 3, 4, 0, -1]) 373 // SharedTypedArrays don't allow initialization via array-like
418 var a12 = new Uint16Array(15) 374 function initializeFromArray(constructor, array) {
375 var buffer = new constructor(array.length);
376 for (var i = 0; i < array.length; ++i) {
377 buffer[i] = array[i];
378 }
379 return buffer;
380 }
381
382 var a11 = initializeFromArray(SharedInt16Array, [1, 2, 3, 4, 0, -1])
383 var a12 = new SharedUint16Array(15)
419 a12.set(a11, 3) 384 a12.set(a11, 3)
420 assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12) 385 assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12)
421 assertThrows(function(){ a11.set(a12) }) 386 assertThrows(function(){ a11.set(a12) })
422 387
423 var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}] 388 var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}]
424 var a22 = new Int32Array(12) 389 var a22 = new SharedInt32Array(12)
425 a22.set(a21, 2) 390 a22.set(a21, 2)
426 assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22) 391 assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22)
427 392
428 var a31 = new Float32Array([2, 4, 6, 8, 11, NaN, 1/0, -3]) 393 var a31 = initializeFromArray(SharedFloat32Array,
394 [2, 4, 6, 8, 11, NaN, 1/0, -3])
429 var a32 = a31.subarray(2, 6) 395 var a32 = a31.subarray(2, 6)
430 a31.set(a32, 4) 396 a31.set(a32, 4)
431 assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31) 397 assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31)
432 assertArrayPrefix([6, 8, 6, 8], a32) 398 assertArrayPrefix([6, 8, 6, 8], a32)
433 399
434 var a4 = new Uint8ClampedArray([3,2,5,6]) 400 var a4 = initializeFromArray(SharedUint8ClampedArray, [3,2,5,6])
435 a4.set(a4) 401 a4.set(a4)
436 assertArrayPrefix([3, 2, 5, 6], a4) 402 assertArrayPrefix([3, 2, 5, 6], a4)
437 403
438 // Cases with overlapping backing store but different element sizes. 404 // Cases with overlapping backing store but different element sizes.
439 var b = new ArrayBuffer(4) 405 var b = new SharedArrayBuffer(4)
440 var a5 = new Int16Array(b) 406 var a5 = new SharedInt16Array(b)
441 var a50 = new Int8Array(b) 407 var a50 = new SharedInt8Array(b)
442 var a51 = new Int8Array(b, 0, 2) 408 var a51 = new SharedInt8Array(b, 0, 2)
443 var a52 = new Int8Array(b, 1, 2) 409 var a52 = new SharedInt8Array(b, 1, 2)
444 var a53 = new Int8Array(b, 2, 2) 410 var a53 = new SharedInt8Array(b, 2, 2)
445 411
446 a5.set([0x5050, 0x0a0a]) 412 a5.set([0x5050, 0x0a0a])
447 assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50) 413 assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50)
448 assertArrayPrefix([0x50, 0x50], a51) 414 assertArrayPrefix([0x50, 0x50], a51)
449 assertArrayPrefix([0x50, 0x0a], a52) 415 assertArrayPrefix([0x50, 0x0a], a52)
450 assertArrayPrefix([0x0a, 0x0a], a53) 416 assertArrayPrefix([0x0a, 0x0a], a53)
451 417
452 a50.set([0x50, 0x50, 0x0a, 0x0a]) 418 a50.set([0x50, 0x50, 0x0a, 0x0a])
453 a51.set(a5) 419 a51.set(a5)
454 assertArrayPrefix([0x50, 0x0a, 0x0a, 0x0a], a50) 420 assertArrayPrefix([0x50, 0x0a, 0x0a, 0x0a], a50)
(...skipping 12 matching lines...) Expand all
467 433
468 a50.set([0x50, 0x51, 0x0a, 0x0b]) 434 a50.set([0x50, 0x51, 0x0a, 0x0b])
469 a5.set(a52) 435 a5.set(a52)
470 assertArrayPrefix([0x0051, 0x000a], a5) 436 assertArrayPrefix([0x0051, 0x000a], a5)
471 437
472 a50.set([0x50, 0x51, 0x0a, 0x0b]) 438 a50.set([0x50, 0x51, 0x0a, 0x0b])
473 a5.set(a53) 439 a5.set(a53)
474 assertArrayPrefix([0x000a, 0x000b], a5) 440 assertArrayPrefix([0x000a, 0x000b], a5)
475 441
476 // Mixed types of same size. 442 // Mixed types of same size.
477 var a61 = new Float32Array([1.2, 12.3]) 443 var a61 = initializeFromArray(SharedFloat32Array, [1.2, 12.3])
478 var a62 = new Int32Array(2) 444 var a62 = new SharedInt32Array(2)
479 a62.set(a61) 445 a62.set(a61)
480 assertArrayPrefix([1, 12], a62) 446 assertArrayPrefix([1, 12], a62)
481 a61.set(a62) 447 a61.set(a62)
482 assertArrayPrefix([1, 12], a61) 448 assertArrayPrefix([1, 12], a61)
483 449
484 // Invalid source 450 // Invalid source
485 var a = new Uint16Array(50); 451 var a = new SharedUint16Array(50);
486 var expected = []; 452 var expected = [];
487 for (i = 0; i < 50; i++) { 453 for (i = 0; i < 50; i++) {
488 a[i] = i; 454 a[i] = i;
489 expected.push(i); 455 expected.push(i);
490 } 456 }
491 a.set({}); 457 a.set({});
492 assertArrayPrefix(expected, a); 458 assertArrayPrefix(expected, a);
493 assertThrows(function() { a.set.call({}) }, TypeError); 459 assertThrows(function() { a.set.call({}) }, TypeError);
494 assertThrows(function() { a.set.call([]) }, TypeError); 460 assertThrows(function() { a.set.call([]) }, TypeError);
495 461
496 assertThrows(function() { a.set(0); }, TypeError); 462 assertThrows(function() { a.set(0); }, TypeError);
497 assertThrows(function() { a.set(0, 1); }, TypeError); 463 assertThrows(function() { a.set(0, 1); }, TypeError);
464
465 // Setting SharedTypedArrays from TypedArrays or vice versa.
466 var a71 = new SharedInt16Array(15);
467 var a72 = new Int16Array(15);
468 assertThrows(function(){ a71.set(a72) }, TypeError);
469 assertThrows(function(){ a72.set(a71) }, TypeError);
498 } 470 }
499 471
500 TestTypedArraySet(); 472 TestTypedArraySet();
501 473
502 function TestTypedArraysWithIllegalIndices() { 474 function TestTypedArraysWithIllegalIndices() {
503 var a = new Int32Array(100); 475 var a = new SharedInt32Array(100);
504 476
505 a[-10] = 10; 477 a[-10] = 10;
506 assertEquals(undefined, a[-10]); 478 assertEquals(undefined, a[-10]);
507 a["-10"] = 10; 479 a["-10"] = 10;
508 assertEquals(undefined, a["-10"]); 480 assertEquals(undefined, a["-10"]);
509 481
510 var s = " -10"; 482 var s = " -10";
511 a[s] = 10; 483 a[s] = 10;
512 assertEquals(10, a[s]); 484 assertEquals(10, a[s]);
513 var s1 = " -10 "; 485 var s1 = " -10 ";
(...skipping 28 matching lines...) Expand all
542 a[x] = 5; 514 a[x] = 5;
543 a[y] = 27; 515 a[y] = 27;
544 assertEquals(27, a[x]); 516 assertEquals(27, a[x]);
545 assertEquals(27, a[y]); 517 assertEquals(27, a[y]);
546 } 518 }
547 519
548 TestTypedArraysWithIllegalIndices(); 520 TestTypedArraysWithIllegalIndices();
549 521
550 function TestTypedArraysWithIllegalIndicesStrict() { 522 function TestTypedArraysWithIllegalIndicesStrict() {
551 'use strict'; 523 'use strict';
552 var a = new Int32Array(100); 524 var a = new SharedInt32Array(100);
553 525
554 a[-10] = 10; 526 a[-10] = 10;
555 assertEquals(undefined, a[-10]); 527 assertEquals(undefined, a[-10]);
556 a["-10"] = 10; 528 a["-10"] = 10;
557 assertEquals(undefined, a["-10"]); 529 assertEquals(undefined, a["-10"]);
558 530
559 var s = " -10"; 531 var s = " -10";
560 a[s] = 10; 532 a[s] = 10;
561 assertEquals(10, a[s]); 533 assertEquals(10, a[s]);
562 var s1 = " -10 "; 534 var s1 = " -10 ";
(...skipping 26 matching lines...) Expand all
589 assertEquals(Infinity, 1/x); 561 assertEquals(Infinity, 1/x);
590 assertEquals(-Infinity, 1/y); 562 assertEquals(-Infinity, 1/y);
591 a[x] = 5; 563 a[x] = 5;
592 a[y] = 27; 564 a[y] = 27;
593 assertEquals(27, a[x]); 565 assertEquals(27, a[x]);
594 assertEquals(27, a[y]); 566 assertEquals(27, a[y]);
595 } 567 }
596 568
597 TestTypedArraysWithIllegalIndicesStrict(); 569 TestTypedArraysWithIllegalIndicesStrict();
598 570
599 // DataView
600 function TestDataViewConstructor() {
601 var ab = new ArrayBuffer(256);
602
603 var d1 = new DataView(ab, 1, 255);
604 assertTrue(ArrayBuffer.isView(d1));
605 assertSame(ab, d1.buffer);
606 assertSame(1, d1.byteOffset);
607 assertSame(255, d1.byteLength);
608
609 var d2 = new DataView(ab, 2);
610 assertSame(ab, d2.buffer);
611 assertSame(2, d2.byteOffset);
612 assertSame(254, d2.byteLength);
613
614 var d3 = new DataView(ab);
615 assertSame(ab, d3.buffer);
616 assertSame(0, d3.byteOffset);
617 assertSame(256, d3.byteLength);
618
619 var d3a = new DataView(ab, 1, 0);
620 assertSame(ab, d3a.buffer);
621 assertSame(1, d3a.byteOffset);
622 assertSame(0, d3a.byteLength);
623
624 var d3b = new DataView(ab, 256, 0);
625 assertSame(ab, d3b.buffer);
626 assertSame(256, d3b.byteOffset);
627 assertSame(0, d3b.byteLength);
628
629 var d3c = new DataView(ab, 256);
630 assertSame(ab, d3c.buffer);
631 assertSame(256, d3c.byteOffset);
632 assertSame(0, d3c.byteLength);
633
634 var d4 = new DataView(ab, 1, 3.1415926);
635 assertSame(ab, d4.buffer);
636 assertSame(1, d4.byteOffset);
637 assertSame(3, d4.byteLength);
638
639
640 // error cases
641 assertThrows(function() { new DataView(ab, -1); }, RangeError);
642 assertThrows(function() { new DataView(ab, 1, -1); }, RangeError);
643 assertThrows(function() { new DataView(); }, TypeError);
644 assertThrows(function() { new DataView([]); }, TypeError);
645 assertThrows(function() { new DataView(ab, 257); }, RangeError);
646 assertThrows(function() { new DataView(ab, 1, 1024); }, RangeError);
647 }
648
649 TestDataViewConstructor();
650
651 function TestDataViewPropertyTypeChecks() {
652 var a = new DataView(new ArrayBuffer(10));
653 function CheckProperty(name) {
654 var d = Object.getOwnPropertyDescriptor(DataView.prototype, name);
655 var o = {}
656 assertThrows(function() {d.get.call(o);}, TypeError);
657 d.get.call(a); // shouldn't throw
658 }
659
660 CheckProperty("buffer");
661 CheckProperty("byteOffset");
662 CheckProperty("byteLength");
663 }
664
665
666 TestDataViewPropertyTypeChecks();
667
668
669 function TestDataViewToStringTag() {
670 var a = new DataView(new ArrayBuffer(10));
671 assertEquals("[object DataView]", Object.prototype.toString.call(a));
672 var desc = Object.getOwnPropertyDescriptor(
673 DataView.prototype, Symbol.toStringTag);
674 assertTrue(desc.configurable);
675 assertFalse(desc.enumerable);
676 assertFalse(desc.writable);
677 assertEquals("DataView", desc.value);
678 }
679
680
681 // General tests for properties 571 // General tests for properties
682 572
683 // Test property attribute [[Enumerable]] 573 // Test property attribute [[Enumerable]]
684 function TestEnumerable(func, obj) { 574 function TestEnumerable(func, obj) {
685 function props(x) { 575 function props(x) {
686 var array = []; 576 var array = [];
687 for (var p in x) array.push(p); 577 for (var p in x) array.push(p);
688 return array.sort(); 578 return array.sort();
689 } 579 }
690 assertArrayEquals([], props(func)); 580 assertArrayEquals([], props(func));
691 assertArrayEquals([], props(func.prototype)); 581 assertArrayEquals([], props(func.prototype));
692 if (obj) 582 if (obj)
693 assertArrayEquals([], props(obj)); 583 assertArrayEquals([], props(obj));
694 } 584 }
695 TestEnumerable(ArrayBuffer, new ArrayBuffer()); 585 TestEnumerable(ArrayBuffer, new SharedArrayBuffer());
696 for(i = 0; i < typedArrayConstructors.length; i++) { 586 for(i = 0; i < typedArrayConstructors.length; i++) {
697 TestEnumerable(typedArrayConstructors[i]); 587 TestEnumerable(typedArrayConstructors[i]);
698 } 588 }
699 TestEnumerable(DataView, new DataView(new ArrayBuffer()));
700 589
701 // Test arbitrary properties on ArrayBuffer 590 // Test arbitrary properties on ArrayBuffer
702 function TestArbitrary(m) { 591 function TestArbitrary(m) {
703 function TestProperty(map, property, value) { 592 function TestProperty(map, property, value) {
704 map[property] = value; 593 map[property] = value;
705 assertEquals(value, map[property]); 594 assertEquals(value, map[property]);
706 } 595 }
707 for (var i = 0; i < 20; i++) { 596 for (var i = 0; i < 20; i++) {
708 TestProperty(m, 'key' + i, 'val' + i); 597 TestProperty(m, 'key' + i, 'val' + i);
709 TestProperty(m, 'foo' + i, 'bar' + i); 598 TestProperty(m, 'foo' + i, 'bar' + i);
710 } 599 }
711 } 600 }
712 TestArbitrary(new ArrayBuffer(256)); 601 TestArbitrary(new SharedArrayBuffer(256));
713 for(i = 0; i < typedArrayConstructors.length; i++) { 602 for(i = 0; i < typedArrayConstructors.length; i++) {
714 TestArbitrary(new typedArrayConstructors[i](10)); 603 TestArbitrary(new typedArrayConstructors[i](10));
715 } 604 }
716 TestArbitrary(new DataView(new ArrayBuffer(256)));
717
718 605
719 // Test direct constructor call 606 // Test direct constructor call
720 assertThrows(function() { ArrayBuffer(); }, TypeError); 607 assertThrows(function() { SharedArrayBuffer(); }, TypeError);
721 assertThrows(function() { DataView(new ArrayBuffer()); }, TypeError); 608 for(i = 0; i < typedArrayConstructors.length; i++) {
609 assertThrows(function(i) { typedArrayConstructors[i](); }.bind(this, i),
610 TypeError);
611 }
OLDNEW
« no previous file with comments | « test/cctest/test-api.cc ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698