michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: Filename: switch2.js michael@0: Description: 'Tests the switch statement' michael@0: michael@0: http://scopus.mcom.com/bugsplat/show_bug.cgi?id=323696 michael@0: michael@0: Author: Norris Boyd michael@0: Date: July 31, 1998 michael@0: */ michael@0: michael@0: var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"'; michael@0: var VERSION = 'no version'; michael@0: var TITLE = 'statements: switch'; michael@0: var BUGNUMBER="323626"; michael@0: michael@0: startTest(); michael@0: writeHeaderToLog("Executing script: switch2.js"); michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: // test defaults not at the end; regression test for a bug that michael@0: // nearly made it into 4.06 michael@0: function f0(i) { michael@0: switch(i) { michael@0: default: michael@0: case "a": michael@0: case "b": michael@0: return "ab*" michael@0: case "c": michael@0: return "c"; michael@0: case "d": michael@0: return "d"; michael@0: } michael@0: return ""; michael@0: } michael@0: new TestCase(SECTION, 'switch statement', michael@0: f0("a"), "ab*"); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f0("b"), "ab*"); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f0("*"), "ab*"); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f0("c"), "c"); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f0("d"), "d"); michael@0: michael@0: function f1(i) { michael@0: switch(i) { michael@0: case "a": michael@0: case "b": michael@0: default: michael@0: return "ab*" michael@0: case "c": michael@0: return "c"; michael@0: case "d": michael@0: return "d"; michael@0: } michael@0: return ""; michael@0: } michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f1("a"), "ab*"); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f1("b"), "ab*"); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f1("*"), "ab*"); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f1("c"), "c"); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f1("d"), "d"); michael@0: michael@0: // Switch on integer; will use TABLESWITCH opcode in C engine michael@0: function f2(i) { michael@0: switch (i) { michael@0: case 0: michael@0: case 1: michael@0: return 1; michael@0: case 2: michael@0: return 2; michael@0: } michael@0: // with no default, control will fall through michael@0: return 3; michael@0: } michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f2(0), 1); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f2(1), 1); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f2(2), 2); michael@0: michael@0: new TestCase(SECTION, 'switch statement', michael@0: f2(3), 3); michael@0: michael@0: // empty switch: make sure expression is evaluated michael@0: var se = 0; michael@0: switch (se = 1) { michael@0: } michael@0: new TestCase(SECTION, 'switch statement', michael@0: se, 1); michael@0: michael@0: // only default michael@0: se = 0; michael@0: switch (se) { michael@0: default: michael@0: se = 1; michael@0: } michael@0: new TestCase(SECTION, 'switch statement', michael@0: se, 1); michael@0: michael@0: // in loop, break should only break out of switch michael@0: se = 0; michael@0: for (var i=0; i < 2; i++) { michael@0: switch (i) { michael@0: case 0: michael@0: case 1: michael@0: break; michael@0: } michael@0: se = 1; michael@0: } michael@0: new TestCase(SECTION, 'switch statement', michael@0: se, 1); michael@0: michael@0: // test "fall through" michael@0: se = 0; michael@0: i = 0; michael@0: switch (i) { michael@0: case 0: michael@0: se++; michael@0: /* fall through */ michael@0: case 1: michael@0: se++; michael@0: break; michael@0: } michael@0: new TestCase(SECTION, 'switch statement', michael@0: se, 2); michael@0: print("hi"); michael@0: michael@0: test(); michael@0: michael@0: // Needed: tests for evaluation time of case expressions. michael@0: // This issue was under debate at ECMA, so postponing for now. michael@0: