1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_5/extensions/regress-178722.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,93 @@ 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 +function f () {return arguments}; 1.47 +var arr5 = f(); 1.48 +arr5.__proto__ = Array.prototype; 1.49 + 1.50 + 1.51 +status = inSection(5); 1.52 +arr = arr5.sort(); 1.53 +actual = arr instanceof Array && arr.length === 0 && arr === arr5; 1.54 +expect = true; 1.55 +addThis(); 1.56 + 1.57 + 1.58 +// now do the same thing, with non-default sorting: 1.59 +function g() {return 1;} 1.60 + 1.61 +status = inSection('5a'); 1.62 +arr = arr5.sort(g); 1.63 +actual = arr instanceof Array && arr.length === 0 && arr === arr5; 1.64 +expect = true; 1.65 +addThis(); 1.66 + 1.67 + 1.68 + 1.69 +//----------------------------------------------------------------------------- 1.70 +test(); 1.71 +//----------------------------------------------------------------------------- 1.72 + 1.73 + 1.74 + 1.75 +function addThis() 1.76 +{ 1.77 + statusitems[UBound] = status; 1.78 + actualvalues[UBound] = actual; 1.79 + expectedvalues[UBound] = expect; 1.80 + UBound++; 1.81 +} 1.82 + 1.83 + 1.84 +function test() 1.85 +{ 1.86 + enterFunc('test'); 1.87 + printBugNumber(BUGNUMBER); 1.88 + printStatus(summary); 1.89 + 1.90 + for (var i=0; i<UBound; i++) 1.91 + { 1.92 + reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 1.93 + } 1.94 + 1.95 + exitFunc ('test'); 1.96 +}