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

Side by Side Diff: tests/corelib/set_iterator_test.dart

Issue 11410086: Use iterator, moveNext(), current. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Address comments. Created 8 years, 1 month 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
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 class FixedHashCode { 5 class FixedHashCode {
6 final int _hashCode; 6 final int _hashCode;
7 const FixedHashCode(this._hashCode); 7 const FixedHashCode(this._hashCode);
8 int get hashCode { return _hashCode; } 8 int get hashCode { return _hashCode; }
9 } 9 }
10 10
11 class SetIteratorTest { 11 class SetIteratorTest {
12 static testMain() { 12 static testMain() {
13 testSmallSet(); 13 testSmallSet();
14 testLargeSet(); 14 testLargeSet();
15 testEmptySet(); 15 testEmptySet();
16 testSetWithDeletedEntries(); 16 testSetWithDeletedEntries();
17 testBug5116829(); 17 testBug5116829();
18 testDifferentSizes(); 18 testDifferentSizes();
19 testDifferentHashCodes(); 19 testDifferentHashCodes();
20 } 20 }
21 21
22 static void testThrows(Iterator<int> it) { 22 static void testThrows(Iterator<int> it) {
23 Expect.equals(false, it.hasNext); 23 Expect.isFalse(it.moveNext());
24 var exception = null; 24 Expect.throws(() { it.current; }, (e) => e is StateError);
25 try {
26 it.next();
27 } on StateError catch (e) {
28 exception = e;
29 }
30 Expect.equals(true, exception != null);
31 } 25 }
32 26
33 static int sum(int expected, Iterator<int> it) { 27 static int sum(int expected, Iterator<int> it) {
34 int count = 0; 28 int count = 0;
35 while (it.hasNext) { 29 while (it.moveNext()) {
36 count += it.next(); 30 count += it.current;
37 } 31 }
38 Expect.equals(expected, count); 32 Expect.equals(expected, count);
39 } 33 }
40 34
41 static void testSmallSet() { 35 static void testSmallSet() {
42 Set<int> set = new Set<int>(); 36 Set<int> set = new Set<int>();
43 set.add(1); 37 set.add(1);
44 set.add(2); 38 set.add(2);
45 set.add(3); 39 set.add(3);
46 40
47 Iterator<int> it = set.iterator(); 41 Iterator<int> it = set.iterator;
48 Expect.equals(true, it.hasNext);
49 sum(6, it); 42 sum(6, it);
50 testThrows(it); 43 testThrows(it);
51 } 44 }
52 45
53 static void testLargeSet() { 46 static void testLargeSet() {
54 Set<int> set = new Set<int>(); 47 Set<int> set = new Set<int>();
55 int count = 0; 48 int count = 0;
56 for (int i = 0; i < 100; i++) { 49 for (int i = 0; i < 100; i++) {
57 count += i; 50 count += i;
58 set.add(i); 51 set.add(i);
59 } 52 }
60 Iterator<int> it = set.iterator(); 53 Iterator<int> it = set.iterator;
61 Expect.equals(true, it.hasNext);
62 sum(count, it); 54 sum(count, it);
63 testThrows(it); 55 testThrows(it);
64 } 56 }
65 57
66 static void testEmptySet() { 58 static void testEmptySet() {
67 Set<int> set = new Set<int>(); 59 Set<int> set = new Set<int>();
68 Iterator<int> it = set.iterator(); 60 Iterator<int> it = set.iterator;
69 Expect.equals(false, it.hasNext);
70 sum(0, it); 61 sum(0, it);
71 testThrows(it); 62 testThrows(it);
72 } 63 }
73 64
74 static void testSetWithDeletedEntries() { 65 static void testSetWithDeletedEntries() {
75 Set<int> set = new Set<int>(); 66 Set<int> set = new Set<int>();
76 for (int i = 0; i < 100; i++) { 67 for (int i = 0; i < 100; i++) {
77 set.add(i); 68 set.add(i);
78 } 69 }
79 for (int i = 0; i < 100; i++) { 70 for (int i = 0; i < 100; i++) {
80 set.remove(i); 71 set.remove(i);
81 } 72 }
82 Iterator<int> it = set.iterator(); 73 Iterator<int> it = set.iterator;
83 Expect.equals(false, it.hasNext); 74 Expect.isFalse(it.moveNext());
75 it = set.iterator;
84 sum(0, it); 76 sum(0, it);
85 testThrows(it); 77 testThrows(it);
86 78
87 int count = 0; 79 int count = 0;
88 for (int i = 0; i < 100; i++) { 80 for (int i = 0; i < 100; i++) {
89 set.add(i); 81 set.add(i);
90 if (i % 2 == 0) set.remove(i); 82 if (i % 2 == 0) set.remove(i);
91 else count += i; 83 else count += i;
92 } 84 }
93 it = set.iterator(); 85 it = set.iterator;
94 Expect.equals(true, it.hasNext);
95 sum(count, it); 86 sum(count, it);
96 testThrows(it); 87 testThrows(it);
97 } 88 }
98 89
99 static void testBug5116829() { 90 static void testBug5116829() {
100 // During iteration we skipped slot 0 of the hashset's key list. "A" was 91 // During iteration we skipped slot 0 of the hashset's key list. "A" was
101 // hashed to slot 0 and therefore triggered the bug. 92 // hashed to slot 0 and therefore triggered the bug.
102 Set<String> mystrs = new Set<String>(); 93 Set<String> mystrs = new Set<String>();
103 mystrs.add("A"); 94 mystrs.add("A");
104 int seen = 0; 95 int seen = 0;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 Expect.equals(true, identical(x, element)); 131 Expect.equals(true, identical(x, element));
141 } 132 }
142 Expect.equals(true, foundIt); 133 Expect.equals(true, foundIt);
143 } 134 }
144 } 135 }
145 } 136 }
146 137
147 main() { 138 main() {
148 SetIteratorTest.testMain(); 139 SetIteratorTest.testMain();
149 } 140 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698