| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Dart test for Splaytrees. | 5 // Dart test for Splaytrees. |
| 6 library splay_tree_test; | 6 library splay_tree_test; |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 | 10 |
| 11 class SplayTreeMapTest { | 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"; |
| 12 | 19 |
| 13 static testMain() { | 20 var correctSolution = ["first", "second", "third", "fourth", "fifth"]; |
| 14 SplayTreeMap tree = new SplayTreeMap(); | |
| 15 tree[1] = "first"; | |
| 16 tree[3] = "third"; | |
| 17 tree[5] = "fifth"; | |
| 18 tree[2] = "second"; | |
| 19 tree[4] = "fourth"; | |
| 20 | 21 |
| 21 var correctSolution = ["first", "second", "third", "fourth", "fifth"]; | 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 }); |
| 22 | 27 |
| 23 tree.forEach((key, value) { | 28 for (var v in ["first", "second", "third", "fourth", "fifth"]) { |
| 24 Expect.equals(true, key >= 1); | 29 Expect.isTrue(tree.containsValue(v)); |
| 25 Expect.equals(true, key <= 5); | 30 }; |
| 26 Expect.equals(value, correctSolution[key - 1]); | 31 Expect.isFalse(tree.containsValue("sixth")); |
| 27 }); | |
| 28 | 32 |
| 29 for (var v in ["first", "second", "third", "fourth", "fifth"]) { | 33 tree[7] = "seventh"; |
| 30 Expect.isTrue(tree.containsValue(v)); | |
| 31 }; | |
| 32 Expect.isFalse(tree.containsValue("sixth")); | |
| 33 | 34 |
| 34 tree[7] = "seventh"; | 35 Expect.equals(1, tree.firstKey()); |
| 36 Expect.equals(7, tree.lastKey()); |
| 35 | 37 |
| 36 Expect.equals(1, tree.firstKey()); | 38 Expect.equals(2, tree.lastKeyBefore(3)); |
| 37 Expect.equals(7, tree.lastKey()); | 39 Expect.equals(4, tree.firstKeyAfter(3)); |
| 38 | 40 |
| 39 Expect.equals(2, tree.lastKeyBefore(3)); | 41 Expect.equals(null, tree.lastKeyBefore(1)); |
| 40 Expect.equals(4, tree.firstKeyAfter(3)); | 42 Expect.equals(2, tree.firstKeyAfter(1)); |
| 41 | 43 |
| 42 Expect.equals(null, tree.lastKeyBefore(1)); | 44 Expect.equals(4, tree.lastKeyBefore(5)); |
| 43 Expect.equals(2, tree.firstKeyAfter(1)); | 45 Expect.equals(7, tree.firstKeyAfter(5)); |
| 44 | 46 |
| 45 Expect.equals(4, tree.lastKeyBefore(5)); | 47 Expect.equals(5, tree.lastKeyBefore(7)); |
| 46 Expect.equals(7, tree.firstKeyAfter(5)); | 48 Expect.equals(null, tree.firstKeyAfter(7)); |
| 47 | 49 |
| 48 Expect.equals(5, tree.lastKeyBefore(7)); | 50 Expect.equals(5, tree.lastKeyBefore(6)); |
| 49 Expect.equals(null, tree.firstKeyAfter(7)); | 51 Expect.equals(7, tree.firstKeyAfter(6)); |
| 50 | 52 |
| 51 Expect.equals(5, tree.lastKeyBefore(6)); | 53 regressRemoveWhere(); |
| 52 Expect.equals(7, tree.firstKeyAfter(6)); | |
| 53 } | |
| 54 } | 54 } |
| 55 | 55 |
| 56 main() { | 56 void regressRemoveWhere() { |
| 57 SplayTreeMapTest.testMain(); | 57 // Regression test. Fix in https://codereview.chromium.org/148523006/ |
| 58 var t = new SplayTreeSet(); |
| 59 t.addAll([1,3,5,7,2,4,6,8,0]); |
| 60 var seen = new List<bool>.filled(9, false); |
| 61 t.removeWhere((x) { |
| 62 // Called only once per element. |
| 63 Expect.isFalse(seen[x], "seen $x"); |
| 64 seen[x] = true; |
| 65 return x.isOdd; |
| 66 }); |
| 58 } | 67 } |
| OLD | NEW |