Debian Codegraph @

rhino/testsrc/tests/ecma_3/Array/regress-130451.js

raw symbols SCIP
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: 25 Mar 2002
9 * SUMMARY: Array.prototype.sort() should not (re-)define .length
10 * See http://bugzilla.mozilla.org/show_bug.cgi?id=130451
11 *
12 * From the ECMA-262 Edition 3 Final spec:
13 *
14 * NOTE: The sort function is intentionally generic; it does not require that
15 * its |this| value be an Array object. Therefore, it can be transferred to
16 * other kinds of objects for use as a method. Whether the sort function can
17 * be applied successfully to a host object is implementation-dependent.
18 *
19 * The interesting parts of this testcase are the contrasting expectations for
20 * Brendan's test below, when applied to Array objects vs. non-Array objects.
21 *
22 */
23 //-----------------------------------------------------------------------------
24 var gTestfile = 'regress-130451.js';
25 var UBound = 0;
26 var BUGNUMBER = 130451;
27 var summary = 'Array.prototype.sort() should not (re-)define .length';
28 var status = '';
29 var statusitems = [];
30 var actual = '';
31 var actualvalues = [];
32 var expect= '';
33 var expectedvalues = [];
34 var arr = [];
35 var cmp = new Function();
36
37
38 /*
39 * First: test Array.prototype.sort() on Array objects
40 */
41 status = inSection(1);
42 arr = [0,1,2,3];
43 cmp = function(x,y) {return x-y;};
44 actual = arr.sort(cmp).length;
45 expect = 4;
46 addThis();
47
48 status = inSection(2);
49 arr = [0,1,2,3];
50 cmp = function(x,y) {return y-x;};
51 actual = arr.sort(cmp).length;
52 expect = 4;
53 addThis();
54
55 status = inSection(3);
56 arr = [0,1,2,3];
57 cmp = function(x,y) {return x-y;};
58 arr.length = 1;
59 actual = arr.sort(cmp).length;
60 expect = 1;
61 addThis();
62
63 /*
64 * This test is by Brendan. Setting arr.length to
65 * 2 and then 4 should cause elements to be deleted.
66 */
67 arr = [0,1,2,3];
68 cmp = function(x,y) {return x-y;};
69 arr.sort(cmp);
70
71 status = inSection(4);
72 actual = arr.join();
73 expect = '0,1,2,3';
74 addThis();
75
76 status = inSection(5);
77 actual = arr.length;
78 expect = 4;
79 addThis();
80
81 status = inSection(6);
82 arr.length = 2;
83 actual = arr.join();
84 expect = '0,1';
85 addThis();
86
87 status = inSection(7);
88 arr.length = 4;
89 actual = arr.join();
90 expect = '0,1,,'; //<---- see how 2,3 have been lost
91 addThis();
92
93
94
95 /*
96 * Now test Array.prototype.sort() on non-Array objects
97 */
98 status = inSection(8);
99 var obj = new Object();
100 obj.sort = Array.prototype.sort;
101 obj.length = 4;
102 obj[0] = 0;
103 obj[1] = 1;
104 obj[2] = 2;
105 obj[3] = 3;
106 cmp = function(x,y) {return x-y;};
107 actual = obj.sort(cmp).length;
108 expect = 4;
109 addThis();
110
111
112 /*
113 * Here again is Brendan's test. Unlike the array case
114 * above, the setting of obj.length to 2 and then 4
115 * should NOT cause elements to be deleted
116 */
117 obj = new Object();
118 obj.sort = Array.prototype.sort;
119 obj.length = 4;
120 obj[0] = 3;
121 obj[1] = 2;
122 obj[2] = 1;
123 obj[3] = 0;
124 cmp = function(x,y) {return x-y;};
125 obj.sort(cmp); //<---- this is what triggered the buggy behavior below
126 obj.join = Array.prototype.join;
127
128 status = inSection(9);
129 actual = obj.join();
130 expect = '0,1,2,3';
131 addThis();
132
133 status = inSection(10);
134 actual = obj.length;
135 expect = 4;
136 addThis();
137
138 status = inSection(11);
139 obj.length = 2;
140 actual = obj.join();
141 expect = '0,1';
142 addThis();
143
144 /*
145 * Before this bug was fixed, |actual| held the value '0,1,,'
146 * as in the Array-object case at top. This bug only occurred
147 * if Array.prototype.sort() had been applied to |obj|,
148 * as we have done higher up.
149 */
150 status = inSection(12);
151 obj.length = 4;
152 actual = obj.join();
153 expect = '0,1,2,3';
154 addThis();
155
156
157
158
159 //-----------------------------------------------------------------------------
160 test();
161 //-----------------------------------------------------------------------------
162
163
164
165 function addThis()
166 {
167 statusitems[UBound] = status;
168 actualvalues[UBound] = actual;
169 expectedvalues[UBound] = expect;
170 UBound++;
171 }
172
173
174 function test()
175 {
176 enterFunc('test');
177 printBugNumber(BUGNUMBER);
178 printStatus(summary);
179
180 for (var i=0; i<UBound; i++)
181 {
182 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
183 }
184
185 exitFunc ('test');
186 }