Index: test/mjsunit/harmony/simd.js |
diff --git a/test/mjsunit/harmony/simd.js b/test/mjsunit/harmony/simd.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2c15930c9527618454b6b0d58a2dd832c9eb7c5f |
--- /dev/null |
+++ b/test/mjsunit/harmony/simd.js |
@@ -0,0 +1,258 @@ |
+// Copyright 2015 the V8 project authors. All rights reserved. |
+// Redistribution and use in source and binary forms, with or without |
+// modification, are permitted provided that the following conditions are |
+// met: |
+// |
+// * Redistributions of source code must retain the above copyright |
+// notice, this list of conditions and the following disclaimer. |
+// * Redistributions in binary form must reproduce the above |
+// copyright notice, this list of conditions and the following |
+// disclaimer in the documentation and/or other materials provided |
+// with the distribution. |
+// * Neither the name of Google Inc. nor the names of its |
+// contributors may be used to endorse or promote products derived |
+// from this software without specific prior written permission. |
+// |
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ |
+// Flags: --harmony-simd --harmony-tostring --allow-natives-syntax |
+ |
+function isValidSimdString(string, value, type, lanes) { |
+ var simdFn = SIMD[type], |
+ parseFn = |
+ type.indexOf('float') === 0 ? Number.parseFloat : Number.parseInt; |
+ var laneStrings = string.split(','); |
+ if (laneStrings.length !== lanes) |
+ return false; |
+ for (var i = 0; i < lanes; i++) { |
+ var fromString = parseFn(laneStrings[i]), |
+ fromValue = simdFn.extractLane(value, i); |
+ if (Math.abs(fromString - fromValue) > Number.EPSILON) |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+ |
rossberg
2015/07/02 13:35:43
Add tests for the SIMD object itself, e.g.:
asser
bbudge
2015/07/06 23:59:05
Thanks. Done.
|
+// Test different forms of constructor calls. |
+function TestConstructor(type, lanes, values) { |
+ var simdFn = SIMD[type]; |
+ assertFalse(Object === simdFn.prototype.constructor) |
+ assertFalse(simdFn === Object.prototype.constructor) |
+ assertSame(simdFn, simdFn.prototype.constructor) |
+ |
+ // Constructors expect values for all lanes. |
+ assertThrows(function () { simdFn() }, TypeError) |
+ assertThrows(function () { simdFn(0) }, TypeError) |
+ switch (lanes) { |
+ case 4: |
+ values.push(simdFn(0, 1, 2, 3)); |
+ values.push(simdFn(-0, NaN, 0, 0.5)); |
+ // Constructors expect values for all lanes. |
+ assertThrows(function () { simdFn(0, 1) }, TypeError) |
+ assertThrows(function () { simdFn(0, 1, 2) }, TypeError) |
+ break |
+ } |
+ for (var i in values) { |
+ assertSame(simdFn, values[i].__proto__.constructor) |
+ assertSame(simdFn, Object(values[i]).__proto__.constructor) |
rossberg
2015/07/02 13:35:43
assertSame(simdFn.prototype, value[i].__proto__)
bbudge
2015/07/06 23:59:05
Done.
|
+ } |
+} |
+ |
+ |
+function TestType(type, lanes, values) { |
+ for (var i in values) { |
+ assertEquals(type, typeof values[i]) |
+ assertTrue(typeof values[i] === type) |
+ assertTrue(typeof Object(values[i]) === 'object') |
+ assertEquals(null, %_ClassOf(values[i])) |
+ assertEquals("Float32x4", %_ClassOf(Object(values[i]))) |
+ } |
+} |
+ |
+ |
+function TestPrototype(type, lanes, values) { |
+ var simdFn = SIMD[type]; |
+ assertSame(Object.prototype, simdFn.prototype.__proto__) |
+ for (var i in values) { |
+ assertSame(simdFn.prototype, values[i].__proto__) |
+ assertSame(simdFn.prototype, Object(values[i]).__proto__) |
+ } |
+} |
+ |
+ |
+function TestValueOf(type, lanes, values) { |
+ var simdFn = SIMD[type]; |
+ for (var i in values) { |
+ assertTrue(values[i] === Object(values[i]).valueOf()) |
+ assertTrue(values[i] === values[i].valueOf()) |
+ assertTrue(simdFn.prototype.valueOf.call(Object(values[i])) === values[i]) |
+ assertTrue(simdFn.prototype.valueOf.call(values[i]) === values[i]) |
+ } |
+} |
+ |
+ |
+function TestGet(type, lanes, values) { |
+ var simdFn = SIMD[type]; |
+ for (var i in values) { |
+ assertEquals(undefined, values[i].a) |
+ assertEquals(undefined, values[i]["a" + "b"]) |
+ assertEquals(undefined, values[i]["" + "1"]) |
+ assertEquals(undefined, values[i][42]) |
+ } |
+} |
+ |
+ |
+function TestToBoolean(type, lanes, values) { |
+ for (var i in values) { |
+ assertTrue(Boolean(Object(values[i]))) |
+ assertFalse(!Object(values[i])) |
+ assertTrue(Boolean(values[i]).valueOf()) |
+ assertFalse(!values[i]) |
+ assertTrue(!!values[i]) |
+ assertTrue(values[i] && true) |
+ assertFalse(!values[i] && false) |
+ assertTrue(!values[i] || true) |
+ assertEquals(1, values[i] ? 1 : 2) |
+ assertEquals(2, !values[i] ? 1 : 2) |
+ if (!values[i]) assertUnreachable(); |
+ if (values[i]) {} else assertUnreachable(); |
+ } |
+} |
+ |
+ |
+function TestToString(type, lanes, values) { |
+ var simdFn = SIMD[type]; |
+ for (var i in values) { |
+ assertEquals(values[i].toString(), String(values[i])) |
+ assertTrue(isValidSimdString(values[i].toString(), values[i], type, lanes)) |
+ assertTrue( |
+ isValidSimdString(Object(values[i]).toString(), values[i], type, lanes)) |
+ assertTrue(isValidSimdString( |
+ simdFn.prototype.toString.call(values[i]), values[i], type, lanes)) |
+ } |
+} |
+ |
+ |
+function TestToNumber(type, lanes, values) { |
+ for (var i in values) { |
+ assertThrows(function() { Number(Object(values[i])) }, TypeError) |
+ assertThrows(function() { +Object(values[i]) }, TypeError) |
+ assertThrows(function() { Number(values[i]) }, TypeError) |
+ assertThrows(function() { values[i] + 0 }, TypeError) |
+ } |
+} |
+ |
+ |
+function TestEquality(type, lanes, values) { |
+ // Every SIMD value should equal itself, and non-strictly equal its wrapper. |
rossberg
2015/07/02 13:35:43
This is lacking tests that make sure that equality
bbudge
2015/07/06 23:59:05
Added tests for structural equality semantics.
|
+ for (var i in values) { |
+ assertSame(values[i], values[i]) |
+ assertEquals(values[i], values[i]) |
+ assertTrue(Object.is(values[i], values[i])) |
+ assertTrue(values[i] === values[i]) |
+ assertTrue(values[i] == values[i]) |
+ assertFalse(values[i] === Object(values[i])) |
+ assertFalse(Object(values[i]) === values[i]) |
+ assertFalse(values[i] == Object(values[i])) |
+ assertFalse(Object(values[i]) == values[i]) |
+ assertTrue(values[i] === values[i].valueOf()) |
+ assertTrue(values[i].valueOf() === values[i]) |
+ assertTrue(values[i] == values[i].valueOf()) |
+ assertTrue(values[i].valueOf() == values[i]) |
+ assertFalse(Object(values[i]) === Object(values[i])) |
+ assertEquals(Object(values[i]).valueOf(), Object(values[i]).valueOf()) |
+ } |
+ |
+ // SIMD values should not equal other SIMD values. |
+ for (var i = 0; i < values.length; ++i) { |
+ for (var j = i + 1; j < values.length; ++j) { |
+ assertFalse(Object.is(values[i], values[j])) |
+ assertFalse(values[i] === values[j]) |
+ assertFalse(values[i] == values[j]) |
+ } |
+ } |
+ |
+ // SIMD values should not be equal to any other kind of object. |
+ var others = [347, 1.275, NaN, "string", null, undefined, {}, function() {}] |
+ for (var i in values) { |
+ for (var j in others) { |
+ assertFalse(values[i] === others[j]) |
+ assertFalse(others[j] === values[i]) |
+ assertFalse(values[i] == others[j]) |
+ assertFalse(others[j] == values[i]) |
+ } |
+ } |
+} |
+ |
+ |
+function TestComparison(type, lanes, values) { |
+ var a = values[0], b = values[1]; |
+ |
+ function lt() { a < b; } |
+ function gt() { a > b; } |
+ function le() { a <= b; } |
+ function ge() { a >= b; } |
+ function lt_same() { a < a; } |
+ function gt_same() { a > a; } |
+ function le_same() { a <= a; } |
+ function ge_same() { a >= a; } |
+ |
+ var throwFuncs = [lt, gt, le, ge, lt_same, gt_same, le_same, ge_same]; |
+ |
+ for (var f of throwFuncs) { |
+ assertThrows(f, TypeError); |
+ %OptimizeFunctionOnNextCall(f); |
+ assertThrows(f, TypeError); |
+ assertThrows(f, TypeError); |
+ } |
+} |
+ |
+ |
+// Test SIMD value wrapping/boxing over non-builtins. |
+function TestCall(type, lanes, values) { |
+ var simdFn = SIMD[type]; |
+ simdFn.prototype.getThisProto = function () { |
+ return Object.getPrototypeOf(this); |
+ } |
+ for (var i in values) { |
+ assertTrue(values[i].getThisProto() === simdFn.prototype) |
+ } |
+} |
+ |
+ |
+function TestAll() { |
+ var types = [ 'float32x4' ]; |
+ |
+ function lanesForType(typeName) { |
+ return Number.parseInt(typeName[typeName.length - 1]); |
+ } |
+ |
+ for (var i = 0; i < types.length; ++i) { |
+ var type = types[i], |
+ lanes = lanesForType(type); |
+ var values = []; |
+ TestConstructor(type, lanes, values); |
+ TestType(type, lanes, values); |
+ TestPrototype(type, lanes, values); |
+ TestValueOf(type, lanes, values); |
+ TestGet(type, lanes, values); |
+ TestToBoolean(type, lanes, values); |
+ TestToString(type, lanes, values); |
+ TestToNumber(type, lanes, values); |
+ TestEquality(type, lanes, values); |
+ TestComparison(type, lanes, values); |
+ TestCall(type, lanes, values); |
+ } |
+} |
+TestAll() |