Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: test/utils_test.dart

Issue 1319303004: Optimize successive calls SourceFile.getLine(). (Closed) Base URL: https://github.com/dart-lang/source_span.git@master
Patch Set: Add boundary case checking back in and remove tests for old binary search. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:test/test.dart'; 5 import 'package:test/test.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', () {
10 test('empty', () {
11 expect(binarySearch([], (x) => true), -1);
12 });
13
14 test('single element', () {
15 expect(binarySearch([1], (x) => true), 0);
16 expect(binarySearch([1], (x) => false), 1);
17 });
18
19 test('no matches', () {
20 var list = [1, 2, 3, 4, 5, 6, 7];
21 expect(binarySearch(list, (x) => false), list.length);
22 });
23
24 test('all match', () {
25 var list = [1, 2, 3, 4, 5, 6, 7];
26 expect(binarySearch(list, (x) => true), 0);
27 });
28
29 test('compare with linear search', () {
30 for (int size = 0; size < 100; size++) {
31 var list = [];
32 for (int i = 0; i < size; i++) {
33 list.add(i);
34 }
35 for (int pos = 0; pos <= size; pos++) {
36 expect(binarySearch(list, (x) => x >= pos),
37 _linearSearch(list, (x) => x >= pos));
38 }
39 }
40 });
41 });
42
43 group('find line start', () { 9 group('find line start', () {
44 test('skip entries in wrong column', () { 10 test('skip entries in wrong column', () {
45 var context = '0_bb\n1_bbb\n2b____\n3bbb\n'; 11 var context = '0_bb\n1_bbb\n2b____\n3bbb\n';
46 var index = findLineStart(context, 'b', 1); 12 var index = findLineStart(context, 'b', 1);
47 expect(index, 11); 13 expect(index, 11);
48 expect(context.substring(index - 1, index + 3), '\n2b_'); 14 expect(context.substring(index - 1, index + 3), '\n2b_');
49 }); 15 });
50 16
51 test('end of line column for empty text', () { 17 test('end of line column for empty text', () {
52 var context = '0123\n56789\nabcdefgh\n'; 18 var context = '0123\n56789\nabcdefgh\n';
(...skipping 27 matching lines...) Expand all
80 }); 46 });
81 } 47 }
82 48
83 _linearSearch(list, predicate) { 49 _linearSearch(list, predicate) {
84 if (list.length == 0) return -1; 50 if (list.length == 0) return -1;
85 for (int i = 0; i < list.length; i++) { 51 for (int i = 0; i < list.length; i++) {
86 if (predicate(list[i])) return i; 52 if (predicate(list[i])) return i;
87 } 53 }
88 return list.length; 54 return list.length;
89 } 55 }
OLDNEW
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698