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