OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Short string. |
| 6 var s = "0123456789"; |
| 7 // Long string. |
| 8 var l = "$s$s$s$s$s$s$s$s$s$s"; |
| 9 // Very long string. |
| 10 var v = "$l$l$l$l$l$l$l$l$l$l"; |
| 11 |
| 12 |
| 13 testSliceSuccess() { |
| 14 // Test different ways to get the same slice from a string. |
| 15 testSlice(String string, int start, int end) { |
| 16 int length = string.length; |
| 17 String expect = string.substring(start, end); |
| 18 Expect.equals(expect, string.slice(start, end), "#${length}[$start:$end]"); |
| 19 if (start < length) { |
| 20 // If start == length, there is no negative representation of the position
. |
| 21 Expect.equals(expect, string.slice(start - length, end), |
| 22 "#${length}[${start - length}:$end]"); |
| 23 } |
| 24 if (end < length) { |
| 25 Expect.equals(expect, string.slice(start, end - length), |
| 26 "#${length}[$start:${end - length}]"); |
| 27 Expect.equals(expect, string.slice(start - length, end - length), |
| 28 "#${length}[${start-length}:${end-length}]"); |
| 29 } else { |
| 30 Expect.equals(expect, string.slice(start), |
| 31 "#${length}[$start]"); |
| 32 if (start < length) { |
| 33 Expect.equals(expect, string.slice(start - length), |
| 34 "#${length}[${start-length}]"); |
| 35 if (start == 0) { |
| 36 Expect.equals(string, string.slice(), "#$length[:]"); |
| 37 } |
| 38 } |
| 39 } |
| 40 } |
| 41 |
| 42 testSliceCombinations(String string) { |
| 43 int length = string.length; |
| 44 List<int> positions = [0, 1, string.length >> 1, length - 1, length]; |
| 45 for (int i = 0; i < positions.length; i++) { |
| 46 for (int j = i; j < positions.length; j++) { |
| 47 testSlice(string, positions[i], positions[j]); |
| 48 } |
| 49 } |
| 50 } |
| 51 |
| 52 testSliceCombinations(s); |
| 53 testSliceCombinations(l); |
| 54 testSliceCombinations(v); |
| 55 } |
| 56 |
| 57 testSliceError() { |
| 58 function expectRangeError(void thunk()) { |
| 59 Expect.throws(thunk, (e) => e is RangeError); |
| 60 }; |
| 61 function expectArgumentError(void thunk()) { |
| 62 Expect.throws(thunk, (e) => e is ArgumentError); |
| 63 }; |
| 64 function badType(void thunk()) { |
| 65 bool checkedMode = false; |
| 66 assert(checkedMode = true); |
| 67 Expect.throws(thunk, |
| 68 (e) => checkedMode ? e is TypeError : e is ArgumentError); |
| 69 } |
| 70 |
| 71 // Invalid start: |
| 72 expectRangeError(() => s.slice(11)); |
| 73 expectRangeError(() => s.slice(-11)); |
| 74 // Invalid end: |
| 75 expectRangeError(() => s.slice(0, 11)); |
| 76 expectRangeError(() => s.slice(0, -11)); |
| 77 // Non-int: |
| 78 badType(() => s.slice(1.5)); |
| 79 badType(() => s.slice(0, 1.5)); |
| 80 badType(() => s.slice("1")); |
| 81 badType(() => s.slice(0, "1")); |
| 82 // Bad order: |
| 83 expectArgumentError(() => s.slice(5, 4)); |
| 84 expectArgumentError(() => s.slice(-5, 4)); |
| 85 expectArgumentError(() => s.slice(5, -6)); |
| 86 expectArgumentError(() => s.slice(-5, -6)); |
| 87 } |
| 88 |
| 89 |
| 90 main() { |
| 91 testSliceSuccess(); |
| 92 testSliceError(); |
| 93 } |
OLD | NEW |