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/
5 *
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.
10 *
11 * The Original Code is Mozilla Communicator client code, released March
12 * 31, 1998.
13 *
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
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 */
22/**
23    File Name:          15.4.4.5.js
24    ECMA Section:       Array.prototype.sort(comparefn)
25    Description:
26
27    This test file tests cases in which the compare function is not supplied.
28
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
32    value if x > y.
33
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.
71    3.Return this object.
72
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.
79       6.Return Result(5).
80       7.Call ToString(x).
81       8.Call ToString(y).
82       9.If Result(7) < Result(8), return 1.
83      10.If Result(7) > Result(8), return 1.
84      11.Return +0.
85
86Note that, because undefined always compared greater than any other value, undefined and nonexistent
87property values always sort to the end of the result. It is implementation-dependent whether or not such
88properties will exist or not at the end of the array when the sort is concluded.
89
90Note that the sort function is intentionally generic; it does not require that its this value be an Array object.
91Therefore it can be transferred to other kinds of objects for use as a method. Whether the sort function can be
92applied successfully to a host object is implementation dependent .
93
94    Author:             christine@netscape.com
95    Date:               12 november 1997
96*/
97
98
99    var SECTION = "15.4.4.5-1";
100    var VERSION = "ECMA_1";
101    startTest();
102    var TITLE   = "Array.prototype.sort(comparefn)";
103
104    writeHeaderToLog( SECTION + " "+ TITLE);
105
106    var testcases = new Array();
107    getTestCases();
108    test();
109
110function test() {
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 );
117
118        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
119    }
120    stopTest();
121    return ( testcases );
122}
123function getTestCases() {
124    var S = new Array();
125    var item = 0;
126
127    // array is empty.
128    S[item++] = "var A = new Array()";
129
130    // array contains one item
131    S[item++] = "var A = new Array( true )";
132
133    // length of array is 2
134    S[item++] = "var A = new Array( true, false, new Boolean(true), new Boolean(false), 'true', 'false' )";
135
136    S[item++] = "var A = new Array(); A[3] = 'undefined'; A[6] = null; A[8] = 'null'; A[0] = void 0";
137
138    S[item] = "var A = new Array( ";
139
140    var limit = 0x0061;
141    for ( var i = 0x007A; i >= limit; i-- ) {
142        S[item] += "\'"+ String.fromCharCode(i) +"\'" ;
143        if ( i > limit ) {
144            S[item] += ",";
145        }
146    }
147
148    S[item] += ")";
149
150    item++;
151
152    for ( var i = 0; i < S.length; i++ ) {
153        CheckItems( S[i] );
154    }
155}
156function CheckItems( S ) {
157    eval( S );
158    var E = Sort( A );
159
160    testcases[testcases.length] = new TestCase(   SECTION,
161                                    S +";  A.sort(); A.length",
162                                    E.length,
163                                    eval( S + "; A.sort(); A.length") );
164
165    for ( var i = 0; i < E.length; i++ ) {
166        testcases[testcases.length] = new TestCase(
167                                            SECTION,
168                                            "A["+i+ "].toString()",
169                                            E[i] +"",
170                                            A[i] +"");
171
172        if ( A[i] == void 0 && typeof A[i] == "undefined" ) {
173            testcases[testcases.length] = new TestCase(
174                                            SECTION,
175                                            "typeof A["+i+ "]",
176                                            typeof E[i],
177                                            typeof A[i] );
178        }
179    }
180}
181function 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]);
186    }
187    this.sort = Array.prototype.sort;
188    this.getClass = Object.prototype.toString;
189}
190function Sort( a ) {
191    for ( i = 0; i < a.length; i++ ) {
192        for ( j = i+1; j < a.length; j++ ) {
193            var lo = a[i];
194            var hi = a[j];
195            var c = Compare( lo, hi );
196            if ( c == 1 ) {
197                a[i] = hi;
198                a[j] = lo;
199            }
200        }
201    }
202    return a;
203}
204function Compare( x, y ) {
205    if ( x == void 0 && y == void 0  && typeof x == "undefined" && typeof y == "undefined" ) {
206        return +0;
207    }
208    if ( x == void 0  && typeof x == "undefined" ) {
209        return 1;
210    }
211    if ( y == void 0 && typeof y == "undefined" ) {
212        return -1;
213    }
214    x = String(x);
215    y = String(y);
216    if ( x < y ) {
217        return -1;
218    }
219    if ( x > y ) {
220        return 1;
221    }
222    return 0;
223}
224