Debian Codegraph @

rhino/testsrc/tests/js1_2/statements/switch2.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'switch2.js';
7
8 /**
9 Filename: switch2.js
10 Description: 'Tests the switch statement'
11
12 http://scopus.mcom.com/bugsplat/show_bug.cgi?id=323696
13
14 Author: Norris Boyd
15 Date: July 31, 1998
16 */
17
18 var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"';
19 var VERSION = 'no version';
20 var TITLE = 'statements: switch';
21 var BUGNUMBER="323626";
22
23 startTest();
24 writeHeaderToLog("Executing script: switch2.js");
25 writeHeaderToLog( SECTION + " "+ TITLE);
26
27 // test defaults not at the end; regression test for a bug that
28 // nearly made it into 4.06
29 function f0(i) {
30 switch(i) {
31 default:
32 case "a":
33 case "b":
34 return "ab*"
35 case "c":
36 return "c";
37 case "d":
38 return "d";
39 }
40 return "";
41 }
42 new TestCase(SECTION, 'switch statement',
43 f0("a"), "ab*");
44
45 new TestCase(SECTION, 'switch statement',
46 f0("b"), "ab*");
47
48 new TestCase(SECTION, 'switch statement',
49 f0("*"), "ab*");
50
51 new TestCase(SECTION, 'switch statement',
52 f0("c"), "c");
53
54 new TestCase(SECTION, 'switch statement',
55 f0("d"), "d");
56
57 function f1(i) {
58 switch(i) {
59 case "a":
60 case "b":
61 default:
62 return "ab*"
63 case "c":
64 return "c";
65 case "d":
66 return "d";
67 }
68 return "";
69 }
70
71 new TestCase(SECTION, 'switch statement',
72 f1("a"), "ab*");
73
74 new TestCase(SECTION, 'switch statement',
75 f1("b"), "ab*");
76
77 new TestCase(SECTION, 'switch statement',
78 f1("*"), "ab*");
79
80 new TestCase(SECTION, 'switch statement',
81 f1("c"), "c");
82
83 new TestCase(SECTION, 'switch statement',
84 f1("d"), "d");
85
86 // Switch on integer; will use TABLESWITCH opcode in C engine
87 function f2(i) {
88 switch (i) {
89 case 0:
90 case 1:
91 return 1;
92 case 2:
93 return 2;
94 }
95 // with no default, control will fall through
96 return 3;
97 }
98
99 new TestCase(SECTION, 'switch statement',
100 f2(0), 1);
101
102 new TestCase(SECTION, 'switch statement',
103 f2(1), 1);
104
105 new TestCase(SECTION, 'switch statement',
106 f2(2), 2);
107
108 new TestCase(SECTION, 'switch statement',
109 f2(3), 3);
110
111 // empty switch: make sure expression is evaluated
112 var se = 0;
113 switch (se = 1) {
114 }
115 new TestCase(SECTION, 'switch statement',
116 se, 1);
117
118 // only default
119 se = 0;
120 switch (se) {
121 default:
122 se = 1;
123 }
124 new TestCase(SECTION, 'switch statement',
125 se, 1);
126
127 // in loop, break should only break out of switch
128 se = 0;
129 for (var i=0; i < 2; i++) {
130 switch (i) {
131 case 0:
132 case 1:
133 break;
134 }
135 se = 1;
136 }
137 new TestCase(SECTION, 'switch statement',
138 se, 1);
139
140 // test "fall through"
141 se = 0;
142 i = 0;
143 switch (i) {
144 case 0:
145 se++;
146 /* fall through */
147 case 1:
148 se++;
149 break;
150 }
151 new TestCase(SECTION, 'switch statement',
152 se, 2);
153 print("hi");
154
155 test();
156
157 // Needed: tests for evaluation time of case expressions.
158 // This issue was under debate at ECMA, so postponing for now.
159