1/* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22/** 23 File Name: 15.4.4.5-3.js 24 ECMA Section: Array.prototype.sort(comparefn) 25 Description: 26 27 This is a regression test for 28 http://scopus/bugsplat/show_bug.cgi?id=117144 29 30 Verify that sort is successfull, even if the sort compare function returns 31 a very large negative or positive value. 32 33 Author: christine@netscape.com 34 Date: 12 november 1997 35*/ 36 37 38 var SECTION = "15.4.4.5-3"; 39 var VERSION = "ECMA_1"; 40 startTest(); 41 var TITLE = "Array.prototype.sort(comparefn)"; 42 43 writeHeaderToLog( SECTION + " "+ TITLE); 44 45 var testcases = new Array(); 46 getTestCases(); 47 test(); 48 49function test() { 50 for ( tc=0; tc < testcases.length; tc++ ) { 51 testcases[tc].passed = writeTestCaseResult( 52 testcases[tc].expect, 53 testcases[tc].actual, 54 testcases[tc].description +" = "+ 55 testcases[tc].actual ); 56 57 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 58 } 59 stopTest(); 60 return ( testcases ); 61} 62 63function getTestCases() { 64 var array = new Array(); 65 66 array[array.length] = new Date( TIME_2000 * Math.PI ); 67 array[array.length] = new Date( TIME_2000 * 10 ); 68 array[array.length] = new Date( TIME_1900 + TIME_1900 ); 69 array[array.length] = new Date(0); 70 array[array.length] = new Date( TIME_2000 ); 71 array[array.length] = new Date( TIME_1900 + TIME_1900 +TIME_1900 ); 72 array[array.length] = new Date( TIME_1900 * Math.PI ); 73 array[array.length] = new Date( TIME_1900 * 10 ); 74 array[array.length] = new Date( TIME_1900 ); 75 array[array.length] = new Date( TIME_2000 + TIME_2000 ); 76 array[array.length] = new Date( 1899, 0, 1 ); 77 array[array.length] = new Date( 2000, 1, 29 ); 78 array[array.length] = new Date( 2000, 0, 1 ); 79 array[array.length] = new Date( 1999, 11, 31 ); 80 81 var testarr1 = new Array() 82 clone( array, testarr1 ); 83 testarr1.sort( comparefn1 ); 84 85 var testarr2 = new Array() 86 clone( array, testarr2 ); 87 testarr2.sort( comparefn2 ); 88 89 testarr3 = new Array(); 90 clone( array, testarr3 ); 91 testarr3.sort( comparefn3 ); 92 93 // when there's no sort function, sort sorts by the toString value of Date. 94 95 var testarr4 = new Array() 96 clone( array, testarr4 ); 97 testarr4.sort(); 98 99 var realarr = new Array(); 100 clone( array, realarr ); 101 realarr.sort( realsort ); 102 103 var stringarr = new Array(); 104 clone( array, stringarr ); 105 stringarr.sort( stringsort ); 106 107 for ( var i = 0, tc = 0; i < array.length; i++, tc++) { 108 testcases[tc] = new TestCase( 109 SECTION, 110 "testarr1["+i+"]", 111 realarr[i], 112 testarr1[i] ); 113 } 114 115 for ( var i=0; i < array.length; i++, tc++) { 116 testcases[tc] = new TestCase( 117 SECTION, 118 "testarr2["+i+"]", 119 realarr[i], 120 testarr2[i] ); 121 } 122 123 for ( var i=0; i < array.length; i++, tc++) { 124 testcases[tc] = new TestCase( 125 SECTION, 126 "testarr3["+i+"]", 127 realarr[i], 128 testarr3[i] ); 129 } 130 131 for ( var i=0; i < array.length; i++, tc++) { 132 testcases[tc] = new TestCase( 133 SECTION, 134 "testarr4["+i+"]", 135 stringarr[i].toString(), 136 testarr4[i].toString() ); 137 } 138 139} 140function comparefn1( x, y ) { 141 return x - y; 142} 143function comparefn2( x, y ) { 144 return x.valueOf() - y.valueOf(); 145} 146function realsort( x, y ) { 147 return ( x.valueOf() == y.valueOf() ? 0 : ( x.valueOf() > y.valueOf() ? 1 : -1 ) ); 148} 149function comparefn3( x, y ) { 150 return ( +x == +y ? 0 : ( x > y ? 1 : -1 ) ); 151} 152function clone( source, target ) { 153 for (i = 0; i < source.length; i++ ) { 154 target[i] = source[i]; 155 } 156} 157function 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}