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

Side by Side Diff: test/codegen/corelib/splay_tree_test.dart

Issue 1945153002: Add corelib tests (Closed) Base URL: https://github.com/dart-lang/dev_compiler@master
Patch Set: error_test and range_error_test now pass Created 4 years, 7 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
OLDNEW
(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 import "package:expect/expect.dart";
8 import 'dart:collection';
9
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 Expect.isFalse(tree.containsValue("sixth"));
32
33 tree[7] = "seventh";
34
35 Expect.equals(1, tree.firstKey());
36 Expect.equals(7, tree.lastKey());
37
38 Expect.equals(2, tree.lastKeyBefore(3));
39 Expect.equals(4, tree.firstKeyAfter(3));
40
41 Expect.equals(null, tree.lastKeyBefore(1));
42 Expect.equals(2, tree.firstKeyAfter(1));
43
44 Expect.equals(4, tree.lastKeyBefore(5));
45 Expect.equals(7, tree.firstKeyAfter(5));
46
47 Expect.equals(5, tree.lastKeyBefore(7));
48 Expect.equals(null, tree.firstKeyAfter(7));
49
50 Expect.equals(5, tree.lastKeyBefore(6));
51 Expect.equals(7, tree.firstKeyAfter(6));
52
53 testSetFrom();
54 regressRemoveWhere();
55 regressRemoveWhere2();
56 regressFromCompare();
57 }
58
59 void regressRemoveWhere() {
60 // Regression test. Fix in https://codereview.chromium.org/148523006/
61 var t = new SplayTreeSet();
62 t.addAll([1, 3, 5, 7, 2, 4, 6, 8, 0]);
63 var seen = new List<bool>.filled(9, false);
64 t.removeWhere((x) {
65 // Called only once per element.
66 Expect.isFalse(seen[x], "seen $x");
67 seen[x] = true;
68 return x.isOdd;
69 });
70 }
71
72 void regressRemoveWhere2() {
73 // Regression test for http://dartbug.com/18676
74 // Removing all elements with removeWhere causes error.
75
76 var t = new SplayTreeSet();
77 t.addAll([1, 2, 3, 4, 5]);
78 t.removeWhere((_) => true); // Should not throw.
79 Expect.isTrue(t.isEmpty);
80 t.addAll([1, 2, 3, 4, 5]);
81 t.retainWhere((_) => false); // Should not throw.
82 Expect.isTrue(t.isEmpty);
83 }
84
85 void testSetFrom() {
86 var set1 = new SplayTreeSet<num>()..addAll([1, 2, 3, 4, 5]);
87 var set2 = new SplayTreeSet<int>.from(set1);
88 Expect.equals(5, set2.length);
89 for (int i = 1; i <= 5; i++) {
90 Expect.isTrue(set2.contains(i));
91 }
92
93 set1 = new SplayTreeSet<num>()..addAll([0, 1, 2.4, 3.14, 5]);
94 set2 = new SplayTreeSet<int>.from(set1.where((x) => x is int));
95 Expect.equals(3, set2.length);
96 }
97
98
99
100 void regressFromCompare() {
101 // Regression test for http://dartbug.com/23387.
102 // The compare and isValidKey arguments to SplayTreeMap.from were ignored.
103
104 int compare(a, b) {
105 if (a is IncomparableKey && b is IncomparableKey) {
106 return b.id - a.id;
107 }
108 throw "isValidKey failure";
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 = new SplayTreeMap<IncomparableKey, int>.from(
116 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(() { map[1] = 42; });
129 Expect.throws(() { map["string"] = 42; });
130 map[key(5)] = 42;
131 Expect.equals(4, map.length);
132 Expect.equals(42, map[key(5)]);
133 }
134
135 class IncomparableKey {
136 final int id;
137 IncomparableKey(this.id);
138 }
OLDNEW
« no previous file with comments | « test/codegen/corelib/splay_tree_from_iterables_test.dart ('k') | test/codegen/corelib/stacktrace_current_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698