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