Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / String / regress-179068.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  *   igor@icesoft.no, 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:    09 November 2002
42  * SUMMARY: Test that interpreter can handle string literals exceeding 64K
43  * See http://bugzilla.mozilla.org/show_bug.cgi?id=179068
44  *
45  * Test that the interpreter can handle string literals exceeding 64K limit.
46  * For that the script passes to eval() "str ='LONG_STRING_LITERAL';" where
47  * LONG_STRING_LITERAL is a string with 200K chars.
48  *
49  *   Igor Bukanov explains the technique used below:
50  *
51  * > Philip Schwartau wrote:
52  * >...
53  * > Here is the heart of the testcase:
54  * >
55  * >   // Generate 200K long string
56  * >   var long_str = duplicate(LONG_STR_SEED, N);
57  * >   var str = "";
58  * >   eval("str='".concat(long_str, "';"));
59  * >   var test_is_ok = (str.length == LONG_STR_SEED.length * N);
60  * >
61  * >
62  * > The testcase creates two identical strings, |long_str| and |str|. It
63  * > uses eval() simply to assign the value of |long_str| to |str|. Why is
64  * > it necessary to have the variable |str|, then? Why not just create
65  * > |long_str| and test it? Wouldn't this be enough:
66  * >
67  * >   // Generate 200K long string
68  * >   var long_str = duplicate(LONG_STR_SEED, N);
69  * >   var test_is_ok = (long_str.length == LONG_STR_SEED.length * N);
70  * >
71  * > Or do we specifically need to test eval() to exercise the interpreter?
72  *
73  * The reason for eval is to test string literals like in 'a string literal
74  * with 100 000 characters...', Rhino deals fine with strings generated at
75  * run time where lengths > 64K. Without eval it would be necessary to have
76  * a test file excedding 64K which is not that polite for CVS and then a
77  * special treatment for the compiled mode in Rhino should be added.
78  *
79  *
80  * >
81  * > If so, is it important to use the concat() method in the assignment, as
82  * > you have done: |eval("str='".concat(long_str, "';"))|, or can we simply
83  * > do |eval("str = long_str;")| ?
84  *
85  * The concat is a replacement for eval("str='"+long_str+"';"), but as
86  * long_str is huge, this leads to constructing first a new string via
87  * "str='"+long_str and then another one via ("str='"+long_str) + "';"
88  * which takes time under JDK 1.1 on a something like StrongArm 200MHz.
89  * Calling concat makes less copies, that is why it is used in the
90  * duplicate function and this is faster then doing recursion like in the
91  * test case to test that 64K different string literals can be handled.
92  *
93  */
94 //-----------------------------------------------------------------------------
95 var UBound = 0;
96 var BUGNUMBER = 179068;
97 var summary = 'Test that interpreter can handle string literals exceeding 64K';
98 var status = '';
99 var statusitems = [];
100 var actual = '';
101 var actualvalues = [];
102 var expect= '';
103 var expectedvalues = [];
104 var LONG_STR_SEED = "0123456789";
105 var N = 20 * 1024;
106 var str = "";
107
108
109 // Generate 200K long string and assign it to |str| via eval()
110 var long_str = duplicate(LONG_STR_SEED, N);
111 eval("str='".concat(long_str, "';"));
112
113 status = inSection(1);
114 actual = str.length == LONG_STR_SEED.length * N
115   expect = true;
116 addThis();
117
118
119
120 //-----------------------------------------------------------------------------
121 test();
122 //-----------------------------------------------------------------------------
123
124
125
126 function duplicate(str, count)
127 {
128   var tmp = new Array(count);
129
130   while (count != 0)
131     tmp[--count] = str;
132
133   return String.prototype.concat.apply("", tmp);
134 }
135
136
137 function addThis()
138 {
139   statusitems[UBound] = status;
140   actualvalues[UBound] = actual;
141   expectedvalues[UBound] = expect;
142   UBound++;
143 }
144
145
146 function test()
147 {
148   enterFunc('test');
149   printBugNumber(BUGNUMBER);
150   printStatus(summary);
151
152   for (var i=0; i<UBound; i++)
153   {
154     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
155   }
156
157   exitFunc ('test');
158 }