|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 15.4.4.5-3.js |
|
9 ECMA Section: Array.prototype.sort(comparefn) |
|
10 Description: |
|
11 |
|
12 This is a regression test for |
|
13 http://scopus/bugsplat/show_bug.cgi?id=117144 |
|
14 |
|
15 Verify that sort is successfull, even if the sort compare function returns |
|
16 a very large negative or positive value. |
|
17 |
|
18 Author: christine@netscape.com |
|
19 Date: 12 november 1997 |
|
20 */ |
|
21 |
|
22 |
|
23 var SECTION = "15.4.4.5-3"; |
|
24 var VERSION = "ECMA_1"; |
|
25 startTest(); |
|
26 var TITLE = "Array.prototype.sort(comparefn)"; |
|
27 |
|
28 writeHeaderToLog( SECTION + " "+ TITLE); |
|
29 |
|
30 var array = new Array(); |
|
31 |
|
32 array[array.length] = new Date( TIME_2000 * Math.PI ); |
|
33 array[array.length] = new Date( TIME_2000 * 10 ); |
|
34 array[array.length] = new Date( TIME_1900 + TIME_1900 ); |
|
35 array[array.length] = new Date(0); |
|
36 array[array.length] = new Date( TIME_2000 ); |
|
37 array[array.length] = new Date( TIME_1900 + TIME_1900 +TIME_1900 ); |
|
38 array[array.length] = new Date( TIME_1900 * Math.PI ); |
|
39 array[array.length] = new Date( TIME_1900 * 10 ); |
|
40 array[array.length] = new Date( TIME_1900 ); |
|
41 array[array.length] = new Date( TIME_2000 + TIME_2000 ); |
|
42 array[array.length] = new Date( 1899, 0, 1 ); |
|
43 array[array.length] = new Date( 2000, 1, 29 ); |
|
44 array[array.length] = new Date( 2000, 0, 1 ); |
|
45 array[array.length] = new Date( 1999, 11, 31 ); |
|
46 |
|
47 var testarr1 = new Array(); |
|
48 clone( array, testarr1 ); |
|
49 testarr1.sort( comparefn1 ); |
|
50 |
|
51 var testarr2 = new Array(); |
|
52 clone( array, testarr2 ); |
|
53 testarr2.sort( comparefn2 ); |
|
54 |
|
55 testarr3 = new Array(); |
|
56 clone( array, testarr3 ); |
|
57 testarr3.sort( comparefn3 ); |
|
58 |
|
59 // when there's no sort function, sort sorts by the toString value of Date. |
|
60 |
|
61 var testarr4 = new Array(); |
|
62 clone( array, testarr4 ); |
|
63 testarr4.sort(); |
|
64 |
|
65 var realarr = new Array(); |
|
66 clone( array, realarr ); |
|
67 realarr.sort( realsort ); |
|
68 |
|
69 var stringarr = new Array(); |
|
70 clone( array, stringarr ); |
|
71 stringarr.sort( stringsort ); |
|
72 |
|
73 for ( var i = 0; i < array.length; i++) { |
|
74 new TestCase( |
|
75 SECTION, |
|
76 "testarr1["+i+"]", |
|
77 realarr[i], |
|
78 testarr1[i] ); |
|
79 } |
|
80 |
|
81 for ( var i=0; i < array.length; i++) { |
|
82 new TestCase( |
|
83 SECTION, |
|
84 "testarr2["+i+"]", |
|
85 realarr[i], |
|
86 testarr2[i] ); |
|
87 } |
|
88 |
|
89 for ( var i=0; i < array.length; i++) { |
|
90 new TestCase( |
|
91 SECTION, |
|
92 "testarr3["+i+"]", |
|
93 realarr[i], |
|
94 testarr3[i] ); |
|
95 } |
|
96 |
|
97 for ( var i=0; i < array.length; i++) { |
|
98 new TestCase( |
|
99 SECTION, |
|
100 "testarr4["+i+"]", |
|
101 stringarr[i].toString(), |
|
102 testarr4[i].toString() ); |
|
103 } |
|
104 |
|
105 test(); |
|
106 |
|
107 function comparefn1( x, y ) { |
|
108 return x - y; |
|
109 } |
|
110 function comparefn2( x, y ) { |
|
111 return x.valueOf() - y.valueOf(); |
|
112 } |
|
113 function realsort( x, y ) { |
|
114 return ( x.valueOf() == y.valueOf() ? 0 : ( x.valueOf() > y.valueOf() ? 1 : -1 ) ); |
|
115 } |
|
116 function comparefn3( x, y ) { |
|
117 return ( x == y ? 0 : ( x > y ? 1: -1 ) ); |
|
118 } |
|
119 function clone( source, target ) { |
|
120 for (i = 0; i < source.length; i++ ) { |
|
121 target[i] = source[i]; |
|
122 } |
|
123 } |
|
124 function stringsort( x, y ) { |
|
125 for ( var i = 0; i < x.toString().length; i++ ) { |
|
126 var d = (x.toString()).charCodeAt(i) - (y.toString()).charCodeAt(i); |
|
127 if ( d > 0 ) { |
|
128 return 1; |
|
129 } else { |
|
130 if ( d < 0 ) { |
|
131 return -1; |
|
132 } else { |
|
133 continue; |
|
134 } |
|
135 } |
|
136 |
|
137 var d = x.length - y.length; |
|
138 |
|
139 if ( d > 0 ) { |
|
140 return 1; |
|
141 } else { |
|
142 if ( d < 0 ) { |
|
143 return -1; |
|
144 } |
|
145 } |
|
146 } |
|
147 return 0; |
|
148 } |