Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_2 / Statements / forin-001.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:          forin-001.js
41  *  ECMA Section:
42  *  Description:        The forin-001 statement
43  *
44  *  Verify that the property name is assigned to the property on the left
45  *  hand side of the for...in expression.
46  *
47  *  Author:             christine@netscape.com
48  *  Date:               28 August 1998
49  */
50 var SECTION = "forin-001";
51 var VERSION = "ECMA_2";
52 var TITLE   = "The for...in  statement";
53 var BUGNUMBER="330890";
54 var BUGNUMBER="http://scopus.mcom.com/bugsplat/show_bug.cgi?id=344855";
55
56 startTest();
57 writeHeaderToLog( SECTION + " "+ TITLE);
58
59 ForIn_1( { length:4, company:"netscape", year:2000, 0:"zero" } );
60 ForIn_2( { length:4, company:"netscape", year:2000, 0:"zero" } );
61 ForIn_3( { length:4, company:"netscape", year:2000, 0:"zero" } );
62
63 //    ForIn_6({ length:4, company:"netscape", year:2000, 0:"zero" });
64 //    ForIn_7({ length:4, company:"netscape", year:2000, 0:"zero" });
65 ForIn_8({ length:4, company:"netscape", year:2000, 0:"zero" });
66
67 test();
68
69 /**
70  *  Verify that the left side argument is evaluated with every iteration.
71  *  Verify that the name of each property of the object is assigned to a
72  *  a property.
73  *
74  */
75 function ForIn_1( object ) {
76   PropertyArray = new Array();
77   ValueArray = new Array();
78
79   for ( PropertyArray[PropertyArray.length] in object ) {
80     ValueArray[ValueArray.length] =
81       object[PropertyArray[PropertyArray.length-1]];
82   }
83
84   for ( var i = 0; i < PropertyArray.length; i++ ) {
85     new TestCase(
86       SECTION,
87       "object[" + PropertyArray[i] +"]",
88       object[PropertyArray[i]],
89       ValueArray[i]
90       );
91   }
92
93   new TestCase(
94     SECTION,
95     "object.length",
96     PropertyArray.length,
97     object.length );
98 }
99
100 /**
101  *  Similar to ForIn_1, except it should increment the counter variable
102  *  every time the left hand expression is evaluated.
103  */
104 function ForIn_2( object ) {
105   PropertyArray = new Array();
106   ValueArray = new Array();
107   var i = 0;
108
109   for ( PropertyArray[i++] in object ) {
110     ValueArray[ValueArray.length] =
111       object[PropertyArray[PropertyArray.length-1]];
112   }
113
114   for ( i = 0; i < PropertyArray.length; i++ ) {
115     new TestCase(
116       SECTION,
117       "object[" + PropertyArray[i] +"]",
118       object[PropertyArray[i]],
119       ValueArray[i]
120       );
121   }
122
123   new TestCase(
124     SECTION,
125     "object.length",
126     PropertyArray.length,
127     object.length );
128 }
129
130 /**
131  *  Break out of a for...in loop
132  *
133  *
134  */
135 function ForIn_3( object ) {
136   var checkBreak = "pass";
137   var properties = new Array();
138   var values = new Array();
139
140   for ( properties[properties.length] in object ) {
141     values[values.length] = object[properties[properties.length-1]];
142     break;
143     checkBreak = "fail";
144   }
145
146   new TestCase(
147     SECTION,
148     "check break out of for...in",
149     "pass",
150     checkBreak );
151
152   new TestCase(
153     SECTION,
154     "properties.length",
155     1,
156     properties.length );
157
158   new TestCase(
159     SECTION,
160     "object["+properties[0]+"]",
161     values[0],
162     object[properties[0]] );
163 }
164
165 /**
166  *  Break out of a labeled for...in loop.
167  */
168 function ForIn_4( object ) {
169   var result1 = 0;
170   var result2 = 0;
171   var result3 = 0;
172   var result4 = 0;
173   var i = 0;
174   var property = new Array();
175
176 butterbean: {
177     result1++;
178
179     for ( property[i++] in object ) {
180       result2++;
181       break;
182       result4++;
183     }
184     result3++;
185   }
186
187   new TestCase(
188     SECTION,
189     "verify labeled statement is only executed once",
190     true,
191     result1 == 1 );
192
193   new TestCase(
194     SECTION,
195     "verify statements in for loop are evaluated",
196     true,
197     result2 == i );
198
199   new TestCase(
200     SECTION,
201     "verify break out of labeled for...in loop",
202     true,
203     result4 == 0 );
204
205   new TestCase(
206     SECTION,
207     "verify break out of labeled block",
208     true,
209     result3 == 0 );
210 }
211
212 /**
213  *  Labeled break out of a labeled for...in loop.
214  */
215 function ForIn_5 (object) {
216   var result1 = 0;
217   var result2 = 0;
218   var result3 = 0;
219   var result4 = 0;
220   var i = 0;
221   var property = new Array();
222
223 bigredbird: {
224     result1++;
225     for ( property[i++] in object ) {
226       result2++;
227       break bigredbird;
228       result4++;
229     }
230     result3++;
231   }
232
233   new TestCase(
234     SECTION,
235     "verify labeled statement is only executed once",
236     true,
237     result1 == 1 );
238
239   new TestCase(
240     SECTION,
241     "verify statements in for loop are evaluated",
242     true,
243     result2 == i );
244
245   new TestCase(
246     SECTION,
247     "verify break out of labeled for...in loop",
248     true,
249     result4 == 0 );
250
251   new TestCase(
252     SECTION,
253     "verify break out of labeled block",
254     true,
255     result3 == 0 );
256 }
257
258 /**
259  *  Labeled continue from a labeled for...in loop
260  */
261 function ForIn_7( object ) {
262   var result1 = 0;
263   var result2 = 0;
264   var result3 = 0;
265   var result4 = 0;
266   var i = 0;
267   var property = new Array();
268
269 bigredbird:
270   for ( property[i++] in object ) {
271     result2++;
272     continue bigredbird;
273     result4++;
274   }
275
276   new TestCase(
277     SECTION,
278     "verify statements in for loop are evaluated",
279     true,
280     result2 == i );
281
282   new TestCase(
283     SECTION,
284     "verify break out of labeled for...in loop",
285     true,
286     result4 == 0 );
287
288   new TestCase(
289     SECTION,
290     "verify break out of labeled block",
291     true,
292     result3 == 1 );
293 }
294
295
296 /**
297  *  continue in a for...in loop
298  *
299  */
300 function ForIn_8( object ) {
301   var checkBreak = "pass";
302   var properties = new Array();
303   var values = new Array();
304
305   for ( properties[properties.length] in object ) {
306     values[values.length] = object[properties[properties.length-1]];
307     break;
308     checkBreak = "fail";
309   }
310
311   new TestCase(
312     SECTION,
313     "check break out of for...in",
314     "pass",
315     checkBreak );
316
317   new TestCase(
318     SECTION,
319     "properties.length",
320     1,
321     properties.length );
322
323   new TestCase(
324     SECTION,
325     "object["+properties[0]+"]",
326     values[0],
327     object[properties[0]] );
328 }
329