1/*
2 * Copyright (c) 2023-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
16// test about variable captures
17function foo(c: () => void): void {
18    c();
19}
20
21function test_captures(): void {
22    const num_const = 1
23    let num = 2
24
25    // capture in lambda
26    foo() {
27        assert num_const == 1: "expected: " + 1 + " actual: " + num_const;
28        assert num == 2: "expected: " + 2 + " actual: " + num;
29        {
30            num++
31            assert num_const == 1: "expected: " + 1 + " actual: " + num_const;
32            assert num == 3: "expected: " + 3 + " actual: " + num;
33        }
34
35        let x: ()=>void = () => {
36            num++  // This capture will cause crash issue(#I80K99)
37        }
38        x();
39
40        if (true) {
41            num++;
42            assert num == 4: "expected: " + 4 + " actual: " + num;
43            assert num_const == 1: "expected: " + 1 + " actual: " + num_const;
44        }
45
46        for (let i = 0; i < 2; ++i) {
47            num++;
48            assert num_const == 1: "expected: " + 1 + " actual: " + num_const;
49        }
50
51        assert num == 6: "expected: " + 6 + " actual: " + num;
52
53        while (true) {
54            if (num < 7) {
55                num++
56            } else {
57                break;
58            }
59        }
60        assert num == 7: "expected: " + 7 + " actual: " + num;
61
62        foo() {
63            num++;
64            assert num == 8: "expected: " + 8 + " actual: " + num;
65        }
66    };
67}
68
69function main() {
70    test_captures();
71}
72