1function assert(actual, expected, message) { 2 if (arguments.length == 1) 3 expected = true; 4 5 if (actual === expected) 6 return; 7 8 if (actual !== null && expected !== null 9 && typeof actual == 'object' && typeof expected == 'object' 10 && actual.toString() === expected.toString()) 11 return; 12 13 throw Error("assertion failed: got |" + actual + "|" + 14 ", expected |" + expected + "|" + 15 (message ? " (" + message + ")" : "")); 16} 17 18function assert_throws(expected_error, func) 19{ 20 var err = false; 21 try { 22 func(); 23 } catch(e) { 24 err = true; 25 if (!(e instanceof expected_error)) { 26 throw Error("unexpected exception type"); 27 } 28 } 29 if (!err) { 30 throw Error("expected exception"); 31 } 32} 33 34// load more elaborate version of assert if available 35try { __loadScript("test_assert.js"); } catch(e) {} 36 37/*----------------*/ 38 39function test_op1() 40{ 41 var r, a; 42 r = 1 + 2; 43 assert(r, 3, "1 + 2 === 3"); 44 45 r = 1 - 2; 46 assert(r, -1, "1 - 2 === -1"); 47 48 r = -1; 49 assert(r, -1, "-1 === -1"); 50 51 r = +2; 52 assert(r, 2, "+2 === 2"); 53 54 r = 2 * 3; 55 assert(r, 6, "2 * 3 === 6"); 56 57 r = 4 / 2; 58 assert(r, 2, "4 / 2 === 2"); 59 60 r = 4 % 3; 61 assert(r, 1, "4 % 3 === 3"); 62 63 r = 4 << 2; 64 assert(r, 16, "4 << 2 === 16"); 65 66 r = 1 << 0; 67 assert(r, 1, "1 << 0 === 1"); 68 69 r = 1 << 31; 70 assert(r, -2147483648, "1 << 31 === -2147483648"); 71 72 r = 1 << 32; 73 assert(r, 1, "1 << 32 === 1"); 74 75 r = (1 << 31) < 0; 76 assert(r, true, "(1 << 31) < 0 === true"); 77 78 r = -4 >> 1; 79 assert(r, -2, "-4 >> 1 === -2"); 80 81 r = -4 >>> 1; 82 assert(r, 0x7ffffffe, "-4 >>> 1 === 0x7ffffffe"); 83 84 r = 1 & 1; 85 assert(r, 1, "1 & 1 === 1"); 86 87 r = 0 | 1; 88 assert(r, 1, "0 | 1 === 1"); 89 90 r = 1 ^ 1; 91 assert(r, 0, "1 ^ 1 === 0"); 92 93 r = ~1; 94 assert(r, -2, "~1 === -2"); 95 96 r = !1; 97 assert(r, false, "!1 === false"); 98 99 assert((1 < 2), true, "(1 < 2) === true"); 100 101 assert((2 > 1), true, "(2 > 1) === true"); 102 103 assert(('b' > 'a'), true, "('b' > 'a') === true"); 104 105 assert(2 ** 8, 256, "2 ** 8 === 256"); 106} 107 108function test_cvt() 109{ 110 assert((NaN | 0) === 0); 111 assert((Infinity | 0) === 0); 112 assert(((-Infinity) | 0) === 0); 113 assert(("12345" | 0) === 12345); 114 assert(("0x12345" | 0) === 0x12345); 115 assert(((4294967296 * 3 - 4) | 0) === -4); 116 117 assert(("12345" >>> 0) === 12345); 118 assert(("0x12345" >>> 0) === 0x12345); 119 assert((NaN >>> 0) === 0); 120 assert((Infinity >>> 0) === 0); 121 assert(((-Infinity) >>> 0) === 0); 122 assert(((4294967296 * 3 - 4) >>> 0) === (4294967296 - 4)); 123} 124 125function test_eq() 126{ 127 assert(null == undefined); 128 assert(undefined == null); 129 assert(true == 1); 130 assert(0 == false); 131 assert("" == 0); 132 assert("123" == 123); 133 assert("122" != 123); 134 assert((new Number(1)) == 1); 135 assert(2 == (new Number(2))); 136 assert((new String("abc")) == "abc"); 137 assert({} != "abc"); 138} 139 140function test_inc_dec() 141{ 142 var a, r; 143 144 a = 1; 145 r = a++; 146 assert(r === 1 && a === 2, true, "++"); 147 148 a = 1; 149 r = ++a; 150 assert(r === 2 && a === 2, true, "++"); 151 152 a = 1; 153 r = a--; 154 assert(r === 1 && a === 0, true, "--"); 155 156 a = 1; 157 r = --a; 158 assert(r === 0 && a === 0, true, "--"); 159 160 a = {x:true}; 161 a.x++; 162 assert(a.x, 2, "++"); 163 164 a = {x:true}; 165 a.x--; 166 assert(a.x, 0, "--"); 167 168 a = [true]; 169 a[0]++; 170 assert(a[0], 2, "++"); 171 172 a = {x:true}; 173 r = a.x++; 174 assert(r === 1 && a.x === 2, true, "++"); 175 176 a = {x:true}; 177 r = a.x--; 178 assert(r === 1 && a.x === 0, true, "--"); 179 180 a = [true]; 181 r = a[0]++; 182 assert(r === 1 && a[0] === 2, true, "++"); 183 184 a = [true]; 185 r = a[0]--; 186 assert(r === 1 && a[0] === 0, true, "--"); 187} 188 189function F(x) 190{ 191 this.x = x; 192} 193 194function test_op2() 195{ 196 var a, b; 197 a = new Object; 198 a.x = 1; 199 assert(a.x, 1, "new"); 200 b = new F(2); 201 assert(b.x, 2, "new"); 202 203 a = {x : 2}; 204 assert(("x" in a), true, "in"); 205 assert(("y" in a), false, "in"); 206 207 a = {}; 208 assert((a instanceof Object), true, "instanceof"); 209 assert((a instanceof String), false, "instanceof"); 210 211 assert((typeof 1), "number", "typeof"); 212 assert((typeof Object), "function", "typeof"); 213 assert((typeof null), "object", "typeof"); 214 assert((typeof unknown_var), "undefined", "typeof"); 215 216 a = {x: 1, if: 2, async: 3}; 217 assert(a.if === 2); 218 assert(a.async === 3); 219} 220 221function test_delete() 222{ 223 var a, err; 224 225 a = {x: 1, y: 1}; 226 assert((delete a.x), true, "delete"); 227 assert(("x" in a), false, "delete"); 228 229 /* the following are not tested by test262 */ 230 assert(delete "abc"[100], true); 231 232 err = false; 233 try { 234 delete null.a; 235 } catch(e) { 236 err = (e instanceof TypeError); 237 } 238 assert(err, true, "delete"); 239 240 err = false; 241 try { 242 a = { f() { delete super.a; } }; 243 a.f(); 244 } catch(e) { 245 err = (e instanceof ReferenceError); 246 } 247 assert(err, true, "delete"); 248} 249 250function test_prototype() 251{ 252 var f = function f() { }; 253 assert(f.prototype.constructor, f, "prototype"); 254 255 var g = function g() { }; 256 /* QuickJS bug */ 257 Object.defineProperty(g, "prototype", { writable: false }); 258 assert(g.prototype.constructor, g, "prototype"); 259} 260 261function test_arguments() 262{ 263 function f2() { 264 assert(arguments.length, 2, "arguments"); 265 assert(arguments[0], 1, "arguments"); 266 assert(arguments[1], 3, "arguments"); 267 } 268 f2(1, 3); 269} 270 271function test_class() 272{ 273 var o; 274 class C { 275 constructor() { 276 this.x = 10; 277 } 278 f() { 279 return 1; 280 } 281 static F() { 282 return -1; 283 } 284 get y() { 285 return 12; 286 } 287 }; 288 class D extends C { 289 constructor() { 290 super(); 291 this.z = 20; 292 } 293 g() { 294 return 2; 295 } 296 static G() { 297 return -2; 298 } 299 h() { 300 return super.f(); 301 } 302 static H() { 303 return super["F"](); 304 } 305 } 306 307 assert(C.F() === -1); 308 assert(Object.getOwnPropertyDescriptor(C.prototype, "y").get.name === "get y"); 309 310 o = new C(); 311 assert(o.f() === 1); 312 assert(o.x === 10); 313 314 assert(D.F() === -1); 315 assert(D.G() === -2); 316 assert(D.H() === -1); 317 318 o = new D(); 319 assert(o.f() === 1); 320 assert(o.g() === 2); 321 assert(o.x === 10); 322 assert(o.z === 20); 323 assert(o.h() === 1); 324 325 /* test class name scope */ 326 var E1 = class E { static F() { return E; } }; 327 assert(E1 === E1.F()); 328}; 329 330function test_template() 331{ 332 var a, b; 333 b = 123; 334 a = `abc${b}d`; 335 assert(a, "abc123d"); 336 337 a = String.raw `abc${b}d`; 338 assert(a, "abc123d"); 339 340 a = "aaa"; 341 b = "bbb"; 342 assert(`aaa${a, b}ccc`, "aaabbbccc"); 343} 344 345function test_template_skip() 346{ 347 var a = "Bar"; 348 var { b = `${a + `a${a}` }baz` } = {}; 349 assert(b, "BaraBarbaz"); 350} 351 352function test_object_literal() 353{ 354 var x = 0, get = 1, set = 2; async = 3; 355 a = { get: 2, set: 3, async: 4 }; 356 assert(JSON.stringify(a), '{"get":2,"set":3,"async":4}'); 357 358 a = { x, get, set, async }; 359 assert(JSON.stringify(a), '{"x":0,"get":1,"set":2,"async":3}'); 360} 361 362function test_regexp_skip() 363{ 364 var a, b; 365 [a, b = /abc\(/] = [1]; 366 assert(a === 1); 367 368 [a, b =/abc\(/] = [2]; 369 assert(a === 2); 370} 371 372function test_labels() 373{ 374 do x: { break x; } while(0); 375 if (1) 376 x: { break x; } 377 else 378 x: { break x; } 379 with ({}) x: { break x; }; 380 while (0) x: { break x; }; 381} 382 383function test_destructuring() 384{ 385 function * g () { return 0; }; 386 var [x] = g(); 387 assert(x, void 0); 388} 389 390function test_spread() 391{ 392 var x; 393 x = [1, 2, ...[3, 4]]; 394 assert(x.toString(), "1,2,3,4"); 395 396 x = [ ...[ , ] ]; 397 assert(Object.getOwnPropertyNames(x).toString(), "0,length"); 398} 399 400function test_function_length() 401{ 402 assert( ((a, b = 1, c) => {}).length, 1); 403 assert( (([a,b]) => {}).length, 1); 404 assert( (({a,b}) => {}).length, 1); 405 assert( ((c, [a,b] = 1, d) => {}).length, 1); 406} 407 408function test_argument_scope() 409{ 410 var f; 411 var c = "global"; 412 413 f = function(a = eval("var arguments")) {}; 414 assert_throws(SyntaxError, f); 415 416 f = function(a = eval("1"), b = arguments[0]) { return b; }; 417 assert(f(12), 12); 418 419 f = function(a, b = arguments[0]) { return b; }; 420 assert(f(12), 12); 421 422 f = function(a, b = () => arguments) { return b; }; 423 assert(f(12)()[0], 12); 424 425 f = function(a = eval("1"), b = () => arguments) { return b; }; 426 assert(f(12)()[0], 12); 427 428 (function() { 429 "use strict"; 430 f = function(a = this) { return a; }; 431 assert(f.call(123), 123); 432 433 f = function f(a = f) { return a; }; 434 assert(f(), f); 435 436 f = function f(a = eval("f")) { return a; }; 437 assert(f(), f); 438 })(); 439 440 f = (a = eval("var c = 1"), probe = () => c) => { 441 var c = 2; 442 assert(c, 2); 443 assert(probe(), 1); 444 } 445 f(); 446 447 f = (a = eval("var arguments = 1"), probe = () => arguments) => { 448 var arguments = 2; 449 assert(arguments, 2); 450 assert(probe(), 1); 451 } 452 f(); 453 454 f = function f(a = eval("var c = 1"), b = c, probe = () => c) { 455 assert(b, 1); 456 assert(c, 1); 457 assert(probe(), 1) 458 } 459 f(); 460 461 assert(c, "global"); 462 f = function f(a, b = c, probe = () => c) { 463 eval("var c = 1"); 464 assert(c, 1); 465 assert(b, "global"); 466 assert(probe(), "global") 467 } 468 f(); 469 assert(c, "global"); 470 471 f = function f(a = eval("var c = 1"), probe = (d = eval("c")) => d) { 472 assert(probe(), 1) 473 } 474 f(); 475} 476 477function test_function_expr_name() 478{ 479 var f; 480 481 /* non strict mode test : assignment to the function name silently 482 fails */ 483 484 f = function myfunc() { 485 myfunc = 1; 486 return myfunc; 487 }; 488 assert(f(), f); 489 490 f = function myfunc() { 491 myfunc = 1; 492 (() => { 493 myfunc = 1; 494 })(); 495 return myfunc; 496 }; 497 assert(f(), f); 498 499 f = function myfunc() { 500 eval("myfunc = 1"); 501 return myfunc; 502 }; 503 assert(f(), f); 504 505 /* strict mode test : assignment to the function name raises a 506 TypeError exception */ 507 508 f = function myfunc() { 509 "use strict"; 510 myfunc = 1; 511 }; 512 assert_throws(TypeError, f); 513 514 f = function myfunc() { 515 "use strict"; 516 (() => { 517 myfunc = 1; 518 })(); 519 }; 520 assert_throws(TypeError, f); 521 522 f = function myfunc() { 523 "use strict"; 524 eval("myfunc = 1"); 525 }; 526 assert_throws(TypeError, f); 527} 528 529test_op1(); 530test_cvt(); 531test_eq(); 532test_inc_dec(); 533test_op2(); 534test_delete(); 535test_prototype(); 536test_arguments(); 537test_class(); 538test_template(); 539test_template_skip(); 540test_object_literal(); 541test_regexp_skip(); 542test_labels(); 543test_destructuring(); 544test_spread(); 545test_function_length(); 546test_argument_scope(); 547test_function_expr_name(); 548