File indexing completed on 2024-04-28 15:28:38

0001 function isNegativeZero(n)
0002 {
0003   return n == 0 && 1 / n < 0;
0004 }
0005 
0006 // operator !
0007 shouldBeTrue("!undefined");
0008 shouldBeTrue("!null");
0009 shouldBeTrue("!!true");
0010 shouldBeTrue("!false");
0011 shouldBeTrue("!!1");
0012 shouldBeTrue("!0");
0013 shouldBeTrue("!!'a'");
0014 shouldBeTrue("!''");
0015 
0016 // unary plus
0017 shouldBe("+9", "9");
0018 shouldBe("var i = 10; +i", "10");
0019 
0020 // negation
0021 shouldBe("-11", "-11");
0022 shouldBe("var i = 12; -i", "-12");
0023 
0024 // increment
0025 shouldBe("var i = 0; ++i;", "1");
0026 shouldBe("var i = 0; ++i; i", "1");
0027 shouldBe("var i = 0; i++;", "0");
0028 shouldBe("var i = 0; i++; i", "1");
0029 shouldBe("var i = true; i++", "1");
0030 shouldBe("var i = true; i++; i", "2");
0031 
0032 // decrement
0033 shouldBe("var i = 0; --i;", "-1");
0034 shouldBe("var i = 0; --i; i", "-1");
0035 shouldBe("var i = 0; i--;", "0");
0036 shouldBe("var i = 0; i--; i", "-1");
0037 shouldBe("var i = true; i--", "1");
0038 shouldBe("var i = true; i--; i", "0");
0039 
0040 // bitwise operators
0041 shouldBe("~0", "-1");
0042 shouldBe("~1", "-2");
0043 shouldBe("~NaN", "-1");
0044 shouldBe("~Infinity", "-1");
0045 shouldBe("~Math.pow(2, 33)", "-1"); // 32 bit overflow
0046 shouldBe("~(Math.pow(2, 32) + Math.pow(2, 31) + 2)",
0047          "2147483645"); // a signedness issue
0048 shouldBe("~null", "-1");
0049 shouldBe("3 & 1", "1");
0050 shouldBe("2 | true", "3");
0051 shouldBe("'3' ^ 1", "2");
0052 shouldBe("3^4&5", "7");
0053 shouldBe("2|4^5", "3");
0054 
0055 shouldBe("1 << 2", "4");
0056 shouldBe("8 >> 1", "4");
0057 shouldBe("1 >> 2", "0");
0058 shouldBe("-8 >> 24", "-1");
0059 shouldBe("8 >>> 2", "2");
0060 shouldBe("-8 >>> 24", "255");
0061 shouldBe("(-2200000000 >> 1) << 1", "2094967296");
0062 shouldBe("Infinity >> 1", "0");
0063 shouldBe("Infinity << 1", "0");
0064 shouldBe("Infinity >>> 1", "0");
0065 shouldBe("NaN >> 1", "0");
0066 shouldBe("NaN << 1", "0");
0067 shouldBe("NaN >>> 1", "0");
0068 shouldBe("8.1 >> 1", "4");
0069 shouldBe("8.1 << 1", "16");
0070 shouldBe("8.1 >>> 1", "4");
0071 shouldBe("8.9 >> 1", "4");
0072 shouldBe("8.9 << 1", "16");
0073 shouldBe("8.9 >>> 1", "4");
0074 shouldBe("Math.pow(2, 32) >> 1", "0");
0075 shouldBe("Math.pow(2, 32) << 1", "0");
0076 shouldBe("Math.pow(2, 32) >>> 1", "0");
0077 
0078 // addition
0079 shouldBe("1+2", "3");
0080 shouldBe("'a'+'b'", "'ab'");
0081 shouldBe("'a'+2", "'a2'");
0082 shouldBe("'2'+'-1'", "'2-1'");
0083 shouldBe("true+'a'", "'truea'");
0084 shouldBe("'a' + null", "'anull'");
0085 shouldBe("true+1", "2");
0086 shouldBe("false+null", "0");
0087 
0088 // substraction
0089 shouldBe("1-3", "-2");
0090 shouldBe("isNaN('a'-3)", "true");
0091 shouldBe("'3'-'-1'", "4");
0092 shouldBe("'4'-2", "2");
0093 shouldBe("true-false", "1");
0094 shouldBe("false-1", "-1");
0095 shouldBe("null-true", "-1");
0096 
0097 // multiplication
0098 shouldBe("2 * 3", "6");
0099 shouldBe("true * 3", "3");
0100 shouldBe("2 * '3'", "6");
0101 shouldBeTrue("isNaN(NaN * 2)");
0102 shouldBeTrue("isNaN(2 * NaN)");
0103 shouldBeTrue("isNaN(NaN * NaN)");
0104 shouldBeTrue("isNaN(Infinity * 0)");
0105 shouldBe("Infinity * Infinity", "Infinity");
0106 shouldBe("Infinity * 1", "Infinity");
0107 shouldBe("Infinity * -1", "-Infinity");
0108 
0109 // division
0110 shouldBe("6 / 4", "1.5");
0111 shouldBe("true / false", "Infinity");
0112 shouldBe("'6' / '2'", "3");
0113 shouldBeTrue("isNaN('x' / 1)");
0114 shouldBeTrue("isNaN(1 / NaN)");
0115 shouldBeTrue("isNaN(Infinity / Infinity)");
0116 shouldBe("Infinity / 0", "Infinity");
0117 shouldBe("-Infinity / 0", "-Infinity");
0118 shouldBe("Infinity / 1", "Infinity");
0119 shouldBe("-Infinity / 1", "-Infinity");
0120 shouldBeTrue("1 / Infinity == +0");
0121 shouldBeTrue("isNegativeZero(1 / -Infinity)");
0122 shouldBeTrue("isNaN(0/0)");
0123 shouldBeTrue("0 / 1 === 0");
0124 shouldBeTrue("isNegativeZero(0 / -1)");
0125 shouldBe("1 / 0", "Infinity");
0126 shouldBe("-1 / 0", "-Infinity");
0127 
0128 // modulo
0129 shouldBe("6 % 4", "2");
0130 shouldBe("'-6' % 4", "-2");
0131 shouldBe("6 % -4", "2");
0132 shouldBe("6 % 1.5", "0");
0133 shouldBe("6.5 % 4", "2.5");
0134 shouldBeTrue("isNaN(6 % NaN)");
0135 shouldBeTrue("isNaN(NaN % 2)");
0136 shouldBeTrue("isNaN(NaN % NaN)");
0137 shouldBeTrue("isNaN(Infinity % 2)");
0138 shouldBeTrue("isNaN(6 % 0)");
0139 shouldBeTrue("isNaN(Infinity % 0)");
0140 shouldBe("6 % Infinity", "6");
0141 shouldBe("0 % 1", "0");
0142 shouldBeTrue("isNegativeZero(-0 % 1)");
0143 
0144 shouldBe("2==2", "true");
0145 shouldBe("1==2", "false");
0146 
0147 shouldBe("1<2", "true");
0148 shouldBe("1<=2", "true");
0149 shouldBe("2<1", "false");
0150 shouldBe("2<=1", "false");
0151 
0152 shouldBe("2>1", "true");
0153 shouldBe("2>=1", "true");
0154 shouldBe("1>=2", "false");
0155 shouldBe("1>2", "false");
0156 
0157 shouldBe("'abc' == 'abc'", "true");
0158 shouldBe("'abc' != 'xyz'", "true");
0159 shouldBeTrue("true == true");
0160 shouldBeTrue("false == false");
0161 shouldBeTrue("true != false");
0162 shouldBeTrue("'a' != null");
0163 shouldBeTrue("'a' != undefined");
0164 shouldBeTrue("null == null");
0165 shouldBeTrue("null == undefined");
0166 shouldBeTrue("undefined == undefined");
0167 shouldBeTrue("NaN != NaN");
0168 shouldBeTrue("true != undefined");
0169 shouldBeTrue("true != null");
0170 shouldBeTrue("false != undefined");
0171 shouldBeTrue("false != null");
0172 shouldBeTrue("'0' == 0");
0173 shouldBeTrue("1 == '1'");
0174 shouldBeTrue("NaN != NaN");
0175 shouldBeTrue("NaN != 0");
0176 shouldBeTrue("NaN != undefined");
0177 shouldBeTrue("true == 1");
0178 shouldBeTrue("true != 2");
0179 shouldBeTrue("1 == true");
0180 shouldBeTrue("false == 0");
0181 shouldBeTrue("0 == false");
0182 
0183 shouldBe("'abc' < 'abx'", "true");
0184 shouldBe("'abc' < 'abcd'", "true");
0185 shouldBe("'abc' < 'abc'", "false");
0186 shouldBe("'abcd' < 'abcd'", "false");
0187 shouldBe("'abx' < 'abc'", "false");
0188 
0189 shouldBe("'abc' <= 'abc'", "true");
0190 shouldBe("'abc' <= 'abx'", "true");
0191 shouldBe("'abx' <= 'abc'", "false");
0192 shouldBe("'abcd' <= 'abc'", "false");
0193 shouldBe("'abc' <= 'abcd'", "true");
0194 
0195 shouldBe("'abc' > 'abx'", "false");
0196 shouldBe("'abc' > 'abc'", "false");
0197 shouldBe("'abcd' > 'abc'", "true");
0198 shouldBe("'abx' > 'abc'", "true");
0199 shouldBe("'abc' > 'abcd'", "false");
0200 
0201 shouldBe("'abc' >= 'abc'", "true");
0202 shouldBe("'abcd' >= 'abc'", "true");
0203 shouldBe("'abx' >= 'abc'", "true");
0204 shouldBe("'abc' >= 'abx'", "false");
0205 shouldBe("'abc' >= 'abx'", "false");
0206 shouldBe("'abc' >= 'abcd'", "false");
0207 
0208 // mixed strings and numbers - results validated in NS+moz+IE5
0209 shouldBeFalse("'abc' <= 0"); // #35246
0210 shouldBeTrue("'' <= 0");
0211 shouldBeTrue("' ' <= 0");
0212 shouldBeTrue("null <= 0");
0213 shouldBeFalse("0 <= 'abc'");
0214 shouldBeTrue("0 <= ''");
0215 shouldBeTrue("0 <= null");
0216 shouldBeTrue("null <= null");
0217 shouldBeTrue("6 < '52'");
0218 shouldBeTrue("6 < '72'"); // #36087
0219 shouldBe("NaN < 0", "false");
0220 shouldBe("NaN <= 0", "false");
0221 shouldBe("NaN > 0", "false");
0222 shouldBe("NaN >= 0", "false");
0223 
0224 // strict comparison ===
0225 shouldBe("0 === false", "false");
0226 shouldBe("undefined === undefined", "true");
0227 shouldBe("null === null", "true");
0228 shouldBe("NaN === NaN", "false");
0229 shouldBe("0.0 === 0", "true");
0230 shouldBe("'abc' === 'abc'", "true");
0231 shouldBe("'a' === 'x'", "false");
0232 shouldBe("1 === '1'", "false");
0233 shouldBe("'1' === 1", "false");
0234 shouldBe("true === true", "true");
0235 shouldBe("false === false", "true");
0236 shouldBe("true === false", "false");
0237 shouldBe("Math === Math", "true");
0238 shouldBe("Math === Boolean", "false");
0239 shouldBe("Infinity === Infinity", "true");
0240 
0241 // !==
0242 shouldBe("0 !== 0", "false");
0243 shouldBe("0 !== 1", "true");
0244 
0245 shouldBe("typeof undefined", "'undefined'");
0246 shouldBe("typeof null", "'object'");
0247 shouldBe("typeof true", "'boolean'");
0248 shouldBe("typeof false", "'boolean'");
0249 shouldBe("typeof 1", "'number'");
0250 shouldBe("typeof 'a'", "'string'");
0251 shouldBe("typeof shouldBe", "'function'");
0252 shouldBe("typeof Number.NaN", "'number'");
0253 
0254 shouldBe("11 && 22", "22");
0255 shouldBe("null && true", "null");
0256 shouldBe("11 || 22", "11");
0257 shouldBe("null || 'a'", "'a'");
0258 
0259 shouldBeUndefined("void 1");
0260 
0261 shouldBeTrue("1 in [1, 2]");
0262 shouldBeFalse("3 in [1, 2]");
0263 shouldBeTrue("'a' in { a:1, b:2 }");
0264 
0265 // instanceof
0266 // Those 2 lines don't parse in Netscape...
0267 shouldBe("(new Boolean()) instanceof Boolean", "true");
0268 shouldBe("(new Boolean()) instanceof Number", "false");
0269 
0270 // Regression test for a  ?: bug that showed up during FB work
0271 function testCond() {
0272     // Need to be in function for var opt
0273     var i = 42;
0274     var j = (i != 42) ? i : 0;
0275 
0276     if (i == 42)
0277         testPassed("Test for ?: not messing up variable...");
0278     else
0279         testFailed("Test for ?: not messing up variable...");
0280 }
0281 
0282 testCond();
0283 
0284 // Parens in some references..
0285 shouldBe("var x = 0; ++(x); x", "1");
0286 shouldBe("var x = 0; (x)++; x", "1");
0287 
0288 // # 173202, and variants
0289 shouldBe("null == 0", "false");
0290 shouldBe("undefined == 0", "false");
0291 shouldBe("false == 0", "true");
0292 shouldBe("true == 0", "false");
0293 shouldBe("true == 1", "true");
0294 
0295 shouldBe("0 == null", "false");
0296 shouldBe("0 == undefined", "false");
0297 shouldBe("0 == false", "true");
0298 shouldBe("0 == true", "false");
0299 shouldBe("1 == true", "true");
0300 
0301 shouldBe("var i = 1; i += 1", "2");
0302 shouldBe("var i = 1; i -= 1", "0");
0303 shouldBe("var i = 2; i *= 3", "6");
0304 shouldBe("var i = 3; i /= 2", "1.5");
0305 shouldBe("var i = 7; i %= 3", "1");
0306 shouldBe("var i = 7; i <<= 1", "14");
0307 shouldBe("var i = 8; i >>= 1", "4");
0308 shouldBe("var i = -8; i >>>= 24", "255");
0309 shouldBe("var i = 3; i &= 5", "1");
0310 shouldBe("var i = 3; i |= 5", "7");
0311 shouldBe("var i = 3; i ^= 5", "6");
0312 
0313 debug("Done.");