1 /* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22 /** 23 File Name: 15.4.4.3-1.js 24 ECMA Section: 15.4.4.3-1 Array.prototype.reverse() 25 Description: 26 27 The elements of the array are rearranged so as to reverse their order. 28 This object is returned as the result of the call. 29 30 1. Call the [[Get]] method of this object with argument "length". 31 2. Call ToUint32(Result(1)). 32 3. Compute floor(Result(2)/2). 33 4. Let k be 0. 34 5. If k equals Result(3), return this object. 35 6. Compute Result(2)k1. 36 7. Call ToString(k). 37 8. ToString(Result(6)). 38 9. Call the [[Get]] method of this object with argument Result(7). 39 10. Call the [[Get]] method of this object with argument Result(8). 40 11. If this object has a property named by Result(8), go to step 12; but 41 if this object has no property named by Result(8), then go to either 42 step 12 or step 14, depending on the implementation. 43 12. Call the [[Put]] method of this object with arguments Result(7) and 44 Result(10). 45 13. Go to step 15. 46 14. Call the [[Delete]] method on this object, providing Result(7) as the 47 name of the property to delete. 48 15. If this object has a property named by Result(7), go to step 16; but if 49 this object has no property named by Result(7), then go to either step 16 50 or step 18, depending on the implementation. 51 16. Call the [[Put]] method of this object with arguments Result(8) and 52 Result(9). 53 17. Go to step 19. 54 18. Call the [[Delete]] method on this object, providing Result(8) as the 55 name of the property to delete. 56 19. Increase k by 1. 57 20. Go to step 5. 58 59 Note that the reverse function is intentionally generic; it does not require 60 that its this value be an Array object. Therefore it can be transferred to other 61 kinds of objects for use as a method. Whether the reverse function can be applied 62 successfully to a host object is implementation dependent. 63 64 Note: Array.prototype.reverse allows some flexibility in implementation 65 regarding array indices that have not been populated. This test covers the 66 cases in which unpopulated indices are not deleted, since the JavaScript 67 implementation does not delete uninitialzed indices. 68 69 Author: christine (at) netscape.com 70 Date: 7 october 1997 71 */ 72 73 var SECTION = "15.4.4.4-1"; 74 var VERSION = "ECMA_1"; 75 startTest(); 76 var testcases = new Array(); 77 78 writeHeaderToLog( SECTION + " Array.prototype.reverse()"); 79 80 getTestCases(); 81 test(); 82 83 function getTestCases() { 84 var ARR_PROTOTYPE = Array.prototype; 85 86 testcases[testcases.length] = new TestCase( SECTION, "Array.prototype.reverse.length", 0, Array.prototype.reverse.length ); 87 testcases[testcases.length] = new TestCase( SECTION, "delete Array.prototype.reverse.length", false, delete Array.prototype.reverse.length ); 88 testcases[testcases.length] = new TestCase( SECTION, "delete Array.prototype.reverse.length; Array.prototype.reverse.length", 0, eval("delete Array.prototype.reverse.length; Array.prototype.reverse.length") ); 89 90 // length of array is 0 91 testcases[testcases.length] = new TestCase( SECTION, 92 "var A = new Array(); A.reverse(); A.length", 93 0, 94 eval("var A = new Array(); A.reverse(); A.length") ); 95 return ( testcases ); 96 } 97 function CheckItems( R, A ) { 98 for ( var i = 0; i < R.length; i++ ) { 99 testcases[testcases.length] = new TestCase( 100 SECTION, 101 "A["+i+ "]", 102 R[i], 103 A[i] ); 104 } 105 } 106 function test() { 107 for ( tc=0; tc < testcases.length; tc++ ) { 108 testcases[tc].passed = writeTestCaseResult( 109 testcases[tc].expect, 110 testcases[tc].actual, 111 testcases[tc].description +" = "+ testcases[tc].actual ); 112 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 113 } 114 stopTest(); 115 return ( testcases ); 116 } 117 function Object_1( value ) { 118 this.array = value.split(","); 119 this.length = this.array.length; 120 for ( var i = 0; i < this.length; i++ ) { 121 this[i] = eval(this.array[i]); 122 } 123 this.join = Array.prototype.reverse; 124 this.getClass = Object.prototype.toString; 125 } 126 function Reverse( array ) { 127 var r2 = array.length; 128 var k = 0; 129 var r3 = Math.floor( r2/2 ); 130 if ( r3 == k ) { 131 return array; 132 } 133 134 for ( k = 0; k < r3; k++ ) { 135 var r6 = r2 - k - 1; 136 // var r7 = String( k ); 137 var r7 = k; 138 var r8 = String( r6 ); 139 140 var r9 = array[r7]; 141 var r10 = array[r8]; 142 143 array[r7] = r10; 144 array[r8] = r9; 145 } 146 147 return array; 148 } 149 function Iterate( array ) { 150 for ( var i = 0; i < array.length; i++ ) { 151 // print( i+": "+ array[String(i)] ); 152 } 153 } 154 155 function Object_1( value ) { 156 this.array = value.split(","); 157 this.length = this.array.length; 158 for ( var i = 0; i < this.length; i++ ) { 159 this[i] = this.array[i]; 160 } 161 this.reverse = Array.prototype.reverse; 162 this.getClass = Object.prototype.toString; 163 } 164