1/* 2 * Copyright (c) 2021-2022 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 16import { expect } from 'chai'; 17import 'mocha'; 18import { 19 compileTsWithType, 20 createLiteralBufferArray, 21 compareLiteralBuffer, 22 createVRegTypePair, 23 compareVReg2Type 24} from "./typeUtils"; 25import { PrimitiveType } from '../../src/base/typeSystem'; 26 27let shift = PrimitiveType._LENGTH; 28 29describe("function tests in function.test.ts", function () { 30 it("test function with no parameter", function () { 31 let fileNames = 'tests/types/function/function_no_para.ts'; 32 let result = compileTsWithType(fileNames); 33 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 34 let locals = functionPg!.getLocals(); 35 // check vreg 36 let extectedVRegTypePair = [ 37 ["#0#emptyFunc", shift + 2], 38 ] 39 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 40 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 41 42 // check liberalBuffer 43 let expectedBuffValues = [ 44 [ 45 [2, 0], [2, 1], [2, 0] 46 ], 47 [ 48 [2, 3], [2, 0], [2, 0], [5, 'local'], 49 [2, 0], [2, 0] 50 ], 51 [ 52 [2, 3], [2, 0], [2, 0], [5, 'emptyFunc'], 53 [2, 0], [2, 0] 54 ] 55 ] 56 let buff = createLiteralBufferArray(expectedBuffValues); 57 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 58 }); 59 60 it("test function with muti parameter", function () { 61 let fileNames = 'tests/types/function/function_multi_para.ts'; 62 let result = compileTsWithType(fileNames); 63 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 64 let locals = functionPg!.getLocals(); 65 // check vreg 66 let extectedVRegTypePair = [ 67 ["#0#num", 1], 68 ["#1#str", 4], 69 ["#0#emptyFunc", shift + 2], 70 ] 71 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 72 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 73 74 // check liberalBuffer 75 let expectedBuffValues = [ 76 [ 77 [2, 0], [2, 2], [2, 0] 78 ], 79 [ 80 [2, 3], [2, 0], [2, 0], [5, 'local'], 81 [2, 0], [2, 0] 82 ], 83 [ 84 [2, 3], [2, 0], [2, 0], [5, 'emptyFunc'], 85 [2, 2], [2, 1], [2, 4], [2, 4] 86 ] 87 ] 88 let buff = createLiteralBufferArray(expectedBuffValues); 89 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 90 }); 91 92 it("test function with same type of paras and return", function () { 93 let fileNames = 'tests/types/function/function_same_para_and_return.ts'; 94 let result = compileTsWithType(fileNames); 95 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 96 let locals = functionPg!.getLocals(); 97 // check vreg 98 let extectedVRegTypePair = [ 99 ["#0#num", 1], 100 ["#1#str", 4], 101 ["#0#num", 1], 102 ["#1#str", 4], 103 ["#0#foo", shift + 2], 104 ["#1#bar", shift + 3], 105 ] 106 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 107 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 108 109 // check liberalBuffer 110 let expectedBuffValues = [ 111 [ 112 [2, 0], [2, 3], [2, 0] 113 ], 114 [ 115 [2, 3],[2, 0],[2, 0],[5, 'twoFunctions'], 116 [2, 0],[2, 0] 117 ], 118 [ 119 [2, 3],[2, 0],[2, 0],[5, 'foo'], 120 [2, 2],[2, 1],[2, 4],[2, 3] 121 ], 122 [ 123 [2, 3],[2, 0],[2, 0],[5, 'bar'], 124 [2, 2],[2, 1],[2, 4],[2, 3] 125 ] 126 ] 127 let buff = createLiteralBufferArray(expectedBuffValues); 128 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 129 }); 130 131 it("test function with class as parameter", function () { 132 let fileNames = 'tests/types/function/function_class_para.ts'; 133 let result = compileTsWithType(fileNames); 134 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 135 let locals = functionPg!.getLocals(); 136 // check vreg 137 let extectedVRegTypePair = [ 138 ["#0#num", 1], 139 ["#1#a", shift + 4], 140 ["#0#foo", shift + 2], 141 ["#1#A", shift + 3], 142 ] 143 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 144 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 145 146 // check liberalBuffer 147 let expectedBuffValues = [ 148 [ 149 [2, 0], [2, 4], [2, 0] 150 ], 151 [ 152 [2, 3], [2, 0], [2, 0], [5, 'localClass'], 153 [2, 0], [2, 0] 154 ], 155 [ 156 [2, 3], [2, 0], [2, 0], [5, 'foo'], 157 [2, 2], [2, 1], [2, shift + 4], [2, 0] 158 ], 159 [ 160 161 [2, 1], [2, 0], [2, 0], [2, 0], 162 [2, 0], [2, 0], [2, 0], [2, 0] 163 ], 164 [ 165 [2, 2], [2, shift + 3] 166 ] 167 ] 168 let buff = createLiteralBufferArray(expectedBuffValues); 169 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 170 }); 171 172 it("test function with class as return", function () { 173 let fileNames = 'tests/types/function/function_class_return.ts'; 174 let result = compileTsWithType(fileNames); 175 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 176 let locals = functionPg!.getLocals(); 177 // check vreg 178 let extectedVRegTypePair = [ 179 ["#0#foo", shift + 2], 180 ] 181 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 182 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 183 184 // check liberalBuffer 185 let expectedBuffValues = [ 186 [ 187 [2, 0], [2, 4], [2, 0] 188 ], 189 [ 190 [2, 3], [2, 0], [2, 0], [5, 'localClass'], 191 [2, 0], [2, 0] 192 ], 193 [ 194 [2, 3], [2, 0], [2, 0], [5, 'foo'], 195 [2, 0], [2, shift + 4] 196 ], 197 [ 198 199 [2, 1], [2, 0], [2, 0], [2, 0], 200 [2, 0], [2, 0], [2, 0], [2, 0] 201 ], 202 [ 203 [2, 2], [2, shift + 3] 204 ] 205 ] 206 let buff = createLiteralBufferArray(expectedBuffValues); 207 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 208 }); 209}); 210