michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: File Name: 15.4.4.5-3.js michael@0: ECMA Section: Array.prototype.sort(comparefn) michael@0: Description: michael@0: michael@0: This is a regression test for michael@0: http://scopus/bugsplat/show_bug.cgi?id=117144 michael@0: michael@0: Verify that sort is successfull, even if the sort compare function returns michael@0: a very large negative or positive value. michael@0: michael@0: Author: christine@netscape.com michael@0: Date: 12 november 1997 michael@0: */ michael@0: michael@0: michael@0: var SECTION = "15.4.4.5-3"; michael@0: var VERSION = "ECMA_1"; michael@0: startTest(); michael@0: var TITLE = "Array.prototype.sort(comparefn)"; michael@0: michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: var array = new Array(); michael@0: michael@0: array[array.length] = new Date( TIME_2000 * Math.PI ); michael@0: array[array.length] = new Date( TIME_2000 * 10 ); michael@0: array[array.length] = new Date( TIME_1900 + TIME_1900 ); michael@0: array[array.length] = new Date(0); michael@0: array[array.length] = new Date( TIME_2000 ); michael@0: array[array.length] = new Date( TIME_1900 + TIME_1900 +TIME_1900 ); michael@0: array[array.length] = new Date( TIME_1900 * Math.PI ); michael@0: array[array.length] = new Date( TIME_1900 * 10 ); michael@0: array[array.length] = new Date( TIME_1900 ); michael@0: array[array.length] = new Date( TIME_2000 + TIME_2000 ); michael@0: array[array.length] = new Date( 1899, 0, 1 ); michael@0: array[array.length] = new Date( 2000, 1, 29 ); michael@0: array[array.length] = new Date( 2000, 0, 1 ); michael@0: array[array.length] = new Date( 1999, 11, 31 ); michael@0: michael@0: var testarr1 = new Array(); michael@0: clone( array, testarr1 ); michael@0: testarr1.sort( comparefn1 ); michael@0: michael@0: var testarr2 = new Array(); michael@0: clone( array, testarr2 ); michael@0: testarr2.sort( comparefn2 ); michael@0: michael@0: testarr3 = new Array(); michael@0: clone( array, testarr3 ); michael@0: testarr3.sort( comparefn3 ); michael@0: michael@0: // when there's no sort function, sort sorts by the toString value of Date. michael@0: michael@0: var testarr4 = new Array(); michael@0: clone( array, testarr4 ); michael@0: testarr4.sort(); michael@0: michael@0: var realarr = new Array(); michael@0: clone( array, realarr ); michael@0: realarr.sort( realsort ); michael@0: michael@0: var stringarr = new Array(); michael@0: clone( array, stringarr ); michael@0: stringarr.sort( stringsort ); michael@0: michael@0: for ( var i = 0; i < array.length; i++) { michael@0: new TestCase( michael@0: SECTION, michael@0: "testarr1["+i+"]", michael@0: realarr[i], michael@0: testarr1[i] ); michael@0: } michael@0: michael@0: for ( var i=0; i < array.length; i++) { michael@0: new TestCase( michael@0: SECTION, michael@0: "testarr2["+i+"]", michael@0: realarr[i], michael@0: testarr2[i] ); michael@0: } michael@0: michael@0: for ( var i=0; i < array.length; i++) { michael@0: new TestCase( michael@0: SECTION, michael@0: "testarr3["+i+"]", michael@0: realarr[i], michael@0: testarr3[i] ); michael@0: } michael@0: michael@0: for ( var i=0; i < array.length; i++) { michael@0: new TestCase( michael@0: SECTION, michael@0: "testarr4["+i+"]", michael@0: stringarr[i].toString(), michael@0: testarr4[i].toString() ); michael@0: } michael@0: michael@0: test(); michael@0: michael@0: function comparefn1( x, y ) { michael@0: return x - y; michael@0: } michael@0: function comparefn2( x, y ) { michael@0: return x.valueOf() - y.valueOf(); michael@0: } michael@0: function realsort( x, y ) { michael@0: return ( x.valueOf() == y.valueOf() ? 0 : ( x.valueOf() > y.valueOf() ? 1 : -1 ) ); michael@0: } michael@0: function comparefn3( x, y ) { michael@0: return ( x == y ? 0 : ( x > y ? 1: -1 ) ); michael@0: } michael@0: function clone( source, target ) { michael@0: for (i = 0; i < source.length; i++ ) { michael@0: target[i] = source[i]; michael@0: } michael@0: } michael@0: function stringsort( x, y ) { michael@0: for ( var i = 0; i < x.toString().length; i++ ) { michael@0: var d = (x.toString()).charCodeAt(i) - (y.toString()).charCodeAt(i); michael@0: if ( d > 0 ) { michael@0: return 1; michael@0: } else { michael@0: if ( d < 0 ) { michael@0: return -1; michael@0: } else { michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: var d = x.length - y.length; michael@0: michael@0: if ( d > 0 ) { michael@0: return 1; michael@0: } else { michael@0: if ( d < 0 ) { michael@0: return -1; michael@0: } michael@0: } michael@0: } michael@0: return 0; michael@0: }