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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6
michael@0 7 /**
michael@0 8 File Name: 15.4.4.5-3.js
michael@0 9 ECMA Section: Array.prototype.sort(comparefn)
michael@0 10 Description:
michael@0 11
michael@0 12 This is a regression test for
michael@0 13 http://scopus/bugsplat/show_bug.cgi?id=117144
michael@0 14
michael@0 15 Verify that sort is successfull, even if the sort compare function returns
michael@0 16 a very large negative or positive value.
michael@0 17
michael@0 18 Author: christine@netscape.com
michael@0 19 Date: 12 november 1997
michael@0 20 */
michael@0 21
michael@0 22
michael@0 23 var SECTION = "15.4.4.5-3";
michael@0 24 var VERSION = "ECMA_1";
michael@0 25 startTest();
michael@0 26 var TITLE = "Array.prototype.sort(comparefn)";
michael@0 27
michael@0 28 writeHeaderToLog( SECTION + " "+ TITLE);
michael@0 29
michael@0 30 var array = new Array();
michael@0 31
michael@0 32 array[array.length] = new Date( TIME_2000 * Math.PI );
michael@0 33 array[array.length] = new Date( TIME_2000 * 10 );
michael@0 34 array[array.length] = new Date( TIME_1900 + TIME_1900 );
michael@0 35 array[array.length] = new Date(0);
michael@0 36 array[array.length] = new Date( TIME_2000 );
michael@0 37 array[array.length] = new Date( TIME_1900 + TIME_1900 +TIME_1900 );
michael@0 38 array[array.length] = new Date( TIME_1900 * Math.PI );
michael@0 39 array[array.length] = new Date( TIME_1900 * 10 );
michael@0 40 array[array.length] = new Date( TIME_1900 );
michael@0 41 array[array.length] = new Date( TIME_2000 + TIME_2000 );
michael@0 42 array[array.length] = new Date( 1899, 0, 1 );
michael@0 43 array[array.length] = new Date( 2000, 1, 29 );
michael@0 44 array[array.length] = new Date( 2000, 0, 1 );
michael@0 45 array[array.length] = new Date( 1999, 11, 31 );
michael@0 46
michael@0 47 var testarr1 = new Array();
michael@0 48 clone( array, testarr1 );
michael@0 49 testarr1.sort( comparefn1 );
michael@0 50
michael@0 51 var testarr2 = new Array();
michael@0 52 clone( array, testarr2 );
michael@0 53 testarr2.sort( comparefn2 );
michael@0 54
michael@0 55 testarr3 = new Array();
michael@0 56 clone( array, testarr3 );
michael@0 57 testarr3.sort( comparefn3 );
michael@0 58
michael@0 59 // when there's no sort function, sort sorts by the toString value of Date.
michael@0 60
michael@0 61 var testarr4 = new Array();
michael@0 62 clone( array, testarr4 );
michael@0 63 testarr4.sort();
michael@0 64
michael@0 65 var realarr = new Array();
michael@0 66 clone( array, realarr );
michael@0 67 realarr.sort( realsort );
michael@0 68
michael@0 69 var stringarr = new Array();
michael@0 70 clone( array, stringarr );
michael@0 71 stringarr.sort( stringsort );
michael@0 72
michael@0 73 for ( var i = 0; i < array.length; i++) {
michael@0 74 new TestCase(
michael@0 75 SECTION,
michael@0 76 "testarr1["+i+"]",
michael@0 77 realarr[i],
michael@0 78 testarr1[i] );
michael@0 79 }
michael@0 80
michael@0 81 for ( var i=0; i < array.length; i++) {
michael@0 82 new TestCase(
michael@0 83 SECTION,
michael@0 84 "testarr2["+i+"]",
michael@0 85 realarr[i],
michael@0 86 testarr2[i] );
michael@0 87 }
michael@0 88
michael@0 89 for ( var i=0; i < array.length; i++) {
michael@0 90 new TestCase(
michael@0 91 SECTION,
michael@0 92 "testarr3["+i+"]",
michael@0 93 realarr[i],
michael@0 94 testarr3[i] );
michael@0 95 }
michael@0 96
michael@0 97 for ( var i=0; i < array.length; i++) {
michael@0 98 new TestCase(
michael@0 99 SECTION,
michael@0 100 "testarr4["+i+"]",
michael@0 101 stringarr[i].toString(),
michael@0 102 testarr4[i].toString() );
michael@0 103 }
michael@0 104
michael@0 105 test();
michael@0 106
michael@0 107 function comparefn1( x, y ) {
michael@0 108 return x - y;
michael@0 109 }
michael@0 110 function comparefn2( x, y ) {
michael@0 111 return x.valueOf() - y.valueOf();
michael@0 112 }
michael@0 113 function realsort( x, y ) {
michael@0 114 return ( x.valueOf() == y.valueOf() ? 0 : ( x.valueOf() > y.valueOf() ? 1 : -1 ) );
michael@0 115 }
michael@0 116 function comparefn3( x, y ) {
michael@0 117 return ( x == y ? 0 : ( x > y ? 1: -1 ) );
michael@0 118 }
michael@0 119 function clone( source, target ) {
michael@0 120 for (i = 0; i < source.length; i++ ) {
michael@0 121 target[i] = source[i];
michael@0 122 }
michael@0 123 }
michael@0 124 function stringsort( x, y ) {
michael@0 125 for ( var i = 0; i < x.toString().length; i++ ) {
michael@0 126 var d = (x.toString()).charCodeAt(i) - (y.toString()).charCodeAt(i);
michael@0 127 if ( d > 0 ) {
michael@0 128 return 1;
michael@0 129 } else {
michael@0 130 if ( d < 0 ) {
michael@0 131 return -1;
michael@0 132 } else {
michael@0 133 continue;
michael@0 134 }
michael@0 135 }
michael@0 136
michael@0 137 var d = x.length - y.length;
michael@0 138
michael@0 139 if ( d > 0 ) {
michael@0 140 return 1;
michael@0 141 } else {
michael@0 142 if ( d < 0 ) {
michael@0 143 return -1;
michael@0 144 }
michael@0 145 }
michael@0 146 }
michael@0 147 return 0;
michael@0 148 }

mercurial