Index: test/mjsunit/harmony/typedarray-from.js |
diff --git a/test/mjsunit/harmony/typedarray-from.js b/test/mjsunit/harmony/typedarray-from.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4c5fcdf1686c6502df3273e8154e4105171df4b0 |
--- /dev/null |
+++ b/test/mjsunit/harmony/typedarray-from.js |
@@ -0,0 +1,127 @@ |
+// Copyright 2015 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Flags: --harmony-arrays |
+ |
+var typedArrayConstructors = [ |
+ Uint8Array, |
+ Int8Array, |
+ Uint16Array, |
+ Int16Array, |
+ Uint32Array, |
+ Int32Array, |
+ Uint8ClampedArray, |
+ Float32Array, |
+ Float64Array]; |
arv (Not doing code reviews)
2015/05/15 00:20:22
] on next line
dehrenberg
2015/05/15 23:39:15
Done.
|
+ |
+typedArrayConstructors.forEach(function(constructor) { |
arv (Not doing code reviews)
2015/05/15 00:20:22
I find for-of loops easier to read
dehrenberg
2015/05/15 23:39:15
Done.
|
+ |
+assertEquals(1, constructor.from.length); |
arv (Not doing code reviews)
2015/05/15 00:20:22
indent
dehrenberg
2015/05/15 23:39:15
Done.
|
+ |
+// TypedArray.from only callable on this subclassing %TypedArray% |
+assertThrows(function () {constructor.from.call(Array, [])}, TypeError); |
+ |
+var construct_fn = constructor.construct; |
arv (Not doing code reviews)
2015/05/15 00:20:22
Where did construct come from?
dehrenberg
2015/05/15 23:39:14
Oops, this was a remnant for a test that I deleted
|
+ |
+function assertArrayLikeEquals(value, expected, type) { |
+ assertInstanceof(value, type); |
arv (Not doing code reviews)
2015/05/15 00:20:22
I tend to prefer to check the __proto__ or Object.
dehrenberg
2015/05/15 23:39:15
Done.
|
+ assertEquals(expected.length, value.length); |
+ for (var i=0; i<value.length; ++i) { |
arv (Not doing code reviews)
2015/05/15 00:20:22
ws around binops
dehrenberg
2015/05/15 23:39:15
Done.
|
+ assertEquals(expected[i], value[i]); |
+ } |
+} |
+ |
+// Assert that calling mapfn with / without thisArg in sloppy and strict modes |
+// works as expected. |
+var global = this; |
+function non_strict(){ assertEquals(global, this); } |
arv (Not doing code reviews)
2015/05/15 00:20:23
ws before {
dehrenberg
2015/05/15 23:39:15
Done.
|
+function strict(){ "use strict"; assertEquals(void 0, this); } |
arv (Not doing code reviews)
2015/05/15 00:20:22
you can use undefined in tests if you want
dehrenberg
2015/05/15 23:39:14
Done.
|
+function strict_null(){ "use strict"; assertEquals(null, this); } |
+constructor.from([1], non_strict); |
+constructor.from([1], non_strict, void 0); |
+constructor.from([1], non_strict, null); |
+constructor.from([1], strict); |
+constructor.from([1], strict, void 0); |
+constructor.from([1], strict_null, null); |
+ |
+// TypedArray.from can only be called on TypedArray constructors |
+assertThrows(function() {constructor.from.call({}, [])}, TypeError); |
+assertThrows(function() {constructor.from.call([], [])}, TypeError); |
+assertThrows(function() {constructor.from.call(1, [])}, TypeError); |
+assertThrows(function() {constructor.from.call(undefined, [])}, TypeError); |
+ |
+// Converting from various other types, demonstrating that it can |
+// operate on array-like objects as well as iterables. |
+// TODO(dehrenberg): constructors should have similar flexibility. |
+assertArrayLikeEquals(constructor.from( |
+ { length: 1, '0': 5 }), [5], constructor); |
arv (Not doing code reviews)
2015/05/15 00:20:22
no need to quote the key here.
dehrenberg
2015/05/15 23:39:15
Done.
|
+ |
+assertArrayLikeEquals(constructor.from( |
+ { length: -1, '0': 5 }), [], constructor); |
+ |
+assertArrayLikeEquals(constructor.from( |
+ [1, 2, 3]), [1, 2, 3], constructor); |
+ |
+var kSet = new Set([1, 2, 3]); |
+assertArrayLikeEquals(constructor.from(kSet), [1, 2, 3], |
+ constructor); |
+ |
+function* generator() { |
+ yield 4; |
+ yield 5; |
+ yield 6; |
+} |
+ |
+assertArrayLikeEquals(constructor.from(generator()), |
+ [4, 5, 6], constructor); |
+ |
+assertThrows(function() { constructor.from(null); }, TypeError); |
+assertThrows(function() { constructor.from(undefined); }, TypeError); |
+assertThrows(function() { constructor.from([], null); }, TypeError); |
+assertThrows(function() { constructor.from([], "noncallable"); }, |
arv (Not doing code reviews)
2015/05/15 00:20:22
stick to either " or '
dehrenberg
2015/05/15 23:39:15
Done.
|
+ TypeError); |
+ |
+var nullIterator = {}; |
+nullIterator[Symbol.iterator] = null; |
+assertArrayLikeEquals(constructor.from(nullIterator), [], |
+ constructor); |
+ |
+var nonObjIterator = {}; |
+nonObjIterator[Symbol.iterator] = function() { return "nonObject"; }; |
+assertThrows(function() { constructor.from(nonObjIterator); }, |
+ TypeError); |
+ |
+assertThrows(function() { constructor.from([], null); }, TypeError); |
+ |
+// Ensure iterator is only accessed once, and only invoked once |
+var called = false; |
+var arr = [1, 2, 3]; |
+var obj = {}; |
+ |
+// Test order --- only get iterator method once |
+function testIterator() { |
+ assertFalse(called, "@@iterator should be called only once"); |
+ called = true; |
+ assertEquals(obj, this); |
+ return arr[Symbol.iterator](); |
+} |
+var getCalled = false; |
+Object.defineProperty(obj, Symbol.iterator, { |
+ get: function() { |
+ assertFalse(getCalled, "@@iterator should be accessed only once"); |
+ getCalled = true; |
+ return testIterator; |
+ }, |
+ set: function() { |
+ assertUnreachable("@@iterator should not be set"); |
+ } |
+}); |
+assertArrayLikeEquals(constructor.from(obj), [1, 2, 3], constructor); |
+assertEquals(getCalled, true); |
+assertEquals(called, true); |
+ |
+assertEquals(constructor, Uint8Array.from.call(constructor, [1]).constructor); |
+assertEquals(Uint8Array, constructor.from.call(Uint8Array, [1]).constructor); |
+ |
+}); |