1/* 2 * Copyright (C) 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 */ 15import taskpool from '@ohos.taskpool'; 16 17function add(value1: number, value2: number): number { 18 "use concurrent" 19 return value1 + value2; 20} 21 22function addOne(value1: number): number { 23 "use concurrent" 24 return value1 + 1; 25} 26 27function printUndefined(value: number | undefined): number | undefined { 28 "use concurrent" 29 return value; 30} 31 32function printNull(value: null): null { 33 "use concurrent" 34 return value; 35} 36 37function funOne(value1: number): number { 38 "use concurrent" 39 console.info(`taskpool: data is: ${value1} `); 40 return value1; 41} 42 43function multiply(value1: number, value2: number): number { 44 "use concurrent" 45 return value1 * value2; 46} 47 48function dividing(value1: number, value2: number): number { 49 "use concurrent" 50 return value1 / value2; 51} 52 53function mathCeil(value1: number): number { 54 "use concurrent" 55 return Math.ceil(value1); 56} 57 58function mathFloor(value1: number): number { 59 "use concurrent" 60 return Math.floor(value1); 61} 62 63function mathRound(value1: number): number { 64 "use concurrent" 65 return Math.round(value1); 66} 67 68function mathPow(value1: number, value2: number): number { 69 "use concurrent" 70 return Math.pow(value1, value2); 71} 72 73function mathSqrt(value1: number): number { 74 "use concurrent" 75 return Math.sqrt(value1); 76} 77 78function mathAbs(value1: number): number { 79 "use concurrent" 80 return Math.abs(value1); 81} 82 83function mathMore(value1: number): number { 84 "use concurrent" 85 return value1 % 5; 86} 87 88function mathMax(value1: number, value2: number): number { 89 "use concurrent" 90 return Math.max(value1, value2); 91} 92 93function mathMin(value1: number, value2: number): number { 94 "use concurrent" 95 return Math.min(value1, value2); 96} 97 98function addition(arg: number): number { 99 "use concurrent" 100 let start: number = new Date().getTime(); 101 while (new Date().getTime() - start < 100) { 102 continue; 103 } 104 return arg; 105} 106 107function additionF(): void { 108 "use concurrent" 109 let start: number = new Date().getTime(); 110 while (new Date().getTime() - start < 100) { 111 continue; 112 } 113} 114 115function adds(value1: string, value2: string): string { 116 "use concurrent" 117 return value1 + value2; 118} 119 120function addAdd(value1: string, value2: string, value3: string): string { 121 "use concurrent" 122 setTimeout(() => { 123 }, 1000); 124 return value1 + value2 + value3; 125} 126 127function printString(args: string): string { 128 "use concurrent" 129 console.info(`printString: ${args}`); 130 return args; 131} 132 133function printArgs(args: number): number { 134 "use concurrent" 135 console.info(`printArgs: ${args}`); 136 return args; 137} 138 139function printABool(args: boolean): boolean { 140 "use concurrent" 141 console.info(`printArgs: ${args}`); 142 return args; 143} 144 145function printABig(args: bigint): bigint { 146 "use concurrent" 147 console.info(`printArgs: ${args}`); 148 return args; 149} 150 151function printSan(args: number): number { 152 "use concurrent" 153 let start: number = new Date().getTime(); 154 while (new Date().getTime() - start < 3000) { 155 continue; 156 } 157 console.info(`printSan: ${args}`); 158 return args; 159} 160 161function printLang(args: number): number { 162 "use concurrent" 163 let start: number = new Date().getTime(); 164 while (new Date().getTime() - start < 1000) { 165 continue; 166 } 167 return args; 168} 169 170function printTime(args: number): number { 171 "use concurrent" 172 let start: number = new Date().getTime(); 173 while (new Date().getTime() - start < 10) { 174 continue; 175 } 176 console.info(`printArgs: ${args}`); 177 return args; 178} 179 180function testTransfer(arg1: ArrayBuffer, arg2: ArrayBuffer): number { 181 "use concurrent" 182 return arg1.byteLength + arg2.byteLength; 183} 184 185function testFunc(arrayBuffer: ArrayBuffer): Int32Array { 186 "use concurrent" 187 let view: Int32Array = new Int32Array(arrayBuffer); 188 let arr: number[] = [11, 12, 13, 14]; 189 for (let i = 0; i < arr.length; i++) { 190 view[i] = arr[i]; 191 } 192 return view; 193} 194 195function throwError(): void { 196 "use concurrent" 197 try { 198 throw new Error(' throw new Error '); 199 } catch (error) { 200 console.info(`no throw new Error: ${JSON.stringify(error)}`); 201 } 202} 203 204function stringToNumber(str: string | number): number { 205 "use concurrent" 206 if (typeof (str) == 'number') { 207 return str; 208 } 209 return str === 'a' ? 2 : str === 'b' ? 4 : 0; 210} 211 212function sendDataCatch(num: number): number | (number | string)[] { 213 "use concurrent" 214 let res: number = num * 10; 215 console.info(`concurrentF: res1 ${res}`); 216 try { 217 taskpool.Task.sendData(res); 218 } catch (error) { 219 console.info(`sendDataCatch: catch error: err1 code => ${error.code} message => ${error.message}`); 220 let errorArray: (number | string)[] = [error.code, error.message]; 221 console.info(`sendDataCatch: err2 ${JSON.stringify(errorArray)}`); 222 return errorArray; 223 } 224 console.info(`concurrentF: res2 ${res}`); 225 return num; 226} 227 228function sendDataFun(num: number): Promise<number | (number | string)[]> { 229 "use concurrent" 230 taskpool.Task.sendData(0); 231 return new Promise((resolve, reject) => { 232 let res1 = num * 20; 233 let errorArray: (number | string)[] = []; 234 try { 235 taskpool.Task.sendData(res1); 236 resolve(errorArray); 237 } catch (error) { 238 console.info(`sendDataFun: catch error: err1 code => ${error.code} message => ${error.message}`); 239 errorArray = [error.code, error.message]; 240 console.info(`sendDataFun: err2 ${JSON.stringify(errorArray)}`); 241 reject(errorArray) 242 } 243 }); 244} 245 246function concurrentF(num: number): number { 247 "use concurrent" 248 let res: number = num * 10; 249 console.info(`concurrentF: res1 ${res}`); 250 taskpool.Task.sendData(res); 251 console.info(`concurrentF: res2 ${res}`); 252 return num; 253} 254 255function spileString(value1: string, value2: string): string { 256 "use concurrent" 257 let str: string = value1 + value2; 258 taskpool.Task.sendData(str); 259 return str; 260} 261 262function funArray(value1: number[]): void { 263 "use concurrent" 264 console.info(`taskpool: data is: ${JSON.stringify(value1)} `); 265 taskpool.Task.sendData(value1); 266} 267 268function funDate(value1: object): void { 269 "use concurrent" 270 console.info(`taskpool: data is: ${JSON.stringify(value1)} `); 271 taskpool.Task.sendData(value1); 272} 273 274function AdditionDelay(delay: number): number { 275 "use concurrent" 276 let start: number = new Date().getTime(); 277 while (new Date().getTime() - start < delay) { 278 continue; 279 } 280 return delay; 281} 282 283export { 284 add, 285 addOne, 286 printUndefined, 287 printNull, 288 funOne, 289 multiply, 290 dividing, 291 mathCeil, 292 mathFloor, 293 mathRound, 294 mathPow, 295 mathSqrt, 296 mathAbs, 297 mathMore, 298 mathMax, 299 mathMin, 300 addition, 301 additionF, 302 adds, 303 addAdd, 304 printString, 305 printArgs, 306 printABool, 307 printABig, 308 printSan, 309 printLang, 310 printTime, 311 testTransfer, 312 testFunc, 313 throwError, 314 stringToNumber, 315 sendDataCatch, 316 sendDataFun, 317 concurrentF, 318 spileString, 319 funArray, 320 funDate, 321 AdditionDelay 322}