OLD | NEW |
| (Empty) |
1 // Copyright (c) 2016, 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 'expect.dart'; | |
6 | |
7 mkOne() => 1; | |
8 mkTwo() => 2; | |
9 | |
10 testNormal() { | |
11 int result; | |
12 switch(mkOne()) { | |
13 case 0: | |
14 result = 0; | |
15 break; | |
16 case 1: | |
17 result = 1; | |
18 break; | |
19 case 2: | |
20 result = 2; | |
21 break; | |
22 default: | |
23 result = 3; | |
24 break; | |
25 } | |
26 Expect.isTrue(result == 1); | |
27 } | |
28 | |
29 testDefault() { | |
30 int result; | |
31 switch(null) { | |
32 case 0: | |
33 result = 0; | |
34 break; | |
35 case 1: | |
36 result = 1; | |
37 break; | |
38 case 2: | |
39 result = 2; | |
40 break; | |
41 default: | |
42 result = 3; | |
43 break; | |
44 } | |
45 Expect.isTrue(result == 3); | |
46 } | |
47 | |
48 testFallThrough() { | |
49 int result; | |
50 switch(mkOne()) { | |
51 case 0: | |
52 result = 0; | |
53 break; | |
54 case 1: | |
55 case 2: | |
56 result = 2; | |
57 break; | |
58 default: | |
59 result = 3; | |
60 break; | |
61 } | |
62 Expect.isTrue(result == 2); | |
63 } | |
64 | |
65 testContinue() { | |
66 int result; | |
67 switch(mkTwo()) { | |
68 case 0: | |
69 result = 0; | |
70 break; | |
71 | |
72 setitto1: | |
73 case 1: | |
74 result = 1; | |
75 continue setitto3; | |
76 | |
77 case 2: | |
78 result = 2; | |
79 continue setitto1; | |
80 | |
81 setitto3: | |
82 default: | |
83 result = 3; | |
84 break; | |
85 } | |
86 Expect.isTrue(result == 3); | |
87 } | |
88 | |
89 testOnlyDefault() { | |
90 int result; | |
91 switch(mkTwo()) { | |
92 default: | |
93 result = 42; | |
94 } | |
95 Expect.isTrue(result == 42); | |
96 } | |
97 | |
98 testOnlyDefaultWithBreak() { | |
99 int result; | |
100 switch(mkTwo()) { | |
101 default: | |
102 result = 42; | |
103 } | |
104 Expect.isTrue(result == 42); | |
105 } | |
106 | |
107 String testReturn() { | |
108 switch(mkOne()) { | |
109 case 0: return "bad"; | |
110 case 1: return "good"; | |
111 default: return "bad"; | |
112 } | |
113 } | |
114 | |
115 regressionTest() { | |
116 Expect.isTrue(regressionTestHelper(0, 0) == -1); | |
117 Expect.isTrue(regressionTestHelper(4, 0) == -1); | |
118 Expect.isTrue(regressionTestHelper(4, 1) == 42); | |
119 } | |
120 | |
121 regressionTestHelper(i, j) { | |
122 switch (i) { | |
123 case 4: | |
124 switch (j) { | |
125 case 1: | |
126 return 42; | |
127 } | |
128 } | |
129 return -1; | |
130 } | |
131 | |
132 regressionTest2() { | |
133 var state = 1; | |
134 while (state < 2) { | |
135 switch (state) { | |
136 case 1: | |
137 state++; | |
138 break; | |
139 case 3: | |
140 case 4: | |
141 // We will translate this currently to an empty [Fragment] which can | |
142 // cause issues if we would like to append/prepend to it. | |
143 assert(false); | |
144 } | |
145 } | |
146 return [1]; | |
147 } | |
148 | |
149 regressionTest3() { | |
150 f(x) { | |
151 switch (x) { | |
152 case 1: | |
153 return 2; | |
154 case 2: | |
155 } | |
156 throw new UnsupportedError("Unexpected constant kind."); | |
157 } | |
158 Expect.isTrue(f(1) == 2); | |
159 } | |
160 | |
161 main() { | |
162 testNormal(); | |
163 testDefault(); | |
164 testFallThrough(); | |
165 testContinue(); | |
166 testOnlyDefault(); | |
167 testOnlyDefaultWithBreak(); | |
168 regressionTest(); | |
169 regressionTest2(); | |
170 regressionTest3(); | |
171 | |
172 var result = testReturn(); | |
173 Expect.isTrue(result == "good"); | |
174 } | |
OLD | NEW |