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 class ForInTest { | |
6 static testMain() { | |
7 testSimple(); | |
8 testBreak(); | |
9 testContinue(); | |
10 testClosure(); | |
11 } | |
12 | |
13 static Set<int> getSmallSet() { | |
14 Set<int> set = new Set<int>(); | |
15 set.add(1); | |
16 set.add(2); | |
17 set.add(4); | |
18 return set; | |
19 } | |
20 | |
21 static void testSimple() { | |
22 Set<int> set = getSmallSet(); | |
23 int count = 0; | |
24 for (final i in set) { | |
25 count += i; | |
26 } | |
27 Expect.equals(7, count); | |
28 | |
29 count = 0; | |
30 for (var i in set) { | |
31 count += i; | |
32 } | |
33 Expect.equals(7, count); | |
34 | |
35 count = 0; | |
36 for (int i in set) { | |
37 count += i; | |
38 } | |
39 Expect.equals(7, count); | |
40 | |
41 count = 0; | |
42 for (final int i in set) { | |
43 count += i; | |
44 } | |
45 Expect.equals(7, count); | |
46 | |
47 count = 0; | |
48 int i = 0; | |
49 Expect.equals(false, set.contains(i)); // Used to test [i] after loop. | |
50 for (i in set) { | |
51 count += i; | |
52 } | |
53 Expect.equals(7, count); | |
54 // TODO(ngeoffray): We should really test that [i] is 4 with a set | |
55 // that preserves order. For now, making sure [i] is in the set | |
56 // will have to do. | |
57 Expect.equals(true, set.contains(i)); | |
58 } | |
59 | |
60 static void testBreak() { | |
61 Set<int> set = getSmallSet(); | |
62 int count = 0; | |
63 for (final i in set) { | |
64 if (i == 4) break; | |
65 count += i; | |
66 } | |
67 Expect.equals(true, count < 4); | |
68 } | |
69 | |
70 static void testContinue() { | |
71 Set<int> set = getSmallSet(); | |
72 int count = 0; | |
73 for (final i in set) { | |
74 if (i < 4) continue; | |
75 count += i; | |
76 } | |
77 Expect.equals(4, count); | |
78 } | |
79 | |
80 static void testClosure() { | |
81 Set<int> set = getSmallSet(); | |
82 List<Function> closures = new List(set.length); | |
83 int index = 0; | |
84 for (var i in set) { | |
85 closures[index++] = () => i; | |
86 } | |
87 | |
88 Expect.equals(index, set.length); | |
89 Expect.equals(7, closures[0]() + closures[1]() + closures[2]()); | |
90 } | |
91 } | |
92 | |
93 main() { | |
94 ForInTest.testMain(); | |
95 } | |
OLD | NEW |