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