summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_2/Statements/forin-001.js
blob: 215de6c4c8e6f6de5b516903a9eedb574b979691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


/**
 *  File Name:          forin-001.js
 *  ECMA Section:
 *  Description:        The forin-001 statement
 *
 *  Verify that the property name is assigned to the property on the left
 *  hand side of the for...in expression.
 *
 *  Author:             christine@netscape.com
 *  Date:               28 August 1998
 */
var SECTION = "forin-001";
var VERSION = "ECMA_2";
var TITLE   = "The for...in  statement";
var BUGNUMBER="330890";
var BUGNUMBER="http://scopus.mcom.com/bugsplat/show_bug.cgi?id=344855";

startTest();
writeHeaderToLog( SECTION + " "+ TITLE);

ForIn_1( { length:4, company:"netscape", year:2000, 0:"zero" } );
ForIn_2( { length:4, company:"netscape", year:2000, 0:"zero" } );
ForIn_3( { length:4, company:"netscape", year:2000, 0:"zero" } );

//    ForIn_6({ length:4, company:"netscape", year:2000, 0:"zero" });
//    ForIn_7({ length:4, company:"netscape", year:2000, 0:"zero" });
ForIn_8({ length:4, company:"netscape", year:2000, 0:"zero" });

test();

/**
 *  Verify that the left side argument is evaluated with every iteration.
 *  Verify that the name of each property of the object is assigned to a
 *  a property.
 *
 */
function ForIn_1( object ) {
  PropertyArray = new Array();
  ValueArray = new Array();

  for ( PropertyArray[PropertyArray.length] in object ) {
    ValueArray[ValueArray.length] =
      object[PropertyArray[PropertyArray.length-1]];
  }

  for ( var i = 0; i < PropertyArray.length; i++ ) {
    new TestCase(
      SECTION,
      "object[" + PropertyArray[i] +"]",
      object[PropertyArray[i]],
      ValueArray[i]
      );
  }

  new TestCase(
    SECTION,
    "object.length",
    PropertyArray.length,
    object.length );
}

/**
 *  Similar to ForIn_1, except it should increment the counter variable
 *  every time the left hand expression is evaluated.
 */
function ForIn_2( object ) {
  PropertyArray = new Array();
  ValueArray = new Array();
  var i = 0;

  for ( PropertyArray[i++] in object ) {
    ValueArray[ValueArray.length] =
      object[PropertyArray[PropertyArray.length-1]];
  }

  for ( i = 0; i < PropertyArray.length; i++ ) {
    new TestCase(
      SECTION,
      "object[" + PropertyArray[i] +"]",
      object[PropertyArray[i]],
      ValueArray[i]
      );
  }

  new TestCase(
    SECTION,
    "object.length",
    PropertyArray.length,
    object.length );
}

/**
 *  Break out of a for...in loop
 *
 *
 */
function ForIn_3( object ) {
  var checkBreak = "pass";
  var properties = new Array();
  var values = new Array();

  for ( properties[properties.length] in object ) {
    values[values.length] = object[properties[properties.length-1]];
    break;
    checkBreak = "fail";
  }

  new TestCase(
    SECTION,
    "check break out of for...in",
    "pass",
    checkBreak );

  new TestCase(
    SECTION,
    "properties.length",
    1,
    properties.length );

  new TestCase(
    SECTION,
    "object["+properties[0]+"]",
    values[0],
    object[properties[0]] );
}

/**
 *  Break out of a labeled for...in loop.
 */
function ForIn_4( object ) {
  var result1 = 0;
  var result2 = 0;
  var result3 = 0;
  var result4 = 0;
  var i = 0;
  var property = new Array();

butterbean: {
    result1++;

    for ( property[i++] in object ) {
      result2++;
      break;
      result4++;
    }
    result3++;
  }

  new TestCase(
    SECTION,
    "verify labeled statement is only executed once",
    true,
    result1 == 1 );

  new TestCase(
    SECTION,
    "verify statements in for loop are evaluated",
    true,
    result2 == i );

  new TestCase(
    SECTION,
    "verify break out of labeled for...in loop",
    true,
    result4 == 0 );

  new TestCase(
    SECTION,
    "verify break out of labeled block",
    true,
    result3 == 0 );
}

/**
 *  Labeled break out of a labeled for...in loop.
 */
function ForIn_5 (object) {
  var result1 = 0;
  var result2 = 0;
  var result3 = 0;
  var result4 = 0;
  var i = 0;
  var property = new Array();

bigredbird: {
    result1++;
    for ( property[i++] in object ) {
      result2++;
      break bigredbird;
      result4++;
    }
    result3++;
  }

  new TestCase(
    SECTION,
    "verify labeled statement is only executed once",
    true,
    result1 == 1 );

  new TestCase(
    SECTION,
    "verify statements in for loop are evaluated",
    true,
    result2 == i );

  new TestCase(
    SECTION,
    "verify break out of labeled for...in loop",
    true,
    result4 == 0 );

  new TestCase(
    SECTION,
    "verify break out of labeled block",
    true,
    result3 == 0 );
}

/**
 *  Labeled continue from a labeled for...in loop
 */
function ForIn_7( object ) {
  var result1 = 0;
  var result2 = 0;
  var result3 = 0;
  var result4 = 0;
  var i = 0;
  var property = new Array();

bigredbird:
  for ( property[i++] in object ) {
    result2++;
    continue bigredbird;
    result4++;
  }

  new TestCase(
    SECTION,
    "verify statements in for loop are evaluated",
    true,
    result2 == i );

  new TestCase(
    SECTION,
    "verify break out of labeled for...in loop",
    true,
    result4 == 0 );

  new TestCase(
    SECTION,
    "verify break out of labeled block",
    true,
    result3 == 1 );
}


/**
 *  continue in a for...in loop
 *
 */
function ForIn_8( object ) {
  var checkBreak = "pass";
  var properties = new Array();
  var values = new Array();

  for ( properties[properties.length] in object ) {
    values[values.length] = object[properties[properties.length-1]];
    break;
    checkBreak = "fail";
  }

  new TestCase(
    SECTION,
    "check break out of for...in",
    "pass",
    checkBreak );

  new TestCase(
    SECTION,
    "properties.length",
    1,
    properties.length );

  new TestCase(
    SECTION,
    "object["+properties[0]+"]",
    values[0],
    object[properties[0]] );
}