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 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) { | |
23 Expect.equals(false, it.hasNext); | |
24 var exception = null; | |
25 try { | |
26 it.next(); | |
27 } on StateError catch (e) { | |
28 exception = e; | |
29 } | |
30 Expect.equals(true, exception != null); | |
31 } | |
32 | |
33 static int sum(int expected, Iterator<int> it) { | 22 static int sum(int expected, Iterator<int> it) { |
34 int count = 0; | 23 int count = 0; |
35 while (it.hasNext) { | 24 while (it.moveNext()) { |
36 count += it.next(); | 25 count += it.current; |
37 } | 26 } |
38 Expect.equals(expected, count); | 27 Expect.equals(expected, count); |
39 } | 28 } |
40 | 29 |
41 static void testSmallSet() { | 30 static void testSmallSet() { |
42 Set<int> set = new Set<int>(); | 31 Set<int> set = new Set<int>(); |
43 set.add(1); | 32 set.add(1); |
44 set.add(2); | 33 set.add(2); |
45 set.add(3); | 34 set.add(3); |
46 | 35 |
47 Iterator<int> it = set.iterator(); | 36 Iterator<int> it = set.iterator; |
48 Expect.equals(true, it.hasNext); | |
49 sum(6, it); | 37 sum(6, it); |
50 testThrows(it); | 38 Expect.isFalse(it.moveNext()); |
| 39 Expect.isNull(it.current); |
51 } | 40 } |
52 | 41 |
53 static void testLargeSet() { | 42 static void testLargeSet() { |
54 Set<int> set = new Set<int>(); | 43 Set<int> set = new Set<int>(); |
55 int count = 0; | 44 int count = 0; |
56 for (int i = 0; i < 100; i++) { | 45 for (int i = 0; i < 100; i++) { |
57 count += i; | 46 count += i; |
58 set.add(i); | 47 set.add(i); |
59 } | 48 } |
60 Iterator<int> it = set.iterator(); | 49 Iterator<int> it = set.iterator; |
61 Expect.equals(true, it.hasNext); | |
62 sum(count, it); | 50 sum(count, it); |
63 testThrows(it); | 51 Expect.isFalse(it.moveNext()); |
| 52 Expect.isNull(it.current); |
64 } | 53 } |
65 | 54 |
66 static void testEmptySet() { | 55 static void testEmptySet() { |
67 Set<int> set = new Set<int>(); | 56 Set<int> set = new Set<int>(); |
68 Iterator<int> it = set.iterator(); | 57 Iterator<int> it = set.iterator; |
69 Expect.equals(false, it.hasNext); | |
70 sum(0, it); | 58 sum(0, it); |
71 testThrows(it); | 59 Expect.isFalse(it.moveNext()); |
| 60 Expect.isNull(it.current); |
72 } | 61 } |
73 | 62 |
74 static void testSetWithDeletedEntries() { | 63 static void testSetWithDeletedEntries() { |
75 Set<int> set = new Set<int>(); | 64 Set<int> set = new Set<int>(); |
76 for (int i = 0; i < 100; i++) { | 65 for (int i = 0; i < 100; i++) { |
77 set.add(i); | 66 set.add(i); |
78 } | 67 } |
79 for (int i = 0; i < 100; i++) { | 68 for (int i = 0; i < 100; i++) { |
80 set.remove(i); | 69 set.remove(i); |
81 } | 70 } |
82 Iterator<int> it = set.iterator(); | 71 Iterator<int> it = set.iterator; |
83 Expect.equals(false, it.hasNext); | 72 Expect.isFalse(it.moveNext()); |
| 73 it = set.iterator; |
84 sum(0, it); | 74 sum(0, it); |
85 testThrows(it); | 75 Expect.isFalse(it.moveNext()); |
| 76 Expect.isNull(it.current); |
86 | 77 |
87 int count = 0; | 78 int count = 0; |
88 for (int i = 0; i < 100; i++) { | 79 for (int i = 0; i < 100; i++) { |
89 set.add(i); | 80 set.add(i); |
90 if (i % 2 == 0) set.remove(i); | 81 if (i % 2 == 0) set.remove(i); |
91 else count += i; | 82 else count += i; |
92 } | 83 } |
93 it = set.iterator(); | 84 it = set.iterator; |
94 Expect.equals(true, it.hasNext); | |
95 sum(count, it); | 85 sum(count, it); |
96 testThrows(it); | 86 Expect.isFalse(it.moveNext()); |
| 87 Expect.isNull(it.current); |
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; |
105 for (String elt in mystrs) { | 96 for (String elt in mystrs) { |
106 seen++; | 97 seen++; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 } |
OLD | NEW |