michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * michael@0: * Date: 09 November 2002 michael@0: * SUMMARY: Test that interpreter can handle string literals exceeding 64K michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=179068 michael@0: * michael@0: * Test that the interpreter can handle string literals exceeding 64K limit. michael@0: * For that the script passes to eval() "str ='LONG_STRING_LITERAL';" where michael@0: * LONG_STRING_LITERAL is a string with 200K chars. michael@0: * michael@0: * Igor Bukanov explains the technique used below: michael@0: * michael@0: * > Philip Schwartau wrote: michael@0: * >... michael@0: * > Here is the heart of the testcase: michael@0: * > michael@0: * > // Generate 200K long string michael@0: * > var long_str = duplicate(LONG_STR_SEED, N); michael@0: * > var str = ""; michael@0: * > eval("str='".concat(long_str, "';")); michael@0: * > var test_is_ok = (str.length == LONG_STR_SEED.length * N); michael@0: * > michael@0: * > michael@0: * > The testcase creates two identical strings, |long_str| and |str|. It michael@0: * > uses eval() simply to assign the value of |long_str| to |str|. Why is michael@0: * > it necessary to have the variable |str|, then? Why not just create michael@0: * > |long_str| and test it? Wouldn't this be enough: michael@0: * > michael@0: * > // Generate 200K long string michael@0: * > var long_str = duplicate(LONG_STR_SEED, N); michael@0: * > var test_is_ok = (long_str.length == LONG_STR_SEED.length * N); michael@0: * > michael@0: * > Or do we specifically need to test eval() to exercise the interpreter? michael@0: * michael@0: * The reason for eval is to test string literals like in 'a string literal michael@0: * with 100 000 characters...', Rhino deals fine with strings generated at michael@0: * run time where lengths > 64K. Without eval it would be necessary to have michael@0: * a test file excedding 64K which is not that polite for CVS and then a michael@0: * special treatment for the compiled mode in Rhino should be added. michael@0: * michael@0: * michael@0: * > michael@0: * > If so, is it important to use the concat() method in the assignment, as michael@0: * > you have done: |eval("str='".concat(long_str, "';"))|, or can we simply michael@0: * > do |eval("str = long_str;")| ? michael@0: * michael@0: * The concat is a replacement for eval("str='"+long_str+"';"), but as michael@0: * long_str is huge, this leads to constructing first a new string via michael@0: * "str='"+long_str and then another one via ("str='"+long_str) + "';" michael@0: * which takes time under JDK 1.1 on a something like StrongArm 200MHz. michael@0: * Calling concat makes less copies, that is why it is used in the michael@0: * duplicate function and this is faster then doing recursion like in the michael@0: * test case to test that 64K different string literals can be handled. michael@0: * michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 179068; michael@0: var summary = 'Test that interpreter can handle string literals exceeding 64K'; michael@0: var status = ''; michael@0: var statusitems = []; michael@0: var actual = ''; michael@0: var actualvalues = []; michael@0: var expect= ''; michael@0: var expectedvalues = []; michael@0: var LONG_STR_SEED = "0123456789"; michael@0: var N = 20 * 1024; michael@0: var str = ""; michael@0: michael@0: michael@0: // Generate 200K long string and assign it to |str| via eval() michael@0: var long_str = duplicate(LONG_STR_SEED, N); michael@0: eval("str='".concat(long_str, "';")); michael@0: michael@0: status = inSection(1); michael@0: actual = str.length == LONG_STR_SEED.length * N michael@0: expect = true; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function duplicate(str, count) michael@0: { michael@0: var tmp = new Array(count); michael@0: michael@0: while (count != 0) michael@0: tmp[--count] = str; michael@0: michael@0: return String.prototype.concat.apply("", tmp); michael@0: } michael@0: michael@0: michael@0: function addThis() michael@0: { michael@0: statusitems[UBound] = status; michael@0: actualvalues[UBound] = actual; michael@0: expectedvalues[UBound] = expect; michael@0: UBound++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus(summary); michael@0: michael@0: for (var i=0; i