blob: 12312ac76aa385f9be26a1230ba61e02262f27ce [file] [log] [blame]
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001// 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
28// Flags: --expose-externalize-string --expose-gc
29// Test data pointer caching of external strings.
30
31function test() {
32 // Test string.charAt.
33 var charat_str = new Array(5);
34 charat_str[0] = "0123456789ABCDEF0123456789ABCDEF\
350123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\
360123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\
370123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\
380123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
39 charat_str[1] = "0123456789ABCDEF";
40 for (var i = 0; i < 6; i++) charat_str[1] += charat_str[1];
41 try { // String can only be externalized once
42 externalizeString(charat_str[0], false);
43 externalizeString(charat_str[1], true);
44 } catch (ex) { }
45 charat_str[2] = charat_str[0].slice(0, -1);
46 charat_str[3] = charat_str[1].slice(0, -1);
47 charat_str[4] = charat_str[0] + charat_str[0];
48
49 for (var i = 0; i < 5; i++) {
50 assertEquals('B', charat_str[i].charAt(6*16 + 11));
51 assertEquals('C', charat_str[i].charAt(6*16 + 12));
52 assertEquals('A', charat_str[i].charAt(3*16 + 10));
53 assertEquals('B', charat_str[i].charAt(3*16 + 11));
54 }
55
56 charat_short = "012";
57 try { // String can only be externalized once
58 externalizeString(charat_short, true);
59 } catch (ex) { }
60 assertEquals("1", charat_short.charAt(1));
61
62 // Test regexp.
63 var re = /(A|B)/;
64 var rere = /(T.{1,2}B)/;
65 var ascii = "ABCDEFGHIJKLMNOPQRST";
66 var twobyte = "_ABCDEFGHIJKLMNOPQRST";
67 try {
68 externalizeString(ascii, false);
69 externalizeString(twobyte, true);
70 } catch (ex) { }
71 assertTrue(isAsciiString(ascii));
72 assertFalse(isAsciiString(twobyte));
73 var ascii_slice = ascii.slice(1,-1);
74 var twobyte_slice = twobyte.slice(2,-1);
75 var ascii_cons = ascii + ascii;
76 var twobyte_cons = twobyte + twobyte;
77 for (var i = 0; i < 2; i++) {
78 assertEquals(["A", "A"], re.exec(ascii));
79 assertEquals(["B", "B"], re.exec(ascii_slice));
80 assertEquals(["TAB", "TAB"], rere.exec(ascii_cons));
81 assertEquals(["A", "A"], re.exec(twobyte));
82 assertEquals(["B", "B"], re.exec(twobyte_slice));
83 assertEquals(["T_AB", "T_AB"], rere.exec(twobyte_cons));
84 }
85}
86
87// Run the test many times to ensure IC-s don't break things.
88for (var i = 0; i < 10; i++) {
89 test();
90}
91
92// Clean up string to make Valgrind happy.
93gc();
94gc();