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 ani_test from "ani_test"; 17 18loadLibrary("ani_test"); 19 20function test_data_struct() { 21 // Test struct creation and display 22 const option: ani_test.Data = { 23 src: "path/to/source", 24 dest: 123, 25 files: ["file1.txt", "file2.txt", "file3.txt"] 26 }; 27 ani_test.showData(option); 28 29 // Test makeData returns expected structure 30 const result = ani_test.makeData(); 31 assertEQ(result.src, "C++ Object"); 32 assertEQ(result.dest, 1); 33 assertEQ(result.files.length, 2); 34 assertEQ(result.files[0], "file.txt"); 35 assertEQ(result.files[1], "file.txt"); 36} 37 38function test_union() { 39 // Test showUnion with different types 40 ani_test.showUnion(null); 41 ani_test.showUnion(1 as int); 42 ani_test.showUnion(2 as float); 43 ani_test.showUnion("Hello from STS!"); 44 45 // Test makeUnion returns expected values 46 const union0 = ani_test.makeUnion(0); 47 assertEQ(union0, null); 48 49 const union1 = ani_test.makeUnion(1); 50 assertEQ(union1, 100); 51 52 const union2 = ani_test.makeUnion(2); 53 assertEQ(union2, 0.5); 54 55 const union3 = ani_test.makeUnion(3); 56 assertEQ(union3, "Hello from C++!"); 57} 58 59function test_optional() { 60 // Test showOptionalInt with value and undefined 61 ani_test.showOptionalInt(100 as int); 62 ani_test.showOptionalInt(undefined); 63 64 // Test makeOptionalInt returns expected values 65 const optTrue = ani_test.makeOptionalInt(true); 66 assertEQ(optTrue, 10); 67 68 const optFalse = ani_test.makeOptionalInt(false); 69 assertEQ(optFalse, undefined); 70} 71 72function test_array() { 73 // Test showArrayInt with array 74 ani_test.showArrayInt([1, 2, 3]); 75 76 // Test makeArrayInt returns expected array 77 const arr = ani_test.makeArrayInt(10, 123); 78 assertEQ(arr.length, 10); 79 for (let i = 0; i < arr.length; i++) { 80 assertEQ(arr[i], 123); 81 } 82} 83 84function test_interface() { 85 // Test makeFoo implementation 86 let impl_foo_list = ani_test.makeFoo(["A", "B", "C"]); 87 assertEQ(impl_foo_list.length, 3); 88 for (let i = 0; i < impl_foo_list.length; i++) { 89 impl_foo_list[i].bar(); 90 } 91 92 // Test UserFoo implementation 93 class UserFoo implements ani_test.Foo { 94 name: string; 95 constructor(name: string) { 96 this.name = name; 97 } 98 bar(): void { 99 console.log("UserFoo(" + this.name + ") is calling bar()"); 100 } 101 } 102 const X: ani_test.Foo = new UserFoo("X") 103 const Y: ani_test.Foo = new UserFoo("Y") 104 const Z: ani_test.Foo = new UserFoo("Z") 105 const userFoos: Array<ani_test.Foo> = [X, Y, Z]; 106 assertEQ(userFoos.length, 3); 107 ani_test.callBar(userFoos); 108} 109 110function main() { 111 const suite = new ArkTestsuite("Ani Test Suite"); 112 suite.addTest("Test Data Struct", test_data_struct); 113 suite.addTest("Test Union", test_union); 114 suite.addTest("Test Optional", test_optional); 115 suite.addTest("Test Array", test_array); 116 suite.addTest("Test Interface", test_interface); 117 exit(suite.run()); 118} 119