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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 assertEquals(["this", "start", "length"], log); | 164 assertEquals(["this", "start", "length"], log); |
165 } | 165 } |
166 { | 166 { |
167 let log = []; | 167 let log = []; |
168 let string = {[Symbol.toPrimitive]() { log.push("this"); return "abc" }}; | 168 let string = {[Symbol.toPrimitive]() { log.push("this"); return "abc" }}; |
169 let start = {[Symbol.toPrimitive]() { log.push("start"); return 0 }}; | 169 let start = {[Symbol.toPrimitive]() { log.push("start"); return 0 }}; |
170 let length = {[Symbol.toPrimitive]() { log.push("length"); return 0 }}; | 170 let length = {[Symbol.toPrimitive]() { log.push("length"); return 0 }}; |
171 assertEquals("", String.prototype.substr.call(string, start, length)); | 171 assertEquals("", String.prototype.substr.call(string, start, length)); |
172 assertEquals(["this", "start", "length"], log); | 172 assertEquals(["this", "start", "length"], log); |
173 } | 173 } |
| 174 |
| 175 // Bounds edge cases. |
| 176 { |
| 177 const str = "abc"; |
| 178 const negativeHeapNumber = -1 * 2**32; |
| 179 const positiveHeapNumber = 2**32; |
| 180 |
| 181 assertEquals("abc", str.substr(negativeHeapNumber)); |
| 182 assertEquals("abc", str.substr(negativeHeapNumber, str.length)); |
| 183 assertEquals("abc", str.substr(-str.length, str.length)); |
| 184 assertEquals("abc", str.substr(0, str.length)); |
| 185 assertEquals("bc", str.substr(-2, str.length)); |
| 186 assertEquals("c", str.substr(-1, str.length)); |
| 187 |
| 188 assertEquals("", str.substr(str.length)); |
| 189 assertEquals("", str.substr(4)); |
| 190 assertEquals("", str.substr(positiveHeapNumber)); |
| 191 |
| 192 assertEquals("abc", str.substr(negativeHeapNumber, positiveHeapNumber)); |
| 193 assertEquals("abc", str.substr(negativeHeapNumber, positiveHeapNumber)); |
| 194 assertEquals("abc", str.substr(-str.length, positiveHeapNumber)); |
| 195 assertEquals("abc", str.substr(0, positiveHeapNumber)); |
| 196 assertEquals("bc", str.substr(-2, positiveHeapNumber)); |
| 197 assertEquals("c", str.substr(-1, positiveHeapNumber)); |
| 198 |
| 199 assertEquals("", str.substr(str.length, positiveHeapNumber)); |
| 200 assertEquals("", str.substr(4, positiveHeapNumber)); |
| 201 assertEquals("", str.substr(positiveHeapNumber, positiveHeapNumber)); |
| 202 |
| 203 assertEquals("", str.substr(negativeHeapNumber, negativeHeapNumber)); |
| 204 assertEquals("", str.substr(negativeHeapNumber, negativeHeapNumber)); |
| 205 assertEquals("", str.substr(-str.length, negativeHeapNumber)); |
| 206 assertEquals("", str.substr(0, negativeHeapNumber)); |
| 207 assertEquals("", str.substr(-2, negativeHeapNumber)); |
| 208 assertEquals("", str.substr(-1, negativeHeapNumber)); |
| 209 |
| 210 assertEquals("", str.substr(str.length, negativeHeapNumber)); |
| 211 assertEquals("", str.substr(4, negativeHeapNumber)); |
| 212 assertEquals("", str.substr(positiveHeapNumber, negativeHeapNumber)); |
| 213 |
| 214 assertEquals("", str.substr(negativeHeapNumber, -1)); |
| 215 assertEquals("", str.substr(negativeHeapNumber, -1)); |
| 216 assertEquals("", str.substr(-str.length, -1)); |
| 217 assertEquals("", str.substr(0, -1)); |
| 218 assertEquals("", str.substr(-2, -1)); |
| 219 assertEquals("", str.substr(-1, -1)); |
| 220 |
| 221 assertEquals("", str.substr(str.length, -1)); |
| 222 assertEquals("", str.substr(4, -1)); |
| 223 assertEquals("", str.substr(positiveHeapNumber, -1)); |
| 224 |
| 225 assertEquals("abc", str.substr(undefined)); |
| 226 assertEquals("abc", str.substr(undefined, undefined)); |
| 227 } |
OLD | NEW |