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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma/Array/15.4.4.5-3.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,148 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 +   File Name:          15.4.4.5-3.js
    1.12 +   ECMA Section:       Array.prototype.sort(comparefn)
    1.13 +   Description:
    1.14 +
    1.15 +   This is a regression test for
    1.16 +   http://scopus/bugsplat/show_bug.cgi?id=117144
    1.17 +
    1.18 +   Verify that sort is successfull, even if the sort compare function returns
    1.19 +   a very large negative or positive value.
    1.20 +
    1.21 +   Author:             christine@netscape.com
    1.22 +   Date:               12 november 1997
    1.23 +*/
    1.24 +
    1.25 +
    1.26 +var SECTION = "15.4.4.5-3";
    1.27 +var VERSION = "ECMA_1";
    1.28 +startTest();
    1.29 +var TITLE   = "Array.prototype.sort(comparefn)";
    1.30 +
    1.31 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.32 +
    1.33 +var array = new Array();
    1.34 +
    1.35 +array[array.length] = new Date( TIME_2000 * Math.PI );
    1.36 +array[array.length] = new Date( TIME_2000 * 10 );
    1.37 +array[array.length] = new Date( TIME_1900 + TIME_1900  );
    1.38 +array[array.length] = new Date(0);
    1.39 +array[array.length] = new Date( TIME_2000 );
    1.40 +array[array.length] = new Date( TIME_1900 + TIME_1900 +TIME_1900 );
    1.41 +array[array.length] = new Date( TIME_1900 * Math.PI );
    1.42 +array[array.length] = new Date( TIME_1900 * 10 );
    1.43 +array[array.length] = new Date( TIME_1900 );
    1.44 +array[array.length] = new Date( TIME_2000 + TIME_2000 );
    1.45 +array[array.length] = new Date( 1899, 0, 1 );
    1.46 +array[array.length] = new Date( 2000, 1, 29 );
    1.47 +array[array.length] = new Date( 2000, 0, 1 );
    1.48 +array[array.length] = new Date( 1999, 11, 31 );
    1.49 +
    1.50 +var testarr1 = new Array();
    1.51 +clone( array, testarr1 );
    1.52 +testarr1.sort( comparefn1 );
    1.53 +
    1.54 +var testarr2 = new Array();
    1.55 +clone( array, testarr2 );
    1.56 +testarr2.sort( comparefn2 );
    1.57 +
    1.58 +testarr3 = new Array();
    1.59 +clone( array, testarr3 );
    1.60 +testarr3.sort( comparefn3 );
    1.61 +
    1.62 +// when there's no sort function, sort sorts by the toString value of Date.
    1.63 +
    1.64 +var testarr4 = new Array();
    1.65 +clone( array, testarr4 );
    1.66 +testarr4.sort();
    1.67 +
    1.68 +var realarr = new Array();
    1.69 +clone( array, realarr );
    1.70 +realarr.sort( realsort );
    1.71 +
    1.72 +var stringarr = new Array();
    1.73 +clone( array, stringarr );
    1.74 +stringarr.sort( stringsort );
    1.75 +
    1.76 +for ( var i = 0; i < array.length; i++) {
    1.77 +  new TestCase(
    1.78 +    SECTION,
    1.79 +    "testarr1["+i+"]",
    1.80 +    realarr[i],
    1.81 +    testarr1[i] );
    1.82 +}
    1.83 +
    1.84 +for ( var i=0; i < array.length; i++) {
    1.85 +  new TestCase(
    1.86 +    SECTION,
    1.87 +    "testarr2["+i+"]",
    1.88 +    realarr[i],
    1.89 +    testarr2[i] );
    1.90 +}
    1.91 +
    1.92 +for ( var i=0; i < array.length; i++) {
    1.93 +  new TestCase(
    1.94 +    SECTION,
    1.95 +    "testarr3["+i+"]",
    1.96 +    realarr[i],
    1.97 +    testarr3[i] );
    1.98 +}
    1.99 +
   1.100 +for ( var i=0; i < array.length; i++) {
   1.101 +  new TestCase(
   1.102 +    SECTION,
   1.103 +    "testarr4["+i+"]",
   1.104 +    stringarr[i].toString(),
   1.105 +    testarr4[i].toString() );
   1.106 +}
   1.107 +
   1.108 +test();
   1.109 +
   1.110 +function comparefn1( x, y ) {
   1.111 +  return x - y;
   1.112 +}
   1.113 +function comparefn2( x, y ) {
   1.114 +  return x.valueOf() - y.valueOf();
   1.115 +}
   1.116 +function realsort( x, y ) {
   1.117 +  return ( x.valueOf() == y.valueOf() ? 0 : ( x.valueOf() > y.valueOf() ? 1 : -1 ) );
   1.118 +}
   1.119 +function comparefn3( x, y ) {
   1.120 +  return ( x == y ? 0 : ( x > y ? 1: -1 ) );
   1.121 +}
   1.122 +function clone( source, target ) {
   1.123 +  for (i = 0; i < source.length; i++ ) {
   1.124 +    target[i] = source[i];
   1.125 +  }
   1.126 +}
   1.127 +function stringsort( x, y ) {
   1.128 +  for ( var i = 0; i < x.toString().length; i++ ) {
   1.129 +    var d = (x.toString()).charCodeAt(i) - (y.toString()).charCodeAt(i);
   1.130 +    if ( d > 0 ) {
   1.131 +      return 1;
   1.132 +    } else {
   1.133 +      if ( d < 0 ) {
   1.134 +	return -1;
   1.135 +      } else {
   1.136 +	continue;
   1.137 +      }
   1.138 +    }
   1.139 +
   1.140 +    var d = x.length - y.length;
   1.141 +
   1.142 +    if  ( d > 0 ) {
   1.143 +      return 1;
   1.144 +    } else {
   1.145 +      if ( d < 0 ) {
   1.146 +	return -1;
   1.147 +      }
   1.148 +    }
   1.149 +  }
   1.150 +  return 0;
   1.151 +}

mercurial