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: 25 Mar 2002 michael@0: * SUMMARY: Array.prototype.sort() should not (re-)define .length michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=130451 michael@0: * michael@0: * From the ECMA-262 Edition 3 Final spec: michael@0: * michael@0: * NOTE: The sort function is intentionally generic; it does not require that michael@0: * its |this| value be an Array object. Therefore, it can be transferred to michael@0: * other kinds of objects for use as a method. Whether the sort function can michael@0: * be applied successfully to a host object is implementation-dependent. michael@0: * michael@0: * The interesting parts of this testcase are the contrasting expectations for michael@0: * Brendan's test below, when applied to Array objects vs. non-Array objects. michael@0: * michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 130451; michael@0: var summary = 'Array.prototype.sort() should not (re-)define .length'; 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: var cmp = new Function(); michael@0: michael@0: michael@0: /* michael@0: * First: test Array.prototype.sort() on Array objects michael@0: */ michael@0: status = inSection(1); michael@0: arr = [0,1,2,3]; michael@0: cmp = function(x,y) {return x-y;}; michael@0: actual = arr.sort(cmp).length; michael@0: expect = 4; michael@0: addThis(); michael@0: michael@0: status = inSection(2); michael@0: arr = [0,1,2,3]; michael@0: cmp = function(x,y) {return y-x;}; michael@0: actual = arr.sort(cmp).length; michael@0: expect = 4; michael@0: addThis(); michael@0: michael@0: status = inSection(3); michael@0: arr = [0,1,2,3]; michael@0: cmp = function(x,y) {return x-y;}; michael@0: arr.length = 1; michael@0: actual = arr.sort(cmp).length; michael@0: expect = 1; michael@0: addThis(); michael@0: michael@0: /* michael@0: * This test is by Brendan. Setting arr.length to michael@0: * 2 and then 4 should cause elements to be deleted. michael@0: */ michael@0: arr = [0,1,2,3]; michael@0: cmp = function(x,y) {return x-y;}; michael@0: arr.sort(cmp); michael@0: michael@0: status = inSection(4); michael@0: actual = arr.join(); michael@0: expect = '0,1,2,3'; michael@0: addThis(); michael@0: michael@0: status = inSection(5); michael@0: actual = arr.length; michael@0: expect = 4; michael@0: addThis(); michael@0: michael@0: status = inSection(6); michael@0: arr.length = 2; michael@0: actual = arr.join(); michael@0: expect = '0,1'; michael@0: addThis(); michael@0: michael@0: status = inSection(7); michael@0: arr.length = 4; michael@0: actual = arr.join(); michael@0: expect = '0,1,,'; //<---- see how 2,3 have been lost michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: /* michael@0: * Now test Array.prototype.sort() on non-Array objects michael@0: */ michael@0: status = inSection(8); michael@0: var obj = new Object(); michael@0: obj.sort = Array.prototype.sort; michael@0: obj.length = 4; michael@0: obj[0] = 0; michael@0: obj[1] = 1; michael@0: obj[2] = 2; michael@0: obj[3] = 3; michael@0: cmp = function(x,y) {return x-y;}; michael@0: actual = obj.sort(cmp).length; michael@0: expect = 4; michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Here again is Brendan's test. Unlike the array case michael@0: * above, the setting of obj.length to 2 and then 4 michael@0: * should NOT cause elements to be deleted michael@0: */ michael@0: obj = new Object(); michael@0: obj.sort = Array.prototype.sort; michael@0: obj.length = 4; michael@0: obj[0] = 3; michael@0: obj[1] = 2; michael@0: obj[2] = 1; michael@0: obj[3] = 0; michael@0: cmp = function(x,y) {return x-y;}; michael@0: obj.sort(cmp); //<---- this is what triggered the buggy behavior below michael@0: obj.join = Array.prototype.join; michael@0: michael@0: status = inSection(9); michael@0: actual = obj.join(); michael@0: expect = '0,1,2,3'; michael@0: addThis(); michael@0: michael@0: status = inSection(10); michael@0: actual = obj.length; michael@0: expect = 4; michael@0: addThis(); michael@0: michael@0: status = inSection(11); michael@0: obj.length = 2; michael@0: actual = obj.join(); michael@0: expect = '0,1'; michael@0: addThis(); michael@0: michael@0: /* michael@0: * Before this bug was fixed, |actual| held the value '0,1,,' michael@0: * as in the Array-object case at top. This bug only occurred michael@0: * if Array.prototype.sort() had been applied to |obj|, michael@0: * as we have done higher up. michael@0: */ michael@0: status = inSection(12); michael@0: obj.length = 4; michael@0: actual = obj.join(); michael@0: expect = '0,1,2,3'; michael@0: addThis(); michael@0: 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