Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2011 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 --allow-natives-syntax
     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 
     60 // assertEquals('', s.substr(0, void 0)); // smjs and rhino
     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++) {
     75   for (var j = 0; j < 25; j++) {
     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";
    103 for (var i = 0; i < 8; i++) x += x;
    104 var xl = x.length;
    105 var cache = [];
    106 for (var i = 0; i < 1000; i++) {
    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";
    115 for (var i = 0; i < 8; i++) x += x;
    116 var xl = x.length;
    117 var cache = [];
    118 for (var i = 0; i < 1000; i++) {
    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);
    138   assertEquals(x.charAt(i*(i+1)/2), z.charAt(0));
    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 // Short substrings.
    164 flat = "abcdefghijkl12345";
    165 cons = flat + flat.toUpperCase();
    166 /x/.exec(cons);  // Flatten cons
    167 slice = "abcdefghijklmn12345".slice(1, -1);
    168 assertEquals("cdefg", flat.substr(2, 5));
    169 assertEquals("cdefg", cons.substr(2, 5));
    170 assertEquals("cdefg", slice.substr(1, 5));
    171 
    172 flat = "abc\u1234defghijkl12345";
    173 cons = flat + flat.toUpperCase();
    174 /x/.exec(cons);  // Flatten cons
    175 slice = "abc\u1234defghijklmn12345".slice(1, -1);
    176 assertEquals("c\u1234def", flat.substr(2, 5));
    177 assertEquals("c\u1234def", cons.substr(2, 5));
    178 assertEquals("c\u1234def", slice.substr(1, 5));
    179 
    180 // Concatenate substrings.
    181 var ascii = 'abcdefghijklmnop';
    182 var utf = '\u03B1\u03B2\u03B3\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9\u03BA\u03BB';
    183 assertEquals("klmno", ascii.substring(10,15) + ascii.substring(16));
    184 assertEquals("\u03B4\u03B7", utf.substring(3,4) + utf.substring(6,7));
    185 assertEquals("klp", ascii.substring(10,12) + ascii.substring(15,16));
    186 assertEquals("\u03B1\u03B4\u03B5", utf.substring(0,1) + utf.substring(5,3));
    187 assertEquals("", ascii.substring(16) + utf.substring(16));
    188 assertEquals("bcdef\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9",
    189     ascii.substring(1,6) + utf.substring(3,9));
    190 assertEquals("\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9abcdefghijklmnop",
    191     utf.substring(3,9) + ascii);
    192 assertEquals("\u03B2\u03B3\u03B4\u03B5\u03B4\u03B5\u03B6\u03B7",
    193     utf.substring(5,1) + utf.substring(3,7));
    194 
    195 // Externalizing strings.
    196 var a = "internalized dummy";
    197 a = "123456789" + "qwertyuiopasdfghjklzxcvbnm";
    198 var b = "23456789qwertyuiopasdfghjklzxcvbn"
    199 assertEquals(a.slice(1,-1), b);
    200 
    201 assertTrue(isOneByteString(a));
    202 externalizeString(a, true);
    203 assertFalse(isOneByteString(a));
    204 
    205 assertEquals(a.slice(1,-1), b);
    206 assertTrue(/3456789qwe/.test(a));
    207 assertEquals(5, a.indexOf("678"));
    208 assertEquals("12345", a.split("6")[0]);
    209 
    210 // Create a slice with an external string as parent string.
    211 var c = a.slice(1,-1);
    212 
    213 function test_crankshaft() {
    214   for (var i = 0; i < 20; i++) {
    215     assertEquals(b.charAt(i), a.charAt(i + 1));
    216     assertEquals(b.charAt(i), c.charAt(i));
    217     assertEquals(b.charAt(4), c.charAt(4));
    218     assertTrue(/3456789qwe/.test(c));
    219     assertEquals(4, c.indexOf("678"));
    220     assertEquals("2345", c.split("6")[0]);
    221   }
    222 }
    223 
    224 test_crankshaft();
    225 %OptimizeFunctionOnNextCall(test_crankshaft);
    226 test_crankshaft();
    227 
    228 var s1 = "12345678901234567890";
    229 var s2 = "abcdefghijklmnopqrstuvwxyz";
    230 var c1 = s1 + s2;
    231 var c2 = s1 + c1 + s2;
    232 assertEquals("234567890123456789", c1.substring(1, 19));
    233 assertEquals("bcdefghijklmno", c1.substring(21, 35));
    234 assertEquals("2345678901234567890abcdefghijklmno", c1.substring(1, 35));
    235 assertEquals("234567890123456789", c2.substring(1, 19));
    236 assertEquals("bcdefghijklmno", c2.substring(41, 55));
    237 assertEquals("2345678901234567890abcdefghijklmno", c2.substring(21, 55));
    238