-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathfor_in_test.dart
97 lines (84 loc) · 1.96 KB
/
for_in_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class ForInTest {
static testMain() {
testSimple();
testBreak();
testContinue();
testClosure();
}
static Set<int> getSmallSet() {
Set<int> set = new Set<int>();
set.add(1);
set.add(2);
set.add(4);
return set;
}
static void testSimple() {
Set<int> set = getSmallSet();
int count = 0;
for (final i in set) {
count += i;
}
Expect.equals(7, count);
count = 0;
for (var i in set) {
count += i;
}
Expect.equals(7, count);
count = 0;
for (int i in set) {
count += i;
}
Expect.equals(7, count);
count = 0;
for (final int i in set) {
count += i;
}
Expect.equals(7, count);
count = 0;
int i = 0;
Expect.equals(false, set.contains(i)); // Used to test [i] after loop.
for (i in set) {
count += i;
}
Expect.equals(7, count);
Expect.equals(true, set.contains(i));
// The default implementation of [Set] preserves order.
Expect.equals(4, i);
}
static void testBreak() {
Set<int> set = getSmallSet();
int count = 0;
for (final i in set) {
if (i == 4) break;
count += i;
}
Expect.equals(true, count < 4);
}
static void testContinue() {
Set<int> set = getSmallSet();
int count = 0;
for (final i in set) {
if (i < 4) continue;
count += i;
}
Expect.equals(4, count);
}
static void testClosure() {
Set<int> set = getSmallSet();
List<Function> closures = [];
int index = 0;
for (var i in set) {
closures.add(() => i);
index++;
}
Expect.equals(index, set.length);
Expect.equals(7, closures[0]() + closures[1]() + closures[2]());
}
}
main() {
ForInTest.testMain();
}