Home | History | Annotate | Download | only in statements
      1 /* The contents of this file are subject to the Netscape Public
      2  * License Version 1.1 (the "License"); you may not use this file
      3  * except in compliance with the License. You may obtain a copy of
      4  * the License at http://www.mozilla.org/NPL/
      5  *
      6  * Software distributed under the License is distributed on an "AS
      7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      8  * implied. See the License for the specific language governing
      9  * rights and limitations under the License.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     14  * The Initial Developer of the Original Code is Netscape Communications
     15  * Corporation. Portions created by Netscape are
     16  * Copyright (C) 1998 Netscape Communications Corporation. All
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 /**
     23 	Filename:     switch2.js
     24 	Description:  'Tests the switch statement'
     25 
     26     http://scopus.mcom.com/bugsplat/show_bug.cgi?id=323696
     27 
     28 	Author:       Norris Boyd
     29 	Date:         July 31, 1998
     30 */
     31 
     32 	var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"';
     33 	var VERSION = 'no version';
     34     startTest();
     35 	var TITLE   = 'statements: switch';
     36 	var BUGNUMBER="323626";
     37 
     38 	writeHeaderToLog("Executing script: switch2.js");
     39 	writeHeaderToLog( SECTION + " "+ TITLE);
     40 
     41 	var count = 0;
     42 	var testcases = new Array();
     43 
     44 	// test defaults not at the end; regression test for a bug that
     45 	// nearly made it into 4.06
     46 	function f0(i) {
     47 	    switch(i) {
     48 		default:
     49 		case "a":
     50 		case "b":
     51 		    return "ab*"
     52 		case "c":
     53 		    return "c";
     54 		case "d":
     55 		    return "d";
     56 	    }
     57 	    return "";
     58 	}
     59 	testcases[count++] = new TestCase(SECTION, 'switch statement',
     60 	                                  f0("a"), "ab*");
     61 
     62 	testcases[count++] = new TestCase(SECTION, 'switch statement',
     63 	                                  f0("b"), "ab*");
     64 
     65 	testcases[count++] = new TestCase(SECTION, 'switch statement',
     66 	                                  f0("*"), "ab*");
     67 
     68 	testcases[count++] = new TestCase(SECTION, 'switch statement',
     69 	                                  f0("c"), "c");
     70 
     71 	testcases[count++] = new TestCase(SECTION, 'switch statement',
     72 	                                  f0("d"), "d");
     73 
     74 	function f1(i) {
     75 	    switch(i) {
     76 		case "a":
     77 		case "b":
     78 		default:
     79 		    return "ab*"
     80 		case "c":
     81 		    return "c";
     82 		case "d":
     83 		    return "d";
     84 	    }
     85 	    return "";
     86 	}
     87 
     88 	testcases[count++] = new TestCase(SECTION, 'switch statement',
     89 	                                  f1("a"), "ab*");
     90 
     91 	testcases[count++] = new TestCase(SECTION, 'switch statement',
     92 	                                  f1("b"), "ab*");
     93 
     94 	testcases[count++] = new TestCase(SECTION, 'switch statement',
     95 	                                  f1("*"), "ab*");
     96 
     97 	testcases[count++] = new TestCase(SECTION, 'switch statement',
     98 	                                  f1("c"), "c");
     99 
    100 	testcases[count++] = new TestCase(SECTION, 'switch statement',
    101 	                                  f1("d"), "d");
    102 
    103 	// Switch on integer; will use TABLESWITCH opcode in C engine
    104 	function f2(i) {
    105 	    switch (i) {
    106 	        case 0:
    107 	        case 1:
    108                     return 1;
    109 	        case 2:
    110  	            return 2;
    111 	    }
    112 	    // with no default, control will fall through
    113 	    return 3;
    114 	}
    115 
    116 	testcases[count++] = new TestCase(SECTION, 'switch statement',
    117 	                                  f2(0), 1);
    118 
    119 	testcases[count++] = new TestCase(SECTION, 'switch statement',
    120 	                                  f2(1), 1);
    121 
    122 	testcases[count++] = new TestCase(SECTION, 'switch statement',
    123 	                                  f2(2), 2);
    124 
    125 	testcases[count++] = new TestCase(SECTION, 'switch statement',
    126 	                                  f2(3), 3);
    127 
    128 	// empty switch: make sure expression is evaluated
    129 	var se = 0;
    130 	switch (se = 1) {
    131 	}
    132 	testcases[count++] = new TestCase(SECTION, 'switch statement',
    133 	                                  se, 1);
    134 
    135 	// only default
    136 	se = 0;
    137 	switch (se) {
    138 	    default:
    139   	        se = 1;
    140 	}
    141 	testcases[count++] = new TestCase(SECTION, 'switch statement',
    142 	                                  se, 1);
    143 
    144 	// in loop, break should only break out of switch
    145 	se = 0;
    146 	for (var i=0; i < 2; i++) {
    147 	    switch (i) {
    148 	        case 0:
    149 	        case 1:
    150 	            break;
    151 	    }
    152 	    se = 1;
    153 	}
    154 	testcases[count++] = new TestCase(SECTION, 'switch statement',
    155 	                                  se, 1);
    156 
    157 	// test "fall through"
    158 	se = 0;
    159 	i = 0;
    160 	switch (i) {
    161 	    case 0:
    162 	        se++;
    163 		/* fall through */
    164 	    case 1:
    165 	        se++;
    166 	        break;
    167 	}
    168 	testcases[count++] = new TestCase(SECTION, 'switch statement',
    169 	                                  se, 2);
    170 
    171     test();
    172 
    173 	// Needed: tests for evaluation time of case expressions.
    174 	// This issue was under debate at ECMA, so postponing for now.
    175 
    176 	function test()	{
    177 	    writeLineToLog("hi");
    178 	   for ( tc=0; tc < testcases.length; tc++ ) {
    179 	        testcases[tc].passed = writeTestCaseResult(
    180 	        testcases[tc].expect,
    181 	        testcases[tc].actual,
    182 	        testcases[tc].description +" = "+
    183 	        testcases[tc].actual );
    184 	        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
    185 	   }
    186 	   stopTest();
    187 	   return ( testcases );
    188 	}
    189