Skip to content

Commit e96f6ca

Browse files
natebiggsCommit Queue
authored andcommitted
[dart2wasm] Add tests for switch case br_table handling.
Catches bug fixed by https://dart-review.googlesource.com/c/sdk/+/442300 Change-Id: I7c3f5439809e6fd3e3403b620ef1fef1db56dbe3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/442320 Reviewed-by: Nate Bosch <[email protected]> Commit-Queue: Nate Biggs <[email protected]>
1 parent c4cc91e commit e96f6ca

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
// Copyright (c) 2025, 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+
// Ensure that switch statements with various types compare case expressions
6+
// to the switch expression value correctly.
7+
8+
import 'package:async_helper/async_helper.dart';
9+
import 'package:expect/expect.dart';
10+
11+
enum Animal { cat, dog, mouse, lion, duck }
12+
13+
enum LongEnum {
14+
a,
15+
b,
16+
c,
17+
d,
18+
e,
19+
f,
20+
g,
21+
h,
22+
i,
23+
j,
24+
k,
25+
l,
26+
m,
27+
n,
28+
o,
29+
p,
30+
q,
31+
r,
32+
s,
33+
t,
34+
u,
35+
v,
36+
w,
37+
x,
38+
y,
39+
z,
40+
aa,
41+
ab,
42+
ac,
43+
ad,
44+
ae,
45+
af,
46+
ag,
47+
ah,
48+
ai,
49+
aj,
50+
ak,
51+
al,
52+
am,
53+
an,
54+
ao,
55+
ap,
56+
aq,
57+
ar,
58+
as,
59+
at,
60+
au,
61+
av,
62+
aw,
63+
ax,
64+
ay,
65+
az,
66+
}
67+
68+
const List<String> strings = ['cat', 'dog', 'mouse', 'lion', 'duck'];
69+
const List<int> ints = [1, 2, 3, 4, 5];
70+
const List<bool> bools = [true, false];
71+
72+
void switches() {
73+
for (final animal in Animal.values) {
74+
switch (animal) {
75+
case Animal.cat:
76+
Expect.equals(animal, Animal.cat);
77+
case Animal.dog:
78+
Expect.equals(animal, Animal.dog);
79+
case Animal.mouse:
80+
Expect.equals(animal, Animal.mouse);
81+
case Animal.lion:
82+
Expect.equals(animal, Animal.lion);
83+
case Animal.duck:
84+
Expect.equals(animal, Animal.duck);
85+
}
86+
}
87+
88+
final longEnum = LongEnum.ad;
89+
switch (longEnum) {
90+
case LongEnum.a:
91+
Expect.equals(LongEnum.a, LongEnum.a);
92+
case LongEnum.b:
93+
Expect.equals(LongEnum.b, LongEnum.b);
94+
default:
95+
Expect.equals(longEnum, LongEnum.ad);
96+
}
97+
98+
for (final string in strings) {
99+
switch (string) {
100+
case 'cat':
101+
Expect.equals(string, 'cat');
102+
case 'dog':
103+
Expect.equals(string, 'dog');
104+
case 'mouse':
105+
Expect.equals(string, 'mouse');
106+
case 'lion':
107+
Expect.equals(string, 'lion');
108+
case 'duck':
109+
Expect.equals(string, 'duck');
110+
default:
111+
Expect.fail('Unknown string');
112+
}
113+
}
114+
115+
for (final i in ints) {
116+
switch (i) {
117+
case 1:
118+
Expect.equals(i, 1);
119+
case 2:
120+
Expect.equals(i, 2);
121+
case 3:
122+
Expect.equals(i, 3);
123+
case 4:
124+
Expect.equals(i, 4);
125+
case 5:
126+
Expect.equals(i, 5);
127+
default:
128+
Expect.fail('Unknown int');
129+
}
130+
}
131+
132+
for (final b in bools) {
133+
switch (b) {
134+
case true:
135+
Expect.isTrue(b);
136+
case false:
137+
Expect.isFalse(b);
138+
}
139+
}
140+
}
141+
142+
Future<void> asyncSwitches() async {
143+
for (final animal in Animal.values) {
144+
switch (animal) {
145+
case Animal.cat:
146+
await 'something';
147+
Expect.equals(animal, Animal.cat);
148+
case Animal.dog:
149+
Expect.equals(animal, Animal.dog);
150+
case Animal.mouse:
151+
Expect.equals(animal, Animal.mouse);
152+
case Animal.lion:
153+
Expect.equals(animal, Animal.lion);
154+
case Animal.duck:
155+
Expect.equals(animal, Animal.duck);
156+
}
157+
}
158+
159+
final longEnum = LongEnum.ad;
160+
switch (longEnum) {
161+
case LongEnum.a:
162+
await 'something';
163+
Expect.equals(LongEnum.a, LongEnum.a);
164+
case LongEnum.b:
165+
Expect.equals(LongEnum.b, LongEnum.b);
166+
default:
167+
Expect.equals(longEnum, LongEnum.ad);
168+
}
169+
170+
for (final string in strings) {
171+
switch (string) {
172+
case 'cat':
173+
await 'something';
174+
Expect.equals(string, 'cat');
175+
case 'dog':
176+
Expect.equals(string, 'dog');
177+
case 'mouse':
178+
Expect.equals(string, 'mouse');
179+
case 'lion':
180+
Expect.equals(string, 'lion');
181+
case 'duck':
182+
Expect.equals(string, 'duck');
183+
default:
184+
Expect.fail('Unknown string');
185+
}
186+
}
187+
188+
for (final i in ints) {
189+
switch (i) {
190+
case 1:
191+
await 'something';
192+
Expect.equals(i, 1);
193+
case 2:
194+
Expect.equals(i, 2);
195+
case 3:
196+
Expect.equals(i, 3);
197+
case 4:
198+
Expect.equals(i, 4);
199+
case 5:
200+
Expect.equals(i, 5);
201+
default:
202+
Expect.fail('Unknown int');
203+
}
204+
}
205+
206+
for (final b in bools) {
207+
switch (b) {
208+
case true:
209+
await 'something';
210+
Expect.isTrue(b);
211+
case false:
212+
Expect.isFalse(b);
213+
}
214+
}
215+
}
216+
217+
Future<void> main() async {
218+
asyncStart();
219+
220+
switches();
221+
await asyncSwitches();
222+
223+
asyncEnd();
224+
}

0 commit comments

Comments
 (0)