Debian Codegraph @

rhino/testsrc/tests/ecma/Array/15.4.4.5-3.js

raw symbols SCIP
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 gTestfile = '15.4.4.5-3.js';
7
8 /**
9 File Name: 15.4.4.5-3.js
10 ECMA Section: Array.prototype.sort(comparefn)
11 Description:
12
13 This is a regression test for
14 http://scopus/bugsplat/show_bug.cgi?id=117144
15
16 Verify that sort is successfull, even if the sort compare function returns
17 a very large negative or positive value.
18
19 Author: [email protected]
20 Date: 12 november 1997
21 */
22
23
24 var SECTION = "15.4.4.5-3";
25 var VERSION = "ECMA_1";
26 startTest();
27 var TITLE = "Array.prototype.sort(comparefn)";
28
29 writeHeaderToLog( SECTION + " "+ TITLE);
30
31 var array = new Array();
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 SECTION,
77 "testarr1["+i+"]",
78 realarr[i],
79 testarr1[i] );
80 }
81
82 for ( var i=0; i < array.length; i++) {
83 new TestCase(
84 SECTION,
85 "testarr2["+i+"]",
86 realarr[i],
87 testarr2[i] );
88 }
89
90 for ( var i=0; i < array.length; i++) {
91 new TestCase(
92 SECTION,
93 "testarr3["+i+"]",
94 realarr[i],
95 testarr3[i] );
96 }
97
98 for ( var i=0; i < array.length; i++) {
99 new TestCase(
100 SECTION,
101 "testarr4["+i+"]",
102 stringarr[i].toString(),
103 testarr4[i].toString() );
104 }
105
106 test();
107
108 function comparefn1( x, y ) {
109 return x - y;
110 }
111 function comparefn2( x, y ) {
112 return x.valueOf() - y.valueOf();
113 }
114 function realsort( x, y ) {
115 return ( x.valueOf() == y.valueOf() ? 0 : ( x.valueOf() > y.valueOf() ? 1 : -1 ) );
116 }
117 function comparefn3( x, y ) {
118 return ( x == y ? 0 : ( x > y ? 1: -1 ) );
119 }
120 function clone( source, target ) {
121 for (i = 0; i < source.length; i++ ) {
122 target[i] = source[i];
123 }
124 }
125 function stringsort( x, y ) {
126 for ( var i = 0; i < x.toString().length; i++ ) {
127 var d = (x.toString()).charCodeAt(i) - (y.toString()).charCodeAt(i);
128 if ( d > 0 ) {
129 return 1;
130 } else {
131 if ( d < 0 ) {
132 return -1;
133 } else {
134 continue;
135 }
136 }
137
138 var d = x.length - y.length;
139
140 if ( d > 0 ) {
141 return 1;
142 } else {
143 if ( d < 0 ) {
144 return -1;
145 }
146 }
147 }
148 return 0;
149 }