|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 15.4.4.3-1.js |
|
9 ECMA Section: 15.4.4.3-1 Array.prototype.reverse() |
|
10 Description: |
|
11 |
|
12 The elements of the array are rearranged so as to reverse their order. |
|
13 This object is returned as the result of the call. |
|
14 |
|
15 1. Call the [[Get]] method of this object with argument "length". |
|
16 2. Call ToUint32(Result(1)). |
|
17 3. Compute floor(Result(2)/2). |
|
18 4. Let k be 0. |
|
19 5. If k equals Result(3), return this object. |
|
20 6. Compute Result(2)k1. |
|
21 7. Call ToString(k). |
|
22 8. ToString(Result(6)). |
|
23 9. Call the [[Get]] method of this object with argument Result(7). |
|
24 10. Call the [[Get]] method of this object with argument Result(8). |
|
25 11. If this object has a property named by Result(8), go to step 12; but |
|
26 if this object has no property named by Result(8), then go to either |
|
27 step 12 or step 14, depending on the implementation. |
|
28 12. Call the [[Put]] method of this object with arguments Result(7) and |
|
29 Result(10). |
|
30 13. Go to step 15. |
|
31 14. Call the [[Delete]] method on this object, providing Result(7) as the |
|
32 name of the property to delete. |
|
33 15. If this object has a property named by Result(7), go to step 16; but if |
|
34 this object has no property named by Result(7), then go to either step 16 |
|
35 or step 18, depending on the implementation. |
|
36 16. Call the [[Put]] method of this object with arguments Result(8) and |
|
37 Result(9). |
|
38 17. Go to step 19. |
|
39 18. Call the [[Delete]] method on this object, providing Result(8) as the |
|
40 name of the property to delete. |
|
41 19. Increase k by 1. |
|
42 20. Go to step 5. |
|
43 |
|
44 Note that the reverse function is intentionally generic; it does not require |
|
45 that its this value be an Array object. Therefore it can be transferred to other |
|
46 kinds of objects for use as a method. Whether the reverse function can be applied |
|
47 successfully to a host object is implementation dependent. |
|
48 |
|
49 Note: Array.prototype.reverse allows some flexibility in implementation |
|
50 regarding array indices that have not been populated. This test covers the |
|
51 cases in which unpopulated indices are not deleted, since the JavaScript |
|
52 implementation does not delete uninitialzed indices. |
|
53 |
|
54 Author: christine@netscape.com |
|
55 Date: 7 october 1997 |
|
56 */ |
|
57 var SECTION = "15.4.4.4-1"; |
|
58 var VERSION = "ECMA_1"; |
|
59 var BUGNUMBER="123724"; |
|
60 startTest(); |
|
61 |
|
62 writeHeaderToLog( SECTION + " Array.prototype.reverse()"); |
|
63 |
|
64 var ARR_PROTOTYPE = Array.prototype; |
|
65 |
|
66 new TestCase( SECTION, |
|
67 "Array.prototype.reverse.length", |
|
68 0, |
|
69 Array.prototype.reverse.length ); |
|
70 |
|
71 new TestCase( SECTION, |
|
72 "delete Array.prototype.reverse.length", |
|
73 false, |
|
74 delete Array.prototype.reverse.length ); |
|
75 |
|
76 new TestCase( SECTION, |
|
77 "delete Array.prototype.reverse.length; Array.prototype.reverse.length", |
|
78 0, |
|
79 eval("delete Array.prototype.reverse.length; Array.prototype.reverse.length") ); |
|
80 |
|
81 // length of array is 0 |
|
82 new TestCase( SECTION, |
|
83 "var A = new Array(); A.reverse(); A.length", |
|
84 0, |
|
85 eval("var A = new Array(); A.reverse(); A.length") ); |
|
86 |
|
87 // length of array is 1 |
|
88 var A = new Array(true); |
|
89 var R = Reverse(A); |
|
90 |
|
91 new TestCase( SECTION, |
|
92 "var A = new Array(true); A.reverse(); A.length", |
|
93 R.length, |
|
94 eval("var A = new Array(true); A.reverse(); A.length") ); |
|
95 |
|
96 CheckItems( R, A ); |
|
97 |
|
98 // length of array is 2 |
|
99 var S = "var A = new Array( true,false )"; |
|
100 eval(S); |
|
101 var R = Reverse(A); |
|
102 |
|
103 new TestCase( SECTION, |
|
104 S +"; A.reverse(); A.length", |
|
105 R.length, |
|
106 eval( S + "; A.reverse(); A.length") ); |
|
107 |
|
108 CheckItems( R, A ); |
|
109 |
|
110 // length of array is 3 |
|
111 var S = "var A = new Array( true,false,null )"; |
|
112 eval(S); |
|
113 var R = Reverse(A); |
|
114 |
|
115 new TestCase( SECTION, |
|
116 S +"; A.reverse(); A.length", |
|
117 R.length, |
|
118 eval( S + "; A.reverse(); A.length") ); |
|
119 |
|
120 CheckItems( R, A ); |
|
121 |
|
122 // length of array is 4 |
|
123 var S = "var A = new Array( true,false,null,void 0 )"; |
|
124 eval(S); |
|
125 var R = Reverse(A); |
|
126 |
|
127 new TestCase( SECTION, |
|
128 S +"; A.reverse(); A.length", |
|
129 R.length, |
|
130 eval( S + "; A.reverse(); A.length") ); |
|
131 CheckItems( R, A ); |
|
132 |
|
133 |
|
134 // some array indexes have not been set |
|
135 var S = "var A = new Array(); A[8] = 'hi', A[3] = 'yo'"; |
|
136 eval(S); |
|
137 var R = Reverse(A); |
|
138 |
|
139 new TestCase( SECTION, |
|
140 S +"; A.reverse(); A.length", |
|
141 R.length, |
|
142 eval( S + "; A.reverse(); A.length") ); |
|
143 |
|
144 CheckItems( R, A ); |
|
145 |
|
146 |
|
147 var OBJECT_OBJECT = new Object(); |
|
148 var FUNCTION_OBJECT = new Function( 'return this' ); |
|
149 var BOOLEAN_OBJECT = new Boolean; |
|
150 var DATE_OBJECT = new Date(0); |
|
151 var STRING_OBJECT = new String('howdy'); |
|
152 var NUMBER_OBJECT = new Number(Math.PI); |
|
153 var ARRAY_OBJECT= new Array(1000); |
|
154 |
|
155 var args = "null, void 0, Math.pow(2,32), 1.234e-32, OBJECT_OBJECT, BOOLEAN_OBJECT, FUNCTION_OBJECT, DATE_OBJECT, STRING_OBJECT,"+ |
|
156 "ARRAY_OBJECT, NUMBER_OBJECT, Math, true, false, 123, '90210'"; |
|
157 |
|
158 var S = "var A = new Array("+args+")"; |
|
159 eval(S); |
|
160 var R = Reverse(A); |
|
161 |
|
162 new TestCase( SECTION, |
|
163 S +"; A.reverse(); A.length", |
|
164 R.length, |
|
165 eval( S + "; A.reverse(); A.length") ); |
|
166 |
|
167 CheckItems( R, A ); |
|
168 |
|
169 var limit = 1000; |
|
170 var args = ""; |
|
171 for (var i = 0; i < limit; i++ ) { |
|
172 args += i +""; |
|
173 if ( i + 1 < limit ) { |
|
174 args += ","; |
|
175 } |
|
176 } |
|
177 |
|
178 var S = "var A = new Array("+args+")"; |
|
179 eval(S); |
|
180 var R = Reverse(A); |
|
181 |
|
182 new TestCase( SECTION, |
|
183 S +"; A.reverse(); A.length", |
|
184 R.length, |
|
185 eval( S + "; A.reverse(); A.length") ); |
|
186 |
|
187 CheckItems( R, A ); |
|
188 |
|
189 var S = "var MYOBJECT = new Object_1( \"void 0, 1, null, 2, \'\'\" )"; |
|
190 eval(S); |
|
191 var R = Reverse( A ); |
|
192 |
|
193 new TestCase( SECTION, |
|
194 S +"; A.reverse(); A.length", |
|
195 R.length, |
|
196 eval( S + "; A.reverse(); A.length") ); |
|
197 |
|
198 CheckItems( R, A ); |
|
199 |
|
200 test(); |
|
201 |
|
202 function CheckItems( R, A ) { |
|
203 for ( var i = 0; i < R.length; i++ ) { |
|
204 new TestCase( |
|
205 SECTION, |
|
206 "A["+i+ "]", |
|
207 R[i], |
|
208 A[i] ); |
|
209 } |
|
210 } |
|
211 |
|
212 function Object_1( value ) { |
|
213 this.array = value.split(","); |
|
214 this.length = this.array.length; |
|
215 for ( var i = 0; i < this.length; i++ ) { |
|
216 this[i] = eval(this.array[i]); |
|
217 } |
|
218 this.join = Array.prototype.reverse; |
|
219 this.getClass = Object.prototype.toString; |
|
220 } |
|
221 |
|
222 function Reverse( array ) { |
|
223 var r2 = array.length; |
|
224 var k = 0; |
|
225 var r3 = Math.floor( r2/2 ); |
|
226 if ( r3 == k ) { |
|
227 return array; |
|
228 } |
|
229 |
|
230 for ( k = 0; k < r3; k++ ) { |
|
231 var r6 = r2 - k - 1; |
|
232 // var r7 = String( k ); |
|
233 var r7 = k; |
|
234 var r8 = String( r6 ); |
|
235 |
|
236 var r9 = array[r7]; |
|
237 var r10 = array[r8]; |
|
238 |
|
239 array[r7] = r10; |
|
240 array[r8] = r9; |
|
241 } |
|
242 |
|
243 return array; |
|
244 } |
|
245 |
|
246 function Iterate( array ) { |
|
247 for ( var i = 0; i < array.length; i++ ) { |
|
248 // print( i+": "+ array[String(i)] ); |
|
249 } |
|
250 } |
|
251 |
|
252 function Object_1( value ) { |
|
253 this.array = value.split(","); |
|
254 this.length = this.array.length; |
|
255 for ( var i = 0; i < this.length; i++ ) { |
|
256 this[i] = this.array[i]; |
|
257 } |
|
258 this.reverse = Array.prototype.reverse; |
|
259 this.getClass = Object.prototype.toString; |
|
260 } |