Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
|
Yang
2011/07/27 11:51:58
Mostly a copy of test/mjsunit/substr.js, but using
| |
| 28 // Flags: --string-slices | |
| 29 | |
| 28 var s = 'abcdefghijklmn'; | 30 var s = 'abcdefghijklmn'; |
| 29 assertEquals(s, s.substr()); | 31 assertEquals(s, s.substr()); |
| 30 assertEquals(s, s.substr(0)); | 32 assertEquals(s, s.substr(0)); |
| 31 assertEquals(s, s.substr('0')); | 33 assertEquals(s, s.substr('0')); |
| 32 assertEquals(s, s.substr(void 0)); | 34 assertEquals(s, s.substr(void 0)); |
| 33 assertEquals(s, s.substr(null)); | 35 assertEquals(s, s.substr(null)); |
| 34 assertEquals(s, s.substr(false)); | 36 assertEquals(s, s.substr(false)); |
| 35 assertEquals(s, s.substr(0.9)); | 37 assertEquals(s, s.substr(0.9)); |
| 36 assertEquals(s, s.substr({ valueOf: function() { return 0; } })); | 38 assertEquals(s, s.substr({ valueOf: function() { return 0; } })); |
| 37 assertEquals(s, s.substr({ toString: function() { return '0'; } })); | 39 assertEquals(s, s.substr({ toString: function() { return '0'; } })); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 63 | 65 |
| 64 | 66 |
| 65 // Test substrings of different lengths and alignments. | 67 // Test substrings of different lengths and alignments. |
| 66 // First ASCII. | 68 // First ASCII. |
| 67 var x = "ASCII"; | 69 var x = "ASCII"; |
| 68 for (var i = 0; i < 25; i++) { | 70 for (var i = 0; i < 25; i++) { |
| 69 x += (i >> 4).toString(16) + (i & 0x0f).toString(16); | 71 x += (i >> 4).toString(16) + (i & 0x0f).toString(16); |
| 70 } | 72 } |
| 71 /x/.exec(x); // Try to force a flatten. | 73 /x/.exec(x); // Try to force a flatten. |
| 72 for (var i = 5; i < 25; i++) { | 74 for (var i = 5; i < 25; i++) { |
| 73 for (var j = 0; j < 25; j++) { | 75 for (var j = 12; j < 25; j++) { |
| 74 var z = x.substring(i, i+j); | 76 var z = x.substring(i, i+j); |
| 75 var w = Math.random() * 42; // Allocate something new in new-space. | 77 var w = Math.random() * 42; // Allocate something new in new-space. |
| 76 assertEquals(j, z.length); | 78 assertEquals(j, z.length); |
| 77 for (var k = 0; k < j; k++) { | 79 for (var k = 0; k < j; k++) { |
| 78 assertEquals(x.charAt(i+k), z.charAt(k)); | 80 assertEquals(x.charAt(i+k), z.charAt(k)); |
| 79 } | 81 } |
| 80 } | 82 } |
| 81 } | 83 } |
| 82 | |
| 83 | |
| 84 // Then two-byte strings. | 84 // Then two-byte strings. |
| 85 x = "UC16\u2028"; // Non-ascii char forces two-byte string. | 85 x = "UC16\u2028"; // Non-ascii char forces two-byte string. |
| 86 for (var i = 0; i < 25; i++) { | 86 for (var i = 0; i < 25; i++) { |
| 87 x += (i >> 4).toString(16) + (i & 0x0f).toString(16); | 87 x += (i >> 4).toString(16) + (i & 0x0f).toString(16); |
| 88 } | 88 } |
| 89 /x/.exec(x); // Try to force a flatten. | 89 /x/.exec(x); // Try to force a flatten. |
| 90 for (var i = 5; i < 25; i++) { | 90 for (var i = 5; i < 25; i++) { |
| 91 for (var j = 0; j < 25; j++) { | 91 for (var j = 0; j < 25; j++) { |
| 92 var z = x.substring(i, i + j); | 92 var z = x.substring(i, i + j); |
| 93 var w = Math.random() * 42; // Allocate something new in new-space. | 93 var w = Math.random() * 42; // Allocate something new in new-space. |
| 94 assertEquals(j, z.length); | 94 assertEquals(j, z.length); |
| 95 for (var k = 0; k < j; k++) { | 95 for (var k = 0; k < j; k++) { |
| 96 assertEquals(x.charAt(i+k), z.charAt(k)); | 96 assertEquals(x.charAt(i+k), z.charAt(k)); |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 | 100 |
| 101 | |
| 102 // Keep creating strings to to force allocation failure on substring creation. | 101 // Keep creating strings to to force allocation failure on substring creation. |
| 103 var x = "0123456789ABCDEF"; | 102 var x = "0123456789ABCDEF"; |
| 104 x += x; // 2^5 | 103 x += x; // 2^5 |
| 105 x += x; | 104 x += x; |
| 106 x += x; | 105 x += x; |
| 107 x += x; | 106 x += x; |
| 108 x += x; | 107 x += x; |
| 109 x += x; // 2^10 | 108 x += x; // 2^10 |
| 110 x += x; | 109 x += x; |
| 111 x += x; | 110 x += x; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 128 x += x; // 2^10 | 127 x += x; // 2^10 |
| 129 x += x; | 128 x += x; |
| 130 x += x; | 129 x += x; |
| 131 var xl = x.length; | 130 var xl = x.length; |
| 132 var cache = []; | 131 var cache = []; |
| 133 for (var i = 0; i < 10000; i++) { | 132 for (var i = 0; i < 10000; i++) { |
| 134 var z = x.substring(i % xl); | 133 var z = x.substring(i % xl); |
| 135 assertEquals(xl - (i % xl), z.length); | 134 assertEquals(xl - (i % xl), z.length); |
| 136 cache.push(z); | 135 cache.push(z); |
| 137 } | 136 } |
| 137 | |
| 138 // Substring of substring. | |
| 139 var cache = []; | |
| 140 var last = x; | |
| 141 var offset = 0; | |
| 142 for (var i = 0; i < 64; i++) { | |
| 143 var z = last.substring(i); | |
| 144 last = z; | |
| 145 cache.push(z); | |
| 146 offset += i; | |
| 147 } | |
| 148 for (var i = 63; i >= 0; i--) { | |
| 149 var z = cache.pop(); | |
| 150 assertTrue(/\u2028123456789ABCDEF/.test(z)); | |
| 151 assertEquals(xl - offset, z.length); | |
| 152 offset -= i; | |
| 153 } | |
| 154 | |
| 155 //Test charAt for different strings. | |
|
antonm
2011/07/27 12:16:39
nit: missing space before Test.
| |
| 156 flat = "abcdefghijkl12345"; | |
| 157 cons = flat + flat.toUpperCase(); | |
| 158 slice = "abcdefghijklmn12345".slice(1, -1); | |
| 159 for ( var i = 0; i < 50; i++) { | |
|
antonm
2011/07/27 12:16:39
nit: unnecessary space after ( (here and below)
| |
| 160 assertEquals(String.fromCharCode(97+i%11), flat.charAt(i%11)); | |
| 161 assertEquals(String.fromCharCode(97+i%11), cons.charAt(i%11)); | |
| 162 assertEquals(String.fromCharCode(98+i%11), slice.charAt(i%11)); | |
| 163 assertEquals(String.fromCharCode(101), slice.charAt(3)); | |
| 164 } | |
| 165 flat = "abcdefghijkl1\u20232345"; | |
| 166 cons = flat + flat.toUpperCase(); | |
| 167 slice = "abcdefghijklmn1\u20232345".slice(1, -1); | |
| 168 for ( var i = 0; i < 50; i++) { | |
| 169 assertEquals(String.fromCharCode(97+i%11), flat.charAt(i%11)); | |
| 170 assertEquals(String.fromCharCode(97+i%11), cons.charAt(i%11)); | |
| 171 assertEquals(String.fromCharCode(98+i%11), slice.charAt(i%11)); | |
| 172 assertEquals(String.fromCharCode(101), slice.charAt(3)); | |
| 173 } | |
| 174 | |
| 175 // Concatenate substrings. | |
| 176 var ascii = 'abcdefghijklmnop'; | |
| 177 var utf = '\u03B1\u03B2\u03B3\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9\u03BA\u03BB'; | |
| 178 assertEquals("klmno", ascii.substring(10,15) + ascii.substring(16)); | |
| 179 assertEquals("\u03B4\u03B7", utf.substring(3,4) + utf.substring(6,7)); | |
| 180 assertEquals("klp", ascii.substring(10,12) + ascii.substring(15,16)); | |
| 181 assertEquals("\u03B1\u03B4\u03B5", utf.substring(0,1) + utf.substring(5,3)); | |
| 182 assertEquals("", ascii.substring(16) + utf.substring(16)); | |
| 183 assertEquals("bcdef\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9", | |
| 184 ascii.substring(1,6) + utf.substring(3,9)); | |
| 185 assertEquals("\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9abcdefghijklmnop", | |
| 186 utf.substring(3,9) + ascii); | |
| 187 assertEquals("\u03B2\u03B3\u03B4\u03B5\u03B4\u03B5\u03B6\u03B7", | |
| 188 utf.substring(5,1) + utf.substring(3,7)); | |
| 189 | |
| OLD | NEW |