1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma/Array/15.4.4.5-2.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,193 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/** 1.11 + File Name: 15.4.4.5-2.js 1.12 + ECMA Section: Array.prototype.sort(comparefn) 1.13 + Description: 1.14 + 1.15 + This test file tests cases in which the compare function is supplied. 1.16 + In this cases, the sort creates a reverse sort. 1.17 + 1.18 + The elements of this array are sorted. The sort is not necessarily stable. 1.19 + If comparefn is provided, it should be a function that accepts two arguments 1.20 + x and y and returns a negative value if x < y, zero if x = y, or a positive 1.21 + value if x > y. 1.22 + 1.23 + 1. Call the [[Get]] method of this object with argument "length". 1.24 + 2. Call ToUint32(Result(1)). 1.25 + 1. Perform an implementation-dependent sequence of calls to the 1.26 + [[Get]] , [[Put]], and [[Delete]] methods of this object and 1.27 + toSortCompare (described below), where the first argument for each call 1.28 + to [[Get]], [[Put]] , or [[Delete]] is a nonnegative integer less 1.29 + than Result(2) and where the arguments for calls to SortCompare are 1.30 + results of previous calls to the [[Get]] method. After this sequence 1.31 + is complete, this object must have the following two properties. 1.32 + (1) There must be some mathematical permutation of the nonnegative 1.33 + integers less than Result(2), such that for every nonnegative integer 1.34 + j less than Result(2), if property old[j] existed, then new[(j)] is 1.35 + exactly the same value as old[j],. but if property old[j] did not exist, 1.36 + then new[(j)] either does not exist or exists with value undefined. 1.37 + (2) If comparefn is not supplied or is a consistent comparison 1.38 + function for the elements of this array, then for all nonnegative 1.39 + integers j and k, each less than Result(2), if old[j] compares less 1.40 + than old[k] (see SortCompare below), then (j) < (k). Here we use the 1.41 + notation old[j] to refer to the hypothetical result of calling the [ 1.42 + [Get]] method of this object with argument j before this step is 1.43 + executed, and the notation new[j] to refer to the hypothetical result 1.44 + of calling the [[Get]] method of this object with argument j after this 1.45 + step has been completely executed. A function is a consistent 1.46 + comparison function for a set of values if (a) for any two of those 1.47 + values (possibly the same value) considered as an ordered pair, it 1.48 + always returns the same value when given that pair of values as its 1.49 + two arguments, and the result of applying ToNumber to this value is 1.50 + not NaN; (b) when considered as a relation, where the pair (x, y) is 1.51 + considered to be in the relation if and only if applying the function 1.52 + to x and y and then applying ToNumber to the result produces a 1.53 + negative value, this relation is a partial order; and (c) when 1.54 + considered as a different relation, where the pair (x, y) is considered 1.55 + to be in the relation if and only if applying the function to x and y 1.56 + and then applying ToNumber to the result produces a zero value (of either 1.57 + sign), this relation is an equivalence relation. In this context, the 1.58 + phrase "x compares less than y" means applying Result(2) to x and y and 1.59 + then applying ToNumber to the result produces a negative value. 1.60 + 3.Return this object. 1.61 + 1.62 + When the SortCompare operator is called with two arguments x and y, the following steps are taken: 1.63 + 1.If x and y are both undefined, return +0. 1.64 + 2.If x is undefined, return 1. 1.65 + 3.If y is undefined, return 1. 1.66 + 4.If the argument comparefn was not provided in the call to sort, go to step 7. 1.67 + 5.Call comparefn with arguments x and y. 1.68 + 6.Return Result(5). 1.69 + 7.Call ToString(x). 1.70 + 8.Call ToString(y). 1.71 + 9.If Result(7) < Result(8), return 1. 1.72 + 10.If Result(7) > Result(8), return 1. 1.73 + 11.Return +0. 1.74 + 1.75 + Note that, because undefined always compared greater than any other value, undefined and nonexistent 1.76 + property values always sort to the end of the result. It is implementation-dependent whether or not such 1.77 + properties will exist or not at the end of the array when the sort is concluded. 1.78 + 1.79 + Note that the sort function is intentionally generic; it does not require that its this value be an Array object. 1.80 + Therefore it can be transferred to other kinds of objects for use as a method. Whether the sort function can be 1.81 + applied successfully to a host object is implementation dependent . 1.82 + 1.83 + Author: christine@netscape.com 1.84 + Date: 12 november 1997 1.85 +*/ 1.86 + 1.87 + 1.88 +var SECTION = "15.4.4.5-2"; 1.89 +var VERSION = "ECMA_1"; 1.90 +startTest(); 1.91 +var TITLE = "Array.prototype.sort(comparefn)"; 1.92 + 1.93 +writeHeaderToLog( SECTION + " "+ TITLE); 1.94 + 1.95 + 1.96 +var S = new Array(); 1.97 +var item = 0; 1.98 + 1.99 +// array is empty. 1.100 +S[item++] = "var A = new Array()"; 1.101 + 1.102 +// array contains one item 1.103 +S[item++] = "var A = new Array( true )"; 1.104 + 1.105 +// length of array is 2 1.106 +S[item++] = "var A = new Array( true, false, new Boolean(true), new Boolean(false), 'true', 'false' )"; 1.107 + 1.108 +S[item++] = "var A = new Array(); A[3] = 'undefined'; A[6] = null; A[8] = 'null'; A[0] = void 0"; 1.109 + 1.110 +S[item] = "var A = new Array( "; 1.111 + 1.112 +var limit = 0x0061; 1.113 +for ( var i = 0x007A; i >= limit; i-- ) { 1.114 + S[item] += "\'"+ String.fromCharCode(i) +"\'" ; 1.115 + if ( i > limit ) { 1.116 + S[item] += ","; 1.117 + } 1.118 +} 1.119 + 1.120 +S[item] += ")"; 1.121 + 1.122 +for ( var i = 0; i < S.length; i++ ) { 1.123 + CheckItems( S[i] ); 1.124 +} 1.125 + 1.126 +test(); 1.127 + 1.128 +function CheckItems( S ) { 1.129 + eval( S ); 1.130 + var E = Sort( A ); 1.131 + 1.132 + new TestCase( SECTION, 1.133 + S +"; A.sort(Compare); A.length", 1.134 + E.length, 1.135 + eval( S + "; A.sort(Compare); A.length") ); 1.136 + 1.137 + for ( var i = 0; i < E.length; i++ ) { 1.138 + new TestCase( 1.139 + SECTION, 1.140 + "A["+i+ "].toString()", 1.141 + E[i] +"", 1.142 + A[i] +""); 1.143 + 1.144 + if ( A[i] == void 0 && typeof A[i] == "undefined" ) { 1.145 + new TestCase( 1.146 + SECTION, 1.147 + "typeof A["+i+ "]", 1.148 + typeof E[i], 1.149 + typeof A[i] ); 1.150 + } 1.151 + } 1.152 +} 1.153 +function Object_1( value ) { 1.154 + this.array = value.split(","); 1.155 + this.length = this.array.length; 1.156 + for ( var i = 0; i < this.length; i++ ) { 1.157 + this[i] = eval(this.array[i]); 1.158 + } 1.159 + this.sort = Array.prototype.sort; 1.160 + this.getClass = Object.prototype.toString; 1.161 +} 1.162 +function Sort( a ) { 1.163 + var r1 = a.length; 1.164 + for ( i = 0; i < a.length; i++ ) { 1.165 + for ( j = i+1; j < a.length; j++ ) { 1.166 + var lo = a[i]; 1.167 + var hi = a[j]; 1.168 + var c = Compare( lo, hi ); 1.169 + if ( c == 1 ) { 1.170 + a[i] = hi; 1.171 + a[j] = lo; 1.172 + } 1.173 + } 1.174 + } 1.175 + return a; 1.176 +} 1.177 +function Compare( x, y ) { 1.178 + if ( x == void 0 && y == void 0 && typeof x == "undefined" && typeof y == "undefined" ) { 1.179 + return +0; 1.180 + } 1.181 + if ( x == void 0 && typeof x == "undefined" ) { 1.182 + return 1; 1.183 + } 1.184 + if ( y == void 0 && typeof y == "undefined" ) { 1.185 + return -1; 1.186 + } 1.187 + x = String(x); 1.188 + y = String(y); 1.189 + if ( x < y ) { 1.190 + return 1; 1.191 + } 1.192 + if ( x > y ) { 1.193 + return -1; 1.194 + } 1.195 + return 0; 1.196 +}