1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16function force(thunk: () => void) {
17    thunk();
18}
19
20function bool2Levels(): void {
21    force(() => {
22        let a0 = true;
23        force(() => {
24            let a1 = a0;
25            assert a0 && a1;
26            a0 = false;
27            assert !a0 && a1;
28            a0 = true;
29            a1 = false;
30            assert a0 && !a1;
31        });
32    });
33}
34
35function bool4Levels(): void {
36    force(() => {
37        let a0 = true;
38        force(() => {
39            let a1 = a0;
40            force(() => {
41                let a2 = a1;
42                force(() => {
43                    let a3 = a2;
44                    assert a0 && a1 && a2 && a3;
45                    a0 = false;
46                    assert !a0 && a1 && a2 && a3;
47                    a3 = false;
48                    assert !a0 && a1 && a2 && !a3;
49                });
50            });
51        });
52    });
53}
54
55function bool4LevelsBi(): void {
56    force(() => {
57        let a0 = true;
58        force(() => {
59            let a1 = a0;
60            force(() => {
61                let a2 = a1;
62                force(() => {
63                    let a3 = a2;
64                    assert a0 && a1 && a2 && a3;
65                    a0 = false;
66                    assert !a0 && a1 && a2 && a3;
67                    a3 = a0;
68                    assert !a0 && a1 && a2 && !a3;
69                });
70                force(() => {
71                    a2 = a0;
72                    assert !a0 && a1 && !a2;
73                });
74            });
75            force(() => {
76                assert !a0 && a1;
77            });
78        });
79        force(() => {
80            assert !a0;
81        });
82    });
83}
84
85function number4Levels(): void {
86    force(() => {
87        let a0 = 1;
88        force(() => {
89            let a1 = a0++;
90            force(() => {
91                let a2 = a1++;
92                force(() => {
93                    let a3 = a2++;
94                    a3++;
95                    assert 8 == a0 + a1 + a2 + a3;
96                });
97            });
98        });
99    });
100}
101
102function arrays(): void {
103    force(() => {
104        let a0 = [1, 0, 0, 0];
105        force(() => {
106            let a1 = a0;
107            a1[1]=a0[0];
108            force(() => {
109                let a2 = a1;
110                a2[2] = a1[1];
111                force(() => {
112                    let a3 = a2;
113                    force(() => {
114                        a3[3] = a2[2];
115                        assert a0[0] + a0[1] + a0[2] + a0[3] == 4;
116                        a0 = new double[5];
117                        assert a0.length == 5;
118                        assert a0[0] + a0[1] + a0[2] + a0[3] + a0[4] == 0;
119                        assert a1[0] + a1[1] + a1[2] + a1[3] == 4;
120                        assert a1 == a2 && a2 == a3;
121                    })
122                });
123            });
124        });
125    });
126}
127
128function main():void {
129    bool2Levels();
130    bool4Levels();
131    bool4LevelsBi();
132    number4Levels();
133    arrays();
134}
135