Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / Array / 15.4.4.5-3.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-3.js
42    ECMA Section:       Array.prototype.sort(comparefn)
43    Description:
44
45    This is a regression test for
46    http://scopus/bugsplat/show_bug.cgi?id=117144
47
48    Verify that sort is successfull, even if the sort compare function returns
49    a very large negative or positive value.
50
51    Author:             christine@netscape.com
52    Date:               12 november 1997
53 */
54
55
56 var SECTION = "15.4.4.5-3";
57 var VERSION = "ECMA_1";
58 startTest();
59 var TITLE   = "Array.prototype.sort(comparefn)";
60
61 writeHeaderToLog( SECTION + " "+ TITLE);
62
63 var array = new Array();
64
65 array[array.length] = new Date( TIME_2000 * Math.PI );
66 array[array.length] = new Date( TIME_2000 * 10 );
67 array[array.length] = new Date( TIME_1900 + TIME_1900  );
68 array[array.length] = new Date(0);
69 array[array.length] = new Date( TIME_2000 );
70 array[array.length] = new Date( TIME_1900 + TIME_1900 +TIME_1900 );
71 array[array.length] = new Date( TIME_1900 * Math.PI );
72 array[array.length] = new Date( TIME_1900 * 10 );
73 array[array.length] = new Date( TIME_1900 );
74 array[array.length] = new Date( TIME_2000 + TIME_2000 );
75 array[array.length] = new Date( 1899, 0, 1 );
76 array[array.length] = new Date( 2000, 1, 29 );
77 array[array.length] = new Date( 2000, 0, 1 );
78 array[array.length] = new Date( 1999, 11, 31 );
79
80 var testarr1 = new Array();
81 clone( array, testarr1 );
82 testarr1.sort( comparefn1 );
83
84 var testarr2 = new Array();
85 clone( array, testarr2 );
86 testarr2.sort( comparefn2 );
87
88 testarr3 = new Array();
89 clone( array, testarr3 );
90 testarr3.sort( comparefn3 );
91
92 // when there's no sort function, sort sorts by the toString value of Date.
93
94 var testarr4 = new Array();
95 clone( array, testarr4 );
96 testarr4.sort();
97
98 var realarr = new Array();
99 clone( array, realarr );
100 realarr.sort( realsort );
101
102 var stringarr = new Array();
103 clone( array, stringarr );
104 stringarr.sort( stringsort );
105
106 for ( var i = 0; i < array.length; i++) {
107   new TestCase(
108     SECTION,
109     "testarr1["+i+"]",
110     realarr[i],
111     testarr1[i] );
112 }
113
114 for ( var i=0; i < array.length; i++) {
115   new TestCase(
116     SECTION,
117     "testarr2["+i+"]",
118     realarr[i],
119     testarr2[i] );
120 }
121
122 for ( var i=0; i < array.length; i++) {
123   new TestCase(
124     SECTION,
125     "testarr3["+i+"]",
126     realarr[i],
127     testarr3[i] );
128 }
129
130 for ( var i=0; i < array.length; i++) {
131   new TestCase(
132     SECTION,
133     "testarr4["+i+"]",
134     stringarr[i].toString(),
135     testarr4[i].toString() );
136 }
137
138 test();
139
140 function comparefn1( x, y ) {
141   return x - y;
142 }
143 function comparefn2( x, y ) {
144   return x.valueOf() - y.valueOf();
145 }
146 function realsort( x, y ) {
147   return ( x.valueOf() == y.valueOf() ? 0 : ( x.valueOf() > y.valueOf() ? 1 : -1 ) );
148 }
149 function comparefn3( x, y ) {
150   return ( x == y ? 0 : ( x > y ? 1: -1 ) );
151 }
152 function clone( source, target ) {
153   for (i = 0; i < source.length; i++ ) {
154     target[i] = source[i];
155   }
156 }
157 function stringsort( x, y ) {
158   for ( var i = 0; i < x.toString().length; i++ ) {
159     var d = (x.toString()).charCodeAt(i) - (y.toString()).charCodeAt(i);
160     if ( d > 0 ) {
161       return 1;
162     } else {
163       if ( d < 0 ) {
164         return -1;
165       } else {
166         continue;
167       }
168     }
169
170     var d = x.length - y.length;
171
172     if  ( d > 0 ) {
173       return 1;
174     } else {
175       if ( d < 0 ) {
176         return -1;
177       }
178     }
179   }
180   return 0;
181 }