| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | |
| 6 | /* |
| 7 | * |
| 8 | * Date: 06 November 2002 |
| 9 | * SUMMARY: arr.sort() should not output |undefined| when |arr| is empty |
| 10 | * See http://bugzilla.mozilla.org/show_bug.cgi?id=178722 |
| 11 | * |
| 12 | * ECMA-262 Ed.3: 15.4.4.11 Array.prototype.sort (comparefn) |
| 13 | * |
| 14 | * 1. Call the [[Get]] method of this object with argument "length". |
| 15 | * 2. Call ToUint32(Result(1)). |
| 16 | * 3. Perform an implementation-dependent sequence of calls to the [[Get]], |
| 17 | * [[Put]], and [[Delete]] methods of this object, etc. etc. |
| 18 | * 4. Return this object. |
| 19 | * |
| 20 | * |
| 21 | * Note that sort() is done in-place on |arr|. In other words, sort() is a |
| 22 | * "destructive" method rather than a "functional" method. The return value |
| 23 | * of |arr.sort()| and |arr| are the same object. |
| 24 | * |
| 25 | * If |arr| is an empty array, the return value of |arr.sort()| should be |
| 26 | * an empty array, not the value |undefined| as was occurring in bug 178722. |
| 27 | * |
| 28 | */ |
| 29 | //----------------------------------------------------------------------------- |
| 30 | var gTestfile = 'regress-178722.js'; |
| 31 | var UBound = 0; |
| 32 | var BUGNUMBER = 178722; |
| 33 | var summary = 'arr.sort() should not output |undefined| when |arr| is empty'; |
| 34 | var status = ''; |
| 35 | var statusitems = []; |
| 36 | var actual = ''; |
| 37 | var actualvalues = []; |
| 38 | var expect= ''; |
| 39 | var expectedvalues = []; |
| 40 | var arr; |
| 41 | |
| 42 | |
| 43 | // create empty array or pseudo-array objects in various ways |
| 44 | function f () {return arguments}; |
| 45 | var arr5 = f(); |
| 46 | arr5.__proto__ = Array.prototype; |
| 47 | |
| 48 | |
| 49 | status = inSection(5); |
| 50 | arr = arr5.sort(); |
| 51 | actual = arr instanceof Array && arr.length === 0 && arr === arr5; |
| 52 | expect = true; |
| 53 | addThis(); |
| 54 | |
| 55 | |
| 56 | // now do the same thing, with non-default sorting: |
| 57 | function g() {return 1;} |
| 58 | |
| 59 | status = inSection('5a'); |
| 60 | arr = arr5.sort(g); |
| 61 | actual = arr instanceof Array && arr.length === 0 && arr === arr5; |
| 62 | expect = true; |
| 63 | addThis(); |
| 64 | |
| 65 | |
| 66 | |
| 67 | //----------------------------------------------------------------------------- |
| 68 | test(); |
| 69 | //----------------------------------------------------------------------------- |
| 70 | |
| 71 | |
| 72 | |
| 73 | function addThis() |
| 74 | { |
| 75 | statusitems[UBound] = status; |
| 76 | actualvalues[UBound] = actual; |
| 77 | expectedvalues[UBound] = expect; |
| 78 | UBound++; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | function test() |
| 83 | { |
| 84 | enterFunc('test'); |
| 85 | printBugNumber(BUGNUMBER); |
| 86 | printStatus(summary); |
| 87 | |
| 88 | for (var i=0; i<UBound; i++) |
| 89 | { |
| 90 | reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 91 | } |
| 92 | |
| 93 | exitFunc ('test'); |
| 94 | } |