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 16/* 17 * @tc.name:container 18 * @tc.desc:test container 19 * @tc.type: FUNC 20 * @tc.require: issueI5NO8G 21 */ 22var LinkedList = undefined; 23if (globalThis["ArkPrivate"] != undefined) { 24 LinkedList = ArkPrivate.Load(ArkPrivate.LinkedList); 25 let list = new LinkedList(); 26 let testArray = [] 27 let map = new Map(); 28 for(let i = 0; i<10; i++) { 29 list.add(i) 30 testArray.push(i) 31 } 32 map.set("test linkedlist has:", list.has(8)) 33 map.set("test linkedlist not has:", list.has(2)) 34 map.set("test linkedlist getLastIndexOf:", list.getLastIndexOf(1) === 1) 35 map.set("test linkedlist getIndexOf:", list.getIndexOf(5) === 5) 36 37 list.removeByIndex(9) 38 39 testArray.splice(9, 1) 40 let res = true 41 for(let i = 0; i < testArray.length; i++) { 42 if (list[i] !== testArray[i]) { 43 res = false 44 } 45 } 46 map.set("test linkedlist removeByIndex:", res) 47 48 const removeRes = list.remove(8) 49 testArray.splice(8, 1) 50 res = true 51 for(let i = 0; i < testArray.length; i++) { 52 if (list[i] !== testArray[i]) { 53 res = false 54 } 55 } 56 map.set("test linkedlist remove:", res) 57 map.set("test linkedlist remove1:", removeRes) 58 map.set("test linkedlist getFirst:", list.getFirst() === 0) 59 map.set("test linkedlist getLast:", list.getLast() === 7) 60 61 list.insert(3, 999) 62 testArray.splice(3, 0, 999) 63 res = true 64 for(let i = 0; i < testArray.length; i++) { 65 if (list[i] !== testArray[i]) { 66 res = false 67 } 68 } 69 map.set("test linkedlist insert:", res) 70 71 list.set(5, 888) 72 testArray[5] = 888 73 res = true 74 for(let i = 0; i < testArray.length; i++) { 75 if (list[i] !== testArray[i]) { 76 res = false 77 } 78 } 79 map.set("test linkedlist set:", res) 80 81 let cloneList = list.clone() 82 res = true 83 if (list.length !== cloneList.length) { 84 res = false 85 } 86 for(let i = 0; i < cloneList.length; i++) { 87 if (list[i] !== cloneList[i]) { 88 res = false 89 } 90 } 91 map.set("test linkedlist clone:", res) 92 93 list.addFirst(1111) 94 map.set("test linkedlist addfirst:", list.getFirst() === 1111) 95 96 const removefirstres = list.removeFirst() 97 map.set("test linkedlist removeFirst:", removefirstres === 1111) 98 99 res = true 100 let i = 0 101 for (const data of list) { 102 if (data !== testArray[i]) { 103 res = false 104 } 105 i++; 106 } 107 map.set("test linkedlist intertor:", res) 108 109 let list1 = new LinkedList(); 110 let testArray1 = [] 111 for (let i = 0; i < 10; i++) { 112 list1.add(i) 113 testArray1.push(i) 114 } 115 116 res = true 117 list1.forEach((i, d) => { 118 if (d !== testArray1[i]) { 119 res = false 120 } 121 }) 122 123 map.set("test linkedlist forEach:", res) 124 list1.clear() 125 map.set("test linkedlist clear:", list1.length === 0) 126 for (let i = 0; i < 10; i++) { 127 list1.add(i) 128 } 129 130 res = true 131 list1.forEach((i, d) => { 132 if (d !== testArray1[i]) { 133 res = false 134 } 135 }) 136 map.set("test linkedlist clear and add:", res) 137 map.set("test linkedlist get:", list.get(1232) === undefined) 138 map.set("test linkedlist getLastIndexOf:", list.getLastIndexOf('abc') === -1) 139 let flag = false; 140 try { 141 list.removeByIndex(99) 142 } catch (error) { 143 flag = true; 144 } 145 map.set("test linkedlist removeByIndex:", flag) 146 147 testArray.splice(5, 1) 148 149 res = true 150 const arr = list.convertToArray() 151 for (let i = 1; i < arr.length; i++) { 152 if (arr[i] !== testArray[i]) { 153 res = false 154 } 155 } 156 map.set("test linkedlist convertToArray:", res) 157 158 let list2 = new LinkedList(); 159 let proxy = new Proxy(list2, {}); 160 let testArray2 = [] 161 for(let i = 0; i<10; i++) { 162 proxy.add(i) 163 testArray2.push(i) 164 } 165 map.set("test linkedlist has:", proxy.has(8)) 166 map.set("test linkedlist not has:", proxy.has(2)) 167 map.set("test linkedlist getLastIndexOf:", proxy.getLastIndexOf(1) === 1) 168 map.set("test linkedlist getIndexOf:", proxy.getIndexOf(5) === 5) 169 170 proxy.removeByIndex(9) 171 172 testArray2.splice(9, 1) 173 res = true 174 for(let i = 0; i < testArray2.length; i++) { 175 if (proxy[i] !== testArray2[i]) { 176 res = false 177 } 178 } 179 map.set("test linkedlist removeByIndex:", res) 180 181 const removeRes1 = proxy.remove(8) 182 testArray2.splice(8, 1) 183 res = true 184 for(let i = 0; i < testArray2.length; i++) { 185 if (proxy[i] !== testArray2[i]) { 186 res = false 187 } 188 } 189 map.set("test linkedlist remove:", res) 190 map.set("test linkedlist remove1:", removeRes1) 191 map.set("test linkedlist getFirst:", proxy.getFirst() === 0) 192 map.set("test linkedlist getLast:", proxy.getLast() === 7) 193 194 proxy.insert(3, 999) 195 testArray2.splice(3, 0, 999) 196 res = true 197 for(let i = 0; i < testArray2.length; i++) { 198 if (proxy[i] !== testArray2[i]) { 199 res = false 200 } 201 } 202 map.set("test linkedlist insert:", res) 203 204 proxy.set(5, 888) 205 testArray2[5] = 888 206 res = true 207 for(let i = 0; i < testArray2.length; i++) { 208 if (proxy[i] !== testArray2[i]) { 209 res = false 210 } 211 } 212 map.set("test linkedlist set:", res) 213 214 let cloneList2 = proxy.clone() 215 res = true 216 if (proxy.length !== cloneList2.length) { 217 res = false 218 } 219 for(let i = 0; i < cloneList.length; i++) { 220 if (proxy[i] !== cloneList2[i]) { 221 res = false 222 } 223 } 224 map.set("test linkedlist clone:", res) 225 226 proxy.addFirst(1111) 227 map.set("test linkedlist addfirst:", proxy.getFirst() === 1111) 228 229 const removefirstres1 = proxy.removeFirst() 230 map.set("test linkedlist removeFirst:", removefirstres1 === 1111) 231 232 let removeList = new LinkedList() 233 let removeProxy = new Proxy(removeList, {}) 234 for(let i = 0; i < 10; i++) { 235 removeProxy.add(i) 236 } 237 for(let i = 9; i >= 0; i--) { 238 removeProxy.add(i) 239 } 240 let removeResult = removeProxy.removeFirstFound(2) 241 res = true 242 if (removeResult !== true || removeProxy.getIndexOf(2) !== 16) { 243 res = false 244 } 245 map.set("test linkedlist removeFirstFound:", res) 246 247 removeResult = removeProxy.removeLast() 248 res = true 249 if (removeResult !== 0 || removeProxy.getLastIndexOf(0) !== 0) { 250 res = false 251 } 252 map.set("test linkedlist removeLast:", res) 253 254 removeResult = removeProxy.removeLastFound(3) 255 res = true 256 if (removeResult !== true || removeProxy.getIndexOf(3) !== 2) { 257 res = false 258 } 259 map.set("test linkedlist removeLastFound:", res) 260 261 262 res = true 263 let j = 0 264 for (const data of proxy) { 265 if (data !== testArray2[j]) { 266 res = false 267 } 268 j++; 269 } 270 271 let itr = proxy[Symbol.iterator](); 272 let tmp = undefined; 273 let arr3 = [] 274 do { 275 tmp = itr.next().value; 276 arr3.push(tmp); 277 } while (tmp != undefined); 278 279 for (let k = 0; k < proxy.length; k++) { 280 if (arr3[k] !== testArray2[k]) { 281 res = false 282 } 283 } 284 285 map.set("test linkedlist intertor:", res) 286 287 let list3 = new LinkedList(); 288 let proxy1 = new Proxy(list3, {}); 289 let testArray3 = [] 290 for (let i = 0; i < 10; i++) { 291 proxy1.add(i) 292 testArray3.push(i) 293 } 294 295 res = true 296 proxy1.forEach((i, d) => { 297 if (d !== testArray3[i]) { 298 res = false 299 } 300 }) 301 302 map.set("test linkedlist forEach:", res) 303 proxy1.clear() 304 map.set("test linkedlist clear:", proxy1.length === 0) 305 map.set("test linkedlist get:", proxy1.get(1232) === undefined) 306 map.set("test linkedlist getLastIndexOf:", proxy1.getLastIndexOf('abc') === -1) 307 flag = false; 308 try { 309 proxy1.removeByIndex(99) 310 } catch (error) { 311 flag = true; 312 } 313 map.set("test linkedlist removeByIndex:", flag) 314 315 testArray3.splice(5, 1) 316 317 res = true 318 const arr1 = proxy1.convertToArray() 319 for (let i = 1; i < arr1.length; i++) { 320 if (arr1[i] !== testArray3[i]) { 321 res = false 322 } 323 } 324 map.set("test linkedlist convertToArray:", res) 325 326 flag = undefined; 327 function elements(value, key, map) { 328 if (!value) { 329 if (!flag) { 330 flag = []; 331 } 332 flag.push(key); 333 } 334 } 335 map.forEach(elements); 336 337 let de = new LinkedList(); 338 try { 339 de.forEach(123); 340 } catch(err) { 341 if (err.name != "BusinessError") { 342 print("LinkedList forEach throw error fail"); 343 } 344 } 345 if (!flag) { 346 print("Test LinkedList success!!!"); 347 } else { 348 print("Test LinkedList fail: " + flag); 349 } 350}