1/* ***** BEGIN LICENSE BLOCK ***** 2* Version: NPL 1.1/GPL 2.0/LGPL 2.1 3* 4* The contents of this file are subject to the Netscape Public License 5* Version 1.1 (the "License"); you may not use this file except in 6* compliance with the License. You may obtain a copy of the License at 7* http://www.mozilla.org/NPL/ 8* 9* Software distributed under the License is distributed on an "AS IS" basis, 10* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 11* for the specific language governing rights and limitations under the 12* License. 13* 14* The Original Code is JavaScript Engine testing utilities. 15* 16* The Initial Developer of the Original Code is Netscape Communications Corp. 17* Portions created by the Initial Developer are Copyright (C) 2002 18* the Initial Developer. All Rights Reserved. 19* 20* Contributor(s): pschwartau@netscape.com 21* 22* Alternatively, the contents of this file may be used under the terms of 23* either the GNU General Public License Version 2 or later (the "GPL"), or 24* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 25* in which case the provisions of the GPL or the LGPL are applicable instead 26* of those above. If you wish to allow use of your version of this file only 27* under the terms of either the GPL or the LGPL, and not to allow others to 28* use your version of this file under the terms of the NPL, indicate your 29* decision by deleting the provisions above and replace them with the notice 30* and other provisions required by the GPL or the LGPL. If you do not delete 31* the provisions above, a recipient may use your version of this file under 32* the terms of any one of the NPL, the GPL or the LGPL. 33* 34* ***** END LICENSE BLOCK ***** 35* 36* 37* Date: 06 November 2002 38* SUMMARY: arr.sort() should not output |undefined| when |arr| is empty 39* See http://bugzilla.mozilla.org/show_bug.cgi?id=178722 40* 41* ECMA-262 Ed.3: 15.4.4.11 Array.prototype.sort (comparefn) 42* 43* 1. Call the [[Get]] method of this object with argument "length". 44* 2. Call ToUint32(Result(1)). 45* 3. Perform an implementation-dependent sequence of calls to the [[Get]], 46* [[Put]], and [[Delete]] methods of this object, etc. etc. 47* 4. Return this object. 48* 49* 50* Note that sort() is done in-place on |arr|. In other words, sort() is a 51* "destructive" method rather than a "functional" method. The return value 52* of |arr.sort()| and |arr| are the same object. 53* 54* If |arr| is an empty array, the return value of |arr.sort()| should be 55* an empty array, not the value |undefined| as was occurring in bug 178722. 56* 57*/ 58//----------------------------------------------------------------------------- 59var UBound = 0; 60var bug = 178722; 61var summary = 'arr.sort() should not output |undefined| when |arr| is empty'; 62var status = ''; 63var statusitems = []; 64var actual = ''; 65var actualvalues = []; 66var expect= ''; 67var expectedvalues = []; 68var arr; 69 70 71// create empty array or pseudo-array objects in various ways 72var arr1 = Array(); 73var arr2 = new Array(); 74var arr3 = []; 75var arr4 = [1]; 76arr4.pop(); 77function f () {return arguments}; 78var arr5 = f(); 79arr5.__proto__ = Array.prototype; 80 81 82status = inSection(1); 83arr = arr1.sort(); 84actual = arr instanceof Array && arr.length === 0 && arr === arr1; 85expect = true; 86addThis(); 87 88status = inSection(2); 89arr = arr2.sort(); 90actual = arr instanceof Array && arr.length === 0 && arr === arr2; 91expect = true; 92addThis(); 93 94status = inSection(3); 95arr = arr3.sort(); 96actual = arr instanceof Array && arr.length === 0 && arr === arr3; 97expect = true; 98addThis(); 99 100status = inSection(4); 101arr = arr4.sort(); 102actual = arr instanceof Array && arr.length === 0 && arr === arr4; 103expect = true; 104addThis(); 105 106status = inSection(5); 107arr = arr5.sort(); 108actual = arr instanceof Array && arr.length === 0 && arr === arr5; 109expect = true; 110addThis(); 111 112 113// now do the same thing, with non-default sorting: 114function g() {return 1;} 115 116status = inSection('1a'); 117arr = arr1.sort(g); 118actual = arr instanceof Array && arr.length === 0 && arr === arr1; 119expect = true; 120addThis(); 121 122status = inSection('2a'); 123arr = arr2.sort(g); 124actual = arr instanceof Array && arr.length === 0 && arr === arr2; 125expect = true; 126addThis(); 127 128status = inSection('3a'); 129arr = arr3.sort(g); 130actual = arr instanceof Array && arr.length === 0 && arr === arr3; 131expect = true; 132addThis(); 133 134status = inSection('4a'); 135arr = arr4.sort(g); 136actual = arr instanceof Array && arr.length === 0 && arr === arr4; 137expect = true; 138addThis(); 139 140status = inSection('5a'); 141arr = arr5.sort(g); 142actual = arr instanceof Array && arr.length === 0 && arr === arr5; 143expect = true; 144addThis(); 145 146 147 148//----------------------------------------------------------------------------- 149test(); 150//----------------------------------------------------------------------------- 151 152 153 154function addThis() 155{ 156 statusitems[UBound] = status; 157 actualvalues[UBound] = actual; 158 expectedvalues[UBound] = expect; 159 UBound++; 160} 161 162 163function test() 164{ 165 enterFunc('test'); 166 printBugNumber(bug); 167 printStatus(summary); 168 169 for (var i=0; i<UBound; i++) 170 { 171 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 172 } 173 174 exitFunc ('test'); 175} 176