tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

S12.14_A15.js (2353B)


      1 // Copyright 2009 the Sputnik authors.  All rights reserved.
      2 // This code is governed by the BSD license found in the LICENSE file.
      3 
      4 /*---
      5 info: |
      6    Using "try" with "catch" or "finally" statement within/without a "switch"
      7    statement
      8 es5id: 12.14_A15
      9 description: Insert try/catch/finally to switch statement
     10 ---*/
     11 
     12 // CHECK#1
     13 function SwitchTest1(value){
     14  var result = 0;
     15  try{  
     16    switch(value) {
     17      case 1:
     18        result += 4;
     19        throw result;
     20        break;
     21      default:
     22        result += 32;
     23        break;
     24      case 4:
     25        result += 64;
     26        throw "ex";
     27    }
     28    return result;
     29  }
     30  catch(e){	
     31    if ((value===1)&&(e!==4)) throw new Test262Error('#1.1: Exception ===4. Actual:  Exception ==='+ e  );
     32    if ((value===4)&&(e!=="ex")) throw new Test262Error('#1.2: Exception ==="ex". Actual:  Exception ==='+ e  );
     33  }
     34  finally{
     35    return result;
     36  }
     37 }
     38 if (SwitchTest1(1)!==4) throw new Test262Error('#1.3: SwitchTest1(1)===4. Actual:  SwitchTest1(1)==='+ SwitchTest1(1) );
     39 if (SwitchTest1(4)!==64) throw new Test262Error('#1.4: SwitchTest1(4)===64. Actual:  SwitchTest1(4)==='+ SwitchTest1(4) );
     40 
     41 // CHECK#2
     42 var c2=0;
     43 function SwitchTest2(value){
     44  var result = 0;
     45  switch(value) {
     46    case 0:
     47      try{  
     48        result += 2;
     49        break;
     50      }
     51      finally{
     52        c2=1;
     53      }
     54    case 1:
     55      result += 4;
     56      break;
     57    default:
     58      result += 32;
     59      break;
     60  }
     61  return result;
     62 }
     63 if (SwitchTest2(1)!==4) throw new Test262Error('#2.1: SwitchTest1(1)===4. Actual:  SwitchTest1(1)==='+ SwitchTest1(1) );
     64 if (c2===1) throw new Test262Error('#2.2: Evaluate finally block');
     65 if (SwitchTest2(0)!==2) throw new Test262Error('#2.3: SwitchTest1(0)===2. Actual:  SwitchTest1(0)==='+ SwitchTest1(0) );
     66 if (c2!==1) throw new Test262Error('#2.4: "finally" block must be evaluated');
     67 
     68 // CHECK#3
     69 function SwitchTest3(value){
     70  var result = 0;
     71  switch(value) {
     72    case 0:
     73      try{  
     74        result += 2;
     75        throw "ex";
     76      }
     77      finally{
     78        break;
     79      }
     80    default:
     81      result += 32;
     82      break;
     83  }
     84  return result;
     85 }
     86 try{
     87  var x3=SwitchTest3(0);
     88  if (x3!==2) throw new Test262Error('#3.1: x3===2. Actual: x3==='+x3);
     89 }
     90 catch(e){
     91  throw new Test262Error('#3.2: Catching exception inside function does not lead to throwing exception outside this function');
     92 }
     93 
     94 reportCompare(0, 0);