blob: 39cc124914cdece4cbc9b30f0bed66a78cc46e82 [file] [log] [blame]
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080028// Flags: --expose-externalize-string --expose-gc
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010029
30var size = 1024;
31
32function test() {
33 var str = "";
34
35 // Build an ascii cons string.
36 for (var i = 0; i < size; i++) {
37 str += String.fromCharCode(i & 0x7f);
38 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 assertTrue(isOneByteString(str));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010040
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 var twoByteExternalWithOneByteData =
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010042 "AA" + (function() { return "A"; })();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 externalizeString(twoByteExternalWithOneByteData, true /* force two-byte */);
44 assertFalse(isOneByteString(twoByteExternalWithOneByteData));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010045
46 var realTwoByteExternalString =
Ben Murdoch3ef787d2012-04-12 10:51:47 +010047 "\u1234\u1234\u1234\u1234" + (function() { return "\u1234"; })();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010048 externalizeString(realTwoByteExternalString);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 assertFalse(isOneByteString(realTwoByteExternalString));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010050
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051 assertTrue(isOneByteString(["a", twoByteExternalWithOneByteData].join("")));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010052
53 // Appending a two-byte string that contains only ascii chars should
54 // still produce an ascii cons.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 var str1 = str + twoByteExternalWithOneByteData;
56 assertTrue(isOneByteString(str1));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010057
58 // Force flattening of the string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 var old_length = str1.length - twoByteExternalWithOneByteData.length;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010060 for (var i = 0; i < old_length; i++) {
61 assertEquals(String.fromCharCode(i & 0x7f), str1[i]);
62 }
63 for (var i = old_length; i < str1.length; i++) {
64 assertEquals("A", str1[i]);
65 }
66
67 // Flattened string should still be ascii.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 assertTrue(isOneByteString(str1));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010069
70 // Lower-casing an ascii string should produce ascii.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 assertTrue(isOneByteString(str1.toLowerCase()));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010072
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 assertFalse(isOneByteString(["a", realTwoByteExternalString].join("")));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010074
75 // Appending a real two-byte string should produce a two-byte cons.
76 var str2 = str + realTwoByteExternalString;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 assertFalse(isOneByteString(str2));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010078
79 // Force flattening of the string.
80 old_length = str2.length - realTwoByteExternalString.length;
81 for (var i = 0; i < old_length; i++) {
82 assertEquals(String.fromCharCode(i & 0x7f), str2[i]);
83 }
84 for (var i = old_length; i < str.length; i++) {
85 assertEquals("\u1234", str2[i]);
86 }
87
88 // Flattened string should still be two-byte.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 assertFalse(isOneByteString(str2));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010090}
91
92// Run the test many times to ensure IC-s don't break things.
93for (var i = 0; i < 10; i++) {
94 test();
95}
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080096
97// Clean up string to make Valgrind happy.
98gc();
99gc();