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: * Date: 06 November 2002 michael@0: * SUMMARY: arr.sort() should not output |undefined| when |arr| is empty michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=178722 michael@0: * michael@0: * ECMA-262 Ed.3: 15.4.4.11 Array.prototype.sort (comparefn) michael@0: * michael@0: * 1. Call the [[Get]] method of this object with argument "length". michael@0: * 2. Call ToUint32(Result(1)). michael@0: * 3. Perform an implementation-dependent sequence of calls to the [[Get]], michael@0: * [[Put]], and [[Delete]] methods of this object, etc. etc. michael@0: * 4. Return this object. michael@0: * michael@0: * michael@0: * Note that sort() is done in-place on |arr|. In other words, sort() is a michael@0: * "destructive" method rather than a "functional" method. The return value michael@0: * of |arr.sort()| and |arr| are the same object. michael@0: * michael@0: * If |arr| is an empty array, the return value of |arr.sort()| should be michael@0: * an empty array, not the value |undefined| as was occurring in bug 178722. michael@0: * michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 178722; michael@0: var summary = 'arr.sort() should not output |undefined| when |arr| is empty'; michael@0: var status = ''; michael@0: var statusitems = []; michael@0: var actual = ''; michael@0: var actualvalues = []; michael@0: var expect= ''; michael@0: var expectedvalues = []; michael@0: var arr; michael@0: michael@0: michael@0: // create empty array or pseudo-array objects in various ways michael@0: function f () {return arguments}; michael@0: var arr5 = f(); michael@0: arr5.__proto__ = Array.prototype; michael@0: michael@0: michael@0: status = inSection(5); michael@0: arr = arr5.sort(); michael@0: actual = arr instanceof Array && arr.length === 0 && arr === arr5; michael@0: expect = true; michael@0: addThis(); michael@0: michael@0: michael@0: // now do the same thing, with non-default sorting: michael@0: function g() {return 1;} michael@0: michael@0: status = inSection('5a'); michael@0: arr = arr5.sort(g); michael@0: actual = arr instanceof Array && arr.length === 0 && arr === arr5; michael@0: expect = true; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function addThis() michael@0: { michael@0: statusitems[UBound] = status; michael@0: actualvalues[UBound] = actual; michael@0: expectedvalues[UBound] = expect; michael@0: UBound++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus(summary); michael@0: michael@0: for (var i=0; i