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