test_loop.js (6917B)
1 function 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 18 // load more elaborate version of assert if available 19 try { __loadScript("test_assert.js"); } catch(e) {} 20 21 /*----------------*/ 22 23 function test_while() 24 { 25 var i, c; 26 i = 0; 27 c = 0; 28 while (i < 3) { 29 c++; 30 i++; 31 } 32 assert(c === 3); 33 } 34 35 function test_while_break() 36 { 37 var i, c; 38 i = 0; 39 c = 0; 40 while (i < 3) { 41 c++; 42 if (i == 1) 43 break; 44 i++; 45 } 46 assert(c === 2 && i === 1); 47 } 48 49 function test_do_while() 50 { 51 var i, c; 52 i = 0; 53 c = 0; 54 do { 55 c++; 56 i++; 57 } while (i < 3); 58 assert(c === 3 && i === 3); 59 } 60 61 function test_for() 62 { 63 var i, c; 64 c = 0; 65 for(i = 0; i < 3; i++) { 66 c++; 67 } 68 assert(c === 3 && i === 3); 69 70 c = 0; 71 for(var j = 0; j < 3; j++) { 72 c++; 73 } 74 assert(c === 3 && j === 3); 75 } 76 77 function test_for_in() 78 { 79 var i, tab, a, b; 80 81 tab = []; 82 for(i in {x:1, y: 2}) { 83 tab.push(i); 84 } 85 assert(tab.toString(), "x,y", "for_in"); 86 87 /* prototype chain test */ 88 a = {x:2, y: 2, "1": 3}; 89 b = {"4" : 3 }; 90 Object.setPrototypeOf(a, b); 91 tab = []; 92 for(i in a) { 93 tab.push(i); 94 } 95 assert(tab.toString(), "1,x,y,4", "for_in"); 96 97 /* non enumerable properties hide enumerables ones in the 98 prototype chain */ 99 a = {y: 2, "1": 3}; 100 Object.defineProperty(a, "x", { value: 1 }); 101 b = {"x" : 3 }; 102 Object.setPrototypeOf(a, b); 103 tab = []; 104 for(i in a) { 105 tab.push(i); 106 } 107 assert(tab.toString(), "1,y", "for_in"); 108 109 /* array optimization */ 110 a = []; 111 for(i = 0; i < 10; i++) 112 a.push(i); 113 tab = []; 114 for(i in a) { 115 tab.push(i); 116 } 117 assert(tab.toString(), "0,1,2,3,4,5,6,7,8,9", "for_in"); 118 119 /* iterate with a field */ 120 a={x:0}; 121 tab = []; 122 for(a.x in {x:1, y: 2}) { 123 tab.push(a.x); 124 } 125 assert(tab.toString(), "x,y", "for_in"); 126 127 /* iterate with a variable field */ 128 a=[0]; 129 tab = []; 130 for(a[0] in {x:1, y: 2}) { 131 tab.push(a[0]); 132 } 133 assert(tab.toString(), "x,y", "for_in"); 134 135 /* variable definition in the for in */ 136 tab = []; 137 for(var j in {x:1, y: 2}) { 138 tab.push(j); 139 } 140 assert(tab.toString(), "x,y", "for_in"); 141 142 /* variable assigment in the for in */ 143 tab = []; 144 for(var k = 2 in {x:1, y: 2}) { 145 tab.push(k); 146 } 147 assert(tab.toString(), "x,y", "for_in"); 148 } 149 150 function test_for_in2() 151 { 152 var i; 153 tab = []; 154 for(i in {x:1, y: 2, z:3}) { 155 if (i === "y") 156 continue; 157 tab.push(i); 158 } 159 assert(tab.toString() == "x,z"); 160 161 tab = []; 162 for(i in {x:1, y: 2, z:3}) { 163 if (i === "z") 164 break; 165 tab.push(i); 166 } 167 assert(tab.toString() == "x,y"); 168 } 169 170 function test_for_in_proxy() { 171 let removed_key = ""; 172 let target = {} 173 let proxy = new Proxy(target, { 174 ownKeys: function() { 175 return ["a", "b", "c"]; 176 }, 177 getOwnPropertyDescriptor: function(target, key) { 178 if (removed_key != "" && key == removed_key) 179 return undefined; 180 else 181 return { enumerable: true, configurable: true, value: this[key] }; 182 } 183 }); 184 let str = ""; 185 for(let o in proxy) { 186 str += " " + o; 187 if (o == "a") 188 removed_key = "b"; 189 } 190 assert(str == " a c"); 191 } 192 193 function test_for_break() 194 { 195 var i, c; 196 c = 0; 197 L1: for(i = 0; i < 3; i++) { 198 c++; 199 if (i == 0) 200 continue; 201 while (1) { 202 break L1; 203 } 204 } 205 assert(c === 2 && i === 1); 206 } 207 208 function test_switch1() 209 { 210 var i, a, s; 211 s = ""; 212 for(i = 0; i < 3; i++) { 213 a = "?"; 214 switch(i) { 215 case 0: 216 a = "a"; 217 break; 218 case 1: 219 a = "b"; 220 break; 221 default: 222 a = "c"; 223 break; 224 } 225 s += a; 226 } 227 assert(s === "abc" && i === 3); 228 } 229 230 function test_switch2() 231 { 232 var i, a, s; 233 s = ""; 234 for(i = 0; i < 4; i++) { 235 a = "?"; 236 switch(i) { 237 case 0: 238 a = "a"; 239 break; 240 case 1: 241 a = "b"; 242 break; 243 case 2: 244 continue; 245 default: 246 a = "" + i; 247 break; 248 } 249 s += a; 250 } 251 assert(s === "ab3" && i === 4); 252 } 253 254 function test_try_catch1() 255 { 256 try { 257 throw "hello"; 258 } catch (e) { 259 assert(e, "hello", "catch"); 260 return; 261 } 262 assert(false, "catch"); 263 } 264 265 function test_try_catch2() 266 { 267 var a; 268 try { 269 a = 1; 270 } catch (e) { 271 a = 2; 272 } 273 assert(a, 1, "catch"); 274 } 275 276 function test_try_catch3() 277 { 278 var s; 279 s = ""; 280 try { 281 s += "t"; 282 } catch (e) { 283 s += "c"; 284 } finally { 285 s += "f"; 286 } 287 assert(s, "tf", "catch"); 288 } 289 290 function test_try_catch4() 291 { 292 var s; 293 s = ""; 294 try { 295 s += "t"; 296 throw "c"; 297 } catch (e) { 298 s += e; 299 } finally { 300 s += "f"; 301 } 302 assert(s, "tcf", "catch"); 303 } 304 305 function test_try_catch5() 306 { 307 var s; 308 s = ""; 309 for(;;) { 310 try { 311 s += "t"; 312 break; 313 s += "b"; 314 } finally { 315 s += "f"; 316 } 317 } 318 assert(s, "tf", "catch"); 319 } 320 321 function test_try_catch6() 322 { 323 function f() { 324 try { 325 s += 't'; 326 return 1; 327 } finally { 328 s += "f"; 329 } 330 } 331 var s = ""; 332 assert(f() === 1); 333 assert(s, "tf", "catch6"); 334 } 335 336 function test_try_catch7() 337 { 338 var s; 339 s = ""; 340 341 try { 342 try { 343 s += "t"; 344 throw "a"; 345 } finally { 346 s += "f"; 347 } 348 } catch(e) { 349 s += e; 350 } finally { 351 s += "g"; 352 } 353 assert(s, "tfag", "catch"); 354 } 355 356 function test_try_catch8() 357 { 358 var i, s; 359 360 s = ""; 361 for(var i in {x:1, y:2}) { 362 try { 363 s += i; 364 throw "a"; 365 } catch (e) { 366 s += e; 367 } finally { 368 s += "f"; 369 } 370 } 371 assert(s === "xafyaf"); 372 } 373 374 test_while(); 375 test_while_break(); 376 test_do_while(); 377 test_for(); 378 test_for_break(); 379 test_switch1(); 380 test_switch2(); 381 test_for_in(); 382 test_for_in2(); 383 test_for_in_proxy(); 384 385 test_try_catch1(); 386 test_try_catch2(); 387 test_try_catch3(); 388 test_try_catch4(); 389 test_try_catch5(); 390 test_try_catch6(); 391 test_try_catch7(); 392 test_try_catch8();