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

Unified Diff: test/mjsunit/harmony/typedarrays-of.js

Issue 660863003: Implement .of() on typed arrays (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 2 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 side-by-side diff with in-line comments
Download patch
« src/harmony-typedarray.js ('K') | « src/harmony-typedarray.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/typedarrays-of.js
diff --git a/test/mjsunit/harmony/typedarrays-of.js b/test/mjsunit/harmony/typedarrays-of.js
new file mode 100644
index 0000000000000000000000000000000000000000..6188bed5fc829924daef0876fbfa4e9d31f8ac15
--- /dev/null
+++ b/test/mjsunit/harmony/typedarrays-of.js
@@ -0,0 +1,149 @@
+// Copyright 2014 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.
+
+// Based on Mozilla Array.of() tests at http://dxr.mozilla.org/mozilla-central/source/js/src/jit-test/tests/collections
+
+// Flags: --harmony-arrays
+
+var typedArrayConstructors = [
+ Uint8Array,
+ Int8Array,
+ Uint16Array,
+ Int16Array,
+ Uint32Array,
+ Int32Array,
+ Uint8ClampedArray,
+ Float32Array,
+ Float64Array];
+
+
+function TestTypedArrayOf(constructor) {
+
+ // %TypedArray%.of basics.
+ assertEquals(constructor.of.length, 0);
wingo 2014/10/21 15:23:13 remove this check, it's done below
+
+ var a = constructor.of();
+ assertEquals(0, a.length);
+ assertEquals(constructor.prototype, Object.getPrototypeOf(a));
+ assertEquals(false, Array.isArray(a));
+
+ // Items are coerced to numerical values.
+ a = constructor.of(undefined, null, [], true, false, 3.14);
+ assertEquals(true, !!a[3]);
wingo 2014/10/21 15:23:13 go ahead and inline these into the branches of the
+ assertEquals(false, !!a[4]);
+
+ // For typed arrays of floating point values, values are not rounded.
+ if (constructor === Float32Array || constructor === Float64Array) {
+ assertEquals(NaN, a[0]);
+ assertEquals(0, a[1]);
+ assertEquals(0, a[2]);
+ assertEquals(true, (a[5] - 3.14) < 1e-6);
+ } else {
+ assertEquals(0, a[0]);
+ assertEquals(0, a[1]);
+ assertEquals(0, a[2]);
+ assertEquals(3, a[5]);
+ }
+
+ var aux = [];
+ for (var i = 0; i < 100; i++)
+ aux[i] = i;
+
+ a = constructor.of.apply(constructor, aux);
+ assertEquals(aux.length, a.length);
+ assertArrayEquals(aux, a);
+
+ // %TypedArray%.of can be transplanted to other constructors.
+
+ var hits = 0;
+ function Bag(length) {
+ assertEquals(arguments.length, 1);
+ assertEquals(length, 2);
+ this.length = length;
+ hits++;
+ }
+ Bag.of = constructor.of;
+
+ hits = 0;
+ a = Bag.of("zero", "one");
+ assertEquals(1, hits);
+ assertEquals(2, a.length);
+ assertArrayEquals(["zero", "one"], a);
+ assertEquals(Bag.prototype, a.__proto__);
+
+ hits = 0;
+ actual = constructor.of.call(Bag, "zero", "one");
+ assertEquals(1, hits);
+ assertEquals(2, a.length);
+ assertArrayEquals(["zero", "one"], a);
+ assertEquals(Bag.prototype, a.__proto__);
+
+ // %TypedArray%.of does not trigger prototype setters.
+ // (It defines elements rather than assigning to them.)
+
+ var status = "pass";
+ Object.defineProperty(constructor.prototype, "0", {set: function(v) {status = "FAIL 1"}});
wingo 2014/10/21 15:23:13 80 columns
+ assertEquals(1, constructor.of(1)[0], 1);
wingo 2014/10/21 15:23:13 does this test what you mean to test? not sure, a
+ assertEquals("pass", status);
+
+ // Array.of calls a "length" setter if one is present.
wingo 2014/10/21 15:23:13 This doesn't appear in the spec AFAICS.
+
+ var hits = 0;
+ var lastObj = null, lastVal = undefined;
+ function setter(v) {
+ hits++;
+ lastObj = this;
+ lastVal = v;
+ }
+
+ // when the setter is on the new object
+ function Pack() {
+ Object.defineProperty(this, "length", {set: setter});
+ }
+ Pack.of = constructor.of;
+ var pack = Pack.of("wolves", "cards", "cigarettes", "lies");
+ assertEquals(lastObj, pack);
+ assertEquals(lastVal, 4);
+
+ // when the setter is on the new object's prototype
+ function Bevy() {}
+ Object.defineProperty(Bevy.prototype, "length", {set: setter});
+ Bevy.of = constructor.of;
+ var bevy = Bevy.of("quail");
+ assertEquals(lastObj, bevy);
+ assertEquals(lastVal, 1);
+
+
+ // Array.of does a strict assignment to the new object's .length.
+ // The assignment is strict even if the code we're calling from is not strict.
+
+ function Empty() {}
+ Empty.of = constructor.of;
+ Object.defineProperty(Empty.prototype, "length", {get: function() { return 0; }});
wingo 2014/10/21 15:23:13 80
+
+ var nothing = new Empty;
+ nothing.length = 2; // no exception; this is not a strict mode assignment
+
+ assertThrows(function() { Empty.of(); }, TypeError);
+
+
+ // Check superficial features of Array.of.
+
+ var desc = Object.getOwnPropertyDescriptor(constructor, "of");
+
+ assertEquals(desc.configurable, true);
wingo 2014/10/21 15:23:13 should be false i think
+ assertEquals(desc.enumerable, false);
+ assertEquals(desc.writable, true);
wingo 2014/10/21 15:23:13 should be false
+ assertEquals(constructor.of.length, 0);
+ assertThrows(function() { new constructor.of() }, TypeError); // not a constructor
wingo 2014/10/21 15:23:13 80
+
+ // When the this-value passed in is not a constructor, the result is an array.
+ [undefined, null, false, "cow"].forEach(function(val) {
wingo 2014/10/21 15:23:13 cleaner to do for (var x of [undefined, null, fal
+ assertEquals(true, constructor.of(val) instanceof constructor);
wingo 2014/10/21 15:23:13 this doesn't appear to test what you intend to tes
+ });
+}
+
+for (var i = 0; i < typedArrayConstructors.length; i++) {
wingo 2014/10/21 15:23:13 for (var constructor of typedArrayConstructors) {
+ TestTypedArrayOf(typedArrayConstructors[i]);
+}
« src/harmony-typedarray.js ('K') | « src/harmony-typedarray.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698