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

Side by Side Diff: tests/corelib/src/SetIteratorTest.dart

Issue 10244009: test rename overhaul: step 7 - corelib tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 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 | Annotate | Revision Log
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 class FixedHashCode implements Hashable {
6 final int _hashCode;
7 const FixedHashCode(this._hashCode);
8 int hashCode() { return _hashCode; }
9 }
10
11 class SetIteratorTest {
12 static testMain() {
13 testSmallSet();
14 testLargeSet();
15 testEmptySet();
16 testSetWithDeletedEntries();
17 testBug5116829();
18 testDifferentSizes();
19 testDifferentHashCodes();
20 }
21
22 static void testThrows(Iterator<int> it) {
23 Expect.equals(false, it.hasNext());
24 var exception = null;
25 try {
26 it.next();
27 } catch (NoMoreElementsException e) {
28 exception = e;
29 }
30 Expect.equals(true, exception != null);
31 }
32
33 static int sum(int expected, Iterator<int> it) {
34 int count = 0;
35 while (it.hasNext()) {
36 count += it.next();
37 }
38 Expect.equals(expected, count);
39 }
40
41 static void testSmallSet() {
42 Set<int> set = new Set<int>();
43 set.add(1);
44 set.add(2);
45 set.add(3);
46
47 Iterator<int> it = set.iterator();
48 Expect.equals(true, it.hasNext());
49 sum(6, it);
50 testThrows(it);
51 }
52
53 static void testLargeSet() {
54 Set<int> set = new Set<int>();
55 int count = 0;
56 for (int i = 0; i < 100; i++) {
57 count += i;
58 set.add(i);
59 }
60 Iterator<int> it = set.iterator();
61 Expect.equals(true, it.hasNext());
62 sum(count, it);
63 testThrows(it);
64 }
65
66 static void testEmptySet() {
67 Set<int> set = new Set<int>();
68 Iterator<int> it = set.iterator();
69 Expect.equals(false, it.hasNext());
70 sum(0, it);
71 testThrows(it);
72 }
73
74 static void testSetWithDeletedEntries() {
75 Set<int> set = new Set<int>();
76 for (int i = 0; i < 100; i++) {
77 set.add(i);
78 }
79 for (int i = 0; i < 100; i++) {
80 set.remove(i);
81 }
82 Iterator<int> it = set.iterator();
83 Expect.equals(false, it.hasNext());
84 sum(0, it);
85 testThrows(it);
86
87 int count = 0;
88 for (int i = 0; i < 100; i++) {
89 set.add(i);
90 if (i % 2 == 0) set.remove(i);
91 else count += i;
92 }
93 it = set.iterator();
94 Expect.equals(true, it.hasNext());
95 sum(count, it);
96 testThrows(it);
97 }
98
99 static void testBug5116829() {
100 // During iteration we skipped slot 0 of the hashset's key list. "A" was
101 // hashed to slot 0 and therefore triggered the bug.
102 Set<String> mystrs = new Set<String>();
103 mystrs.add("A");
104 int seen = 0;
105 for (String elt in mystrs) {
106 seen++;
107 Expect.equals("A", elt);
108 }
109 Expect.equals(1, seen);
110 }
111
112 static void testDifferentSizes() {
113 for (int i = 1; i < 20; i++) {
114 Set set = new Set();
115 int sum = 0;
116 for (int j = 0; j < i; j++) {
117 set.add(j);
118 sum += j;
119 }
120 int count = 0;
121 int controlSum = 0;
122 for (int x in set) {
123 controlSum += x;
124 count++;
125 }
126 Expect.equals(i, count);
127 Expect.equals(sum, controlSum);
128 }
129 }
130
131 static void testDifferentHashCodes() {
132 for (int i = -20; i < 20; i++) {
133 Set set = new Set();
134 var element = new FixedHashCode(i);
135 set.add(element);
136 Expect.equals(1, set.length);
137 bool foundIt = false;
138 for (var x in set) {
139 foundIt = true;
140 Expect.equals(true, x === element);
141 }
142 Expect.equals(true, foundIt);
143 }
144 }
145 }
146
147 main() {
148 SetIteratorTest.testMain();
149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698