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) { | 22 static void testThrows(Iterator<int> it) { |
23 Expect.equals(false, it.hasNext()); | 23 Expect.equals(false, it.hasNext); |
24 var exception = null; | 24 var exception = null; |
25 try { | 25 try { |
26 it.next(); | 26 it.next(); |
27 } on NoMoreElementsException catch (e) { | 27 } on NoMoreElementsException catch (e) { |
28 exception = e; | 28 exception = e; |
29 } | 29 } |
30 Expect.equals(true, exception != null); | 30 Expect.equals(true, exception != null); |
31 } | 31 } |
32 | 32 |
33 static int sum(int expected, Iterator<int> it) { | 33 static int sum(int expected, Iterator<int> it) { |
34 int count = 0; | 34 int count = 0; |
35 while (it.hasNext()) { | 35 while (it.hasNext) { |
36 count += it.next(); | 36 count += it.next(); |
37 } | 37 } |
38 Expect.equals(expected, count); | 38 Expect.equals(expected, count); |
39 } | 39 } |
40 | 40 |
41 static void testSmallSet() { | 41 static void testSmallSet() { |
42 Set<int> set = new Set<int>(); | 42 Set<int> set = new Set<int>(); |
43 set.add(1); | 43 set.add(1); |
44 set.add(2); | 44 set.add(2); |
45 set.add(3); | 45 set.add(3); |
46 | 46 |
47 Iterator<int> it = set.iterator(); | 47 Iterator<int> it = set.iterator(); |
48 Expect.equals(true, it.hasNext()); | 48 Expect.equals(true, it.hasNext); |
49 sum(6, it); | 49 sum(6, it); |
50 testThrows(it); | 50 testThrows(it); |
51 } | 51 } |
52 | 52 |
53 static void testLargeSet() { | 53 static void testLargeSet() { |
54 Set<int> set = new Set<int>(); | 54 Set<int> set = new Set<int>(); |
55 int count = 0; | 55 int count = 0; |
56 for (int i = 0; i < 100; i++) { | 56 for (int i = 0; i < 100; i++) { |
57 count += i; | 57 count += i; |
58 set.add(i); | 58 set.add(i); |
59 } | 59 } |
60 Iterator<int> it = set.iterator(); | 60 Iterator<int> it = set.iterator(); |
61 Expect.equals(true, it.hasNext()); | 61 Expect.equals(true, it.hasNext); |
62 sum(count, it); | 62 sum(count, it); |
63 testThrows(it); | 63 testThrows(it); |
64 } | 64 } |
65 | 65 |
66 static void testEmptySet() { | 66 static void testEmptySet() { |
67 Set<int> set = new Set<int>(); | 67 Set<int> set = new Set<int>(); |
68 Iterator<int> it = set.iterator(); | 68 Iterator<int> it = set.iterator(); |
69 Expect.equals(false, it.hasNext()); | 69 Expect.equals(false, it.hasNext); |
70 sum(0, it); | 70 sum(0, it); |
71 testThrows(it); | 71 testThrows(it); |
72 } | 72 } |
73 | 73 |
74 static void testSetWithDeletedEntries() { | 74 static void testSetWithDeletedEntries() { |
75 Set<int> set = new Set<int>(); | 75 Set<int> set = new Set<int>(); |
76 for (int i = 0; i < 100; i++) { | 76 for (int i = 0; i < 100; i++) { |
77 set.add(i); | 77 set.add(i); |
78 } | 78 } |
79 for (int i = 0; i < 100; i++) { | 79 for (int i = 0; i < 100; i++) { |
80 set.remove(i); | 80 set.remove(i); |
81 } | 81 } |
82 Iterator<int> it = set.iterator(); | 82 Iterator<int> it = set.iterator(); |
83 Expect.equals(false, it.hasNext()); | 83 Expect.equals(false, it.hasNext); |
84 sum(0, it); | 84 sum(0, it); |
85 testThrows(it); | 85 testThrows(it); |
86 | 86 |
87 int count = 0; | 87 int count = 0; |
88 for (int i = 0; i < 100; i++) { | 88 for (int i = 0; i < 100; i++) { |
89 set.add(i); | 89 set.add(i); |
90 if (i % 2 == 0) set.remove(i); | 90 if (i % 2 == 0) set.remove(i); |
91 else count += i; | 91 else count += i; |
92 } | 92 } |
93 it = set.iterator(); | 93 it = set.iterator(); |
94 Expect.equals(true, it.hasNext()); | 94 Expect.equals(true, it.hasNext); |
95 sum(count, it); | 95 sum(count, it); |
96 testThrows(it); | 96 testThrows(it); |
97 } | 97 } |
98 | 98 |
99 static void testBug5116829() { | 99 static void testBug5116829() { |
100 // During iteration we skipped slot 0 of the hashset's key list. "A" was | 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. | 101 // hashed to slot 0 and therefore triggered the bug. |
102 Set<String> mystrs = new Set<String>(); | 102 Set<String> mystrs = new Set<String>(); |
103 mystrs.add("A"); | 103 mystrs.add("A"); |
104 int seen = 0; | 104 int seen = 0; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 Expect.equals(true, x === element); | 140 Expect.equals(true, x === element); |
141 } | 141 } |
142 Expect.equals(true, foundIt); | 142 Expect.equals(true, foundIt); |
143 } | 143 } |
144 } | 144 } |
145 } | 145 } |
146 | 146 |
147 main() { | 147 main() { |
148 SetIteratorTest.testMain(); | 148 SetIteratorTest.testMain(); |
149 } | 149 } |
OLD | NEW |