1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma/Array/15.4.4.4-1.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,260 @@ 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: 15.4.4.3-1.js 1.12 + ECMA Section: 15.4.4.3-1 Array.prototype.reverse() 1.13 + Description: 1.14 + 1.15 + The elements of the array are rearranged so as to reverse their order. 1.16 + This object is returned as the result of the call. 1.17 + 1.18 + 1. Call the [[Get]] method of this object with argument "length". 1.19 + 2. Call ToUint32(Result(1)). 1.20 + 3. Compute floor(Result(2)/2). 1.21 + 4. Let k be 0. 1.22 + 5. If k equals Result(3), return this object. 1.23 + 6. Compute Result(2)k1. 1.24 + 7. Call ToString(k). 1.25 + 8. ToString(Result(6)). 1.26 + 9. Call the [[Get]] method of this object with argument Result(7). 1.27 + 10. Call the [[Get]] method of this object with argument Result(8). 1.28 + 11. If this object has a property named by Result(8), go to step 12; but 1.29 + if this object has no property named by Result(8), then go to either 1.30 + step 12 or step 14, depending on the implementation. 1.31 + 12. Call the [[Put]] method of this object with arguments Result(7) and 1.32 + Result(10). 1.33 + 13. Go to step 15. 1.34 + 14. Call the [[Delete]] method on this object, providing Result(7) as the 1.35 + name of the property to delete. 1.36 + 15. If this object has a property named by Result(7), go to step 16; but if 1.37 + this object has no property named by Result(7), then go to either step 16 1.38 + or step 18, depending on the implementation. 1.39 + 16. Call the [[Put]] method of this object with arguments Result(8) and 1.40 + Result(9). 1.41 + 17. Go to step 19. 1.42 + 18. Call the [[Delete]] method on this object, providing Result(8) as the 1.43 + name of the property to delete. 1.44 + 19. Increase k by 1. 1.45 + 20. Go to step 5. 1.46 + 1.47 + Note that the reverse function is intentionally generic; it does not require 1.48 + that its this value be an Array object. Therefore it can be transferred to other 1.49 + kinds of objects for use as a method. Whether the reverse function can be applied 1.50 + successfully to a host object is implementation dependent. 1.51 + 1.52 + Note: Array.prototype.reverse allows some flexibility in implementation 1.53 + regarding array indices that have not been populated. This test covers the 1.54 + cases in which unpopulated indices are not deleted, since the JavaScript 1.55 + implementation does not delete uninitialzed indices. 1.56 + 1.57 + Author: christine@netscape.com 1.58 + Date: 7 october 1997 1.59 +*/ 1.60 +var SECTION = "15.4.4.4-1"; 1.61 +var VERSION = "ECMA_1"; 1.62 +var BUGNUMBER="123724"; 1.63 +startTest(); 1.64 + 1.65 +writeHeaderToLog( SECTION + " Array.prototype.reverse()"); 1.66 + 1.67 +var ARR_PROTOTYPE = Array.prototype; 1.68 + 1.69 +new TestCase( SECTION, 1.70 + "Array.prototype.reverse.length", 1.71 + 0, 1.72 + Array.prototype.reverse.length ); 1.73 + 1.74 +new TestCase( SECTION, 1.75 + "delete Array.prototype.reverse.length", 1.76 + false, 1.77 + delete Array.prototype.reverse.length ); 1.78 + 1.79 +new TestCase( SECTION, 1.80 + "delete Array.prototype.reverse.length; Array.prototype.reverse.length", 1.81 + 0, 1.82 + eval("delete Array.prototype.reverse.length; Array.prototype.reverse.length") ); 1.83 + 1.84 +// length of array is 0 1.85 +new TestCase( SECTION, 1.86 + "var A = new Array(); A.reverse(); A.length", 1.87 + 0, 1.88 + eval("var A = new Array(); A.reverse(); A.length") ); 1.89 + 1.90 +// length of array is 1 1.91 +var A = new Array(true); 1.92 +var R = Reverse(A); 1.93 + 1.94 +new TestCase( SECTION, 1.95 + "var A = new Array(true); A.reverse(); A.length", 1.96 + R.length, 1.97 + eval("var A = new Array(true); A.reverse(); A.length") ); 1.98 + 1.99 +CheckItems( R, A ); 1.100 + 1.101 +// length of array is 2 1.102 +var S = "var A = new Array( true,false )"; 1.103 +eval(S); 1.104 +var R = Reverse(A); 1.105 + 1.106 +new TestCase( SECTION, 1.107 + S +"; A.reverse(); A.length", 1.108 + R.length, 1.109 + eval( S + "; A.reverse(); A.length") ); 1.110 + 1.111 +CheckItems( R, A ); 1.112 + 1.113 +// length of array is 3 1.114 +var S = "var A = new Array( true,false,null )"; 1.115 +eval(S); 1.116 +var R = Reverse(A); 1.117 + 1.118 +new TestCase( SECTION, 1.119 + S +"; A.reverse(); A.length", 1.120 + R.length, 1.121 + eval( S + "; A.reverse(); A.length") ); 1.122 + 1.123 +CheckItems( R, A ); 1.124 + 1.125 +// length of array is 4 1.126 +var S = "var A = new Array( true,false,null,void 0 )"; 1.127 +eval(S); 1.128 +var R = Reverse(A); 1.129 + 1.130 +new TestCase( SECTION, 1.131 + S +"; A.reverse(); A.length", 1.132 + R.length, 1.133 + eval( S + "; A.reverse(); A.length") ); 1.134 +CheckItems( R, A ); 1.135 + 1.136 + 1.137 +// some array indexes have not been set 1.138 +var S = "var A = new Array(); A[8] = 'hi', A[3] = 'yo'"; 1.139 +eval(S); 1.140 +var R = Reverse(A); 1.141 + 1.142 +new TestCase( SECTION, 1.143 + S +"; A.reverse(); A.length", 1.144 + R.length, 1.145 + eval( S + "; A.reverse(); A.length") ); 1.146 + 1.147 +CheckItems( R, A ); 1.148 + 1.149 + 1.150 +var OBJECT_OBJECT = new Object(); 1.151 +var FUNCTION_OBJECT = new Function( 'return this' ); 1.152 +var BOOLEAN_OBJECT = new Boolean; 1.153 +var DATE_OBJECT = new Date(0); 1.154 +var STRING_OBJECT = new String('howdy'); 1.155 +var NUMBER_OBJECT = new Number(Math.PI); 1.156 +var ARRAY_OBJECT= new Array(1000); 1.157 + 1.158 +var args = "null, void 0, Math.pow(2,32), 1.234e-32, OBJECT_OBJECT, BOOLEAN_OBJECT, FUNCTION_OBJECT, DATE_OBJECT, STRING_OBJECT,"+ 1.159 + "ARRAY_OBJECT, NUMBER_OBJECT, Math, true, false, 123, '90210'"; 1.160 + 1.161 +var S = "var A = new Array("+args+")"; 1.162 +eval(S); 1.163 +var R = Reverse(A); 1.164 + 1.165 +new TestCase( SECTION, 1.166 + S +"; A.reverse(); A.length", 1.167 + R.length, 1.168 + eval( S + "; A.reverse(); A.length") ); 1.169 + 1.170 +CheckItems( R, A ); 1.171 + 1.172 +var limit = 1000; 1.173 +var args = ""; 1.174 +for (var i = 0; i < limit; i++ ) { 1.175 + args += i +""; 1.176 + if ( i + 1 < limit ) { 1.177 + args += ","; 1.178 + } 1.179 +} 1.180 + 1.181 +var S = "var A = new Array("+args+")"; 1.182 +eval(S); 1.183 +var R = Reverse(A); 1.184 + 1.185 +new TestCase( SECTION, 1.186 + S +"; A.reverse(); A.length", 1.187 + R.length, 1.188 + eval( S + "; A.reverse(); A.length") ); 1.189 + 1.190 +CheckItems( R, A ); 1.191 + 1.192 +var S = "var MYOBJECT = new Object_1( \"void 0, 1, null, 2, \'\'\" )"; 1.193 +eval(S); 1.194 +var R = Reverse( A ); 1.195 + 1.196 +new TestCase( SECTION, 1.197 + S +"; A.reverse(); A.length", 1.198 + R.length, 1.199 + eval( S + "; A.reverse(); A.length") ); 1.200 + 1.201 +CheckItems( R, A ); 1.202 + 1.203 +test(); 1.204 + 1.205 +function CheckItems( R, A ) { 1.206 + for ( var i = 0; i < R.length; i++ ) { 1.207 + new TestCase( 1.208 + SECTION, 1.209 + "A["+i+ "]", 1.210 + R[i], 1.211 + A[i] ); 1.212 + } 1.213 +} 1.214 + 1.215 +function Object_1( value ) { 1.216 + this.array = value.split(","); 1.217 + this.length = this.array.length; 1.218 + for ( var i = 0; i < this.length; i++ ) { 1.219 + this[i] = eval(this.array[i]); 1.220 + } 1.221 + this.join = Array.prototype.reverse; 1.222 + this.getClass = Object.prototype.toString; 1.223 +} 1.224 + 1.225 +function Reverse( array ) { 1.226 + var r2 = array.length; 1.227 + var k = 0; 1.228 + var r3 = Math.floor( r2/2 ); 1.229 + if ( r3 == k ) { 1.230 + return array; 1.231 + } 1.232 + 1.233 + for ( k = 0; k < r3; k++ ) { 1.234 + var r6 = r2 - k - 1; 1.235 +// var r7 = String( k ); 1.236 + var r7 = k; 1.237 + var r8 = String( r6 ); 1.238 + 1.239 + var r9 = array[r7]; 1.240 + var r10 = array[r8]; 1.241 + 1.242 + array[r7] = r10; 1.243 + array[r8] = r9; 1.244 + } 1.245 + 1.246 + return array; 1.247 +} 1.248 + 1.249 +function Iterate( array ) { 1.250 + for ( var i = 0; i < array.length; i++ ) { 1.251 +// print( i+": "+ array[String(i)] ); 1.252 + } 1.253 +} 1.254 + 1.255 +function Object_1( value ) { 1.256 + this.array = value.split(","); 1.257 + this.length = this.array.length; 1.258 + for ( var i = 0; i < this.length; i++ ) { 1.259 + this[i] = this.array[i]; 1.260 + } 1.261 + this.reverse = Array.prototype.reverse; 1.262 + this.getClass = Object.prototype.toString; 1.263 +}