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 |
(...skipping 26 matching lines...) Expand all Loading... | |
37 assertEquals(s, s.substr({ toString: function() { return '0'; } })); | 37 assertEquals(s, s.substr({ toString: function() { return '0'; } })); |
38 | 38 |
39 var s1 = s.substring(1); | 39 var s1 = s.substring(1); |
40 assertEquals(s1, s.substr(1)); | 40 assertEquals(s1, s.substr(1)); |
41 assertEquals(s1, s.substr('1')); | 41 assertEquals(s1, s.substr('1')); |
42 assertEquals(s1, s.substr(true)); | 42 assertEquals(s1, s.substr(true)); |
43 assertEquals(s1, s.substr(1.1)); | 43 assertEquals(s1, s.substr(1.1)); |
44 assertEquals(s1, s.substr({ valueOf: function() { return 1; } })); | 44 assertEquals(s1, s.substr({ valueOf: function() { return 1; } })); |
45 assertEquals(s1, s.substr({ toString: function() { return '1'; } })); | 45 assertEquals(s1, s.substr({ toString: function() { return '1'; } })); |
46 | 46 |
47 for (var i = 0; i < s.length; i++) | 47 //for (var i = 0; i < s.length; i++) |
Søren Thygesen Gjesse
2010/02/03 14:25:36
Code in comments.
Lasse Reichstein
2011/06/14 11:13:06
Whoops. Removed.
| |
48 for (var j = i; j < s.length + 5; j++) | 48 // for (var j = i; j < s.length + 5; j++) |
49 assertEquals(s.substring(i, j), s.substr(i, j - i)); | 49 // assertEquals(s.substring(i, j), s.substr(i, j - i)); |
50 | 50 |
51 assertEquals(s.substring(s.length - 1), s.substr(-1)); | 51 assertEquals(s.substring(s.length - 1), s.substr(-1)); |
52 assertEquals(s.substring(s.length - 1), s.substr(-1.2)); | 52 assertEquals(s.substring(s.length - 1), s.substr(-1.2)); |
53 assertEquals(s.substring(s.length - 1), s.substr(-1.7)); | 53 assertEquals(s.substring(s.length - 1), s.substr(-1.7)); |
54 assertEquals(s.substring(s.length - 2), s.substr(-2)); | 54 assertEquals(s.substring(s.length - 2), s.substr(-2)); |
55 assertEquals(s.substring(s.length - 2), s.substr(-2.3)); | 55 assertEquals(s.substring(s.length - 2), s.substr(-2.3)); |
56 assertEquals(s.substring(s.length - 2, s.length - 1), s.substr(-2, 1)); | 56 assertEquals(s.substring(s.length - 2, s.length - 1), s.substr(-2, 1)); |
57 assertEquals(s, s.substr(-100)); | 57 assertEquals(s, s.substr(-100)); |
58 assertEquals('abc', s.substr(-100, 3)); | 58 assertEquals('abc', s.substr(-100, 3)); |
59 assertEquals(s1, s.substr(-s.length + 1)); | 59 assertEquals(s1, s.substr(-s.length + 1)); |
60 | 60 |
61 // assertEquals('', s.substr(0, void 0)); // smjs and rhino | 61 // assertEquals('', s.substr(0, void 0)); // smjs and rhino |
62 assertEquals('abcdefghijklmn', s.substr(0, void 0)); // kjs and v8 | 62 assertEquals('abcdefghijklmn', s.substr(0, void 0)); // kjs and v8 |
63 assertEquals('', s.substr(0, null)); | 63 assertEquals('', s.substr(0, null)); |
64 assertEquals(s, s.substr(0, String(s.length))); | 64 assertEquals(s, s.substr(0, String(s.length))); |
65 assertEquals('a', s.substr(0, true)); | 65 assertEquals('a', s.substr(0, true)); |
66 | |
67 | |
68 // Test substrings of different lengths and alignments. | |
69 // First ASCII. | |
70 var x = "ASCII"; | |
71 for (var i = 0; i < 25; i++) { | |
72 x += (i >> 4).toString(16) + (i & 0x0f).toString(16); | |
73 } | |
74 /x/.exec(x); // Try to force a flatten. | |
75 for (var i = 5; i < 25; i++) { | |
76 for (var j = 0; j < 25; j++) { | |
77 var z = x.substring(i, i+j); | |
78 var w = Math.random() * 42; // Allocate something new in new-space. | |
79 assertEquals(j, z.length); | |
80 for (var k = 0; k < j; k++) { | |
81 assertEquals(x.charAt(i+k), z.charAt(k)); | |
82 } | |
83 } | |
84 } | |
85 | |
86 | |
87 // Then two-byte strings. | |
88 x = "UC16\u2028"; // Non-ascii char forces two-byte string. | |
89 for (var i = 0; i < 25; i++) { | |
90 x += (i >> 4).toString(16) + (i & 0x0f).toString(16); | |
91 } | |
92 /x/.exec(x); // Try to force a flatten. | |
93 for (var i = 5; i < 25; i++) { | |
94 for (var j = 0; j < 25; j++) { | |
95 var z = x.substring(i, i + j); | |
96 var w = Math.random() * 42; // Allocate something new in new-space. | |
97 assertEquals(j, z.length); | |
98 for (var k = 0; k < j; k++) { | |
99 assertEquals(x.charAt(i+k), z.charAt(k)); | |
100 } | |
101 } | |
102 } | |
103 | |
104 | |
105 // Keep creating strings to to force allocation failure on substring creation. | |
106 var x = "0123456789ABCDEF"; | |
107 x += x; // 2^5 | |
108 x += x; | |
109 x += x; | |
110 x += x; | |
111 x += x; | |
112 x += x; // 2^10 | |
113 x += x; | |
114 x += x; | |
115 var xl = x.length; | |
116 var cache = []; | |
117 for (var i = 0; i < 10000; i++) { | |
118 var z = x.substring(i % xl); | |
119 assertEquals(xl - (i % xl), z.length); | |
120 cache.push(z); | |
121 } | |
122 | |
123 | |
124 // Same with two-byte strings | |
125 var x = "\u2028123456789ABCDEF"; | |
126 x += x; // 2^5 | |
127 x += x; | |
128 x += x; | |
129 x += x; | |
130 x += x; | |
131 x += x; // 2^10 | |
132 x += x; | |
133 x += x; | |
134 var xl = x.length; | |
135 var cache = []; | |
136 for (var i = 0; i < 10000; i++) { | |
137 var z = x.substring(i % xl); | |
138 assertEquals(xl - (i % xl), z.length); | |
139 cache.push(z); | |
140 } | |
OLD | NEW |