1/*
2* Copyright (c) 2022-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 low 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
16let count = 2
17let n = 9
18let a: int[] = new int[count];
19let v: int[] = new int[count];
20type P = Promise<Int> | null
21function assert_eq(value1: int, value2: int): void {
22    if (value1 == value2) {
23        return;
24    }
25    console.println("Values of type int are not equal: " + value1 + " != " + value2);
26    throw new Error();
27}
28function ufib(n: int) : Int {
29    if (n >= 0 && n < count) {
30        return v[n];
31    }
32    let p: P[] = new P[count]
33    for (let i = 0; i < count; ++i) {
34        p[i] = launch ufib(n-1-i);
35    }
36    let result = 0
37    for (let i = 0; i < count; ++i) {
38        result = result + p[i]!.awaitResolution() * a[i];
39    }
40    return result;
41}
42function ufib_seq(n: int) : int {
43    if (n >= 0 && n < count) {
44        return v[n];
45    }
46    let result = 0
47    for (let i = 0; i < count; ++i) {
48        result = result + ufib_seq(n-1-i) * a[i];
49    }
50    return result;
51}
52export function main(): int {
53    a[0] = 2;
54    v[0] = 6;
55    a[1] = 2;
56    v[1] = 7;
57    let seq_result = ufib_seq(n);
58    let p = launch ufib(n);
59    let co_result = p.awaitResolution();
60    assert_eq(co_result as int, seq_result);
61    return 0;
62}
63