1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_2/Statements/forin-001.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,297 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/** 1.11 + * File Name: forin-001.js 1.12 + * ECMA Section: 1.13 + * Description: The forin-001 statement 1.14 + * 1.15 + * Verify that the property name is assigned to the property on the left 1.16 + * hand side of the for...in expression. 1.17 + * 1.18 + * Author: christine@netscape.com 1.19 + * Date: 28 August 1998 1.20 + */ 1.21 +var SECTION = "forin-001"; 1.22 +var VERSION = "ECMA_2"; 1.23 +var TITLE = "The for...in statement"; 1.24 +var BUGNUMBER="330890"; 1.25 +var BUGNUMBER="http://scopus.mcom.com/bugsplat/show_bug.cgi?id=344855"; 1.26 + 1.27 +startTest(); 1.28 +writeHeaderToLog( SECTION + " "+ TITLE); 1.29 + 1.30 +ForIn_1( { length:4, company:"netscape", year:2000, 0:"zero" } ); 1.31 +ForIn_2( { length:4, company:"netscape", year:2000, 0:"zero" } ); 1.32 +ForIn_3( { length:4, company:"netscape", year:2000, 0:"zero" } ); 1.33 + 1.34 +// ForIn_6({ length:4, company:"netscape", year:2000, 0:"zero" }); 1.35 +// ForIn_7({ length:4, company:"netscape", year:2000, 0:"zero" }); 1.36 +ForIn_8({ length:4, company:"netscape", year:2000, 0:"zero" }); 1.37 + 1.38 +test(); 1.39 + 1.40 +/** 1.41 + * Verify that the left side argument is evaluated with every iteration. 1.42 + * Verify that the name of each property of the object is assigned to a 1.43 + * a property. 1.44 + * 1.45 + */ 1.46 +function ForIn_1( object ) { 1.47 + PropertyArray = new Array(); 1.48 + ValueArray = new Array(); 1.49 + 1.50 + for ( PropertyArray[PropertyArray.length] in object ) { 1.51 + ValueArray[ValueArray.length] = 1.52 + object[PropertyArray[PropertyArray.length-1]]; 1.53 + } 1.54 + 1.55 + for ( var i = 0; i < PropertyArray.length; i++ ) { 1.56 + new TestCase( 1.57 + SECTION, 1.58 + "object[" + PropertyArray[i] +"]", 1.59 + object[PropertyArray[i]], 1.60 + ValueArray[i] 1.61 + ); 1.62 + } 1.63 + 1.64 + new TestCase( 1.65 + SECTION, 1.66 + "object.length", 1.67 + PropertyArray.length, 1.68 + object.length ); 1.69 +} 1.70 + 1.71 +/** 1.72 + * Similar to ForIn_1, except it should increment the counter variable 1.73 + * every time the left hand expression is evaluated. 1.74 + */ 1.75 +function ForIn_2( object ) { 1.76 + PropertyArray = new Array(); 1.77 + ValueArray = new Array(); 1.78 + var i = 0; 1.79 + 1.80 + for ( PropertyArray[i++] in object ) { 1.81 + ValueArray[ValueArray.length] = 1.82 + object[PropertyArray[PropertyArray.length-1]]; 1.83 + } 1.84 + 1.85 + for ( i = 0; i < PropertyArray.length; i++ ) { 1.86 + new TestCase( 1.87 + SECTION, 1.88 + "object[" + PropertyArray[i] +"]", 1.89 + object[PropertyArray[i]], 1.90 + ValueArray[i] 1.91 + ); 1.92 + } 1.93 + 1.94 + new TestCase( 1.95 + SECTION, 1.96 + "object.length", 1.97 + PropertyArray.length, 1.98 + object.length ); 1.99 +} 1.100 + 1.101 +/** 1.102 + * Break out of a for...in loop 1.103 + * 1.104 + * 1.105 + */ 1.106 +function ForIn_3( object ) { 1.107 + var checkBreak = "pass"; 1.108 + var properties = new Array(); 1.109 + var values = new Array(); 1.110 + 1.111 + for ( properties[properties.length] in object ) { 1.112 + values[values.length] = object[properties[properties.length-1]]; 1.113 + break; 1.114 + checkBreak = "fail"; 1.115 + } 1.116 + 1.117 + new TestCase( 1.118 + SECTION, 1.119 + "check break out of for...in", 1.120 + "pass", 1.121 + checkBreak ); 1.122 + 1.123 + new TestCase( 1.124 + SECTION, 1.125 + "properties.length", 1.126 + 1, 1.127 + properties.length ); 1.128 + 1.129 + new TestCase( 1.130 + SECTION, 1.131 + "object["+properties[0]+"]", 1.132 + values[0], 1.133 + object[properties[0]] ); 1.134 +} 1.135 + 1.136 +/** 1.137 + * Break out of a labeled for...in loop. 1.138 + */ 1.139 +function ForIn_4( object ) { 1.140 + var result1 = 0; 1.141 + var result2 = 0; 1.142 + var result3 = 0; 1.143 + var result4 = 0; 1.144 + var i = 0; 1.145 + var property = new Array(); 1.146 + 1.147 +butterbean: { 1.148 + result1++; 1.149 + 1.150 + for ( property[i++] in object ) { 1.151 + result2++; 1.152 + break; 1.153 + result4++; 1.154 + } 1.155 + result3++; 1.156 + } 1.157 + 1.158 + new TestCase( 1.159 + SECTION, 1.160 + "verify labeled statement is only executed once", 1.161 + true, 1.162 + result1 == 1 ); 1.163 + 1.164 + new TestCase( 1.165 + SECTION, 1.166 + "verify statements in for loop are evaluated", 1.167 + true, 1.168 + result2 == i ); 1.169 + 1.170 + new TestCase( 1.171 + SECTION, 1.172 + "verify break out of labeled for...in loop", 1.173 + true, 1.174 + result4 == 0 ); 1.175 + 1.176 + new TestCase( 1.177 + SECTION, 1.178 + "verify break out of labeled block", 1.179 + true, 1.180 + result3 == 0 ); 1.181 +} 1.182 + 1.183 +/** 1.184 + * Labeled break out of a labeled for...in loop. 1.185 + */ 1.186 +function ForIn_5 (object) { 1.187 + var result1 = 0; 1.188 + var result2 = 0; 1.189 + var result3 = 0; 1.190 + var result4 = 0; 1.191 + var i = 0; 1.192 + var property = new Array(); 1.193 + 1.194 +bigredbird: { 1.195 + result1++; 1.196 + for ( property[i++] in object ) { 1.197 + result2++; 1.198 + break bigredbird; 1.199 + result4++; 1.200 + } 1.201 + result3++; 1.202 + } 1.203 + 1.204 + new TestCase( 1.205 + SECTION, 1.206 + "verify labeled statement is only executed once", 1.207 + true, 1.208 + result1 == 1 ); 1.209 + 1.210 + new TestCase( 1.211 + SECTION, 1.212 + "verify statements in for loop are evaluated", 1.213 + true, 1.214 + result2 == i ); 1.215 + 1.216 + new TestCase( 1.217 + SECTION, 1.218 + "verify break out of labeled for...in loop", 1.219 + true, 1.220 + result4 == 0 ); 1.221 + 1.222 + new TestCase( 1.223 + SECTION, 1.224 + "verify break out of labeled block", 1.225 + true, 1.226 + result3 == 0 ); 1.227 +} 1.228 + 1.229 +/** 1.230 + * Labeled continue from a labeled for...in loop 1.231 + */ 1.232 +function ForIn_7( object ) { 1.233 + var result1 = 0; 1.234 + var result2 = 0; 1.235 + var result3 = 0; 1.236 + var result4 = 0; 1.237 + var i = 0; 1.238 + var property = new Array(); 1.239 + 1.240 +bigredbird: 1.241 + for ( property[i++] in object ) { 1.242 + result2++; 1.243 + continue bigredbird; 1.244 + result4++; 1.245 + } 1.246 + 1.247 + new TestCase( 1.248 + SECTION, 1.249 + "verify statements in for loop are evaluated", 1.250 + true, 1.251 + result2 == i ); 1.252 + 1.253 + new TestCase( 1.254 + SECTION, 1.255 + "verify break out of labeled for...in loop", 1.256 + true, 1.257 + result4 == 0 ); 1.258 + 1.259 + new TestCase( 1.260 + SECTION, 1.261 + "verify break out of labeled block", 1.262 + true, 1.263 + result3 == 1 ); 1.264 +} 1.265 + 1.266 + 1.267 +/** 1.268 + * continue in a for...in loop 1.269 + * 1.270 + */ 1.271 +function ForIn_8( object ) { 1.272 + var checkBreak = "pass"; 1.273 + var properties = new Array(); 1.274 + var values = new Array(); 1.275 + 1.276 + for ( properties[properties.length] in object ) { 1.277 + values[values.length] = object[properties[properties.length-1]]; 1.278 + break; 1.279 + checkBreak = "fail"; 1.280 + } 1.281 + 1.282 + new TestCase( 1.283 + SECTION, 1.284 + "check break out of for...in", 1.285 + "pass", 1.286 + checkBreak ); 1.287 + 1.288 + new TestCase( 1.289 + SECTION, 1.290 + "properties.length", 1.291 + 1, 1.292 + properties.length ); 1.293 + 1.294 + new TestCase( 1.295 + SECTION, 1.296 + "object["+properties[0]+"]", 1.297 + values[0], 1.298 + object[properties[0]] ); 1.299 +} 1.300 +