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 library map_test; | 5 library map_test; |
6 import 'dart:collection'; | 6 import 'dart:collection'; |
7 | 7 |
8 // Test that length/isEmpty opertions are constant time on | 8 // Test that length/isEmpty opertions are constant time on |
9 // maps, strings and collections. | 9 // maps, strings and collections. |
10 | 10 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 for (int i = 0; i < n; i++) { | 45 for (int i = 0; i < n; i++) { |
46 list[i] = i; | 46 list[i] = i; |
47 } | 47 } |
48 testLength(list, n); | 48 testLength(list, n); |
49 } | 49 } |
50 | 50 |
51 | 51 |
52 void testLength(var lengthable, int size) { | 52 void testLength(var lengthable, int size) { |
53 print(lengthable.runtimeType); // Show what hangs the test. | 53 print(lengthable.runtimeType); // Show what hangs the test. |
54 int length = 0; | 54 int length = 0; |
55 // If length or isEmpty is not a constant-time (or very fast) operation, | 55 // If length, isEmpty or isNotEmpty is not a constant-time (or very fast) |
56 // this will timeout. | 56 // operation, this will timeout. |
57 for (int i = 0; i < 100000; i++) { | 57 for (int i = 0; i < 100000; i++) { |
58 if (!lengthable.isEmpty) length += lengthable.length; | 58 if (!lengthable.isEmpty) length += lengthable.length; |
| 59 if (lengthable.isNotEmpty) length += lengthable.length; |
59 } | 60 } |
60 if (length != size * 100000) throw "Bad length: $length / size: $size"; | 61 if (length != size * 200000) throw "Bad length: $length / size: $size"; |
61 } | 62 } |
62 | 63 |
63 | 64 |
64 main() { | 65 main() { |
65 const int N = 100000; | 66 const int N = 100000; |
66 testMap(new HashMap(), N); | 67 testMap(new HashMap(), N); |
67 testMap(new LinkedHashMap(), N); | 68 testMap(new LinkedHashMap(), N); |
68 testMap(new SplayTreeMap(), N); | 69 testMap(new SplayTreeMap(), N); |
69 testCollection(new HashSet(), N); | 70 testCollection(new HashSet(), N); |
70 testCollection(new LinkedHashSet(), N); | 71 testCollection(new LinkedHashSet(), N); |
71 testCollection(new ListQueue(), N); | 72 testCollection(new ListQueue(), N); |
72 testCollection(new DoubleLinkedQueue(), N); | 73 testCollection(new DoubleLinkedQueue(), N); |
73 testList(new List()..length = N, N); | 74 testList(new List()..length = N, N); |
74 testList(new List(N), N); | 75 testList(new List(N), N); |
75 testString(N); | 76 testString(N); |
76 } | 77 } |
OLD | NEW |