1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_2/statements/switch2.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,158 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/** 1.11 + Filename: switch2.js 1.12 + Description: 'Tests the switch statement' 1.13 + 1.14 + http://scopus.mcom.com/bugsplat/show_bug.cgi?id=323696 1.15 + 1.16 + Author: Norris Boyd 1.17 + Date: July 31, 1998 1.18 +*/ 1.19 + 1.20 +var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"'; 1.21 +var VERSION = 'no version'; 1.22 +var TITLE = 'statements: switch'; 1.23 +var BUGNUMBER="323626"; 1.24 + 1.25 +startTest(); 1.26 +writeHeaderToLog("Executing script: switch2.js"); 1.27 +writeHeaderToLog( SECTION + " "+ TITLE); 1.28 + 1.29 +// test defaults not at the end; regression test for a bug that 1.30 +// nearly made it into 4.06 1.31 +function f0(i) { 1.32 + switch(i) { 1.33 + default: 1.34 + case "a": 1.35 + case "b": 1.36 + return "ab*" 1.37 + case "c": 1.38 + return "c"; 1.39 + case "d": 1.40 + return "d"; 1.41 + } 1.42 + return ""; 1.43 +} 1.44 +new TestCase(SECTION, 'switch statement', 1.45 + f0("a"), "ab*"); 1.46 + 1.47 +new TestCase(SECTION, 'switch statement', 1.48 + f0("b"), "ab*"); 1.49 + 1.50 +new TestCase(SECTION, 'switch statement', 1.51 + f0("*"), "ab*"); 1.52 + 1.53 +new TestCase(SECTION, 'switch statement', 1.54 + f0("c"), "c"); 1.55 + 1.56 +new TestCase(SECTION, 'switch statement', 1.57 + f0("d"), "d"); 1.58 + 1.59 +function f1(i) { 1.60 + switch(i) { 1.61 + case "a": 1.62 + case "b": 1.63 + default: 1.64 + return "ab*" 1.65 + case "c": 1.66 + return "c"; 1.67 + case "d": 1.68 + return "d"; 1.69 + } 1.70 + return ""; 1.71 +} 1.72 + 1.73 +new TestCase(SECTION, 'switch statement', 1.74 + f1("a"), "ab*"); 1.75 + 1.76 +new TestCase(SECTION, 'switch statement', 1.77 + f1("b"), "ab*"); 1.78 + 1.79 +new TestCase(SECTION, 'switch statement', 1.80 + f1("*"), "ab*"); 1.81 + 1.82 +new TestCase(SECTION, 'switch statement', 1.83 + f1("c"), "c"); 1.84 + 1.85 +new TestCase(SECTION, 'switch statement', 1.86 + f1("d"), "d"); 1.87 + 1.88 +// Switch on integer; will use TABLESWITCH opcode in C engine 1.89 +function f2(i) { 1.90 + switch (i) { 1.91 + case 0: 1.92 + case 1: 1.93 + return 1; 1.94 + case 2: 1.95 + return 2; 1.96 + } 1.97 + // with no default, control will fall through 1.98 + return 3; 1.99 +} 1.100 + 1.101 +new TestCase(SECTION, 'switch statement', 1.102 + f2(0), 1); 1.103 + 1.104 +new TestCase(SECTION, 'switch statement', 1.105 + f2(1), 1); 1.106 + 1.107 +new TestCase(SECTION, 'switch statement', 1.108 + f2(2), 2); 1.109 + 1.110 +new TestCase(SECTION, 'switch statement', 1.111 + f2(3), 3); 1.112 + 1.113 +// empty switch: make sure expression is evaluated 1.114 +var se = 0; 1.115 +switch (se = 1) { 1.116 +} 1.117 +new TestCase(SECTION, 'switch statement', 1.118 + se, 1); 1.119 + 1.120 +// only default 1.121 +se = 0; 1.122 +switch (se) { 1.123 +default: 1.124 + se = 1; 1.125 +} 1.126 +new TestCase(SECTION, 'switch statement', 1.127 + se, 1); 1.128 + 1.129 +// in loop, break should only break out of switch 1.130 +se = 0; 1.131 +for (var i=0; i < 2; i++) { 1.132 + switch (i) { 1.133 + case 0: 1.134 + case 1: 1.135 + break; 1.136 + } 1.137 + se = 1; 1.138 +} 1.139 +new TestCase(SECTION, 'switch statement', 1.140 + se, 1); 1.141 + 1.142 +// test "fall through" 1.143 +se = 0; 1.144 +i = 0; 1.145 +switch (i) { 1.146 +case 0: 1.147 + se++; 1.148 + /* fall through */ 1.149 +case 1: 1.150 + se++; 1.151 + break; 1.152 +} 1.153 +new TestCase(SECTION, 'switch statement', 1.154 + se, 2); 1.155 +print("hi"); 1.156 + 1.157 +test(); 1.158 + 1.159 +// Needed: tests for evaluation time of case expressions. 1.160 +// This issue was under debate at ECMA, so postponing for now. 1.161 +