Debian Codegraph @

js/src/tests/non262/Array/15.4.4.5-3.js

raw
1 /* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 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: [email protected]
19 Date: 12 november 1997
20 */
21
22
23 var SECTION = "15.4.4.5-3";
24 var TITLE = "Array.prototype.sort(comparefn)";
25
26 writeHeaderToLog( SECTION + " "+ TITLE);
27
28 var array = new Array();
29
30 var TIME_2000 = 946684800000;
31 var TIME_1900 = -2208988800000;
32
33 array[array.length] = new Date( TIME_2000 * Math.PI );
34 array[array.length] = new Date( TIME_2000 * 10 );
35 array[array.length] = new Date( TIME_1900 + TIME_1900 );
36 array[array.length] = new Date(0);
37 array[array.length] = new Date( TIME_2000 );
38 array[array.length] = new Date( TIME_1900 + TIME_1900 +TIME_1900 );
39 array[array.length] = new Date( TIME_1900 * Math.PI );
40 array[array.length] = new Date( TIME_1900 * 10 );
41 array[array.length] = new Date( TIME_1900 );
42 array[array.length] = new Date( TIME_2000 + TIME_2000 );
43 array[array.length] = new Date( 1899, 0, 1 );
44 array[array.length] = new Date( 2000, 1, 29 );
45 array[array.length] = new Date( 2000, 0, 1 );
46 array[array.length] = new Date( 1999, 11, 31 );
47
48 var testarr1 = new Array();
49 clone( array, testarr1 );
50 testarr1.sort( comparefn1 );
51
52 var testarr2 = new Array();
53 clone( array, testarr2 );
54 testarr2.sort( comparefn2 );
55
56 testarr3 = new Array();
57 clone( array, testarr3 );
58 testarr3.sort( comparefn3 );
59
60 // when there's no sort function, sort sorts by the toString value of Date.
61
62 var testarr4 = new Array();
63 clone( array, testarr4 );
64 testarr4.sort();
65
66 var realarr = new Array();
67 clone( array, realarr );
68 realarr.sort( realsort );
69
70 var stringarr = new Array();
71 clone( array, stringarr );
72 stringarr.sort( stringsort );
73
74 for ( var i = 0; i < array.length; i++) {
75 new TestCase(
76 "testarr1["+i+"]",
77 realarr[i],
78 testarr1[i] );
79 }
80
81 for ( var i=0; i < array.length; i++) {
82 new TestCase(
83 "testarr2["+i+"]",
84 realarr[i],
85 testarr2[i] );
86 }
87
88 for ( var i=0; i < array.length; i++) {
89 new TestCase(
90 "testarr3["+i+"]",
91 realarr[i],
92 testarr3[i] );
93 }
94
95 for ( var i=0; i < array.length; i++) {
96 new TestCase(
97 "testarr4["+i+"]",
98 stringarr[i].toString(),
99 testarr4[i].toString() );
100 }
101
102 test();
103
104 function comparefn1( x, y ) {
105 return x - y;
106 }
107 function comparefn2( x, y ) {
108 return x.valueOf() - y.valueOf();
109 }
110 function realsort( x, y ) {
111 return ( x.valueOf() == y.valueOf() ? 0 : ( x.valueOf() > y.valueOf() ? 1 : -1 ) );
112 }
113 function comparefn3( x, y ) {
114 return ( x == y ? 0 : ( x > y ? 1: -1 ) );
115 }
116 function clone( source, target ) {
117 for (i = 0; i < source.length; i++ ) {
118 target[i] = source[i];
119 }
120 }
121 function stringsort( x, y ) {
122 for ( var i = 0; i < x.toString().length; i++ ) {
123 var d = (x.toString()).charCodeAt(i) - (y.toString()).charCodeAt(i);
124 if ( d > 0 ) {
125 return 1;
126 } else {
127 if ( d < 0 ) {
128 return -1;
129 } else {
130 continue;
131 }
132 }
133
134 var d = x.length - y.length;
135
136 if ( d > 0 ) {
137 return 1;
138 } else {
139 if ( d < 0 ) {
140 return -1;
141 }
142 }
143 }
144 return 0;
145 }