js/src/tests/js1_5/Array/regress-178722.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_5/Array/regress-178722.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,130 @@
     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 + * Date:    06 November 2002
    1.12 + * SUMMARY: arr.sort() should not output |undefined| when |arr| is empty
    1.13 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=178722
    1.14 + *
    1.15 + * ECMA-262 Ed.3: 15.4.4.11 Array.prototype.sort (comparefn)
    1.16 + *
    1.17 + * 1. Call the [[Get]] method of this object with argument "length".
    1.18 + * 2. Call ToUint32(Result(1)).
    1.19 + * 3. Perform an implementation-dependent sequence of calls to the [[Get]],
    1.20 + *    [[Put]], and [[Delete]] methods of this object, etc. etc.
    1.21 + * 4. Return this object.
    1.22 + *
    1.23 + *
    1.24 + * Note that sort() is done in-place on |arr|. In other words, sort() is a
    1.25 + * "destructive" method rather than a "functional" method. The return value
    1.26 + * of |arr.sort()| and |arr| are the same object.
    1.27 + *
    1.28 + * If |arr| is an empty array, the return value of |arr.sort()| should be
    1.29 + * an empty array, not the value |undefined| as was occurring in bug 178722.
    1.30 + *
    1.31 + */
    1.32 +//-----------------------------------------------------------------------------
    1.33 +var UBound = 0;
    1.34 +var BUGNUMBER = 178722;
    1.35 +var summary = 'arr.sort() should not output |undefined| when |arr| is empty';
    1.36 +var status = '';
    1.37 +var statusitems = [];
    1.38 +var actual = '';
    1.39 +var actualvalues = [];
    1.40 +var expect= '';
    1.41 +var expectedvalues = [];
    1.42 +var arr;
    1.43 +
    1.44 +
    1.45 +// create empty array or pseudo-array objects in various ways
    1.46 +var arr1 = Array();
    1.47 +var arr2 = new Array();
    1.48 +var arr3 = [];
    1.49 +var arr4 = [1];
    1.50 +arr4.pop();
    1.51 +
    1.52 +
    1.53 +status = inSection(1);
    1.54 +arr = arr1.sort();
    1.55 +actual = arr instanceof Array && arr.length === 0 && arr === arr1;
    1.56 +expect = true;
    1.57 +addThis();
    1.58 +
    1.59 +status = inSection(2);
    1.60 +arr = arr2.sort();
    1.61 +actual = arr instanceof Array && arr.length === 0 && arr === arr2;
    1.62 +expect = true;
    1.63 +addThis();
    1.64 +
    1.65 +status = inSection(3);
    1.66 +arr = arr3.sort();
    1.67 +actual = arr instanceof Array && arr.length === 0 && arr === arr3;
    1.68 +expect = true;
    1.69 +addThis();
    1.70 +
    1.71 +status = inSection(4);
    1.72 +arr = arr4.sort();
    1.73 +actual = arr instanceof Array && arr.length === 0 && arr === arr4;
    1.74 +expect = true;
    1.75 +addThis();
    1.76 +
    1.77 +// now do the same thing, with non-default sorting:
    1.78 +function g() {return 1;}
    1.79 +
    1.80 +status = inSection('1a');
    1.81 +arr = arr1.sort(g);
    1.82 +actual = arr instanceof Array && arr.length === 0 && arr === arr1;
    1.83 +expect = true;
    1.84 +addThis();
    1.85 +
    1.86 +status = inSection('2a');
    1.87 +arr = arr2.sort(g);
    1.88 +actual = arr instanceof Array && arr.length === 0 && arr === arr2;
    1.89 +expect = true;
    1.90 +addThis();
    1.91 +
    1.92 +status = inSection('3a');
    1.93 +arr = arr3.sort(g);
    1.94 +actual = arr instanceof Array && arr.length === 0 && arr === arr3;
    1.95 +expect = true;
    1.96 +addThis();
    1.97 +
    1.98 +status = inSection('4a');
    1.99 +arr = arr4.sort(g);
   1.100 +actual = arr instanceof Array && arr.length === 0 && arr === arr4;
   1.101 +expect = true;
   1.102 +addThis();
   1.103 +
   1.104 +
   1.105 +
   1.106 +//-----------------------------------------------------------------------------
   1.107 +test();
   1.108 +//-----------------------------------------------------------------------------
   1.109 +
   1.110 +
   1.111 +
   1.112 +function addThis()
   1.113 +{
   1.114 +  statusitems[UBound] = status;
   1.115 +  actualvalues[UBound] = actual;
   1.116 +  expectedvalues[UBound] = expect;
   1.117 +  UBound++;
   1.118 +}
   1.119 +
   1.120 +
   1.121 +function test()
   1.122 +{
   1.123 +  enterFunc('test');
   1.124 +  printBugNumber(BUGNUMBER);
   1.125 +  printStatus(summary);
   1.126 +
   1.127 +  for (var i=0; i<UBound; i++)
   1.128 +  {
   1.129 +    reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
   1.130 +  }
   1.131 +
   1.132 +  exitFunc ('test');
   1.133 +}

mercurial