erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 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 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 28 | // Flags: --expose-externalize-string --allow-natives-syntax |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 29 | |
| 30 | var s = 'abcdefghijklmn'; |
| 31 | assertEquals(s, s.substr()); |
| 32 | assertEquals(s, s.substr(0)); |
| 33 | assertEquals(s, s.substr('0')); |
| 34 | assertEquals(s, s.substr(void 0)); |
| 35 | assertEquals(s, s.substr(null)); |
| 36 | assertEquals(s, s.substr(false)); |
| 37 | assertEquals(s, s.substr(0.9)); |
| 38 | assertEquals(s, s.substr({ valueOf: function() { return 0; } })); |
| 39 | assertEquals(s, s.substr({ toString: function() { return '0'; } })); |
| 40 | |
| 41 | var s1 = s.substring(1); |
| 42 | assertEquals(s1, s.substr(1)); |
| 43 | assertEquals(s1, s.substr('1')); |
| 44 | assertEquals(s1, s.substr(true)); |
| 45 | assertEquals(s1, s.substr(1.1)); |
| 46 | assertEquals(s1, s.substr({ valueOf: function() { return 1; } })); |
| 47 | assertEquals(s1, s.substr({ toString: function() { return '1'; } })); |
| 48 | |
| 49 | |
| 50 | assertEquals(s.substring(s.length - 1), s.substr(-1)); |
| 51 | assertEquals(s.substring(s.length - 1), s.substr(-1.2)); |
| 52 | assertEquals(s.substring(s.length - 1), s.substr(-1.7)); |
| 53 | assertEquals(s.substring(s.length - 2), s.substr(-2)); |
| 54 | assertEquals(s.substring(s.length - 2), s.substr(-2.3)); |
| 55 | assertEquals(s.substring(s.length - 2, s.length - 1), s.substr(-2, 1)); |
| 56 | assertEquals(s, s.substr(-100)); |
| 57 | assertEquals('abc', s.substr(-100, 3)); |
| 58 | assertEquals(s1, s.substr(-s.length + 1)); |
| 59 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 60 | // assertEquals('', s.substr(0, void 0)); // smjs and rhino |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 61 | assertEquals('abcdefghijklmn', s.substr(0, void 0)); // kjs and v8 |
| 62 | assertEquals('', s.substr(0, null)); |
| 63 | assertEquals(s, s.substr(0, String(s.length))); |
| 64 | assertEquals('a', s.substr(0, true)); |
| 65 | |
| 66 | |
| 67 | // Test substrings of different lengths and alignments. |
| 68 | // First ASCII. |
| 69 | var x = "ASCII"; |
| 70 | for (var i = 0; i < 25; i++) { |
| 71 | x += (i >> 4).toString(16) + (i & 0x0f).toString(16); |
| 72 | } |
| 73 | /x/.exec(x); // Try to force a flatten. |
| 74 | for (var i = 5; i < 25; i++) { |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 75 | for (var j = 0; j < 25; j++) { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 76 | var z = x.substring(i, i+j); |
| 77 | var w = Math.random() * 42; // Allocate something new in new-space. |
| 78 | assertEquals(j, z.length); |
| 79 | for (var k = 0; k < j; k++) { |
| 80 | assertEquals(x.charAt(i+k), z.charAt(k)); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | // Then two-byte strings. |
| 85 | x = "UC16\u2028"; // Non-ascii char forces two-byte string. |
| 86 | for (var i = 0; i < 25; i++) { |
| 87 | x += (i >> 4).toString(16) + (i & 0x0f).toString(16); |
| 88 | } |
| 89 | /x/.exec(x); // Try to force a flatten. |
| 90 | for (var i = 5; i < 25; i++) { |
| 91 | for (var j = 0; j < 25; j++) { |
| 92 | var z = x.substring(i, i + j); |
| 93 | var w = Math.random() * 42; // Allocate something new in new-space. |
| 94 | assertEquals(j, z.length); |
| 95 | for (var k = 0; k < j; k++) { |
| 96 | assertEquals(x.charAt(i+k), z.charAt(k)); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Keep creating strings to to force allocation failure on substring creation. |
| 102 | var x = "0123456789ABCDEF"; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 103 | for (var i = 0; i < 8; i++) x += x; |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 104 | var xl = x.length; |
| 105 | var cache = []; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 106 | for (var i = 0; i < 1000; i++) { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 107 | var z = x.substring(i % xl); |
| 108 | assertEquals(xl - (i % xl), z.length); |
| 109 | cache.push(z); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | // Same with two-byte strings |
| 114 | var x = "\u2028123456789ABCDEF"; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 115 | for (var i = 0; i < 8; i++) x += x; |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 116 | var xl = x.length; |
| 117 | var cache = []; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 118 | for (var i = 0; i < 1000; i++) { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 119 | var z = x.substring(i % xl); |
| 120 | assertEquals(xl - (i % xl), z.length); |
| 121 | cache.push(z); |
| 122 | } |
| 123 | |
| 124 | // Substring of substring. |
| 125 | var cache = []; |
| 126 | var last = x; |
| 127 | var offset = 0; |
| 128 | for (var i = 0; i < 64; i++) { |
| 129 | var z = last.substring(i); |
| 130 | last = z; |
| 131 | cache.push(z); |
| 132 | offset += i; |
| 133 | } |
| 134 | for (var i = 63; i >= 0; i--) { |
| 135 | var z = cache.pop(); |
| 136 | assertTrue(/\u2028123456789ABCDEF/.test(z)); |
| 137 | assertEquals(xl - offset, z.length); |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 138 | assertEquals(x.charAt(i*(i+1)/2), z.charAt(0)); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 139 | offset -= i; |
| 140 | } |
| 141 | |
| 142 | // Test charAt for different strings. |
| 143 | function f(s1, s2, s3, i) { |
| 144 | assertEquals(String.fromCharCode(97+i%11), s1.charAt(i%11)); |
| 145 | assertEquals(String.fromCharCode(97+i%11), s2.charAt(i%11)); |
| 146 | assertEquals(String.fromCharCode(98+i%11), s3.charAt(i%11)); |
| 147 | assertEquals(String.fromCharCode(101), s3.charAt(3)); |
| 148 | } |
| 149 | |
| 150 | flat = "abcdefghijkl12345"; |
| 151 | cons = flat + flat.toUpperCase(); |
| 152 | slice = "abcdefghijklmn12345".slice(1, -1); |
| 153 | for ( var i = 0; i < 1000; i++) { |
| 154 | f(flat, cons, slice, i); |
| 155 | } |
| 156 | flat = "abcdefghijkl1\u20232345"; |
| 157 | cons = flat + flat.toUpperCase(); |
| 158 | slice = "abcdefghijklmn1\u20232345".slice(1, -1); |
| 159 | for ( var i = 0; i < 1000; i++) { |
| 160 | f(flat, cons, slice, i); |
| 161 | } |
| 162 | |
| 163 | // Concatenate substrings. |
| 164 | var ascii = 'abcdefghijklmnop'; |
| 165 | var utf = '\u03B1\u03B2\u03B3\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9\u03BA\u03BB'; |
| 166 | assertEquals("klmno", ascii.substring(10,15) + ascii.substring(16)); |
| 167 | assertEquals("\u03B4\u03B7", utf.substring(3,4) + utf.substring(6,7)); |
| 168 | assertEquals("klp", ascii.substring(10,12) + ascii.substring(15,16)); |
| 169 | assertEquals("\u03B1\u03B4\u03B5", utf.substring(0,1) + utf.substring(5,3)); |
| 170 | assertEquals("", ascii.substring(16) + utf.substring(16)); |
| 171 | assertEquals("bcdef\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9", |
| 172 | ascii.substring(1,6) + utf.substring(3,9)); |
| 173 | assertEquals("\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9abcdefghijklmnop", |
| 174 | utf.substring(3,9) + ascii); |
| 175 | assertEquals("\u03B2\u03B3\u03B4\u03B5\u03B4\u03B5\u03B6\u03B7", |
| 176 | utf.substring(5,1) + utf.substring(3,7)); |
| 177 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 178 | // Externalizing strings. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 179 | var a = "123456789" + "qwertyuiopasdfghjklzxcvbnm"; |
| 180 | var b = "23456789qwertyuiopasdfghjklzxcvbn" |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 181 | assertEquals(a.slice(1,-1), b); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 182 | |
| 183 | assertTrue(isAsciiString(a)); |
| 184 | externalizeString(a, true); |
| 185 | assertFalse(isAsciiString(a)); |
| 186 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 187 | assertEquals(a.slice(1,-1), b); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 188 | assertTrue(/3456789qwe/.test(a)); |
| 189 | assertEquals(5, a.indexOf("678")); |
| 190 | assertEquals("12345", a.split("6")[0]); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 191 | |
| 192 | // Create a slice with an external string as parent string. |
| 193 | var c = a.slice(1,-1); |
| 194 | |
| 195 | function test_crankshaft() { |
| 196 | for (var i = 0; i < 20; i++) { |
| 197 | assertEquals(b.charAt(i), a.charAt(i + 1)); |
| 198 | assertEquals(b.charAt(i), c.charAt(i)); |
| 199 | assertEquals(b.charAt(4), c.charAt(4)); |
| 200 | assertTrue(/3456789qwe/.test(c)); |
| 201 | assertEquals(4, c.indexOf("678")); |
| 202 | assertEquals("2345", c.split("6")[0]); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | test_crankshaft(); |
| 207 | %OptimizeFunctionOnNextCall(test_crankshaft); |
| 208 | test_crankshaft(); |