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