1/* 2* Copyright (C) 2025 HiHope Open Source Organization. 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*/ 15import { ArkTSUtils, HashMap, MessageEvents, taskpool, worker } from '@kit.ArkTS'; 16// import { add } from 'sharelibrary'; 17import { sleep, printArgs2 } from './Ulits'; 18 19export async function test1() { 20 const count = 42; 21 return count; 22} 23; 24 25export let test2 = async () => { 26 const count = 42; 27 return count; 28} 29 30@Concurrent 31export async function test3() { 32 const count = 42; 33 return count; 34} 35; 36 37@Sendable 38export async function test4() { 39 const count = 42; 40 return count; 41} 42; 43 44 45export async function test5() { 46 throw new Error('For test'); 47} 48; 49 50export async function test6() { 51 throw new Error('For test'); 52} 53; 54 55export async function test7(param: null) { 56 throw new Error('For test'); 57} 58; 59 60export async function test8() { 61 const count = '42'; 62 return count; 63} 64; 65 66export async function test9(name?: string) { 67 const count = '42'; 68 return count; 69} 70; 71 72export async function test10(name?: string) { 73 const count = 42; 74 return count; 75} 76; 77 78export async function test11(name?: string) { 79 const count = true; 80 return count; 81} 82; 83 84export async function test12(name?: string) { 85 const count: bigint = 42n; 86 return count; 87} 88; 89 90 91interface testObject { 92 age: number; 93} 94 95export async function test13(name?: string): Promise<testObject> { 96 return { 97 age: 30 98 }; 99} 100 101export async function test14(name?: string): Promise<testObject> { 102 return { 103 age: 30 104 }; 105} 106 107export async function test15(name?: string) { 108 const arr = [41, 42, 43] 109 return arr; 110} 111 112export async function test16(name?: string): Promise<() => string> { 113 return () => 'Hello, World!'; 114} 115 116export async function test17(name?: string) { 117 throw new Error('For test'); 118 return; 119} 120; 121 122export async function test18(name?: string) { 123 throw new Error('For test'); 124 return null; 125} 126; 127 128export async function test19(name?: string) { 129 throw new Error('For test'); 130 return undefined; 131} 132; 133 134export async function test20(name?: string) { 135 return new Error('For test'); 136} 137; 138 139 140class Person { 141 private name: string = ''; 142 private surname: string = ''; 143 144 constructor(n: string, sn: string) { 145 this.name = n; 146 this.surname = sn; 147 } 148 149 fullName(): string { 150 return this.name + ' ' + this.surname; 151 } 152} 153 154export async function test21(name?: string) { 155 let p = new Person('John', 'Smith'); 156 return p; 157} 158; 159 160 161@Sendable 162class Person1 { 163 private name: string = ''; 164 private surname: string = ''; 165 166 constructor(n: string, sn: string) { 167 this.name = n; 168 this.surname = sn; 169 } 170 171 fullName(): string { 172 return this.name + ' ' + this.surname; 173 } 174} 175 176export async function test22(name?: string) { 177 let p = new Person1('John', 'Smith'); 178 return p; 179} 180; 181 182 183@Sendable 184function for23test(name?: string) { 185 return 42; 186}; 187 188 189export async function test23(name?: string) { 190 return for23test; 191} 192; 193 194class Person2 { 195 private name: string = ''; 196 private surname: string = ''; 197 198 constructor(n: string, sn: string) { 199 this.name = n; 200 this.surname = sn; 201 } 202 203 fullName(): string { 204 return this.name + ' ' + this.surname; 205 } 206 207 test() { 208 return new Person1('John1', 'Smith1'); 209 } 210} 211 212export async function test24(name?: string) { 213 let p = new Person2('John2', 'Smith2'); 214 return p; 215} 216; 217 218 219export async function test25() { 220 throw new Error('For test'); 221} 222; 223 224@Concurrent 225function for26test() { 226 console.log('for test'); 227 return 42; 228}; 229 230 231export async function test26() { 232 let task: taskpool.Task = new taskpool.Task(for26test); 233 const result = await taskpool.execute(task); 234 return result; 235} 236; 237 238 239async function for27test() { 240 console.log('for test'); 241 return 42; 242}; 243 244export async function test27() { 245 const result = for27test(); 246 return result; 247} 248; 249 250async function for28test() { 251 console.log('for test'); 252 return 42; 253}; 254 255export async function test28() { 256 const result = await for28test(); 257 return result; 258} 259; 260 261export async function test29() { 262 const caseName = 'ConcurrencyModularImportAndExportTest2900'; 263 let returnCount = 0; 264 let workThread: worker.ThreadWorker; 265 workThread = new worker.ThreadWorker('../components/workers/Worker'); 266 workThread.onexit = () => { 267 console.log('onexit'); 268 } 269 workThread.postMessage(caseName); 270 workThread.onmessage = (e: MessageEvents): void => { 271 returnCount = e.data; 272 console.info(`${caseName} test end`); 273 } 274 console.log('Exit worker'); 275 while (returnCount != 42) { 276 await sleep(300); 277 } 278 return returnCount; 279} 280; 281 282export async function test30() { 283 let count = 0; 284 setTimeout(() => { 285 count = 42; 286 }, 1000); 287 while (count != 42) { 288 await sleep(300); 289 } 290 return count; 291} 292; 293 294export async function test31() { 295 let count = 0; 296 await new Promise<void>((resolve, reject) => { 297 count = 42; 298 resolve(); 299 }); 300 return count; 301} 302; 303 304class For32Test { 305 private name: string = ''; 306 private age: number = 0; 307 308 constructor(n: string, age: number) { 309 this.name = n; 310 this.age = age; 311 } 312 313 fullName(): string { 314 return this.name; 315 } 316 317 getAge(): number { 318 return this.age; 319 } 320} 321 322export async function test32() { 323 let p = new For32Test('John', 24); 324 let result = p.getAge(); 325 return result; 326} 327; 328 329 330@Sendable 331class For33test { 332 private name: string = ''; 333 private age: number = 0; 334 335 constructor(n: string, age: number) { 336 this.name = n; 337 this.age = age; 338 } 339 340 fullName(): string { 341 return this.name; 342 } 343 344 getAge(): number { 345 return this.age; 346 } 347} 348 349export async function test33() { 350 let p = new For33test('John', 24); 351 let result = p.getAge(); 352 return result; 353} 354; 355 356 357class For34Test { 358 private name: string = ''; 359 private age: number = 0; 360 361 constructor(n: string, age: number) { 362 this.name = n; 363 this.age = age; 364 } 365 366 fullName(): string { 367 return this.name; 368 } 369 370 getAge(): number { 371 new For33test('John', 42) 372 return this.age; 373 } 374} 375 376export async function test34() { 377 let p = new For34Test('John', 24); 378 let result = p.getAge(); 379 return result; 380} 381; 382