Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_2 / Statements / while-004.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 JavaScript Engine testing utilities.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communication Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38
39 /**
40  *  File Name:          while-004
41  *  ECMA Section:
42  *  Description:        while statement
43  *
44  *  Author:             christine@netscape.com
45  *  Date:               11 August 1998
46  */
47 var SECTION = "while-004";
48 var VERSION = "ECMA_2";
49 var TITLE   = "while statement";
50 var BUGNUMBER="316725";
51
52 startTest();
53 writeHeaderToLog( SECTION + " "+ TITLE);
54
55 DoWhile_1();
56 DoWhile_2();
57 DoWhile_3();
58 DoWhile_4();
59 DoWhile_5();
60
61 test();
62
63 /**
64  *  Break out of a while by calling return.
65  *
66  *  Tests:  12.6.2 step 6.
67  */
68 function dowhile() {
69   result = "pass";
70
71   while (true) {
72     return result;
73     result = "fail: hit code after return statement";
74     break;
75   }
76 }
77
78 function DoWhile_1() {
79   description = "return statement in a while block";
80
81   result = dowhile();
82
83   new TestCase(
84     SECTION,
85     "DoWhile_1" + description,
86     "pass",
87     result );
88 }
89
90 /**
91  *  While with a labeled continue statement.  Verify that statements
92  *  after the continue statement are not evaluated.
93  *
94  *  Tests: 12.6.2 step 8.
95  *
96  */
97 function DoWhile_2() {
98   var description = "while with a labeled continue statement";
99   var result1 = "pass";
100   var result2 = "fail: did not execute code after loop, but inside label";
101   var i = 0;
102   var j = 0;
103
104 theloop:
105   while( i++ < 10  ) {
106     j++;
107     continue theloop;
108     result1 = "failed:  hit code after continue statement";
109   }
110   result2 = "pass";
111
112   new TestCase(
113     SECTION,
114     "DoWhile_2:  " +description + " - code inside the loop, before the continue should be executed ("+j+")",
115     true,
116     j == 10 );
117
118   new TestCase(
119     SECTION,
120     "DoWhile_2:  " +description +" - code after labeled continue should not be executed",
121     "pass",
122     result1 );
123
124   new TestCase(
125     SECTION,
126     "DoWhile_2:  " +description +" - code after loop but inside label should be executed",
127     "pass",
128     result2 );
129 }
130
131 /**
132  *  While with a labeled break.
133  *
134  */
135 function DoWhile_3() {
136   var description = "while with a labeled break statement";
137   var result1 = "pass";
138   var result2 = "pass";
139   var result3 = "fail: did not get to code after label";
140
141 woohoo: {
142     while( true ) {
143       break woohoo;
144       result1 = "fail: got to code after a break";
145     }
146     result2 = "fail: got to code outside of loop but inside label";
147   }
148
149   result3 = "pass";
150
151   new TestCase(
152     SECTION,
153     "DoWhile_3: " +description +" - verify break out of loop",
154     "pass",
155     result1 );
156
157
158   new TestCase(
159     SECTION,
160     "DoWhile_3: " +description +" - verify break out of label",
161     "pass",
162     result2 );
163
164   new TestCase(
165     SECTION,
166     "DoWhile_3: " +description + " - verify correct exit from label",
167     "pass",
168     result3 );
169 }
170
171
172 /**
173  *  Labled while with an unlabeled break
174  *
175  */
176 function DoWhile_4() {
177   var description = "labeled while with an unlabeled break";
178   var result1 = "pass";
179   var result2 = "pass";
180   var result3 = "fail: did not evaluate statement after label";
181
182 woohooboy: {
183     while( true ) {
184       break woohooboy;
185       result1 = "fail: got to code after the break";
186     }
187     result2 = "fail: broke out of while, but not out of label";
188   }
189   result3 = "pass";
190
191   new TestCase(
192     SECTION,
193     "DoWhile_4: " +description +" - verify break out of while loop",
194     "pass",
195     result1 );
196
197   new TestCase(
198     SECTION,
199     "DoWhile_4: " +description + " - verify break out of label",
200     "pass",
201     result2 );
202
203   new TestCase(
204     SECTION,
205     "DoWhile_4: " +description +" - verify that statements after label are evaluated",
206     "pass",
207     result3 );
208 }
209
210 /**
211  *  in this case, should behave the same way as
212  *
213  *
214  */
215 function DoWhile_5() {
216   var description = "while with a labeled continue statement";
217   var result1 = "pass";
218   var result2 = "fail: did not execute code after loop, but inside label";
219   var i = 0;
220   var j = 0;
221
222 theloop: {
223     j++;
224     while( i++ < 10  ) {
225       continue;
226       result1 = "failed:  hit code after continue statement";
227     }
228     result2 = "pass";
229   }
230
231   new TestCase(
232     SECTION,
233     "DoWhile_5: " +description + " - continue should not execute statements above the loop",
234     true,
235     ( j == 1 ) );
236
237   new TestCase(
238     SECTION,
239     "DoWhile_5: " +description +" - code after labeled continue should not be executed",
240     "pass",
241     result1 );
242
243   new TestCase(
244     SECTION,
245     "DoWhile_5: " +description +" - code after loop but inside label should be executed",
246     "pass",
247     result2 );
248 }
249