1 /* The contents of this file are subject to the Netscape Public
2 * License Version 1.1 (the "License"); you may not use this file
3 * except in compliance with the License. You may obtain a copy of
4 * the License at http://www.mozilla.org/NPL/
6 * Software distributed under the License is distributed on an "AS
7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8 * implied. See the License for the specific language governing
9 * rights and limitations under the License.
11 * The Original Code is Mozilla Communicator client code, released March
14 * The Initial Developer of the Original Code is Netscape Communications
15 * Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
23 File Name: 15.4.4.5.js
24 ECMA Section: Array.prototype.sort(comparefn)
27 This test file tests cases in which the compare function is not supplied.
29 The elements of this array are sorted. The sort is not necessarily stable.
30 If comparefn is provided, it should be a function that accepts two arguments
31 x and y and returns a negative value if x < y, zero if x = y, or a positive
34 1. Call the [[Get]] method of this object with argument "length".
35 2. Call ToUint32(Result(1)).
36 1. Perform an implementation-dependent sequence of calls to the
37 [[Get]] , [[Put]], and [[Delete]] methods of this object and
38 toSortCompare (described below), where the first argument for each call
39 to [[Get]], [[Put]] , or [[Delete]] is a nonnegative integer less
40 than Result(2) and where the arguments for calls to SortCompare are
41 results of previous calls to the [[Get]] method. After this sequence
42 is complete, this object must have the following two properties.
43 (1) There must be some mathematical permutation of the nonnegative
44 integers less than Result(2), such that for every nonnegative integer
45 j less than Result(2), if property old[j] existed, then new[(j)] is
46 exactly the same value as old[j],. but if property old[j] did not exist,
47 then new[(j)] either does not exist or exists with value undefined.
48 (2) If comparefn is not supplied or is a consistent comparison
49 function for the elements of this array, then for all nonnegative
50 integers j and k, each less than Result(2), if old[j] compares less
51 than old[k] (see SortCompare below), then (j) < (k). Here we use the
52 notation old[j] to refer to the hypothetical result of calling the [
53 [Get]] method of this object with argument j before this step is
54 executed, and the notation new[j] to refer to the hypothetical result
55 of calling the [[Get]] method of this object with argument j after this
56 step has been completely executed. A function is a consistent
57 comparison function for a set of values if (a) for any two of those
58 values (possibly the same value) considered as an ordered pair, it
59 always returns the same value when given that pair of values as its
60 two arguments, and the result of applying ToNumber to this value is
61 not NaN; (b) when considered as a relation, where the pair (x, y) is
62 considered to be in the relation if and only if applying the function
63 to x and y and then applying ToNumber to the result produces a
64 negative value, this relation is a partial order; and (c) when
65 considered as a different relation, where the pair (x, y) is considered
66 to be in the relation if and only if applying the function to x and y
67 and then applying ToNumber to the result produces a zero value (of either
68 sign), this relation is an equivalence relation. In this context, the
69 phrase "x compares less than y" means applying Result(2) to x and y and
70 then applying ToNumber to the result produces a negative value.
73 When the SortCompare operator is called with two arguments x and y, the following steps are taken:
74 1.If x and y are both undefined, return +0.
75 2.If x is undefined, return 1.
76 3.If y is undefined, return 1.
77 4.If the argument comparefn was not provided in the call to sort, go to step 7.
78 5.Call comparefn with arguments x and y.
82 9.If Result(7) < Result(8), return 1.
83 10.If Result(7) > Result(8), return 1.
86 Note that, because undefined always compared greater than any other value, undefined and nonexistent
87 property values always sort to the end of the result. It is implementation-dependent whether or not such
88 properties will exist or not at the end of the array when the sort is concluded.
90 Note that the sort function is intentionally generic; it does not require that its this value be an Array object.
91 Therefore it can be transferred to other kinds of objects for use as a method. Whether the sort function can be
92 applied successfully to a host object is implementation dependent .
94 Author: christine@netscape.com
95 Date: 12 november 1997
99 var SECTION
= "15.4.4.5-1";
100 var VERSION
= "ECMA_1";
102 var TITLE
= "Array.prototype.sort(comparefn)";
104 writeHeaderToLog( SECTION
+ " "+ TITLE
);
106 var testcases
= new Array();
111 for ( tc
=0; tc
< testcases
.length
; tc
++ ) {
112 testcases
[tc
].passed
= writeTestCaseResult(
113 testcases
[tc
].expect
,
114 testcases
[tc
].actual
,
115 testcases
[tc
].description
+" = "+
116 testcases
[tc
].actual
);
118 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
121 return ( testcases
);
123 function getTestCases() {
128 S
[item
++] = "var A = new Array()";
130 // array contains one item
131 S
[item
++] = "var A = new Array( true )";
133 // length of array is 2
134 S
[item
++] = "var A = new Array( true, false, new Boolean(true), new Boolean(false), 'true', 'false' )";
136 S
[item
++] = "var A = new Array(); A[3] = 'undefined'; A[6] = null; A[8] = 'null'; A[0] = void 0";
138 S
[item
] = "var A = new Array( ";
141 for ( var i
= 0x007A; i
>= limit
; i
-- ) {
142 S
[item
] += "\'"+ String
.fromCharCode(i
) +"\'" ;
152 for ( var i
= 0; i
< S
.length
; i
++ ) {
156 function CheckItems( S
) {
160 testcases
[testcases
.length
] = new TestCase( SECTION
,
161 S
+"; A.sort(); A.length",
163 eval( S
+ "; A.sort(); A.length") );
165 for ( var i
= 0; i
< E
.length
; i
++ ) {
166 testcases
[testcases
.length
] = new TestCase(
168 "A["+i
+ "].toString()",
172 if ( A
[i
] == void 0 && typeof A
[i
] == "undefined" ) {
173 testcases
[testcases
.length
] = new TestCase(
181 function Object_1( value
) {
182 this.array
= value
.split(",");
183 this.length
= this.array
.length
;
184 for ( var i
= 0; i
< this.length
; i
++ ) {
185 this[i
] = eval(this.array
[i
]);
187 this.sort
= Array
.prototype.sort
;
188 this.getClass
= Object
.prototype.toString
;
191 for ( i
= 0; i
< a
.length
; i
++ ) {
192 for ( j
= i
+1; j
< a
.length
; j
++ ) {
195 var c
= Compare( lo
, hi
);
204 function Compare( x
, y
) {
205 if ( x
== void 0 && y
== void 0 && typeof x
== "undefined" && typeof y
== "undefined" ) {
208 if ( x
== void 0 && typeof x
== "undefined" ) {
211 if ( y
== void 0 && typeof y
== "undefined" ) {