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 */ 15import LightWeightMap from "@ohos.util.LightWeightMap"; 16import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' 17export default function LightWeightMapTest() { 18describe("LightWeightMapTest", function () { 19 20 /** 21 * @tc.name: testConstructor001 22 * @tc.desc: Create an LightWeightMap instance. For example: let lightWeightMap = new LightWeightMap(). 23 */ 24 it("testConstructor001", 0, function () { 25 try { 26 let lightWeightMap = new LightWeightMap(); 27 expect(lightWeightMap != undefined).assertEqual(true); 28 } catch (err) { 29 expect(err.name).assertEqual("BusinessError"); 30 expect(err.code).assertEqual(10200012); 31 expect(err.message).assertEqual("The LightWeightMap's constructor cannot be directly invoked"); 32 } 33 }); 34 35 /** 36 * @tc.name: testSet002 37 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set(1, "A"). 38 */ 39 it("testSet002", 0, function () { 40 let lightWeightMap = new LightWeightMap(); 41 lightWeightMap.set(1, "A"); 42 let res = lightWeightMap.hasValue("A"); 43 expect(res).assertEqual(true); 44 let res1 = lightWeightMap.hasKey(1); 45 expect(res1).assertEqual(true); 46 }); 47 48 /** 49 * @tc.name: testSet003 50 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set("a", "A"). 51 */ 52 it("testSet003", 0, function () { 53 let lightWeightMap = new LightWeightMap(); 54 lightWeightMap.set("a", "A"); 55 let res = lightWeightMap.hasValue("A"); 56 expect(res).assertEqual(true); 57 let res1 = lightWeightMap.hasKey("a"); 58 expect(res1).assertEqual(true); 59 }); 60 61 /** 62 * @tc.name: testSet004 63 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set("a", "A"). 64 */ 65 it("testSet004", 0, function () { 66 let lightWeightMap = new LightWeightMap(); 67 let a = [1, 2, 3, 4]; 68 lightWeightMap.set(1, a); 69 let res = lightWeightMap.hasValue(a); 70 expect(res).assertEqual(true); 71 let res1 = lightWeightMap.hasKey(1); 72 expect(res1).assertEqual(true); 73 }); 74 75 /** 76 * @tc.name: testSet005 77 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 78 */ 79 it("testSet005", 0, function () { 80 let lightWeightMap = new LightWeightMap(); 81 let c = {name: "lili", age: "13"}; 82 lightWeightMap.set(1, c); 83 let res = lightWeightMap.hasValue(c); 84 expect(res).assertEqual(true); 85 let res1 = lightWeightMap.hasKey(1); 86 expect(res1).assertEqual(true); 87 }); 88 89 /** 90 * @tc.name: testGet006 91 * @tc.desc: Get the corresponding value through the key. For example: lightWeightMap.get(4). 92 */ 93 it("testGet006", 0, function () { 94 let lightWeightMap = new LightWeightMap(); 95 lightWeightMap.set(1, "A"); 96 lightWeightMap.set(2, "B"); 97 lightWeightMap.set(3, "C"); 98 lightWeightMap.set(4, "D"); 99 lightWeightMap.set(5, "E"); 100 let res = lightWeightMap.get(4); 101 expect(res).assertEqual("D"); 102 }); 103 104 /** 105 * @tc.name: testLength007 106 * @tc.desc: Get the number of key value pairs in the lightWeightMap instance. For example: lightWeightMap.length. 107 */ 108 it("testLength007", 0, function () { 109 let lightWeightMap = new LightWeightMap(); 110 lightWeightMap.set(1, "A"); 111 lightWeightMap.set(2, "B"); 112 lightWeightMap.set(3, "C"); 113 lightWeightMap.set(4, "D"); 114 lightWeightMap.set(5, "E"); 115 let res = lightWeightMap.length; 116 expect(res).assertEqual(5); 117 }); 118 119 /** 120 * @tc.name: testHasAll008 121 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 122 */ 123 it("testHasAll008", 0, function () { 124 let lightWeightMap = new LightWeightMap(); 125 lightWeightMap.set("a", "A"); 126 lightWeightMap.set("b", "B"); 127 lightWeightMap.set("c", "C"); 128 lightWeightMap.set("d", "D"); 129 lightWeightMap.set("e", "E"); 130 let lightWeightMap1 = new LightWeightMap(); 131 lightWeightMap1.set("a", "A"); 132 lightWeightMap1.set("d", "D"); 133 let res = lightWeightMap.hasAll(lightWeightMap1); 134 expect(res).assertEqual(true); 135 }); 136 137 /** 138 * @tc.name: testConstructor001 139 * @tc.desc: Determine whether the LightWeightMap contains the specified key. For example: lightWeightMap.hasKey("a"). 140 */ 141 it("testHasKey009", 0, function () { 142 let lightWeightMap = new LightWeightMap(); 143 lightWeightMap.set("a", "A"); 144 lightWeightMap.set("b", "B"); 145 lightWeightMap.set("c", "C"); 146 lightWeightMap.set("d", "D"); 147 lightWeightMap.set("e", "E"); 148 let res = lightWeightMap.hasKey("a"); 149 expect(res).assertEqual(true); 150 let res1 = lightWeightMap.hasKey(1); 151 expect(res1).assertEqual(false); 152 }); 153 154 /** 155 * @tc.name: testHasValue010 156 * @tc.desc: Determine whether the LightWeightMap contains the specified value. 157 * For example: lightWeightMap.hasValue("A"). 158 */ 159 it("testHasValue010", 0, function () { 160 let lightWeightMap = new LightWeightMap(); 161 lightWeightMap.set("a", "A"); 162 lightWeightMap.set("b", "B"); 163 lightWeightMap.set("c", "C"); 164 lightWeightMap.set("d", "D"); 165 lightWeightMap.set("e", "E"); 166 let res = lightWeightMap.hasValue("A"); 167 expect(res).assertEqual(true); 168 let res1 = lightWeightMap.hasValue(1); 169 expect(res1).assertEqual(false); 170 }); 171 172 /** 173 * @tc.name: testIncreaseCapacityTo011 174 * @tc.desc: Expand the LightWeightMap instance capacity to the specified value. 175 * For example: lightWeightMap.increaseCapacityTo(3). 176 */ 177 it("testIncreaseCapacityTo011", 0, function () { 178 let lightWeightMap = new LightWeightMap(); 179 lightWeightMap.set("a", "A"); 180 lightWeightMap.set("b", "B"); 181 lightWeightMap.set("c", "C"); 182 lightWeightMap.set("d", "D"); 183 lightWeightMap.set("e", "E"); 184 lightWeightMap.increaseCapacityTo(3); 185 let length = lightWeightMap.length; 186 expect(length).assertEqual(5); 187 }); 188 189 /** 190 * @tc.name: testEntries012 191 * @tc.desc: Get all key value pairs collection in lightWeightMap. 192 */ 193 it("testEntries012", 0, function () { 194 let lightWeightMap = new LightWeightMap(); 195 lightWeightMap.set(1, "A"); 196 lightWeightMap.set(2, "B"); 197 lightWeightMap.set(3, "C"); 198 lightWeightMap.set(4, "D"); 199 lightWeightMap.set(5, "E"); 200 let res = lightWeightMap.entries(); 201 expect(JSON.stringify(res.next().value)).assertEqual('[1,"A"]'); 202 expect(JSON.stringify(res.next().value)).assertEqual('[2,"B"]'); 203 expect(JSON.stringify(res.next().value)).assertEqual('[3,"C"]'); 204 expect(JSON.stringify(res.next().value)).assertEqual('[4,"D"]'); 205 expect(JSON.stringify(res.next().value)).assertEqual('[5,"E"]'); 206 }); 207 208 /** 209 * @tc.number: SUB_COMMONLIBRARY_ETSUTILS_LIGHTWEIGHTMAP_01003 210 * @tc.name: testEntries013 211 * @tc.desc: Get all key value pairs collection in lightWeightMap. 212 * @tc.size: MediumTest 213 * @tc.type: Function 214 * @tc.level: Level 2 215 */ 216 it("testEntries013", 0, function () { 217 let lightWeightMap = new LightWeightMap(); 218 let res = lightWeightMap.entries(); 219 expect(undefined).assertEqual(res.next().value); 220 }); 221 222 /** 223 * @tc.name: testGetIndexOfKey013 224 * @tc.desc: Find the index of the key value pair according to the corresponding key. 225 * If no key is specified, return -1. 226 */ 227 it("testGetIndexOfKey013", 0, function () { 228 let lightWeightMap = new LightWeightMap(); 229 lightWeightMap.set(1, "A"); 230 lightWeightMap.set(2, "B"); 231 lightWeightMap.set(3, "C"); 232 lightWeightMap.set(4, "D"); 233 lightWeightMap.set(5, "E"); 234 let res = lightWeightMap.getIndexOfKey(2); 235 expect(res).assertEqual(1); 236 }); 237 238 /** 239 * @tc.number: SUB_COMMONLIBRARY_ETSUTILS_LIGHTWEIGHTMAP_01002 240 * @tc.name: testGetIndexOfKey014 241 * @tc.desc: Find the index of the key value pair according to the corresponding key. 242 * If no key is specified, return -1. 243 * @tc.size: MediumTest 244 * @tc.type: Function 245 * @tc.level: Level 2 246 */ 247 it("testGetIndexOfKey014", 0, function () { 248 let lightWeightMap = new LightWeightMap(); 249 lightWeightMap.set(1, "A"); 250 lightWeightMap.set(2, "B"); 251 lightWeightMap.set(3, "C"); 252 lightWeightMap.set(4, "D"); 253 lightWeightMap.set(5, "E"); 254 let res = lightWeightMap.getIndexOfKey(6); 255 expect(res).assertEqual(-1); 256 }); 257 258 /** 259 * @tc.name: testGetIndexOfValue014 260 * @tc.desc: Find the index of the key value pair according to the corresponding value. 261 * If no key is specified, return -1. 262 */ 263 it("testGetIndexOfValue014", 0, function () { 264 let lightWeightMap = new LightWeightMap(); 265 lightWeightMap.set(1, "A"); 266 lightWeightMap.set(2, "B"); 267 lightWeightMap.set(3, "C"); 268 lightWeightMap.set(4, "D"); 269 lightWeightMap.set(5, "E"); 270 lightWeightMap.setValueAt(4, "F"); 271 let res = lightWeightMap.getIndexOfValue("F"); 272 expect(res).assertEqual(4); 273 }); 274 275 /** 276 * @tc.number: SUB_COMMONLIBRARY_ETSUTILS_LIGHTWEIGHTMAP_01001 277 * @tc.name: testGetIndexOfValue015 278 * @tc.desc: Find the index of the key value pair according to the corresponding value. 279 * If no key is specified, return -1. 280 * @tc.size: MediumTest 281 * @tc.type: Function 282 * @tc.level: Level 2 283 */ 284 it("testGetIndexOfValue015", 0, function () { 285 let lightWeightMap = new LightWeightMap(); 286 lightWeightMap.set(1, "A"); 287 lightWeightMap.set(2, "B"); 288 lightWeightMap.set(3, "C"); 289 lightWeightMap.set(4, "D"); 290 lightWeightMap.set(5, "E"); 291 let res = lightWeightMap.getIndexOfValue("G"); 292 expect(res).assertEqual(-1); 293 }); 294 295 /** 296 * @tc.name: testIsEmpty015 297 * @tc.desc: Determine whether the LightWeightMap instance is empty. For example: lightWeightMap.isEmpty(). 298 */ 299 it("testIsEmpty015", 0, function () { 300 let lightWeightMap = new LightWeightMap(); 301 let res1 = lightWeightMap.isEmpty(); 302 expect(res1).assertEqual(true); 303 lightWeightMap.set(1, "A"); 304 lightWeightMap.set(2, "B"); 305 lightWeightMap.set(3, "C"); 306 lightWeightMap.set(4, "D"); 307 lightWeightMap.set(5, "E"); 308 let res2 = lightWeightMap.isEmpty(); 309 expect(res2).assertEqual(false); 310 }); 311 312 /** 313 * @tc.name: testGetKeyAt016 314 * @tc.desc: Find the key of the key value pair according to the corresponding index. 315 * For example: lightWeightMap.getKeyAt(1). 316 */ 317 it("testGetKeyAt016", 0, function () { 318 let lightWeightMap = new LightWeightMap(); 319 lightWeightMap.set(1, "A"); 320 lightWeightMap.set(2, "B"); 321 lightWeightMap.set(3, "C"); 322 lightWeightMap.set(4, "D"); 323 lightWeightMap.set(5, "E"); 324 let res = lightWeightMap.getKeyAt(1); 325 expect(res).assertEqual(2); 326 }); 327 328 /** 329 * @tc.name: testKeys017 330 * @tc.desc: Get a collection of all the keys in the LightWeightMap. For example: lightWeightMap.keys(). 331 */ 332 it("testKeys017", 0, function () { 333 let lightWeightMap = new LightWeightMap(); 334 lightWeightMap.set(1, "A"); 335 lightWeightMap.set(2, "B"); 336 lightWeightMap.set(3, "C"); 337 lightWeightMap.set(4, "D"); 338 lightWeightMap.set(5, "E"); 339 let res = lightWeightMap.keys(); 340 expect(res.next().value).assertEqual(1); 341 expect(res.next().value).assertEqual(2); 342 expect(res.next().value).assertEqual(3); 343 expect(res.next().value).assertEqual(4); 344 expect(res.next().value).assertEqual(5); 345 }); 346 347 /** 348 * @tc.name: testSetAll018 349 * @tc.desc: Copy key value pairs from one LightWeightMap to another. 350 */ 351 it("testSetAll018", 0, function () { 352 let lightWeightMap = new LightWeightMap(); 353 lightWeightMap.set(1, "A"); 354 lightWeightMap.set(2, "B"); 355 lightWeightMap.set(3, "C"); 356 lightWeightMap.set(4, "D"); 357 lightWeightMap.set(5, "E"); 358 let lightWeightMap1 = new LightWeightMap(); 359 lightWeightMap1.set(6, "A"); 360 lightWeightMap1.set(7, "B"); 361 lightWeightMap.setAll(lightWeightMap1); 362 for (let i = 1; i < 8; i++) { 363 expect(lightWeightMap.hasKey(i)).assertEqual(true); 364 } 365 }); 366 367 /** 368 * @tc.name: testRemove019 369 * @tc.desc: Delete key value pairs according to key. For example: lightWeightMap.remove(3). 370 */ 371 it("testRemove019", 0, function () { 372 let lightWeightMap = new LightWeightMap(); 373 lightWeightMap.set(1, "A"); 374 lightWeightMap.set(2, "B"); 375 lightWeightMap.set(3, "C"); 376 lightWeightMap.set(4, "D"); 377 lightWeightMap.set(5, "E"); 378 let res = lightWeightMap.remove(3); 379 expect(res).assertEqual("C"); 380 let res1 = lightWeightMap.hasValue("C"); 381 expect(res1).assertEqual(false); 382 }); 383 384 /** 385 * @tc.name: testRemoveAt020 386 * @tc.desc: Delete key value pairs according to index. For example: lightWeightMap.removeAt(1). 387 */ 388 it("testRemoveAt020", 0, function () { 389 let lightWeightMap = new LightWeightMap(); 390 lightWeightMap.set(1, "A"); 391 lightWeightMap.set(2, "B"); 392 lightWeightMap.set(3, "C"); 393 lightWeightMap.set(4, "D"); 394 lightWeightMap.set(5, "E"); 395 let res = lightWeightMap.removeAt(1); 396 expect(res).assertEqual(true); 397 let res1 = lightWeightMap.hasValue("A"); 398 expect(res1).assertEqual(true); 399 let res2 = lightWeightMap.hasValue("B"); 400 expect(res2).assertEqual(false); 401 let res3 = lightWeightMap.removeAt(10); 402 expect(res3).assertEqual(false); 403 }); 404 405 /** 406 * @tc.name: testClear021 407 * @tc.desc: Clear all key value pairs in LightWeightMap. For example: lightWeightMap.clear(). 408 */ 409 it("testClear021", 0, function () { 410 let lightWeightMap = new LightWeightMap(); 411 lightWeightMap.set(1, "A"); 412 lightWeightMap.set(2, "B"); 413 lightWeightMap.set(3, "C"); 414 lightWeightMap.set(4, "D"); 415 lightWeightMap.set(5, "E"); 416 lightWeightMap.clear(); 417 let res = lightWeightMap.length; 418 expect(res).assertEqual(0); 419 let isEmpty = lightWeightMap.isEmpty(); 420 expect(isEmpty).assertEqual(true); 421 }); 422 423 /** 424 * @tc.name: testSetValueAt022 425 * @tc.desc: Modify the value of the key value pair according to the corresponding index. 426 * For example: setValueAt(0, "a"). 427 */ 428 it("testSetValueAt022", 0, function () { 429 let lightWeightMap = new LightWeightMap(); 430 lightWeightMap.set(1, "A"); 431 lightWeightMap.set(2, "B"); 432 lightWeightMap.set(3, "C"); 433 lightWeightMap.set(4, "D"); 434 lightWeightMap.set(5, "E"); 435 lightWeightMap.setValueAt(0, "a"); 436 let res = lightWeightMap.get(1); 437 expect(res).assertEqual("a"); 438 }); 439 440 /** 441 * @tc.name: testForEach023 442 * @tc.desc: Traverse all key value pairs in the LightWeightMap instance. 443 */ 444 it("testForEach023", 0, function () { 445 let lightWeightMap = new LightWeightMap(); 446 lightWeightMap.set(1, "A"); 447 lightWeightMap.set(2, "B"); 448 lightWeightMap.set(3, "C"); 449 lightWeightMap.set(4, "D"); 450 lightWeightMap.set(5, "E"); 451 let arr = []; 452 lightWeightMap.forEach((value, index) => { 453 arr.push(value); 454 }); 455 let arr1 = ["A", "B", "C", "D", "E"]; 456 for (let i = 0; i < arr1.length; i++) { 457 expect(arr[i]).assertEqual(arr1[i]); 458 } 459 }); 460 461 /** 462 * @tc.name: testToString024 463 * @tc.desc: Use "," to splice the elements in the LightWeightMap instance into a string. 464 * For example: lightWeightMap.toString(). 465 */ 466 it("testToString024", 0, function () { 467 let lightWeightMap = new LightWeightMap(); 468 lightWeightMap.set(1, "A"); 469 lightWeightMap.set(2, "B"); 470 lightWeightMap.set(3, "C"); 471 let res = lightWeightMap.toString(); 472 expect(res).assertEqual("1:A,2:B,3:C"); 473 }); 474 475 /** 476 * @tc.name: testValues025 477 * @tc.desc: Get a collection of all the values in the LightWeightMap. For example: lightWeightMap.values(). 478 */ 479 it("testValues025", 0, function () { 480 let lightWeightMap = new LightWeightMap(); 481 lightWeightMap.set(1, "A"); 482 lightWeightMap.set(2, "B"); 483 lightWeightMap.set(3, "C"); 484 lightWeightMap.set(4, "D"); 485 lightWeightMap.set(5, "E"); 486 let res = lightWeightMap.values(); 487 expect(res.next().value).assertEqual("A"); 488 expect(res.next().value).assertEqual("B"); 489 expect(res.next().value).assertEqual("C"); 490 expect(res.next().value).assertEqual("D"); 491 expect(res.next().value).assertEqual("E"); 492 }); 493 494 /** 495 * @tc.name: testGetValueAt026 496 * @tc.desc: Get the value of the key value pair according to the corresponding index. 497 * For example: lightWeightMap.getValueAt(1). 498 */ 499 it("testGetValueAt026", 0, function () { 500 let lightWeightMap = new LightWeightMap(); 501 lightWeightMap.set(1, "A"); 502 lightWeightMap.set(2, "B"); 503 lightWeightMap.set(3, "C"); 504 lightWeightMap.set(4, "D"); 505 lightWeightMap.set(5, "E"); 506 let res = lightWeightMap.getValueAt(1); 507 expect(res).assertEqual("B"); 508 }); 509 510 /** 511 * @tc.name: testIterator027 512 * @tc.desc: Iterate over all key value pairs in the LightWeightMap. 513 */ 514 it("testIterator027", 0, function () { 515 let lightWeightMap = new LightWeightMap(); 516 lightWeightMap.set(1, "A"); 517 lightWeightMap.set(2, "B"); 518 lightWeightMap.set(3, "C"); 519 lightWeightMap.set(4, "D"); 520 lightWeightMap.set(5, "E"); 521 let iters = lightWeightMap[Symbol.iterator](); 522 let flag = true; 523 for (let i = 0, len = lightWeightMap.length; i < len; i++) { 524 let entry = iters.next().value; 525 let res = lightWeightMap.get(entry[0]); 526 if (res != entry[1]) { 527 flag = false; 528 break; 529 } 530 } 531 expect(flag).assertEqual(true); 532 }); 533 534 /** 535 * @tc.name: testSet028 536 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set(1, null). 537 */ 538 it("testSet028", 0, function () { 539 let lightWeightMap = new LightWeightMap(); 540 lightWeightMap.set(1, null); 541 let res = lightWeightMap.hasValue(null); 542 expect(res).assertEqual(true); 543 let res1 = lightWeightMap.hasKey(1); 544 expect(res1).assertEqual(true); 545 }); 546 547 /** 548 * @tc.name: testSet029 549 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set(null, null). 550 */ 551 it("testSet029", 0, function () { 552 let lightWeightMap = new LightWeightMap(); 553 lightWeightMap.set(null, null); 554 let res = lightWeightMap.hasValue(null); 555 expect(res).assertEqual(true); 556 let res1 = lightWeightMap.hasKey(null); 557 expect(res1).assertEqual(true); 558 }); 559 560 /** 561 * @tc.name: testSet030 562 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set(true, false). 563 */ 564 it("testSet030", 0, function () { 565 let lightWeightMap = new LightWeightMap(); 566 lightWeightMap.set(true, false); 567 let res = lightWeightMap.hasValue(false); 568 expect(res).assertEqual(true); 569 let res1 = lightWeightMap.hasKey(true); 570 expect(res1).assertEqual(true); 571 }); 572 573 /** 574 * @tc.name: testSet031 575 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set(1.23, 321). 576 */ 577 it("testSet031", 0, function () { 578 let lightWeightMap = new LightWeightMap(); 579 lightWeightMap.set(1.23, 321); 580 let res = lightWeightMap.hasValue(321); 581 expect(res).assertEqual(true); 582 let res1 = lightWeightMap.hasKey(1.23); 583 expect(res1).assertEqual(true); 584 }); 585 586 /** 587 * @tc.name: testHasAll032 588 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 589 */ 590 it("testHasAll032", 0, function () { 591 let lightWeightMap = new LightWeightMap(); 592 lightWeightMap.set("a", "A"); 593 lightWeightMap.set("b", "B"); 594 lightWeightMap.set("c", "C"); 595 lightWeightMap.set("d", "D"); 596 lightWeightMap.set("e", "E"); 597 let lightWeightMap1 = new LightWeightMap(); 598 lightWeightMap1.set("a1", "A1"); 599 lightWeightMap1.set("d1", "D1"); 600 let res = lightWeightMap.hasAll(lightWeightMap1); 601 expect(res).assertEqual(false); 602 }); 603 604 /** 605 * @tc.name: testHasAll033 606 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 607 */ 608 it("testHasAll033", 0, function () { 609 let lightWeightMap = new LightWeightMap(); 610 lightWeightMap.set("a", "A"); 611 lightWeightMap.set("b", "B"); 612 lightWeightMap.set("c", "C"); 613 lightWeightMap.set("d", "D"); 614 lightWeightMap.set("e", "E"); 615 let lightWeightMap1 = new LightWeightMap(); 616 lightWeightMap1.set("a", "A1"); 617 lightWeightMap1.set("d", "D1"); 618 let res = lightWeightMap.hasAll(lightWeightMap1); 619 expect(res).assertEqual(false); 620 }); 621 622 /** 623 * @tc.name: testRemove034 624 * @tc.desc: Delete key value pairs according to key. For example: lightWeightMap.remove(3). 625 */ 626 it("testRemove034", 0, function () { 627 let lightWeightMap = new LightWeightMap(); 628 let res = lightWeightMap.remove(3); 629 expect(res).assertEqual(undefined); 630 }); 631 632 /** 633 * @tc.name: testRemoveAt035 634 * @tc.desc: Delete key value pairs according to index. For example: lightWeightMap.removeAt(1). 635 */ 636 it("testRemoveAt035", 0, function () { 637 let lightWeightMap = new LightWeightMap(); 638 let res = lightWeightMap.removeAt(1); 639 expect(res).assertEqual(false); 640 }); 641 642 /** 643 * @tc.name: testIncreaseCapacityTo036 644 * @tc.desc: Expand the LightWeightMap instance capacity to the specified value. 645 * For example: lightWeightMap.increaseCapacityTo(10). 646 */ 647 it("testIncreaseCapacityTo036", 0, function () { 648 let lightWeightMap = new LightWeightMap(); 649 lightWeightMap.set("a", "A"); 650 lightWeightMap.set("b", "B"); 651 lightWeightMap.set("c", "C"); 652 lightWeightMap.set("d", "D"); 653 lightWeightMap.set("e", "E"); 654 let res = lightWeightMap.increaseCapacityTo(10); 655 expect(res).assertEqual(undefined); 656 }); 657 658 /** 659 * @tc.name: testIncreaseCapacityTo037 660 * @tc.desc: Expand the LightWeightMap instance capacity to the specified value. 661 * For example: lightWeightMap.increaseCapacityTo("qwe"). 662 */ 663 it("testIncreaseCapacityTo037", 0, function () { 664 let lightWeightMap = new LightWeightMap(); 665 lightWeightMap.set("a", "A"); 666 lightWeightMap.set("b", "B"); 667 lightWeightMap.set("c", "C"); 668 lightWeightMap.set("d", "D"); 669 lightWeightMap.set("e", "E"); 670 try { 671 lightWeightMap.increaseCapacityTo("qwe"); 672 expect(true).assertEqual(false); 673 } catch (err) { 674 expect(err.name).assertEqual("BusinessError"); 675 expect(err.code).assertEqual(401); 676 expect(err.message).assertEqual(`The type of "minimumCapacity" must be small integer. Received value is: qwe`); 677 } 678 }); 679 680 /** 681 * @tc.name: testRemoveAt038 682 * @tc.desc: Delete key value pairs according to index. For example: lightWeightMap.removeAt("a"). 683 */ 684 it("testRemoveAt038", 0, function () { 685 let lightWeightMap = new LightWeightMap(); 686 try { 687 let res = lightWeightMap.removeAt("a"); 688 expect(true).assertEqual(false); 689 } catch (err) { 690 expect(err.name).assertEqual("BusinessError"); 691 expect(err.code).assertEqual(401); 692 expect(err.message).assertEqual(`The type of "index" must be small integer. Received value is: a`); 693 } 694 }); 695 696 /** 697 * @tc.name: testGetValueAt039 698 * @tc.desc: Get the value of the key value pair according to the corresponding index. 699 * For example: lightWeightMap.getValueAt("a"). 700 */ 701 it("testGetValueAt039", 0, function () { 702 let lightWeightMap = new LightWeightMap(); 703 lightWeightMap.set(1, "A"); 704 lightWeightMap.set(2, "B"); 705 lightWeightMap.set(3, "C"); 706 lightWeightMap.set(4, "D"); 707 lightWeightMap.set(5, "E"); 708 try { 709 let res = lightWeightMap.getValueAt("a"); 710 expect(true).assertEqual(false); 711 } catch (err) { 712 expect(err.name).assertEqual("BusinessError"); 713 expect(err.code).assertEqual(401); 714 expect(err.message).assertEqual(`The type of "index" must be small integer. Received value is: a`); 715 } 716 }); 717 718 /** 719 * @tc.name: testGetKeyAt040 720 * @tc.desc: Find the key of the key value pair according to the corresponding index. 721 * For example: lightWeightMap.getKeyAt("a"). 722 */ 723 it("testGetKeyAt040", 0, function () { 724 let lightWeightMap = new LightWeightMap(); 725 lightWeightMap.set(1, "A"); 726 lightWeightMap.set(2, "B"); 727 lightWeightMap.set(3, "C"); 728 lightWeightMap.set(4, "D"); 729 lightWeightMap.set(5, "E"); 730 try { 731 let res = lightWeightMap.getKeyAt("a"); 732 expect(true).assertEqual(false); 733 } catch (err) { 734 expect(err.name).assertEqual("BusinessError"); 735 expect(err.code).assertEqual(401); 736 expect(err.message).assertEqual(`The type of "index" must be small integer. Received value is: a`); 737 } 738 }); 739 740 /** 741 * @tc.name: testConstructor001 742 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 743 */ 744 it("testHasAll041", 0, function () { 745 let lightWeightMap = new LightWeightMap(); 746 let lightWeightMap1 = new LightWeightMap(); 747 let res = lightWeightMap.hasAll(lightWeightMap1); 748 expect(res).assertEqual(true); 749 }); 750 751 /** 752 * @tc.name: testSet042 753 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 754 */ 755 it("testSet042", 0, function () { 756 let lightWeightMap = new LightWeightMap(); 757 for (let i = 0; i < 100; i++) { 758 lightWeightMap.set(i, "A"); 759 } 760 let res = lightWeightMap.get(99); 761 expect(res).assertEqual("A"); 762 let res1 = lightWeightMap.length; 763 expect(res1).assertEqual(100); 764 }); 765 766 /** 767 * @tc.name: testSet043 768 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 769 */ 770 it("testSet043", 0, function () { 771 let lightWeightMap = new LightWeightMap(); 772 for (let i = 0; i < 100; i++) { 773 lightWeightMap.set(1, i); 774 } 775 let res = lightWeightMap.get(1); 776 expect(res).assertEqual(99); 777 let res1 = lightWeightMap.length; 778 expect(res1).assertEqual(1); 779 }); 780 781 /** 782 * @tc.name: testSet044 783 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 784 */ 785 it("testSet044", 0, function () { 786 let lightWeightMap = new LightWeightMap(); 787 for (let i = 0; i < 10000; i++) { 788 lightWeightMap.set(i, i); 789 } 790 let res1 = lightWeightMap.length; 791 expect(res1).assertEqual(10000); 792 for (let index = 0; index < 10000; index++) { 793 let resKey = lightWeightMap.hasKey(index); 794 expect(resKey).assertEqual(true); 795 let resValue = lightWeightMap.hasValue(index); 796 expect(resValue).assertEqual(true); 797 } 798 }); 799 800 /** 801 * @tc.name: testSet045 802 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 803 */ 804 it("testSet045", 0, function () { 805 let lightWeightMap = new LightWeightMap(); 806 for (let i = 0; i < 5000; i++) { 807 lightWeightMap.set(i, i); 808 } 809 for (let i = -1; i > -5001; i--) { 810 lightWeightMap.set(i, i); 811 } 812 let res1 = lightWeightMap.length; 813 expect(res1).assertEqual(10000); 814 for (let index = 0; index < 5000; index++) { 815 let resKey = lightWeightMap.hasKey(index); 816 expect(resKey).assertEqual(true); 817 let resValue = lightWeightMap.hasValue(index); 818 expect(resValue).assertEqual(true); 819 } 820 for (let i = -1; i > -5001; i--) { 821 let resKey = lightWeightMap.hasKey(i); 822 expect(resKey).assertEqual(true); 823 let resValue = lightWeightMap.hasValue(i); 824 expect(resValue).assertEqual(true); 825 } 826 }); 827 828 /** 829 * @tc.name: testSet046 830 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 831 */ 832 it("testSet046", 0, function () { 833 let lightWeightMap = new LightWeightMap(); 834 lightWeightMap.set(true, 0.001); 835 let res = lightWeightMap.hasValue(0.001); 836 expect(res).assertEqual(true); 837 let res1 = lightWeightMap.hasKey(true); 838 expect(res1).assertEqual(true); 839 }); 840 841 /** 842 * @tc.name: testSet047 843 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 844 */ 845 it("testSet047", 0, function () { 846 let lightWeightMap = new LightWeightMap(); 847 let a = [1, 2, 3, 4]; 848 lightWeightMap.set(a, 1); 849 let res = lightWeightMap.hasValue(1); 850 expect(res).assertEqual(true); 851 let res1 = lightWeightMap.hasKey(a); 852 expect(res1).assertEqual(true); 853 }); 854 855 /** 856 * @tc.name: testSet048 857 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 858 */ 859 it("testSet048", 0, function () { 860 let lightWeightMap = new LightWeightMap(); 861 let a = {abc: 1}; 862 lightWeightMap.set(a, ""); 863 let res = lightWeightMap.hasValue(""); 864 expect(res).assertEqual(true); 865 let res1 = lightWeightMap.hasKey(a); 866 expect(res1).assertEqual(true); 867 }); 868 869 /** 870 * @tc.name: testGet049 871 * @tc.desc: Get the corresponding value through the key. For example: lightWeightMap.get(10). 872 */ 873 it("testGet049", 0, function () { 874 let lightWeightMap = new LightWeightMap(); 875 lightWeightMap.set(1, "A"); 876 lightWeightMap.set(2, "B"); 877 lightWeightMap.set(3, "C"); 878 lightWeightMap.set(4, "D"); 879 lightWeightMap.set(5, "E"); 880 let res = lightWeightMap.get(10); 881 expect(res).assertEqual(undefined); 882 }); 883 884 /** 885 * @tc.name: testHasAll050 886 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 887 */ 888 it("testHasAll050", 0, function () { 889 let lightWeightMap = new LightWeightMap(); 890 lightWeightMap.set("a", "A"); 891 lightWeightMap.set("b", "B"); 892 lightWeightMap.set("c", "C"); 893 lightWeightMap.set("d", "D"); 894 lightWeightMap.set("e", "E"); 895 let lightWeightMap1 = new LightWeightMap(); 896 lightWeightMap1.set("a", "A"); 897 lightWeightMap1.set("d", "D1"); 898 let res = lightWeightMap.hasAll(lightWeightMap1); 899 expect(res).assertEqual(false); 900 }); 901 902 /** 903 * @tc.name: testHasAll051 904 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 905 */ 906 it("testHasAll051", 0, function () { 907 let lightWeightMap = new LightWeightMap(); 908 try { 909 lightWeightMap.hasAll([1, 2, 3]); 910 expect(true).assertEqual(false); 911 } catch (err) { 912 expect(err.name).assertEqual("BusinessError"); 913 expect(err.code).assertEqual(401); 914 expect(err.message).assertEqual(`The type of "map" must be LightWeightMap. Received value is: 1,2,3`); 915 } 916 }); 917 918 /** 919 * @tc.name: testGetKeyAt052 920 * @tc.desc: Find the key of the key value pair according to the corresponding index. 921 * For example: lightWeightMap.getKeyAt(6). 922 */ 923 it("testGetKeyAt052", 0, function () { 924 let lightWeightMap = new LightWeightMap(); 925 lightWeightMap.set(1, "A"); 926 lightWeightMap.set(2, "B"); 927 lightWeightMap.set(3, "C"); 928 lightWeightMap.set(4, "D"); 929 lightWeightMap.set(5, "E"); 930 try { 931 let res = lightWeightMap.getKeyAt(6); 932 expect(true).assertEqual(false); 933 } catch (err) { 934 expect(err.name).assertEqual("BusinessError"); 935 expect(err.code).assertEqual(10200001); 936 expect(err.message).assertEqual(`The value of "index" is out of range. It must be >= 0 && <= 4. Received value is: 6`); 937 } 938 }); 939 940 /** 941 * @tc.name: testSetAll053 942 * @tc.desc: Copy key value pairs from one LightWeightMap to another. 943 */ 944 it("testSetAll053", 0, function () { 945 let lightWeightMap = new LightWeightMap(); 946 lightWeightMap.set(1, "A"); 947 lightWeightMap.set(2, "B"); 948 try { 949 lightWeightMap.setAll([1, 2, 3]); 950 expect(true).assertEqual(false); 951 } catch (err) { 952 expect(err.name).assertEqual("BusinessError"); 953 expect(err.code).assertEqual(401); 954 expect(err.message).assertEqual(`The type of "map" must be LightWeightMap. Received value is: 1,2,3`); 955 } 956 }); 957 958 /** 959 * @tc.name: testSetValueAt054 960 * @tc.desc: Modify the value of the key value pair according to the corresponding index. 961 * For example: lightWeightMap.setValueAt("a", "a"). 962 */ 963 it("testSetValueAt054", 0, function () { 964 let lightWeightMap = new LightWeightMap(); 965 lightWeightMap.set(1, "A"); 966 lightWeightMap.set(2, "B"); 967 try { 968 lightWeightMap.setValueAt("a", "a"); 969 expect(true).assertEqual(false); 970 } catch (err) { 971 expect(err.name).assertEqual("BusinessError"); 972 expect(err.code).assertEqual(401); 973 expect(err.message).assertEqual(`The type of "index" must be small integer. Received value is: a`); 974 } 975 }); 976 977 /** 978 * @tc.name: testSetValueAt055 979 * @tc.desc: Modify the value of the key value pair according to the corresponding index. 980 * For example: lightWeightMap.setValueAt(3, "a"). 981 */ 982 it("testSetValueAt055", 0, function () { 983 let lightWeightMap = new LightWeightMap(); 984 lightWeightMap.set(1, "A"); 985 lightWeightMap.set(2, "B"); 986 try { 987 lightWeightMap.setValueAt(3, "a"); 988 expect(true).assertEqual(false); 989 } catch (err) { 990 expect(err.name).assertEqual("BusinessError"); 991 expect(err.code).assertEqual(10200001); 992 expect(err.message).assertEqual(`The value of "index" is out of range. It must be >= 0 && <= 1. Received value is: 3`); 993 } 994 }); 995 996 /** 997 * @tc.name: testGetValueAt056 998 * @tc.desc: Get the value of the key value pair according to the corresponding index. 999 * For example: lightWeightMap.getValueAt(6). 1000 */ 1001 it("testGetValueAt056", 0, function () { 1002 let lightWeightMap = new LightWeightMap(); 1003 lightWeightMap.set(1, "A"); 1004 lightWeightMap.set(2, "B"); 1005 lightWeightMap.set(3, "C"); 1006 lightWeightMap.set(4, "D"); 1007 lightWeightMap.set(5, "E"); 1008 try { 1009 lightWeightMap.getValueAt(6); 1010 expect(true).assertEqual(false); 1011 } catch (err) { 1012 expect(err.name).assertEqual("BusinessError"); 1013 expect(err.code).assertEqual(10200001); 1014 expect(err.message).assertEqual(`The value of "index" is out of range. It must be >= 0 && <= 4. Received value is: 6`); 1015 } 1016 }); 1017}); 1018} 1019