js/src/tests/ecma_5/Number/defaultvalue.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommonn.org/licenses/publicdomain/
michael@0 4 */
michael@0 5
michael@0 6 var BUGNUMBER = 645464;
michael@0 7 var summary =
michael@0 8 "[[DefaultValue]] behavior wrong for Number with overridden valueOf/toString";
michael@0 9
michael@0 10 print(BUGNUMBER + ": " + summary);
michael@0 11
michael@0 12 /**************
michael@0 13 * BEGIN TEST *
michael@0 14 **************/
michael@0 15
michael@0 16
michael@0 17 // equality
michael@0 18
michael@0 19 var n = new Number();
michael@0 20 assertEq(n == 0, true);
michael@0 21
michael@0 22 var n2 = new Number();
michael@0 23 n2.valueOf = function() { return 17; };
michael@0 24 assertEq(n2 == 17, true);
michael@0 25
michael@0 26 var n3 = new Number();
michael@0 27 n3.toString = function() { return 42; };
michael@0 28 assertEq(n3 == 0, true);
michael@0 29
michael@0 30 function testEquality()
michael@0 31 {
michael@0 32 var n = new Number();
michael@0 33 assertEq(n == 0, true);
michael@0 34
michael@0 35 var n2 = new Number();
michael@0 36 n2.valueOf = function() { return 17; };
michael@0 37 assertEq(n2 == 17, true);
michael@0 38
michael@0 39 var n3 = new Number();
michael@0 40 n3.toString = function() { return 42; };
michael@0 41 assertEq(n3 == 0, true);
michael@0 42 }
michael@0 43 testEquality();
michael@0 44
michael@0 45
michael@0 46 // addition of Number to number
michael@0 47
michael@0 48 var n = new Number();
michael@0 49 assertEq(n + 5, 5);
michael@0 50
michael@0 51 var n2 = new Number();
michael@0 52 n2.toString = function() { return 9; };
michael@0 53 assertEq(n2 + 3, 3);
michael@0 54
michael@0 55 var n3 = new Number();
michael@0 56 n3.valueOf = function() { return 17; };
michael@0 57 assertEq(n3 + 5, 22);
michael@0 58
michael@0 59 function testNumberAddition()
michael@0 60 {
michael@0 61 var n = new Number();
michael@0 62 assertEq(n + 5, 5);
michael@0 63
michael@0 64 var n2 = new Number();
michael@0 65 n2.toString = function() { return 9; };
michael@0 66 assertEq(n2 + 3, 3);
michael@0 67
michael@0 68 var n3 = new Number();
michael@0 69 n3.valueOf = function() { return 17; };
michael@0 70 assertEq(n3 + 5, 22);
michael@0 71 }
michael@0 72 testNumberAddition();
michael@0 73
michael@0 74
michael@0 75 // addition of Number to Number
michael@0 76
michael@0 77 var n = new Number();
michael@0 78 assertEq(n + n, 0);
michael@0 79
michael@0 80 var n2 = new Number();
michael@0 81 n2.toString = function() { return 5; };
michael@0 82 assertEq(n2 + n2, 0);
michael@0 83
michael@0 84 var n3 = new Number();
michael@0 85 n3.valueOf = function() { return 8.5; };
michael@0 86 assertEq(n3 + n3, 17);
michael@0 87
michael@0 88 function testNonNumberAddition()
michael@0 89 {
michael@0 90 var n = new Number();
michael@0 91 assertEq(n + n, 0);
michael@0 92
michael@0 93 var n2 = new Number();
michael@0 94 n2.toString = function() { return 5; };
michael@0 95 assertEq(n2 + n2, 0);
michael@0 96
michael@0 97 var n3 = new Number();
michael@0 98 n3.valueOf = function() { return 8.5; };
michael@0 99 assertEq(n3 + n3, 17);
michael@0 100 }
michael@0 101 testNonNumberAddition();
michael@0 102
michael@0 103
michael@0 104 // Number as bracketed property name
michael@0 105
michael@0 106 var obj = { 0: 17, 8: 42, 9: 8675309 };
michael@0 107
michael@0 108 var n = new Number();
michael@0 109 assertEq(obj[n], 17);
michael@0 110
michael@0 111 var n2 = new Number();
michael@0 112 n2.valueOf = function() { return 8; }
michael@0 113 assertEq(obj[n2], 17);
michael@0 114
michael@0 115 var n3 = new Number();
michael@0 116 n3.toString = function() { return 9; };
michael@0 117 assertEq(obj[n3], 8675309);
michael@0 118
michael@0 119 function testPropertyNameToNumber()
michael@0 120 {
michael@0 121 var obj = { 0: 17, 8: 42, 9: 8675309 };
michael@0 122
michael@0 123 var n = new Number();
michael@0 124 assertEq(obj[n], 17);
michael@0 125
michael@0 126 var n2 = new Number();
michael@0 127 n2.valueOf = function() { return 8; }
michael@0 128 assertEq(obj[n2], 17);
michael@0 129
michael@0 130 var n3 = new Number();
michael@0 131 n3.toString = function() { return 9; };
michael@0 132 assertEq(obj[n3], 8675309);
michael@0 133 }
michael@0 134 testPropertyNameToNumber();
michael@0 135
michael@0 136
michael@0 137 // Number as property name with |in| operator
michael@0 138
michael@0 139 var n = new Number();
michael@0 140 assertEq(n in { 0: 5 }, true);
michael@0 141
michael@0 142 var n2 = new Number();
michael@0 143 n2.toString = function() { return "baz"; };
michael@0 144 assertEq(n2 in { baz: 42 }, true);
michael@0 145
michael@0 146 var n3 = new Number();
michael@0 147 n3.valueOf = function() { return "quux"; };
michael@0 148 assertEq(n3 in { 0: 17 }, true);
michael@0 149
michael@0 150 function testInOperatorName()
michael@0 151 {
michael@0 152 var n = new Number();
michael@0 153 assertEq(n in { 0: 5 }, true);
michael@0 154
michael@0 155 var n2 = new Number();
michael@0 156 n2.toString = function() { return "baz"; };
michael@0 157 assertEq(n2 in { baz: 42 }, true);
michael@0 158
michael@0 159 var n3 = new Number();
michael@0 160 n3.valueOf = function() { return "quux"; };
michael@0 161 assertEq(n3 in { 0: 17 }, true);
michael@0 162 }
michael@0 163 testInOperatorName();
michael@0 164
michael@0 165 /******************************************************************************/
michael@0 166
michael@0 167 if (typeof reportCompare === "function")
michael@0 168 reportCompare(true, true);
michael@0 169
michael@0 170 print("All tests passed!");

mercurial