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