Debian Codegraph @

rhino/testsrc/tests/ecma/Array/15.4.4.4-1.js

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