Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / Array / 15.4.4.5-2.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Mozilla Communicator client code, released
16  * March 31, 1998.
17  *
18  * The Initial Developer of the Original Code is
19  * Netscape Communications Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 1998
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39
40 /**
41    File Name:          15.4.4.5-2.js
42    ECMA Section:       Array.prototype.sort(comparefn)
43    Description:
44
45    This test file tests cases in which the compare function is supplied.
46    In this cases, the sort creates a reverse sort.
47
48    The elements of this array are sorted. The sort is not necessarily stable.
49    If comparefn is provided, it should be a function that accepts two arguments
50    x and y and returns a negative value if x < y, zero if x = y, or a positive
51    value if x > y.
52
53    1.   Call the [[Get]] method of this object with argument "length".
54    2.   Call ToUint32(Result(1)).
55    1.  Perform an implementation-dependent sequence of calls to the
56    [[Get]] , [[Put]], and [[Delete]] methods of this object and
57    toSortCompare (described below), where the first argument for each call
58    to [[Get]], [[Put]] , or [[Delete]] is a nonnegative integer less
59    than Result(2) and where the arguments for calls to SortCompare are
60    results of previous calls to the [[Get]] method. After this sequence
61    is complete, this object must have the following two properties.
62    (1) There must be some mathematical permutation of the nonnegative
63    integers less than Result(2), such that for every nonnegative integer
64    j less than Result(2), if property old[j] existed, then new[(j)] is
65    exactly the same value as old[j],. but if property old[j] did not exist,
66    then new[(j)] either does not exist or exists with value undefined.
67    (2) If comparefn is not supplied or is a consistent comparison
68    function for the elements of this array, then for all nonnegative
69    integers j and k, each less than Result(2), if old[j] compares less
70    than old[k] (see SortCompare below), then (j) < (k). Here we use the
71    notation old[j] to refer to the hypothetical result of calling the [
72    [Get]] method of this object with argument j before this step is
73    executed, and the notation new[j] to refer to the hypothetical result
74    of calling the [[Get]] method of this object with argument j after this
75    step has been completely executed. A function is a consistent
76    comparison function for a set of values if (a) for any two of those
77    values (possibly the same value) considered as an ordered pair, it
78    always returns the same value when given that pair of values as its
79    two arguments, and the result of applying ToNumber to this value is
80    not NaN; (b) when considered as a relation, where the pair (x, y) is
81    considered to be in the relation if and only if applying the function
82    to x and y and then applying ToNumber to the result produces a
83    negative value, this relation is a partial order; and (c) when
84    considered as a different relation, where the pair (x, y) is considered
85    to be in the relation if and only if applying the function to x and y
86    and then applying ToNumber to the result produces a zero value (of either
87    sign), this relation is an equivalence relation. In this context, the
88    phrase "x compares less than y" means applying Result(2) to x and y and
89    then applying ToNumber to the result produces a negative value.
90    3.Return this object.
91
92    When the SortCompare operator is called with two arguments x and y, the following steps are taken:
93    1.If x and y are both undefined, return +0.
94    2.If x is undefined, return 1.
95    3.If y is undefined, return 1.
96    4.If the argument comparefn was not provided in the call to sort, go to step 7.
97    5.Call comparefn with arguments x and y.
98    6.Return Result(5).
99    7.Call ToString(x).
100    8.Call ToString(y).
101    9.If Result(7) < Result(8), return 1.
102    10.If Result(7) > Result(8), return 1.
103    11.Return +0.
104
105    Note that, because undefined always compared greater than any other value, undefined and nonexistent
106    property values always sort to the end of the result. It is implementation-dependent whether or not such
107    properties will exist or not at the end of the array when the sort is concluded.
108
109    Note that the sort function is intentionally generic; it does not require that its this value be an Array object.
110    Therefore it can be transferred to other kinds of objects for use as a method. Whether the sort function can be
111    applied successfully to a host object is implementation dependent .
112
113    Author:             christine@netscape.com
114    Date:               12 november 1997
115 */
116
117
118 var SECTION = "15.4.4.5-2";
119 var VERSION = "ECMA_1";
120 startTest();
121 var TITLE   = "Array.prototype.sort(comparefn)";
122
123 writeHeaderToLog( SECTION + " "+ TITLE);
124
125
126 var S = new Array();
127 var item = 0;
128
129 // array is empty.
130 S[item++] = "var A = new Array()";
131
132 // array contains one item
133 S[item++] = "var A = new Array( true )";
134
135 // length of array is 2
136 S[item++] = "var A = new Array( true, false, new Boolean(true), new Boolean(false), 'true', 'false' )";
137
138 S[item++] = "var A = new Array(); A[3] = 'undefined'; A[6] = null; A[8] = 'null'; A[0] = void 0";
139
140 S[item] = "var A = new Array( ";
141
142 var limit = 0x0061;
143 for ( var i = 0x007A; i >= limit; i-- ) {
144   S[item] += "\'"+ String.fromCharCode(i) +"\'" ;
145   if ( i > limit ) {
146     S[item] += ",";
147   }
148 }
149
150 S[item] += ")";
151
152 for ( var i = 0; i < S.length; i++ ) {
153   CheckItems( S[i] );
154 }
155
156 test();
157
158 function CheckItems( S ) {
159   eval( S );
160   var E = Sort( A );
161
162   new TestCase(   SECTION,
163                   S +";  A.sort(Compare); A.length",
164                   E.length,
165                   eval( S + "; A.sort(Compare); A.length") );
166
167   for ( var i = 0; i < E.length; i++ ) {
168     new TestCase(
169       SECTION,
170       "A["+i+ "].toString()",
171       E[i] +"",
172       A[i] +"");
173
174     if ( A[i] == void 0 && typeof A[i] == "undefined" ) {
175       new TestCase(
176         SECTION,
177         "typeof A["+i+ "]",
178         typeof E[i],
179         typeof A[i] );
180     }
181   }
182 }
183 function Object_1( value ) {
184   this.array = value.split(",");
185   this.length = this.array.length;
186   for ( var i = 0; i < this.length; i++ ) {
187     this[i] = eval(this.array[i]);
188   }
189   this.sort = Array.prototype.sort;
190   this.getClass = Object.prototype.toString;
191 }
192 function Sort( a ) {
193   var r1 = a.length;
194   for ( i = 0; i < a.length; i++ ) {
195     for ( j = i+1; j < a.length; j++ ) {
196       var lo = a[i];
197       var hi = a[j];
198       var c = Compare( lo, hi );
199       if ( c == 1 ) {
200         a[i] = hi;
201         a[j] = lo;
202       }
203     }
204   }
205   return a;
206 }
207 function Compare( x, y ) {
208   if ( x == void 0 && y == void 0  && typeof x == "undefined" && typeof y == "undefined" ) {
209     return +0;
210   }
211   if ( x == void 0  && typeof x == "undefined" ) {
212     return 1;
213   }
214   if ( y == void 0 && typeof y == "undefined" ) {
215     return -1;
216   }
217   x = String(x);
218   y = String(y);
219   if ( x < y ) {
220     return 1;
221   }
222   if ( x > y ) {
223     return -1;
224   }
225   return 0;
226 }