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