Home | History | Annotate | Download | only in Array
      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 (at) 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 //-----------------------------------------------------------------------------
     59 var UBound = 0;
     60 var bug = 178722;
     61 var summary = 'arr.sort() should not output |undefined| when |arr| is empty';
     62 var status = '';
     63 var statusitems = [];
     64 var actual = '';
     65 var actualvalues = [];
     66 var expect= '';
     67 var expectedvalues = [];
     68 var arr;
     69 
     70 
     71 // create empty array or pseudo-array objects in various ways
     72 var arr1 = Array();
     73 var arr2 = new Array();
     74 var arr3 = [];
     75 var arr4 = [1];
     76 arr4.pop();
     77 function f () {return arguments};
     78 var arr5 = f();
     79 arr5.__proto__ = Array.prototype;
     80 
     81 
     82 status = inSection(1);
     83 arr = arr1.sort();
     84 actual = arr instanceof Array && arr.length === 0 && arr === arr1;
     85 expect = true;
     86 addThis();
     87 
     88 status = inSection(2);
     89 arr = arr2.sort();
     90 actual = arr instanceof Array && arr.length === 0 && arr === arr2;
     91 expect = true;
     92 addThis();
     93 
     94 status = inSection(3);
     95 arr = arr3.sort();
     96 actual = arr instanceof Array && arr.length === 0 && arr === arr3;
     97 expect = true;
     98 addThis();
     99 
    100 status = inSection(4);
    101 arr = arr4.sort();
    102 actual = arr instanceof Array && arr.length === 0 && arr === arr4;
    103 expect = true;
    104 addThis();
    105 
    106 status = inSection(5);
    107 arr = arr5.sort();
    108 actual = arr instanceof Array && arr.length === 0 && arr === arr5;
    109 expect = true;
    110 addThis();
    111 
    112 
    113 // now do the same thing, with non-default sorting:
    114 function g() {return 1;}
    115 
    116 status = inSection('1a');
    117 arr = arr1.sort(g);
    118 actual = arr instanceof Array && arr.length === 0 && arr === arr1;
    119 expect = true;
    120 addThis();
    121 
    122 status = inSection('2a');
    123 arr = arr2.sort(g);
    124 actual = arr instanceof Array && arr.length === 0 && arr === arr2;
    125 expect = true;
    126 addThis();
    127 
    128 status = inSection('3a');
    129 arr = arr3.sort(g);
    130 actual = arr instanceof Array && arr.length === 0 && arr === arr3;
    131 expect = true;
    132 addThis();
    133 
    134 status = inSection('4a');
    135 arr = arr4.sort(g);
    136 actual = arr instanceof Array && arr.length === 0 && arr === arr4;
    137 expect = true;
    138 addThis();
    139 
    140 status = inSection('5a');
    141 arr = arr5.sort(g);
    142 actual = arr instanceof Array && arr.length === 0 && arr === arr5;
    143 expect = true;
    144 addThis();
    145 
    146 
    147 
    148 //-----------------------------------------------------------------------------
    149 test();
    150 //-----------------------------------------------------------------------------
    151 
    152 
    153 
    154 function addThis()
    155 {
    156   statusitems[UBound] = status;
    157   actualvalues[UBound] = actual;
    158   expectedvalues[UBound] = expect;
    159   UBound++;
    160 }
    161 
    162 
    163 function 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