Chromium Code Reviews| 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..400952988926395f65b486afd8e2a5624c6f0dc0 | 
| --- /dev/null | 
| +++ b/test/mjsunit/harmony/simd.js | 
| @@ -0,0 +1,281 @@ | 
| +// 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 | 
| +// Flags: --noalways-opt | 
| + | 
| +function isValidSimdString(string, value, type, lanes) { | 
| + var simdFn = SIMD[type], | 
| + parseFn = | 
| + type.indexOf('float') === 0 ? Number.parseFloat : Number.parseInt, | 
| + indexOfOpenParen = string.indexOf('('); | 
| + // Check prefix for correct type name. | 
| + if (string.substr(0, indexOfOpenParen).toUpperCase() !== type.toUpperCase()) | 
| + return false; | 
| + // Remove type name and open parenthesis. | 
| + string = string.substr(indexOfOpenParen + 1); | 
| + 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; | 
| +} | 
| + | 
| + | 
| +// Tests for the global SIMD object. | 
| +function TestSIMDObject() { | 
| + assertSame(typeof SIMD, 'object'); | 
| + assertSame(SIMD.constructor, Object); | 
| + assertSame(Object.getPrototypeOf(SIMD), Object.prototype); | 
| + assertSame(SIMD + "", "[object SIMD]"); | 
| +} | 
| +TestSIMDObject() | 
| + | 
| + | 
| +// Test different forms of constructor calls. This test populates 'values' with | 
| +// a variety of SIMD values as a side effect, which are used by other tests. | 
| +function TestConstructor(type, lanes, values) { | 
| + var simdFn = SIMD[type]; | 
| + assertFalse(Object === simdFn.prototype.constructor) | 
| + assertFalse(simdFn === Object.prototype.constructor) | 
| + assertSame(simdFn, simdFn.prototype.constructor) | 
| + | 
| + // The constructor expects 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(1, 2, 3, 4)); | 
| + values.push(simdFn(0, 0, 0, 0)); | 
| + values.push(simdFn(1, 1, 1, 1)); | 
| + values.push(simdFn(-0, NaN, 0, 0.5)); | 
| + // The constructor expects 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) | 
| + assertSame(simdFn.prototype, values[i].__proto__) | 
| + assertSame(simdFn.prototype, Object(values[i]).__proto__) | 
| + } | 
| +} | 
| + | 
| + | 
| +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. | 
| + 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()) | 
| + } | 
| + | 
| + // Structural equivalence. | 
| + assertTrue(SIMD.float32x4(1, 2, 3, 4) == SIMD.float32x4(1, 2, 3, 4)); | 
| + assertTrue(SIMD.float32x4(1, 2, 3, 4) === SIMD.float32x4(1, 2, 3, 4)); | 
| + assertFalse(SIMD.float32x4(1, 2, 3, 4) == SIMD.float32x4(1, 2, 3, 5)); | 
| + assertFalse(SIMD.float32x4(1, 2, 3, 4) === SIMD.float32x4(1, 2, 3, 5)); | 
| + // Components that are NaNs should not be considered equal though. | 
| + assertFalse(SIMD.float32x4(1, 2, 3, NaN) == SIMD.float32x4(1, 2, 3, NaN)); | 
| + assertFalse(SIMD.float32x4(1, 2, 3, NaN) === SIMD.float32x4(1, 2, 3, NaN)); | 
| 
 
bbudge
2015/07/08 21:38:42
These tests are changed to work for other types in
 
 | 
| + | 
| + // 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 TestSIMDTypes() { | 
| + 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); | 
| + } | 
| +} | 
| +TestSIMDTypes() |