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

Side by Side Diff: test/mjsunit/harmony/typedarrays.js

Issue 14740017: Implement TypedArray.subarray method. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 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 | Annotate | Revision Log
« no previous file with comments | « src/typedarray.js ('k') | no next file » | 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 2013 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
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 var ab = new ArrayBuffer(1024); 61 var ab = new ArrayBuffer(1024);
62 assertSame(1024, ab.byteLength); 62 assertSame(1024, ab.byteLength);
63 63
64 assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError); 64 assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError);
65 } 65 }
66 66
67 TestByteLengthNotWritable(); 67 TestByteLengthNotWritable();
68 68
69 function TestSlice(expectedResultLen, initialLen, start, end) { 69 function TestSlice(expectedResultLen, initialLen, start, end) {
70 var ab = new ArrayBuffer(initialLen); 70 var ab = new ArrayBuffer(initialLen);
71 var a1 = new Uint8Array(ab);
72 for (var i = 0; i < a1.length; i++) {
73 a1[i] = 0xCA;
74 }
71 var slice = ab.slice(start, end); 75 var slice = ab.slice(start, end);
72 assertSame(expectedResultLen, slice.byteLength); 76 assertSame(expectedResultLen, slice.byteLength);
77 var a2 = new Uint8Array(slice);
78 for (var i = 0; i < a2.length; i++) {
79 assertSame(0xCA, a2[i]);
80 }
73 } 81 }
74 82
75 function TestArrayBufferSlice() { 83 function TestArrayBufferSlice() {
76 var ab = new ArrayBuffer(1024); 84 var ab = new ArrayBuffer(1024);
77 var ab1 = ab.slice(512, 1024); 85 var ab1 = ab.slice(512, 1024);
78 assertSame(512, ab1.byteLength); 86 assertSame(512, ab1.byteLength);
79 87
80 TestSlice(512, 1024, 512, 1024); 88 TestSlice(512, 1024, 512, 1024);
81 TestSlice(512, 1024, 512); 89 TestSlice(512, 1024, 512);
82 90
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 TestTypedArray(Uint8Array, 1, 0xFF); 256 TestTypedArray(Uint8Array, 1, 0xFF);
249 TestTypedArray(Int8Array, 1, -0x7F); 257 TestTypedArray(Int8Array, 1, -0x7F);
250 TestTypedArray(Uint16Array, 2, 0xFFFF); 258 TestTypedArray(Uint16Array, 2, 0xFFFF);
251 TestTypedArray(Int16Array, 2, -0x7FFF); 259 TestTypedArray(Int16Array, 2, -0x7FFF);
252 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF); 260 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF);
253 TestTypedArray(Int32Array, 4, -0x7FFFFFFF); 261 TestTypedArray(Int32Array, 4, -0x7FFFFFFF);
254 TestTypedArray(Float32Array, 4, 0.5); 262 TestTypedArray(Float32Array, 4, 0.5);
255 TestTypedArray(Float64Array, 8, 0.5); 263 TestTypedArray(Float64Array, 8, 0.5);
256 TestTypedArray(Uint8ClampedArray, 1, 0xFF); 264 TestTypedArray(Uint8ClampedArray, 1, 0xFF);
257 265
266 function SubarrayTestCase(constructor, item, expectedResultLen, expectedStartInd ex,
267 initialLen, start, end) {
268 var a = new constructor(initialLen);
269 var s = a.subarray(start, end);
270 assertSame(constructor, s.constructor);
271 assertSame(expectedResultLen, s.length);
272 if (s.length > 0) {
273 s[0] = item;
274 assertSame(item, a[expectedStartIndex]);
275 }
276 }
277
278 function TestSubArray(constructor, item) {
279 SubarrayTestCase(constructor, item, 512, 512, 1024, 512, 1024);
280 SubarrayTestCase(constructor, item, 512, 512, 1024, 512);
281
282 SubarrayTestCase(constructor, item, 0, undefined, 0, 1, 20);
283 SubarrayTestCase(constructor, item, 100, 0, 100, 0, 100);
284 SubarrayTestCase(constructor, item, 100, 0, 100, 0, 1000);
285 SubarrayTestCase(constructor, item, 0, undefined, 100, 5, 1);
286
287 SubarrayTestCase(constructor, item, 1, 89, 100, -11, -10);
288 SubarrayTestCase(constructor, item, 9, 90, 100, -10, 99);
289 SubarrayTestCase(constructor, item, 0, undefined, 100, -10, 80);
290 SubarrayTestCase(constructor, item, 10,80, 100, 80, -10);
291
292 SubarrayTestCase(constructor, item, 10,90, 100, 90, "100");
293 SubarrayTestCase(constructor, item, 10,90, 100, "90", "100");
294
295 SubarrayTestCase(constructor, item, 0, undefined, 100, 90, "abc");
296 SubarrayTestCase(constructor, item, 10,0, 100, "abc", 10);
297
298 SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.96);
299 SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.01);
300 SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.01);
301 SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.96);
302
303
304 SubarrayTestCase(constructor, item, 10,90, 100, 90);
305 SubarrayTestCase(constructor, item, 10,90, 100, -10);
306 }
307
308 TestSubArray(Uint8Array, 0xFF);
309 TestSubArray(Int8Array, -0x7F);
310 TestSubArray(Uint16Array, 0xFFFF);
311 TestSubArray(Int16Array, -0x7FFF);
312 TestSubArray(Uint32Array, 0xFFFFFFFF);
313 TestSubArray(Int32Array, -0x7FFFFFFF);
314 TestSubArray(Float32Array, 0.5);
315 TestSubArray(Float64Array, 0.5);
316 TestSubArray(Uint8ClampedArray, 0xFF);
317
258 function TestTypedArrayOutOfRange(constructor, value, result) { 318 function TestTypedArrayOutOfRange(constructor, value, result) {
259 var a = new constructor(1); 319 var a = new constructor(1);
260 a[0] = value; 320 a[0] = value;
261 assertSame(result, a[0]); 321 assertSame(result, a[0]);
262 } 322 }
263 323
264 TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA); 324 TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA);
265 TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF); 325 TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF);
266 TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80); 326 TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80);
267 327
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 } 369 }
310 for (var i = 0; i < 20; i++) { 370 for (var i = 0; i < 20; i++) {
311 TestProperty(m, i, 'val' + i); 371 TestProperty(m, i, 'val' + i);
312 TestProperty(m, 'foo' + i, 'bar' + i); 372 TestProperty(m, 'foo' + i, 'bar' + i);
313 } 373 }
314 } 374 }
315 TestArbitrary(new ArrayBuffer(256)); 375 TestArbitrary(new ArrayBuffer(256));
316 376
317 // Test direct constructor call 377 // Test direct constructor call
318 assertTrue(ArrayBuffer() instanceof ArrayBuffer); 378 assertTrue(ArrayBuffer() instanceof ArrayBuffer);
OLDNEW
« no previous file with comments | « src/typedarray.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698