| Index: test/mjsunit/equals.js
|
| ===================================================================
|
| --- test/mjsunit/equals.js (revision 0)
|
| +++ test/mjsunit/equals.js (revision 0)
|
| @@ -0,0 +1,114 @@
|
| +// Copyright 2008 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: --allow-natives-syntax
|
| +
|
| +function IS_STRING(arg) { return (typeof(arg) === 'string'); }
|
| +function IS_NUMBER(arg) { return (typeof(arg) === 'number'); }
|
| +function IS_BOOLEAN(arg) { return (typeof(arg) === 'boolean'); }
|
| +function IS_OBJECT(arg) { return %_IsObject(arg); }
|
| +function IS_FUNCTION(arg) { return %_IsFunction(arg); }
|
| +const NO_HINT = 0;
|
| +
|
| +// ECMA-262, section 11.9.1, page 55.
|
| +function EQUALS(x, y) {
|
| + if (IS_STRING(x) && IS_STRING(y)) return %StringEquals(x, y);
|
| +
|
| + // NOTE: We use iteration instead of recursion, because it is
|
| + // difficult to call EQUALS with the correct setting of 'this' in
|
| + // an efficient way.
|
| + while (true) {
|
| + if (IS_NUMBER(x)) {
|
| + if (y == null) return 1; // not equal
|
| + return %NumberEquals(x, %ToNumber(y));
|
| + } else if (IS_STRING(x)) {
|
| + if (IS_STRING(y)) return %StringEquals(x, y);
|
| + if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
|
| + if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
|
| + if (y == null) return 1; // not equal
|
| + y = %ToPrimitive(y, NO_HINT);
|
| + } else if (IS_BOOLEAN(x)) {
|
| + if (IS_BOOLEAN(y)) {
|
| + return %_ObjectEquals(x, y) ? 0 : 1;
|
| + }
|
| + if (y == null) return 1; // not equal
|
| + return %NumberEquals(%ToNumber(x), %ToNumber(y));
|
| + } else if (x == null) {
|
| + // NOTE: This checks for both null and undefined.
|
| + return (y == null) ? 0 : 1;
|
| + } else {
|
| + // x is not a number, boolean, null or undefined.
|
| + if (y == null) return 1; // not equal
|
| + if (IS_OBJECT(y)) {
|
| + return %_ObjectEquals(x, y) ? 0 : 1;
|
| + }
|
| + if (IS_FUNCTION(y)) {
|
| + return %_ObjectEquals(x, y) ? 0 : 1;
|
| + }
|
| +
|
| + x = %ToPrimitive(x, NO_HINT);
|
| + }
|
| + }
|
| +}
|
| +
|
| +var values = [
|
| + 0,
|
| + 1,
|
| + -1,
|
| + NaN,
|
| + function() {return 0;},
|
| + function() {return 1;},
|
| + {},
|
| + 'a',
|
| + 'b',
|
| + 'c',
|
| + {toString: function(){return 'a'}},
|
| + {valueOf: function(){return 1;}},
|
| + {valueOf: function(){return 'b'}},
|
| + [1, 2, 3],
|
| + ['1', '2', '3'],
|
| + {a: 'b'},
|
| + 1.126,
|
| + void 0,
|
| + null,
|
| + undefined,
|
| + 100000000000000000,
|
| + '1.126'
|
| +];
|
| +
|
| +for (var i = 0; i < values.length; i++) {
|
| + for (var j = 0; j < values.length; j++) {
|
| + var x = values[i];
|
| + var y = values[j];
|
| +
|
| + var r1 = EQUALS(x, y); // Reference implementation.
|
| + var r2 = (x == y) ? 0 : 1;
|
| +
|
| + assertEquals(r1, r2, 'Compared ' + x + ' vs ' + y);
|
| + }
|
| +}
|
| +
|
|
|