aboutsummaryrefslogtreecommitdiff
path: root/compiler/tests/runtime/switch.test.lc
blob: 21f6db9ddb1c171561ae57280a220b87913cdfcd (plain)
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

linkname "";

int main()
{
    int i = 2;
    var int state = 0;

    // Integer switch
    switch i {
    case 1 { assert :false; }
    case 2 { state = 1; }
    case 3 { assert :false; }
    }
    assert state == 1;

    // Integer switch with default
    switch i {
    case 1 { assert :false; }
    case 2 { state = 2; }
    case 3 { assert :false; }
    default { assert :false; }
    }
    assert state == 2;

    // Integer switch with reached default
    switch i {
    case 1 { assert :false; }
    case 3 { assert :false; }
    default { state = 3; }
    }
    assert state == 3;

    // Integer switch with "with" statements
    var int bug = 0;
    switch i {
    case 0 with bug = 10 { assert :false; }
    case 1 with bug = 11
    case 2 with state = 4
    case 3 with bug = 12 {
        assert state == 4;
        state = 5;
    }
    case 4 with bug = 13 { assert :false; }
    default { assert :false; }
    }
    assert bug == 0;
    assert state == 5;

    ////////////////////////////////////////////////////////////////////////
    // Now we test the same with an array value instead of int
    byte#[2] arr = [1, 2];
    state = 0;

    // Array switch
    switch arr {
    case [1,0] { assert :false; }
    case [1,2] { state = 1; }
    case [2,1] { assert :false; }
    }
    assert state == 1;

    // Integer switch with default
    switch arr {
    case [1,0] { assert :false; }
    case [1,2] { state = 2; }
    case [2,1] { assert :false; }
    default { assert :false; }
    }
    assert state == 2;

    // Array switch with reached default
    switch arr {
    case [1,0] { assert :false; }
    case [2,1] { assert :false; }
    default { state = 3; }
    }
    assert state == 3;

    // Array switch with "with" statements
    bug = 0;
    switch arr {
    case [0,0] with bug = 10 { assert :false; }
    case [1,0] with bug = 11
    case [1,2] with state = 4
    case [2,1] with bug = 12 {
        assert state == 4;
        state = 5;
    }
    case [4,4] with bug = 13 { assert :false; }
    default { assert :false; }
    }
    assert bug == 0;
    assert state == 5;

    return 0;
}