1 /* ***** BEGIN LICENSE BLOCK *****
\r
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
\r
4 * The contents of this file are subject to the Mozilla Public License Version
\r
5 * 1.1 (the "License"); you may not use this file except in compliance with
\r
6 * the License. You may obtain a copy of the License at
\r
7 * http://www.mozilla.org/MPL/
\r
9 * Software distributed under the License is distributed on an "AS IS" basis,
\r
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
\r
11 * for the specific language governing rights and limitations under the
\r
14 * The Original Code is JavaScript Engine testing utilities.
\r
16 * The Initial Developer of the Original Code is
\r
17 * Netscape Communications Corp.
\r
18 * Portions created by the Initial Developer are Copyright (C) 2002
\r
19 * the Initial Developer. All Rights Reserved.
\r
22 * zen-parse@gmx.net, pschwartau@netscape.com
\r
24 * Alternatively, the contents of this file may be used under the terms of
\r
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
\r
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
\r
27 * in which case the provisions of the GPL or the LGPL are applicable instead
\r
28 * of those above. If you wish to allow use of your version of this file only
\r
29 * under the terms of either the GPL or the LGPL, and not to allow others to
\r
30 * use your version of this file under the terms of the MPL, indicate your
\r
31 * decision by deleting the provisions above and replace them with the notice
\r
32 * and other provisions required by the GPL or the LGPL. If you do not delete
\r
33 * the provisions above, a recipient may use your version of this file under
\r
34 * the terms of any one of the MPL, the GPL or the LGPL.
\r
36 * ***** END LICENSE BLOCK *****
\r
39 * Date: 16 July 2002
\r
40 * SUMMARY: Testing that Array.sort() doesn't crash on very large arrays
\r
41 * See http://bugzilla.mozilla.org/show_bug.cgi?id=157652
\r
43 * How large can a JavaScript array be?
\r
44 * ECMA-262 Ed.3 Final, Section 15.4.2.2 : new Array(len)
\r
46 * This states that |len| must be a a uint32 (unsigned 32-bit integer).
\r
47 * Note the UBound for uint32's is 2^32 -1 = 0xFFFFFFFF = 4,294,967,295.
\r
50 * js> var arr = new Array(0xFFFFFFFF)
\r
54 * js> var arr = new Array(0x100000000)
\r
55 * RangeError: invalid array length
\r
58 * We'll try the largest possible array first, then a couple others.
\r
59 * We're just testing that we don't crash on Array.sort().
\r
61 * Try to be good about memory by nulling each array variable after it is
\r
62 * used. This will tell the garbage collector the memory is no longer needed.
\r
64 * As of 2002-08-13, the JS shell runs out of memory no matter what we do,
\r
65 * when trying to sort such large arrays.
\r
67 * We only want to test that we don't CRASH on the sort. So it will be OK
\r
68 * if we get the JS "out of memory" error. Note this terminates the test
\r
69 * with exit code 3. Therefore we put
\r
71 * |expectExitCode(3);|
\r
73 * The only problem will arise if the JS shell ever DOES have enough memory
\r
74 * to do the sort. Then this test will terminate with the normal exit code 0
\r
77 * Right now, I can't see any other way to do this, because "out of memory"
\r
78 * is not a catchable error: it cannot be trapped with try...catch.
\r
81 * FURTHER HEADACHE: Rhino can't seem to handle the largest array: it hangs.
\r
82 * So we skip this case in Rhino. Here is correspondence with Igor Bukanov.
\r
83 * He explains that Rhino isn't actually hanging; it's doing the huge sort:
\r
85 * Philip Schwartau wrote:
\r
89 * > I'm getting a graceful OOM message on trying to sort certain large
\r
90 * > arrays. But if the array is too big, Rhino simply hangs. Note that ECMA
\r
91 * > allows array lengths to be anything less than Math.pow(2,32), so the
\r
92 * > arrays I'm sorting are legal.
\r
94 * > Note below, I'm getting an instantaneous OOM error on arr.sort() for LEN
\r
95 * > = Math.pow(2, 30). So shouldn't I also get one for every LEN between
\r
96 * > that and Math.pow(2, 32)? For some reason, I start to hang with 100% CPU
\r
97 * > as LEN hits, say, Math.pow(2, 31) and higher. SpiderMonkey gives OOM
\r
98 * > messages for all of these. Should I file a bug on this?
\r
100 * Igor Bukanov wrote:
\r
102 * This is due to different sorting algorithm Rhino uses when sorting
\r
103 * arrays with length > Integer.MAX_VALUE. If length can fit Java int,
\r
104 * Rhino first copies internal spare array to a temporary buffer, and then
\r
105 * sorts it, otherwise it sorts array directly. In case of very spare
\r
106 * arrays, that Array(big_number) generates, it is rather inefficient and
\r
107 * generates OutOfMemory if length fits int. It may be worth in your case
\r
108 * to optimize sorting to take into account array spareness, but then it
\r
109 * would be a good idea to file a bug about ineficient sorting of spare
\r
110 * arrays both in case of Rhino and SpiderMonkey as SM always uses a
\r
111 * temporary buffer.
\r
114 //-----------------------------------------------------------------------------
\r
116 var summary = "Testing that Array.sort() doesn't crash on very large arrays";
\r
118 printBugNumber(bug);
\r
119 printStatus(summary);
\r
122 var IN_RHINO = inRhino();
\r
126 var a1=Array(0xFFFFFFFF);
\r
131 var a2 = Array(0x40000000);
\r
135 var a3=Array(0x10000000/4);
\r