OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 // Dart test for Splaytrees. | |
6 library splay_tree_test; | |
7 | |
8 import "package:expect/expect.dart"; | |
9 import 'dart:collection'; | |
10 | |
11 main() { | |
12 // Simple tests. | |
13 SplayTreeMap tree = new SplayTreeMap(); | |
14 tree[1] = "first"; | |
15 tree[3] = "third"; | |
16 tree[5] = "fifth"; | |
17 tree[2] = "second"; | |
18 tree[4] = "fourth"; | |
19 | |
20 var correctSolution = ["first", "second", "third", "fourth", "fifth"]; | |
21 | |
22 tree.forEach((key, value) { | |
23 Expect.equals(true, key >= 1); | |
24 Expect.equals(true, key <= 5); | |
25 Expect.equals(value, correctSolution[key - 1]); | |
26 }); | |
27 | |
28 for (var v in ["first", "second", "third", "fourth", "fifth"]) { | |
29 Expect.isTrue(tree.containsValue(v)); | |
30 } | |
31 ; | |
32 Expect.isFalse(tree.containsValue("sixth")); | |
33 | |
34 tree[7] = "seventh"; | |
35 | |
36 Expect.equals(1, tree.firstKey()); | |
37 Expect.equals(7, tree.lastKey()); | |
38 | |
39 Expect.equals(2, tree.lastKeyBefore(3)); | |
40 Expect.equals(4, tree.firstKeyAfter(3)); | |
41 | |
42 Expect.equals(null, tree.lastKeyBefore(1)); | |
43 Expect.equals(2, tree.firstKeyAfter(1)); | |
44 | |
45 Expect.equals(4, tree.lastKeyBefore(5)); | |
46 Expect.equals(7, tree.firstKeyAfter(5)); | |
47 | |
48 Expect.equals(5, tree.lastKeyBefore(7)); | |
49 Expect.equals(null, tree.firstKeyAfter(7)); | |
50 | |
51 Expect.equals(5, tree.lastKeyBefore(6)); | |
52 Expect.equals(7, tree.firstKeyAfter(6)); | |
53 | |
54 testSetFrom(); | |
55 regressRemoveWhere(); | |
56 regressRemoveWhere2(); | |
57 regressFromCompare(); | |
58 } | |
59 | |
60 void regressRemoveWhere() { | |
61 // Regression test. Fix in https://codereview.chromium.org/148523006/ | |
62 var t = new SplayTreeSet(); | |
63 t.addAll([1, 3, 5, 7, 2, 4, 6, 8, 0]); | |
64 var seen = new List<bool>.filled(9, false); | |
65 t.removeWhere((x) { | |
66 // Called only once per element. | |
67 Expect.isFalse(seen[x], "seen $x"); | |
68 seen[x] = true; | |
69 return x.isOdd; | |
70 }); | |
71 } | |
72 | |
73 void regressRemoveWhere2() { | |
74 // Regression test for http://dartbug.com/18676 | |
75 // Removing all elements with removeWhere causes error. | |
76 | |
77 var t = new SplayTreeSet(); | |
78 t.addAll([1, 2, 3, 4, 5]); | |
79 t.removeWhere((_) => true); // Should not throw. | |
80 Expect.isTrue(t.isEmpty); | |
81 t.addAll([1, 2, 3, 4, 5]); | |
82 t.retainWhere((_) => false); // Should not throw. | |
83 Expect.isTrue(t.isEmpty); | |
84 } | |
85 | |
86 void testSetFrom() { | |
87 var set1 = new SplayTreeSet<num>()..addAll([1, 2, 3, 4, 5]); | |
88 var set2 = new SplayTreeSet<int>.from(set1); | |
89 Expect.equals(5, set2.length); | |
90 for (int i = 1; i <= 5; i++) { | |
91 Expect.isTrue(set2.contains(i)); | |
92 } | |
93 | |
94 set1 = new SplayTreeSet<num>()..addAll([0, 1, 2.4, 3.14, 5]); | |
95 set2 = new SplayTreeSet<int>.from(set1.where((x) => x is int)); | |
96 Expect.equals(3, set2.length); | |
97 } | |
98 | |
99 void regressFromCompare() { | |
100 // Regression test for http://dartbug.com/23387. | |
101 // The compare and isValidKey arguments to SplayTreeMap.from were ignored. | |
102 | |
103 int compare(a, b) { | |
104 if (a is IncomparableKey && b is IncomparableKey) { | |
105 return b.id - a.id; | |
106 } | |
107 throw "isValidKey failure"; | |
108 } | |
109 | |
110 bool isValidKey(o) => o is IncomparableKey; | |
111 IncomparableKey key(int n) => new IncomparableKey(n); | |
112 | |
113 var entries = {key(0): 0, key(1): 1, key(2): 2, key(0): 0}; | |
114 Expect.equals(4, entries.length); | |
115 var map = | |
116 new SplayTreeMap<IncomparableKey, int>.from(entries, compare, isValidKey); | |
117 Expect.equals(3, map.length); | |
118 for (int i = 0; i < 3; i++) { | |
119 Expect.isTrue(map.containsKey(key(i))); | |
120 Expect.equals(i, map[key(i)]); | |
121 } | |
122 Expect.isFalse(map.containsKey(key(5))); | |
123 Expect.isFalse(map.containsKey(1)); | |
124 Expect.isFalse(map.containsKey("string")); | |
125 Expect.equals(null, map[key(5)]); | |
126 Expect.equals(null, map[1]); | |
127 Expect.equals(null, map["string"]); | |
128 Expect.throws(() { | |
129 map[1] = 42; | |
130 }); | |
131 Expect.throws(() { | |
132 map["string"] = 42; | |
133 }); | |
134 map[key(5)] = 42; | |
135 Expect.equals(4, map.length); | |
136 Expect.equals(42, map[key(5)]); | |
137 } | |
138 | |
139 class IncomparableKey { | |
140 final int id; | |
141 IncomparableKey(this.id); | |
142 } | |
OLD | NEW |