| 1 |
/* -*- indent-tabs-mode: nil; js-indent-level: 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 UBound = 0; |
| 31 |
var BUGNUMBER = 178722; |
| 32 |
var summary = 'arr.sort() should not output |undefined| when |arr| is empty'; |
| 33 |
var status = ''; |
| 34 |
var statusitems = []; |
| 35 |
var actual = ''; |
| 36 |
var actualvalues = []; |
| 37 |
var expect= ''; |
| 38 |
var expectedvalues = []; |
| 39 |
var arr; |
| 40 |
|
| 41 |
|
| 42 |
// create empty array or pseudo-array objects in various ways |
| 43 |
var arr1 = Array(); |
| 44 |
var arr2 = new Array(); |
| 45 |
var arr3 = []; |
| 46 |
var arr4 = [1]; |
| 47 |
arr4.pop(); |
| 48 |
|
| 49 |
|
| 50 |
status = inSection(1); |
| 51 |
arr = arr1.sort(); |
| 52 |
actual = arr instanceof Array && arr.length === 0 && arr === arr1; |
| 53 |
expect = true; |
| 54 |
addThis(); |
| 55 |
|
| 56 |
status = inSection(2); |
| 57 |
arr = arr2.sort(); |
| 58 |
actual = arr instanceof Array && arr.length === 0 && arr === arr2; |
| 59 |
expect = true; |
| 60 |
addThis(); |
| 61 |
|
| 62 |
status = inSection(3); |
| 63 |
arr = arr3.sort(); |
| 64 |
actual = arr instanceof Array && arr.length === 0 && arr === arr3; |
| 65 |
expect = true; |
| 66 |
addThis(); |
| 67 |
|
| 68 |
status = inSection(4); |
| 69 |
arr = arr4.sort(); |
| 70 |
actual = arr instanceof Array && arr.length === 0 && arr === arr4; |
| 71 |
expect = true; |
| 72 |
addThis(); |
| 73 |
|
| 74 |
// now do the same thing, with non-default sorting: |
| 75 |
function g() {return 1;} |
| 76 |
|
| 77 |
status = inSection('1a'); |
| 78 |
arr = arr1.sort(g); |
| 79 |
actual = arr instanceof Array && arr.length === 0 && arr === arr1; |
| 80 |
expect = true; |
| 81 |
addThis(); |
| 82 |
|
| 83 |
status = inSection('2a'); |
| 84 |
arr = arr2.sort(g); |
| 85 |
actual = arr instanceof Array && arr.length === 0 && arr === arr2; |
| 86 |
expect = true; |
| 87 |
addThis(); |
| 88 |
|
| 89 |
status = inSection('3a'); |
| 90 |
arr = arr3.sort(g); |
| 91 |
actual = arr instanceof Array && arr.length === 0 && arr === arr3; |
| 92 |
expect = true; |
| 93 |
addThis(); |
| 94 |
|
| 95 |
status = inSection('4a'); |
| 96 |
arr = arr4.sort(g); |
| 97 |
actual = arr instanceof Array && arr.length === 0 && arr === arr4; |
| 98 |
expect = true; |
| 99 |
addThis(); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
//----------------------------------------------------------------------------- |
| 104 |
test(); |
| 105 |
//----------------------------------------------------------------------------- |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
function addThis() |
| 110 |
{ |
| 111 |
statusitems[UBound] = status; |
| 112 |
actualvalues[UBound] = actual; |
| 113 |
expectedvalues[UBound] = expect; |
| 114 |
UBound++; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
function test() |
| 119 |
{ |
| 120 |
printBugNumber(BUGNUMBER); |
| 121 |
printStatus(summary); |
| 122 |
|
| 123 |
for (var i=0; i<UBound; i++) |
| 124 |
{ |
| 125 |
reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 126 |
} |
| 127 |
} |