1/* 2 * Copyright (c) 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("array tests in array.test.ts", function() { 30 it("test array with primitives", function() { 31 let fileNames = 'tests/types/array/array_primitives.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 ["#3#num", shift + 1], 38 ["#4#bool", shift + 2], 39 ["#5#str", shift + 3], 40 ["#6#sym", shift + 4], 41 ["#7#nu", shift + 5], 42 ["#8#und", shift + 6], 43 ] 44 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 45 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 46 47 // check liberalBuffer 48 let expectedBuffValues = [ 49 [ 50 [2, 0], [2, 6], [2, 0] 51 ], 52 [ 53 [2, 5], [2, 1] 54 ], 55 [ 56 [2, 5], [2, 2] 57 ], 58 [ 59 [2, 5], [2, 4] 60 ], 61 [ 62 [2, 5], [2, 5] 63 ], 64 [ 65 [2, 5], [2, 6] 66 ], 67 [ 68 [2, 5], [2, 7] 69 ] 70 ] 71 let buff = createLiteralBufferArray(expectedBuffValues); 72 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 73 }); 74 75 it("test array with class", function() { 76 let fileNames = 'tests/types/array/array_class.ts'; 77 let result = compileTsWithType(fileNames); 78 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 79 let locals = functionPg!.getLocals(); 80 // check vreg 81 let extectedVRegTypePair = [ 82 ["#3#A", shift + 1], 83 ["#4#arrayA", shift + 3], 84 ] 85 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 86 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 87 88 // check liberalBuffer 89 let expectedBuffValues = [ 90 [ 91 [2, 0], [2, 3], [2, 0] 92 ], 93 [ 94 [2, 1], [2, 0], [2, 0], [2, 0], 95 [2, 0], [2, 0], [2, 0], [2, 0] 96 ], 97 [ 98 [2, 2], [2, 51] 99 ], 100 [ 101 [2, 5], [2, 52] 102 ] 103 ] 104 let buff = createLiteralBufferArray(expectedBuffValues); 105 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 106 }); 107 108 it("test array with multi same primitive", function() { 109 let fileNames = 'tests/types/array/array_multi_same_primi.ts'; 110 let result = compileTsWithType(fileNames); 111 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 112 let locals = functionPg!.getLocals(); 113 // check vreg 114 let extectedVRegTypePair = [ 115 ["#3#num1", shift + 1], 116 ["#4#num2", shift + 1], 117 ["#5#bool1", shift + 2], 118 ["#6#bool2", shift + 2], 119 ["#7#str1", shift + 3], 120 ["#8#str2", shift + 3], 121 ["#9#sym1", shift + 4], 122 ["#10#sym2", shift + 4], 123 ["#11#nu1", shift + 5], 124 ["#12#nu2", shift + 5], 125 ["#13#und1", shift + 6], 126 ["#14#und2", shift + 6], 127 ] 128 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 129 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 130 131 // check liberalBuffer 132 let expectedBuffValues = [ 133 [ 134 [2, 0], [2, 6], [2, 0] 135 ], 136 [ 137 [2, 5], [2, 1] 138 ], 139 [ 140 [2, 5], [2, 2] 141 ], 142 [ 143 [2, 5], [2, 4] 144 ], 145 [ 146 [2, 5], [2, 5] 147 ], 148 [ 149 [2, 5], [2, 6] 150 ], 151 [ 152 [2, 5], [2, 7] 153 ] 154 ] 155 let buff = createLiteralBufferArray(expectedBuffValues); 156 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 157 }); 158 159 it("test array with multi same class", function() { 160 let fileNames = 'tests/types/array/array_multi_same_class.ts'; 161 let result = compileTsWithType(fileNames); 162 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 163 let locals = functionPg!.getLocals(); 164 // check vreg 165 let extectedVRegTypePair = [ 166 ["#3#A", shift + 1], 167 ["#4#arrayA", shift + 3], 168 ["#5#arrayB", shift + 3], 169 ] 170 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 171 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 172 173 // check liberalBuffer 174 let expectedBuffValues = [ 175 [ 176 [2, 0], [2, 3], [2, 0] 177 ], 178 [ 179 [2, 1], [2, 0], [2, 0], [2, 0], 180 [2, 0], [2, 0], [2, 0], [2, 0] 181 ], 182 [ 183 [2, 2], [2, 51] 184 ], 185 [ 186 [2, 5], [2, 52] 187 ] 188 ] 189 let buff = createLiteralBufferArray(expectedBuffValues); 190 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 191 }); 192 193 it("test array with union", function() { 194 let fileNames = 'tests/types/array/array_union.ts'; 195 let result = compileTsWithType(fileNames); 196 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 197 let locals = functionPg!.getLocals(); 198 // check vreg 199 let extectedVRegTypePair = [ 200 ["#3#a", shift + 2], 201 ["#4#b", shift + 2], 202 ] 203 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 204 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 205 206 // check liberalBuffer 207 let expectedBuffValues = [ 208 [ 209 [2, 0], [2, 2], [2, 0] 210 ], 211 [ 212 [2, 4], [2, 2], [2, 4], [2, 1], 213 ], 214 [ 215 [2, 5], [2, 51] 216 ] 217 ] 218 let buff = createLiteralBufferArray(expectedBuffValues); 219 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 220 }); 221 222 it("test array with object", function() { 223 let fileNames = 'tests/types/array/array_object.ts'; 224 let result = compileTsWithType(fileNames); 225 let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); 226 let locals = functionPg!.getLocals(); 227 // check vreg 228 let extectedVRegTypePair = [ 229 ["#3#a", shift + 2], 230 ] 231 let vreg2TypeMap = createVRegTypePair(extectedVRegTypePair); 232 expect(compareVReg2Type(vreg2TypeMap, locals), "check vreg typeInfo").to.be.true; 233 234 // check liberalBuffer 235 let expectedBuffValues = [ 236 [ 237 [2, 0], [2, 2], [2, 0] 238 ], 239 [ 240 [2, 6], [2, 2], [5, "element1"], [2, 1], 241 [5, "element2"], [2, 4], 242 ], 243 [ 244 [2, 5], [2, 51] 245 ] 246 ] 247 let buff = createLiteralBufferArray(expectedBuffValues); 248 expect(compareLiteralBuffer(buff, result.literalBufferArray), "check literal buffer").to.be.true; 249 }); 250}); 251