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: 15.4.4.3-1.js michael@0: ECMA Section: 15.4.4.3-1 Array.prototype.reverse() michael@0: Description: michael@0: michael@0: The elements of the array are rearranged so as to reverse their order. michael@0: This object is returned as the result of the call. michael@0: michael@0: 1. Call the [[Get]] method of this object with argument "length". michael@0: 2. Call ToUint32(Result(1)). michael@0: 3. Compute floor(Result(2)/2). michael@0: 4. Let k be 0. michael@0: 5. If k equals Result(3), return this object. michael@0: 6. Compute Result(2)k1. michael@0: 7. Call ToString(k). michael@0: 8. ToString(Result(6)). michael@0: 9. Call the [[Get]] method of this object with argument Result(7). michael@0: 10. Call the [[Get]] method of this object with argument Result(8). michael@0: 11. If this object has a property named by Result(8), go to step 12; but michael@0: if this object has no property named by Result(8), then go to either michael@0: step 12 or step 14, depending on the implementation. michael@0: 12. Call the [[Put]] method of this object with arguments Result(7) and michael@0: Result(10). michael@0: 13. Go to step 15. michael@0: 14. Call the [[Delete]] method on this object, providing Result(7) as the michael@0: name of the property to delete. michael@0: 15. If this object has a property named by Result(7), go to step 16; but if michael@0: this object has no property named by Result(7), then go to either step 16 michael@0: or step 18, depending on the implementation. michael@0: 16. Call the [[Put]] method of this object with arguments Result(8) and michael@0: Result(9). michael@0: 17. Go to step 19. michael@0: 18. Call the [[Delete]] method on this object, providing Result(8) as the michael@0: name of the property to delete. michael@0: 19. Increase k by 1. michael@0: 20. Go to step 5. michael@0: michael@0: Note that the reverse function is intentionally generic; it does not require michael@0: that its this value be an Array object. Therefore it can be transferred to other michael@0: kinds of objects for use as a method. Whether the reverse function can be applied michael@0: successfully to a host object is implementation dependent. michael@0: michael@0: Note: Array.prototype.reverse allows some flexibility in implementation michael@0: regarding array indices that have not been populated. This test covers the michael@0: cases in which unpopulated indices are not deleted, since the JavaScript michael@0: implementation does not delete uninitialzed indices. michael@0: michael@0: Author: christine@netscape.com michael@0: Date: 7 october 1997 michael@0: */ michael@0: michael@0: var SECTION = "15.4.4.4-1"; michael@0: var VERSION = "ECMA_1"; michael@0: startTest(); michael@0: michael@0: writeHeaderToLog( SECTION + " Array.prototype.reverse()"); michael@0: michael@0: var ARR_PROTOTYPE = Array.prototype; michael@0: michael@0: new TestCase( SECTION, "Array.prototype.reverse.length", 0, Array.prototype.reverse.length ); michael@0: new TestCase( SECTION, "delete Array.prototype.reverse.length", false, delete Array.prototype.reverse.length ); michael@0: new TestCase( SECTION, "delete Array.prototype.reverse.length; Array.prototype.reverse.length", 0, eval("delete Array.prototype.reverse.length; Array.prototype.reverse.length") ); michael@0: michael@0: // length of array is 0 michael@0: new TestCase( SECTION, michael@0: "var A = new Array(); A.reverse(); A.length", michael@0: 0, michael@0: eval("var A = new Array(); A.reverse(); A.length") ); michael@0: michael@0: test(); michael@0: michael@0: function CheckItems( R, A ) { michael@0: for ( var i = 0; i < R.length; i++ ) { michael@0: new TestCase( michael@0: SECTION, michael@0: "A["+i+ "]", michael@0: R[i], michael@0: A[i] ); michael@0: } michael@0: } michael@0: test(); michael@0: michael@0: function Object_1( value ) { michael@0: this.array = value.split(","); michael@0: this.length = this.array.length; michael@0: for ( var i = 0; i < this.length; i++ ) { michael@0: this[i] = eval(this.array[i]); michael@0: } michael@0: this.join = Array.prototype.reverse; michael@0: this.getClass = Object.prototype.toString; michael@0: } michael@0: function Reverse( array ) { michael@0: var r2 = array.length; michael@0: var k = 0; michael@0: var r3 = Math.floor( r2/2 ); michael@0: if ( r3 == k ) { michael@0: return array; michael@0: } michael@0: michael@0: for ( k = 0; k < r3; k++ ) { michael@0: var r6 = r2 - k - 1; michael@0: // var r7 = String( k ); michael@0: var r7 = k; michael@0: var r8 = String( r6 ); michael@0: michael@0: var r9 = array[r7]; michael@0: var r10 = array[r8]; michael@0: michael@0: array[r7] = r10; michael@0: array[r8] = r9; michael@0: } michael@0: michael@0: return array; michael@0: } michael@0: function Iterate( array ) { michael@0: for ( var i = 0; i < array.length; i++ ) { michael@0: // print( i+": "+ array[String(i)] ); michael@0: } michael@0: } michael@0: michael@0: function Object_1( value ) { michael@0: this.array = value.split(","); michael@0: this.length = this.array.length; michael@0: for ( var i = 0; i < this.length; i++ ) { michael@0: this[i] = this.array[i]; michael@0: } michael@0: this.reverse = Array.prototype.reverse; michael@0: this.getClass = Object.prototype.toString; michael@0: }