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