Skip to content

Commit 49b2e62

Browse files
committed
tests: added enum-array tests
1 parent 0e24be7 commit 49b2e62

File tree

6 files changed

+134
-22
lines changed

6 files changed

+134
-22
lines changed

ast/array_type.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ fn void ArrayType.printPostName(const ArrayType* t, string_buffer.Buf* out) {
113113
} else {
114114
out.print("%d", t.size);
115115
}
116+
//if (t.isEnumIndex()) out.add(" e");
116117
out.add1(']');
117118
}
118119

119120
fn void ArrayType.print(const ArrayType* t, string_buffer.Buf* out) {
120-
// TODO print preName, postName
121121
ArrayType* at = t.elem.getArrayTypeOrNil();
122122
if (at) at.printPreName(out);
123123
else t.elem.printInner(out, false, true, true);

test/expr/array/array_index.c2

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @warnings{no-unused}
2+
module test;
3+
4+
type Enum enum u8 { A, B, C }
5+
6+
type Struct struct {
7+
char[Enum] c;
8+
}
9+
10+
fn void test1() {
11+
Struct s;
12+
s.c[(Enum)10] = 20; // @error{array out-of-bounds access [10] in array of [3]}
13+
}
14+

test/expr/array/enum_array.c2

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @warnings{no-unused}
12
module test;
23

34
import stdio local;
@@ -12,27 +13,34 @@ u32[Enum] a = {
1213

1314
static_assert(elemsof(a), elemsof(Enum));
1415

15-
public fn i32 main() {
16-
printf("%d\n", a[No]);
17-
printf("%d\n", a[Yes]);
18-
printf("%d\n", a[Maybe]);
19-
a[No] = 3;
20-
a[Yes] = 4;
21-
a[Maybe] = 5;
22-
printf("%d\n", a[No]);
23-
printf("%d\n", a[Yes]);
24-
printf("%d\n", a[Maybe]);
25-
printf("%d\n", a[0 ? Enum.Yes : Enum.No]);
26-
Enum e = Yes;
27-
printf("%d\n", a[e]);
28-
printf("%d\n", a[Enum.max]);
29-
i32 i = 1;
30-
i32[] aa = {
31-
printf("%d\n", a[No + 1]), // @error{use of undeclared identifier 'No'}
32-
printf("%d\n", a[1 ? Yes : // @error{use of undeclared identifier 'Yes'}
33-
No]), // @error{use of undeclared identifier 'No'}
34-
printf("%d\n", a[1]), // @error{array subscript must have enum type 'test.Enum'}
35-
printf("%d\n", a[i]), // @error{array subscript must have enum type 'test.Enum'}
16+
type Foo enum u8 { A, B, C }
17+
18+
fn Enum getYes() { return Yes; }
19+
20+
const Enum[2] Other = { Maybe, Yes }
21+
22+
public fn i32 main(i32 argc, const char** argv) {
23+
Enum e = Maybe;
24+
25+
u32[] aa = {
26+
a[No],
27+
a[Yes],
28+
a[Enum.Yes],
29+
a[argc ? Enum.Yes : Enum.No],
30+
a[Other[1]],
31+
a[getYes()],
32+
a[(Enum)argc],
33+
a[e],
34+
a[Enum.max],
35+
36+
a[Foo.B], // @error{array subscript must have enum type 'test.Enum'}
37+
a[argc], // @error{array subscript must have enum type 'test.Enum'}
38+
a[No + 1], // @error{use of undeclared identifier 'No'}
39+
a[1 ? Yes : // @error{use of undeclared identifier 'Yes'}
40+
No], // @error{use of undeclared identifier 'No'}
41+
a[1], // @error{array subscript must have enum type 'test.Enum'}
42+
a[argc], // @error{array subscript must have enum type 'test.Enum'}
3643
}
3744
return 0;
3845
}
46+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// @warnings{no-unused}
2+
module test;
3+
4+
type Enum enum u8 { No, Yes, Maybe }
5+
6+
u32[Enum] a = {
7+
[No]= 0,
8+
[Yes]= 1,
9+
[Maybe]= 2,
10+
}
11+
12+
type Foo enum u8 { A, B, C }
13+
14+
fn Enum getYes() { return Yes; }
15+
const Enum[2] Other = { Maybe, Yes }
16+
17+
type Bar u32[Enum];
18+
Bar b;
19+
20+
public fn i32 main(i32 argc, const char** argv) {
21+
22+
u32[] aa = {
23+
b[No],
24+
b[Enum.Yes],
25+
b[argc ? Enum.Yes : Enum.No],
26+
b[Other[1]],
27+
b[getYes()],
28+
b[(Enum)argc],
29+
30+
b[Foo.B], // @error{array subscript must have enum type 'test.Enum'}
31+
b[argc], // @error{array subscript must have enum type 'test.Enum'}
32+
b[No + 1], // @error{use of undeclared identifier 'No'}
33+
b[1 ? Yes : // @error{use of undeclared identifier 'Yes'}
34+
No], // @error{use of undeclared identifier 'No'}
35+
}
36+
return 0;
37+
}
38+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// @warnings{no-unused}
2+
module test;
3+
4+
import stdio local;
5+
6+
type Enum enum u8 { No, Yes, Maybe }
7+
type Enum2 enum u8 { A, B, C, D }
8+
9+
u32[Enum][Enum2] a = {
10+
[No] = { 1, 2, 3, 4 },
11+
[Yes] = { 5, 6, 7, 8 },
12+
[Maybe] = { 9, 10, 11, 12 },
13+
}
14+
15+
static_assert(elemsof(a), elemsof(Enum));
16+
17+
18+
fn Enum getYes() { return Yes; }
19+
20+
const Enum[2] Other = { Maybe, Yes }
21+
22+
public fn i32 main(i32 argc, const char** argv) {
23+
Enum e = Maybe;
24+
25+
u32[] aa = {
26+
a[No][D],
27+
a[Yes][B],
28+
a[Enum.Yes][C],
29+
a[argc ? Enum.Yes : Enum.No][A],
30+
a[Other[1]][B],
31+
a[getYes()][B],
32+
a[(Enum)argc][B],
33+
a[e][B],
34+
a[Enum.max][Enum2.max],
35+
36+
a[Enum2.B][A], // @error{array subscript must have enum type 'test.Enum'}
37+
a[Yes][Enum.Yes], // @error{array subscript must have enum type 'test.Enum2'}
38+
a[argc][B], // @error{array subscript must have enum type 'test.Enum'}
39+
a[Yes][argc], // @error{array subscript must have enum type 'test.Enum2'}
40+
}
41+
return 0;
42+
}
43+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @warnings{no-unused}
2+
module test;
3+
4+
type Foo enum u8 { A, B, C }
5+
6+
type T1 u32[Foo];
7+
8+
public type T2 u32[Foo]; // @error{public declaration using non-public variable 'test.Foo'}
9+

0 commit comments

Comments
 (0)