michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: * File Name: forin-001.js michael@0: * ECMA Section: michael@0: * Description: The forin-001 statement michael@0: * michael@0: * Verify that the property name is assigned to the property on the left michael@0: * hand side of the for...in expression. michael@0: * michael@0: * Author: christine@netscape.com michael@0: * Date: 28 August 1998 michael@0: */ michael@0: var SECTION = "forin-001"; michael@0: var VERSION = "ECMA_2"; michael@0: var TITLE = "The for...in statement"; michael@0: var BUGNUMBER="330890"; michael@0: var BUGNUMBER="http://scopus.mcom.com/bugsplat/show_bug.cgi?id=344855"; michael@0: michael@0: startTest(); michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: ForIn_1( { length:4, company:"netscape", year:2000, 0:"zero" } ); michael@0: ForIn_2( { length:4, company:"netscape", year:2000, 0:"zero" } ); michael@0: ForIn_3( { length:4, company:"netscape", year:2000, 0:"zero" } ); michael@0: michael@0: // ForIn_6({ length:4, company:"netscape", year:2000, 0:"zero" }); michael@0: // ForIn_7({ length:4, company:"netscape", year:2000, 0:"zero" }); michael@0: ForIn_8({ length:4, company:"netscape", year:2000, 0:"zero" }); michael@0: michael@0: test(); michael@0: michael@0: /** michael@0: * Verify that the left side argument is evaluated with every iteration. michael@0: * Verify that the name of each property of the object is assigned to a michael@0: * a property. michael@0: * michael@0: */ michael@0: function ForIn_1( object ) { michael@0: PropertyArray = new Array(); michael@0: ValueArray = new Array(); michael@0: michael@0: for ( PropertyArray[PropertyArray.length] in object ) { michael@0: ValueArray[ValueArray.length] = michael@0: object[PropertyArray[PropertyArray.length-1]]; michael@0: } michael@0: michael@0: for ( var i = 0; i < PropertyArray.length; i++ ) { michael@0: new TestCase( michael@0: SECTION, michael@0: "object[" + PropertyArray[i] +"]", michael@0: object[PropertyArray[i]], michael@0: ValueArray[i] michael@0: ); michael@0: } michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "object.length", michael@0: PropertyArray.length, michael@0: object.length ); michael@0: } michael@0: michael@0: /** michael@0: * Similar to ForIn_1, except it should increment the counter variable michael@0: * every time the left hand expression is evaluated. michael@0: */ michael@0: function ForIn_2( object ) { michael@0: PropertyArray = new Array(); michael@0: ValueArray = new Array(); michael@0: var i = 0; michael@0: michael@0: for ( PropertyArray[i++] in object ) { michael@0: ValueArray[ValueArray.length] = michael@0: object[PropertyArray[PropertyArray.length-1]]; michael@0: } michael@0: michael@0: for ( i = 0; i < PropertyArray.length; i++ ) { michael@0: new TestCase( michael@0: SECTION, michael@0: "object[" + PropertyArray[i] +"]", michael@0: object[PropertyArray[i]], michael@0: ValueArray[i] michael@0: ); michael@0: } michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "object.length", michael@0: PropertyArray.length, michael@0: object.length ); michael@0: } michael@0: michael@0: /** michael@0: * Break out of a for...in loop michael@0: * michael@0: * michael@0: */ michael@0: function ForIn_3( object ) { michael@0: var checkBreak = "pass"; michael@0: var properties = new Array(); michael@0: var values = new Array(); michael@0: michael@0: for ( properties[properties.length] in object ) { michael@0: values[values.length] = object[properties[properties.length-1]]; michael@0: break; michael@0: checkBreak = "fail"; michael@0: } michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "check break out of for...in", michael@0: "pass", michael@0: checkBreak ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "properties.length", michael@0: 1, michael@0: properties.length ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "object["+properties[0]+"]", michael@0: values[0], michael@0: object[properties[0]] ); michael@0: } michael@0: michael@0: /** michael@0: * Break out of a labeled for...in loop. michael@0: */ michael@0: function ForIn_4( object ) { michael@0: var result1 = 0; michael@0: var result2 = 0; michael@0: var result3 = 0; michael@0: var result4 = 0; michael@0: var i = 0; michael@0: var property = new Array(); michael@0: michael@0: butterbean: { michael@0: result1++; michael@0: michael@0: for ( property[i++] in object ) { michael@0: result2++; michael@0: break; michael@0: result4++; michael@0: } michael@0: result3++; michael@0: } michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify labeled statement is only executed once", michael@0: true, michael@0: result1 == 1 ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify statements in for loop are evaluated", michael@0: true, michael@0: result2 == i ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify break out of labeled for...in loop", michael@0: true, michael@0: result4 == 0 ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify break out of labeled block", michael@0: true, michael@0: result3 == 0 ); michael@0: } michael@0: michael@0: /** michael@0: * Labeled break out of a labeled for...in loop. michael@0: */ michael@0: function ForIn_5 (object) { michael@0: var result1 = 0; michael@0: var result2 = 0; michael@0: var result3 = 0; michael@0: var result4 = 0; michael@0: var i = 0; michael@0: var property = new Array(); michael@0: michael@0: bigredbird: { michael@0: result1++; michael@0: for ( property[i++] in object ) { michael@0: result2++; michael@0: break bigredbird; michael@0: result4++; michael@0: } michael@0: result3++; michael@0: } michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify labeled statement is only executed once", michael@0: true, michael@0: result1 == 1 ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify statements in for loop are evaluated", michael@0: true, michael@0: result2 == i ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify break out of labeled for...in loop", michael@0: true, michael@0: result4 == 0 ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify break out of labeled block", michael@0: true, michael@0: result3 == 0 ); michael@0: } michael@0: michael@0: /** michael@0: * Labeled continue from a labeled for...in loop michael@0: */ michael@0: function ForIn_7( object ) { michael@0: var result1 = 0; michael@0: var result2 = 0; michael@0: var result3 = 0; michael@0: var result4 = 0; michael@0: var i = 0; michael@0: var property = new Array(); michael@0: michael@0: bigredbird: michael@0: for ( property[i++] in object ) { michael@0: result2++; michael@0: continue bigredbird; michael@0: result4++; michael@0: } michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify statements in for loop are evaluated", michael@0: true, michael@0: result2 == i ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify break out of labeled for...in loop", michael@0: true, michael@0: result4 == 0 ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "verify break out of labeled block", michael@0: true, michael@0: result3 == 1 ); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * continue in a for...in loop michael@0: * michael@0: */ michael@0: function ForIn_8( object ) { michael@0: var checkBreak = "pass"; michael@0: var properties = new Array(); michael@0: var values = new Array(); michael@0: michael@0: for ( properties[properties.length] in object ) { michael@0: values[values.length] = object[properties[properties.length-1]]; michael@0: break; michael@0: checkBreak = "fail"; michael@0: } michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "check break out of for...in", michael@0: "pass", michael@0: checkBreak ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "properties.length", michael@0: 1, michael@0: properties.length ); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "object["+properties[0]+"]", michael@0: values[0], michael@0: object[properties[0]] ); michael@0: } michael@0: