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.5.js |
michael@0 | 9 | ECMA Section: Array.prototype.sort(comparefn) |
michael@0 | 10 | Description: |
michael@0 | 11 | |
michael@0 | 12 | This test file tests cases in which the compare function is not supplied. |
michael@0 | 13 | |
michael@0 | 14 | The elements of this array are sorted. The sort is not necessarily stable. |
michael@0 | 15 | If comparefn is provided, it should be a function that accepts two arguments |
michael@0 | 16 | x and y and returns a negative value if x < y, zero if x = y, or a positive |
michael@0 | 17 | value if x > y. |
michael@0 | 18 | |
michael@0 | 19 | 1. Call the [[Get]] method of this object with argument "length". |
michael@0 | 20 | 2. Call ToUint32(Result(1)). |
michael@0 | 21 | 1. Perform an implementation-dependent sequence of calls to the |
michael@0 | 22 | [[Get]] , [[Put]], and [[Delete]] methods of this object and |
michael@0 | 23 | toSortCompare (described below), where the first argument for each call |
michael@0 | 24 | to [[Get]], [[Put]] , or [[Delete]] is a nonnegative integer less |
michael@0 | 25 | than Result(2) and where the arguments for calls to SortCompare are |
michael@0 | 26 | results of previous calls to the [[Get]] method. After this sequence |
michael@0 | 27 | is complete, this object must have the following two properties. |
michael@0 | 28 | (1) There must be some mathematical permutation of the nonnegative |
michael@0 | 29 | integers less than Result(2), such that for every nonnegative integer |
michael@0 | 30 | j less than Result(2), if property old[j] existed, then new[(j)] is |
michael@0 | 31 | exactly the same value as old[j],. but if property old[j] did not exist, |
michael@0 | 32 | then new[(j)] either does not exist or exists with value undefined. |
michael@0 | 33 | (2) If comparefn is not supplied or is a consistent comparison |
michael@0 | 34 | function for the elements of this array, then for all nonnegative |
michael@0 | 35 | integers j and k, each less than Result(2), if old[j] compares less |
michael@0 | 36 | than old[k] (see SortCompare below), then (j) < (k). Here we use the |
michael@0 | 37 | notation old[j] to refer to the hypothetical result of calling the [ |
michael@0 | 38 | [Get]] method of this object with argument j before this step is |
michael@0 | 39 | executed, and the notation new[j] to refer to the hypothetical result |
michael@0 | 40 | of calling the [[Get]] method of this object with argument j after this |
michael@0 | 41 | step has been completely executed. A function is a consistent |
michael@0 | 42 | comparison function for a set of values if (a) for any two of those |
michael@0 | 43 | values (possibly the same value) considered as an ordered pair, it |
michael@0 | 44 | always returns the same value when given that pair of values as its |
michael@0 | 45 | two arguments, and the result of applying ToNumber to this value is |
michael@0 | 46 | not NaN; (b) when considered as a relation, where the pair (x, y) is |
michael@0 | 47 | considered to be in the relation if and only if applying the function |
michael@0 | 48 | to x and y and then applying ToNumber to the result produces a |
michael@0 | 49 | negative value, this relation is a partial order; and (c) when |
michael@0 | 50 | considered as a different relation, where the pair (x, y) is considered |
michael@0 | 51 | to be in the relation if and only if applying the function to x and y |
michael@0 | 52 | and then applying ToNumber to the result produces a zero value (of either |
michael@0 | 53 | sign), this relation is an equivalence relation. In this context, the |
michael@0 | 54 | phrase "x compares less than y" means applying Result(2) to x and y and |
michael@0 | 55 | then applying ToNumber to the result produces a negative value. |
michael@0 | 56 | 3.Return this object. |
michael@0 | 57 | |
michael@0 | 58 | When the SortCompare operator is called with two arguments x and y, the following steps are taken: |
michael@0 | 59 | 1.If x and y are both undefined, return +0. |
michael@0 | 60 | 2.If x is undefined, return 1. |
michael@0 | 61 | 3.If y is undefined, return 1. |
michael@0 | 62 | 4.If the argument comparefn was not provided in the call to sort, go to step 7. |
michael@0 | 63 | 5.Call comparefn with arguments x and y. |
michael@0 | 64 | 6.Return Result(5). |
michael@0 | 65 | 7.Call ToString(x). |
michael@0 | 66 | 8.Call ToString(y). |
michael@0 | 67 | 9.If Result(7) < Result(8), return 1. |
michael@0 | 68 | 10.If Result(7) > Result(8), return 1. |
michael@0 | 69 | 11.Return +0. |
michael@0 | 70 | |
michael@0 | 71 | Note that, because undefined always compared greater than any other value, undefined and nonexistent |
michael@0 | 72 | property values always sort to the end of the result. It is implementation-dependent whether or not such |
michael@0 | 73 | properties will exist or not at the end of the array when the sort is concluded. |
michael@0 | 74 | |
michael@0 | 75 | Note that the sort function is intentionally generic; it does not require that its this value be an Array object. |
michael@0 | 76 | Therefore it can be transferred to other kinds of objects for use as a method. Whether the sort function can be |
michael@0 | 77 | applied successfully to a host object is implementation dependent . |
michael@0 | 78 | |
michael@0 | 79 | Author: christine@netscape.com |
michael@0 | 80 | Date: 12 november 1997 |
michael@0 | 81 | */ |
michael@0 | 82 | |
michael@0 | 83 | |
michael@0 | 84 | var SECTION = "15.4.4.5-1"; |
michael@0 | 85 | var VERSION = "ECMA_1"; |
michael@0 | 86 | startTest(); |
michael@0 | 87 | var TITLE = "Array.prototype.sort(comparefn)"; |
michael@0 | 88 | |
michael@0 | 89 | writeHeaderToLog( SECTION + " "+ TITLE); |
michael@0 | 90 | var S = new Array(); |
michael@0 | 91 | var item = 0; |
michael@0 | 92 | |
michael@0 | 93 | // array is empty. |
michael@0 | 94 | S[item++] = "var A = new Array()"; |
michael@0 | 95 | |
michael@0 | 96 | // array contains one item |
michael@0 | 97 | S[item++] = "var A = new Array( true )"; |
michael@0 | 98 | |
michael@0 | 99 | // length of array is 2 |
michael@0 | 100 | S[item++] = "var A = new Array( true, false, new Boolean(true), new Boolean(false), 'true', 'false' )"; |
michael@0 | 101 | |
michael@0 | 102 | S[item++] = "var A = new Array(); A[3] = 'undefined'; A[6] = null; A[8] = 'null'; A[0] = void 0"; |
michael@0 | 103 | |
michael@0 | 104 | S[item] = "var A = new Array( "; |
michael@0 | 105 | |
michael@0 | 106 | var limit = 0x0061; |
michael@0 | 107 | for ( var i = 0x007A; i >= limit; i-- ) { |
michael@0 | 108 | S[item] += "\'"+ String.fromCharCode(i) +"\'" ; |
michael@0 | 109 | if ( i > limit ) { |
michael@0 | 110 | S[item] += ","; |
michael@0 | 111 | } |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | S[item] += ")"; |
michael@0 | 115 | |
michael@0 | 116 | item++; |
michael@0 | 117 | |
michael@0 | 118 | for ( var i = 0; i < S.length; i++ ) { |
michael@0 | 119 | CheckItems( S[i] ); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | test(); |
michael@0 | 123 | |
michael@0 | 124 | function CheckItems( S ) { |
michael@0 | 125 | eval( S ); |
michael@0 | 126 | var E = Sort( A ); |
michael@0 | 127 | |
michael@0 | 128 | new TestCase( SECTION, |
michael@0 | 129 | S +"; A.sort(); A.length", |
michael@0 | 130 | E.length, |
michael@0 | 131 | eval( S + "; A.sort(); A.length") ); |
michael@0 | 132 | |
michael@0 | 133 | for ( var i = 0; i < E.length; i++ ) { |
michael@0 | 134 | new TestCase( |
michael@0 | 135 | SECTION, |
michael@0 | 136 | "A["+i+ "].toString()", |
michael@0 | 137 | E[i] +"", |
michael@0 | 138 | A[i] +""); |
michael@0 | 139 | |
michael@0 | 140 | if ( A[i] == void 0 && typeof A[i] == "undefined" ) { |
michael@0 | 141 | new TestCase( |
michael@0 | 142 | SECTION, |
michael@0 | 143 | "typeof A["+i+ "]", |
michael@0 | 144 | typeof E[i], |
michael@0 | 145 | typeof A[i] ); |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | } |
michael@0 | 149 | function Object_1( value ) { |
michael@0 | 150 | this.array = value.split(","); |
michael@0 | 151 | this.length = this.array.length; |
michael@0 | 152 | for ( var i = 0; i < this.length; i++ ) { |
michael@0 | 153 | this[i] = eval(this.array[i]); |
michael@0 | 154 | } |
michael@0 | 155 | this.sort = Array.prototype.sort; |
michael@0 | 156 | this.getClass = Object.prototype.toString; |
michael@0 | 157 | } |
michael@0 | 158 | function Sort( a ) { |
michael@0 | 159 | for ( i = 0; i < a.length; i++ ) { |
michael@0 | 160 | for ( j = i+1; j < a.length; j++ ) { |
michael@0 | 161 | var lo = a[i]; |
michael@0 | 162 | var hi = a[j]; |
michael@0 | 163 | var c = Compare( lo, hi ); |
michael@0 | 164 | if ( c == 1 ) { |
michael@0 | 165 | a[i] = hi; |
michael@0 | 166 | a[j] = lo; |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | return a; |
michael@0 | 171 | } |
michael@0 | 172 | function Compare( x, y ) { |
michael@0 | 173 | if ( x == void 0 && y == void 0 && typeof x == "undefined" && typeof y == "undefined" ) { |
michael@0 | 174 | return +0; |
michael@0 | 175 | } |
michael@0 | 176 | if ( x == void 0 && typeof x == "undefined" ) { |
michael@0 | 177 | return 1; |
michael@0 | 178 | } |
michael@0 | 179 | if ( y == void 0 && typeof y == "undefined" ) { |
michael@0 | 180 | return -1; |
michael@0 | 181 | } |
michael@0 | 182 | x = String(x); |
michael@0 | 183 | y = String(y); |
michael@0 | 184 | if ( x < y ) { |
michael@0 | 185 | return -1; |
michael@0 | 186 | } |
michael@0 | 187 | if ( x > y ) { |
michael@0 | 188 | return 1; |
michael@0 | 189 | } |
michael@0 | 190 | return 0; |
michael@0 | 191 | } |