OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'package:unittest/unittest.dart'; | 5 import 'package:unittest/unittest.dart'; |
6 import 'package:source_span/src/utils.dart'; | 6 import 'package:source_span/src/utils.dart'; |
7 | 7 |
8 main() { | 8 main() { |
9 group('binary search', () { | 9 group('binary search', () { |
10 test('empty', () { | 10 test('empty', () { |
(...skipping 21 matching lines...) Expand all Loading... |
32 for (int i = 0; i < size; i++) { | 32 for (int i = 0; i < size; i++) { |
33 list.add(i); | 33 list.add(i); |
34 } | 34 } |
35 for (int pos = 0; pos <= size; pos++) { | 35 for (int pos = 0; pos <= size; pos++) { |
36 expect(binarySearch(list, (x) => x >= pos), | 36 expect(binarySearch(list, (x) => x >= pos), |
37 _linearSearch(list, (x) => x >= pos)); | 37 _linearSearch(list, (x) => x >= pos)); |
38 } | 38 } |
39 } | 39 } |
40 }); | 40 }); |
41 }); | 41 }); |
| 42 |
| 43 group('find line start', () { |
| 44 test('skip entries in wrong column', () { |
| 45 var context = '0_bb\n1_bbb\n2b____\n3bbb\n'; |
| 46 var index = findLineStart(context, 'b', 1); |
| 47 expect(index, 11); |
| 48 expect(context.substring(index - 1, index + 3), '\n2b_'); |
| 49 }); |
| 50 |
| 51 test('end of line column for empty text', () { |
| 52 var context = '0123\n56789\nabcdefgh\n'; |
| 53 var index = findLineStart(context, '', 5); |
| 54 expect(index, 5); |
| 55 expect(context[index], '5'); |
| 56 }); |
| 57 |
| 58 test('column at the end of the file for empty text', () { |
| 59 var context = '0\n2\n45\n'; |
| 60 var index = findLineStart(context, '', 2); |
| 61 expect(index, 4); |
| 62 expect(context[index], '4'); |
| 63 |
| 64 context = '0\n2\n45'; |
| 65 index = findLineStart(context, '', 2); |
| 66 expect(index, 4); |
| 67 }); |
| 68 |
| 69 test('found on the first line', () { |
| 70 var context = '0\n2\n45\n'; |
| 71 var index = findLineStart(context, '0', 0); |
| 72 expect(index, 0); |
| 73 }); |
| 74 |
| 75 test('not found', () { |
| 76 var context = '0\n2\n45\n'; |
| 77 var index = findLineStart(context, '0', 1); |
| 78 expect(index, isNull); |
| 79 }); |
| 80 }); |
42 } | 81 } |
43 | 82 |
44 _linearSearch(list, predicate) { | 83 _linearSearch(list, predicate) { |
45 if (list.length == 0) return -1; | 84 if (list.length == 0) return -1; |
46 for (int i = 0; i < list.length; i++) { | 85 for (int i = 0; i < list.length; i++) { |
47 if (predicate(list[i])) return i; | 86 if (predicate(list[i])) return i; |
48 } | 87 } |
49 return list.length; | 88 return list.length; |
50 } | 89 } |
OLD | NEW |