1/* 2 * Copyright (c) 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 */ 15import {BusinessError} from "@ohos.base"; 16import * as StringIo from "string_io"; 17import * as StringOp from "string_op"; 18 19loadLibrary("ani_string"); 20 21function concatString() { 22 let str = StringOp.concatString("abc", "d"); 23 assertEQ(str, "abcd") 24} 25 26function makeString() { 27 let str = StringOp.makeString("abc", 3); 28 assertEQ(str, "abcabcabc") 29} 30 31function split() { 32 let strPair = StringOp.split("abcd", 2); 33 assertEQ(strPair._0, "ab") 34 assertEQ(strPair._1, "cd") 35} 36 37function split2() { 38 let strArray = StringOp.split2("abcd", 2); 39 assertEQ(strArray[0], "ab") 40 assertEQ(strArray[1], "cd") 41} 42 43function to_i32() { 44 let num = StringOp.to_i32("123"); 45 assertEQ(num, 123) 46} 47 48function from_i32() { 49 let str = StringOp.from_i32(123); 50 assertEQ(str, "123") 51} 52 53function to_f32() { 54 let num = StringOp.to_f32("123.123"); 55 assertEQ((num - 123.123) > -1e-6 || (num - 123.123) < 1e-6, true) 56} 57 58function from_f32() { 59 let str = StringOp.from_f32(123.123f); 60 assertEQ(str, "123.123") 61} 62 63function input() { 64 let str = StringIo.input(); 65 assertEQ(str, "input") 66} 67 68function println() { 69 StringIo.println("println string"); 70} 71 72function print() { 73 StringIo.print("print string"); 74 StringIo.println(" ---"); 75} 76 77function testPlayString() { 78 let playStr = StringOp.makePlayStringIface(); 79 assertEQ(playStr.name, "PlayString") 80 81 let res = playStr.pickString(["abc", "edf", "xyz"], 1, 2) 82 assertEQ(res, "edfxyz") 83 84 res = playStr.pickString(["abc", "edf", "xyz"], 0, 1) 85 assertEQ(res, "abcedf") 86} 87 88function concatString2() { 89 let s = "a"; 90 let sArr: string[] = ["b", "c", "d"]; 91 let numbersU8: byte[] = [1, 2, 3, 4]; 92 let buffer: ArrayBuffer = new ArrayBuffer(numbersU8.length); 93 for (let i = 0; i < numbersU8.length; i++) { 94 buffer.set(i, numbersU8[i]); 95 } 96 assertEQ(StringOp.concatString2(s, 1, sArr, true, buffer), "abcd1234"); 97 assertEQ(StringOp.concatString2(s, 1, sArr, false, buffer), "a"); 98} 99 100 101function main() { 102 console.log("run main ... StringTest ...") 103 104 const suite = new ArkTestsuite("StringTest") 105 suite.addTest("concatString", concatString) 106 suite.addTest("concatString2", concatString2) 107 suite.addTest("makeString", makeString) 108 suite.addTest("split", split) 109 suite.addTest("split2", split2) 110 suite.addTest("to_i32", to_i32) 111 suite.addTest("from_i32", from_i32) 112 suite.addTest("to_f32", to_f32) 113 suite.addTest("from_f32", from_f32) 114 suite.addTest("input", input) 115 suite.addTest("println", println) 116 suite.addTest("print", print) 117 suite.addTest("testPlayString", testPlayString) 118 exit(suite.run()) 119} 120