1/* 2 * Copyright (c) 2023-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 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 16const FLOAT_PI: float = 3.14; 17 18async function byteFunc(): Promise<Object> { 19 return -3 as byte as Object; 20} 21 22async function charFunc(): Promise<Object> { 23 return c'x' as Object; 24} 25 26async function shortFunc(): Promise<Object> { 27 return 32767 as short as Object; 28} 29 30async function intFunc(): Promise<Object> { 31 return 5 as Object; 32} 33 34async function longFunc(): Promise<Object> { 35 return 777 as long as Object; 36} 37 38async function floatFunc(): Promise<Object> { 39 return FLOAT_PI as Object; 40} 41 42async function doubleFunc(): Promise<Object> { 43 return 2.71 as Object; 44} 45 46let passed: boolean = false; 47let errorMessage = ""; 48 49function reset(): void { 50 passed = false; 51 errorMessage = ""; 52} 53 54function check(test: String): void { 55 if (!passed) { 56 console.println("Test " + test + " failed: " + errorMessage); 57 throw new Error(); 58 } 59} 60 61export function main(): int { 62 reset(); 63 let byteP: Promise<Object> = byteFunc(); 64 let pThen = byteP.then<Object>((value: Object): Object => { 65 if ((value as Byte) == -3) { 66 passed = true; 67 } else { 68 errorMessage = "Expected -3 but got " + value; 69 } 70 return value 71 }); 72 await byteP; 73 await (pThen as Promise<Object>); 74 check("byteFunc"); 75 76 reset(); 77 let charP: Promise<Object> = charFunc(); 78 pThen = charP.then<Object>((value: Object): Object => { 79 if ((value as Char) == c'x') { 80 passed = true; 81 } else { 82 errorMessage = "Expected 'x' but got " + value; 83 } 84 return value 85 }); 86 await charP; 87 await (pThen as Promise<Object>); 88 check("charFunc"); 89 90 reset(); 91 let shortP: Promise<Object> = shortFunc(); 92 pThen = shortP.then<Object>((value: Object): Object => { 93 if ((value as Short) == 32767) { 94 passed = true; 95 } else { 96 errorMessage = "Expected 32767 but got " + value; 97 } 98 return value 99 }); 100 await shortP; 101 await (pThen as Promise<Object>); 102 check("shortFunc"); 103 104 reset(); 105 let intP: Promise<Object> = intFunc(); 106 pThen = intP.then<Object>((value: Object): Object => { 107 if ((value as Int) == 5) { 108 passed = true; 109 } else { 110 errorMessage = "Expected 5 but got " + value; 111 } 112 return value 113 }); 114 await intP; 115 await (pThen as Promise<Object>); 116 check("intFunc"); 117 118 reset(); 119 let longP: Promise<Object> = longFunc(); 120 pThen = longP.then<Object>((value: Object): Object => { 121 if ((value as Long) == 777) { 122 passed = true; 123 } else { 124 errorMessage = "Expected 777 but got " + value; 125 console.println(errorMessage); 126 } 127 return value 128 }); 129 await longP; 130 await (pThen as Promise<Object>); 131 check("longFunc"); 132 133 reset(); 134 let floatP: Promise<Object> = floatFunc(); 135 pThen = floatP.then<Object>((value: Object): Object => { 136 if ((value as Float) == FLOAT_PI) { 137 passed = true; 138 } else { 139 errorMessage = "Expected " + FLOAT_PI + " but got " + value; 140 console.println(errorMessage); 141 } 142 return value 143 }); 144 await floatP; 145 await (pThen as Promise<Object>); 146 check("floatFunc"); 147 148 reset(); 149 let doubleP: Promise<Object> = doubleFunc(); 150 pThen = doubleP.then<Object>((value: Object): Object => { 151 if ((value as Double) == 2.71) { 152 passed = true; 153 } else { 154 errorMessage = "Expected 2.71 but got " + value; 155 } 156 return value 157 }); 158 await doubleP; 159 await (pThen as Promise<Object>); 160 check("doubleFunc"); 161 return 0; 162} 163