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
59Note that the reverse function is intentionally generic; it does not require
60that its this value be an Array object. Therefore it can be transferred to other
61kinds of objects for use as a method. Whether the reverse function can be applied
62successfully 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@netscape.com
70    Date:               7 october 1997
71*/
72    var SECTION = "15.4.4.4-1";
73    var VERSION = "ECMA_1";
74    startTest();
75    var BUGNUMBER="123724";
76
77    writeHeaderToLog( SECTION + " Array.prototype.reverse()");
78
79    var testcases = new Array();
80    getTestCases();
81    test();
82
83function 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
96    // length of array is 1
97    var A = new Array(true);
98    var R = Reverse(A);
99
100    testcases[testcases.length] = new TestCase(   SECTION,
101                                    "var A = new Array(true);   A.reverse(); A.length",
102                                    R.length,
103                                    eval("var A = new Array(true);   A.reverse(); A.length") );
104    CheckItems( R, A );
105
106    // length of array is 2
107    var S = "var A = new Array( true,false )";
108    eval(S);
109    var R = Reverse(A);
110
111    testcases[testcases.length] =   new TestCase(
112                                    SECTION,
113                                    S +";  A.reverse(); A.length",
114                                    R.length,
115                                    eval( S + "; A.reverse(); A.length") );
116
117    CheckItems(  R, A );
118
119    // length of array is 3
120    var S = "var A = new Array( true,false,null )";
121    eval(S);
122    var R = Reverse(A);
123
124    testcases[testcases.length] = new TestCase(   SECTION,
125                                    S +";  A.reverse(); A.length",
126                                    R.length,
127                                    eval( S + "; A.reverse(); A.length") );
128    CheckItems( R, A );
129
130    // length of array is 4
131    var S = "var A = new Array( true,false,null,void 0 )";
132    eval(S);
133    var R = Reverse(A);
134
135    testcases[testcases.length] = new TestCase(   SECTION,
136                                    S +";  A.reverse(); A.length",
137                                    R.length,
138                                    eval( S + "; A.reverse(); A.length") );
139    CheckItems( R, A );
140
141
142    // some array indexes have not been set
143    var S = "var A = new Array(); A[8] = 'hi', A[3] = 'yo'";
144    eval(S);
145    var R = Reverse(A);
146
147    testcases[testcases.length] = new TestCase(   SECTION,
148                                    S +";  A.reverse(); A.length",
149                                    R.length,
150                                    eval( S + "; A.reverse(); A.length") );
151    CheckItems( R, A );
152
153
154    var OBJECT_OBJECT = new Object();
155    var FUNCTION_OBJECT = new Function( 'return this' );
156    var BOOLEAN_OBJECT = new Boolean;
157    var DATE_OBJECT = new Date(0);
158    var STRING_OBJECT = new String('howdy');
159    var NUMBER_OBJECT = new Number(Math.PI);
160    var ARRAY_OBJECT= new Array(1000);
161
162     var args = "null, void 0, Math.pow(2,32), 1.234e-32, OBJECT_OBJECT, BOOLEAN_OBJECT, FUNCTION_OBJECT, DATE_OBJECT, STRING_OBJECT,"+
163                "ARRAY_OBJECT, NUMBER_OBJECT, Math, true, false, 123, '90210'";
164
165    var S = "var A = new Array("+args+")";
166    eval(S);
167    var R = Reverse(A);
168
169    testcases[testcases.length] = new TestCase(   SECTION,
170                                    S +";  A.reverse(); A.length",
171                                    R.length,
172                                    eval( S + "; A.reverse(); A.length") );
173    CheckItems( R, A );
174
175    var limit = 1000;
176    var args = "";
177    for (var i = 0; i < limit; i++ ) {
178        args += i +"";
179        if ( i + 1 < limit ) {
180            args += ",";
181        }
182    }
183
184    var S = "var A = new Array("+args+")";
185    eval(S);
186    var R = Reverse(A);
187
188    testcases[testcases.length] = new TestCase(   SECTION,
189                                    S +";  A.reverse(); A.length",
190                                    R.length,
191                                    eval( S + "; A.reverse(); A.length") );
192    CheckItems( R, A );
193
194    var S = "var MYOBJECT = new Object_1( \"void 0, 1, null, 2, \'\'\" )";
195    eval(S);
196    var R = Reverse( A );
197
198    testcases[testcases.length] = new TestCase(   SECTION,
199                                    S +";  A.reverse(); A.length",
200                                    R.length,
201                                    eval( S + "; A.reverse(); A.length") );
202    CheckItems( R, A );
203
204    return ( testcases );
205}
206function CheckItems( R, A ) {
207    for ( var i = 0; i < R.length; i++ ) {
208        testcases[testcases.length] = new TestCase(
209                                            SECTION,
210                                            "A["+i+ "]",
211                                            R[i],
212                                            A[i] );
213    }
214}
215function test() {
216    for ( tc=0; tc < testcases.length; tc++ ) {
217        testcases[tc].passed = writeTestCaseResult(
218                            testcases[tc].expect,
219                            testcases[tc].actual,
220                            testcases[tc].description +" = "+ testcases[tc].actual );
221        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
222    }
223    stopTest();
224    return ( testcases );
225}
226function Object_1( value ) {
227    this.array = value.split(",");
228    this.length = this.array.length;
229    for ( var i = 0; i < this.length; i++ ) {
230        this[i] = eval(this.array[i]);
231    }
232    this.join = Array.prototype.reverse;
233    this.getClass = Object.prototype.toString;
234}
235function Reverse( array ) {
236    var r2 = array.length;
237    var k = 0;
238    var r3 = Math.floor( r2/2 );
239    if ( r3 == k ) {
240        return array;
241    }
242
243    for ( k = 0;  k < r3; k++ ) {
244        var r6 = r2 - k - 1;
245//        var r7 = String( k );
246        var r7 = k;
247        var r8 = String( r6 );
248
249        var r9 = array[r7];
250        var r10 = array[r8];
251
252        array[r7] = r10;
253        array[r8] = r9;
254    }
255
256    return array;
257}
258function Iterate( array ) {
259    for ( var i = 0; i < array.length; i++ ) {
260//        print( i+": "+ array[String(i)] );
261    }
262}
263
264function Object_1( value ) {
265    this.array = value.split(",");
266    this.length = this.array.length;
267    for ( var i = 0; i < this.length; i++ ) {
268        this[i] = this.array[i];
269    }
270    this.reverse = Array.prototype.reverse;
271    this.getClass = Object.prototype.toString;
272}
273