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.name: testGetIndexOfKey013 210 * @tc.desc: Find the index of the key value pair according to the corresponding key. 211 * If no key is specified, return -1. 212 */ 213 it("testGetIndexOfKey013", 0, function () { 214 let lightWeightMap = new LightWeightMap(); 215 lightWeightMap.set(1, "A"); 216 lightWeightMap.set(2, "B"); 217 lightWeightMap.set(3, "C"); 218 lightWeightMap.set(4, "D"); 219 lightWeightMap.set(5, "E"); 220 let res = lightWeightMap.getIndexOfKey(2); 221 expect(res).assertEqual(1); 222 }); 223 224 /** 225 * @tc.name: testGetIndexOfValue014 226 * @tc.desc: Find the index of the key value pair according to the corresponding value. 227 * If no key is specified, return -1. 228 */ 229 it("testGetIndexOfValue014", 0, function () { 230 let lightWeightMap = new LightWeightMap(); 231 lightWeightMap.set(1, "A"); 232 lightWeightMap.set(2, "B"); 233 lightWeightMap.set(3, "C"); 234 lightWeightMap.set(4, "D"); 235 lightWeightMap.set(5, "E"); 236 lightWeightMap.setValueAt(4, "F"); 237 let res = lightWeightMap.getIndexOfValue("F"); 238 expect(res).assertEqual(4); 239 }); 240 241 /** 242 * @tc.name: testIsEmpty015 243 * @tc.desc: Determine whether the LightWeightMap instance is empty. For example: lightWeightMap.isEmpty(). 244 */ 245 it("testIsEmpty015", 0, function () { 246 let lightWeightMap = new LightWeightMap(); 247 let res1 = lightWeightMap.isEmpty(); 248 expect(res1).assertEqual(true); 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 res2 = lightWeightMap.isEmpty(); 255 expect(res2).assertEqual(false); 256 }); 257 258 /** 259 * @tc.name: testGetKeyAt016 260 * @tc.desc: Find the key of the key value pair according to the corresponding index. 261 * For example: lightWeightMap.getKeyAt(1). 262 */ 263 it("testGetKeyAt016", 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 let res = lightWeightMap.getKeyAt(1); 271 expect(res).assertEqual(2); 272 }); 273 274 /** 275 * @tc.name: testKeys017 276 * @tc.desc: Get a collection of all the keys in the LightWeightMap. For example: lightWeightMap.keys(). 277 */ 278 it("testKeys017", 0, function () { 279 let lightWeightMap = new LightWeightMap(); 280 lightWeightMap.set(1, "A"); 281 lightWeightMap.set(2, "B"); 282 lightWeightMap.set(3, "C"); 283 lightWeightMap.set(4, "D"); 284 lightWeightMap.set(5, "E"); 285 let res = lightWeightMap.keys(); 286 expect(res.next().value).assertEqual(1); 287 expect(res.next().value).assertEqual(2); 288 expect(res.next().value).assertEqual(3); 289 expect(res.next().value).assertEqual(4); 290 expect(res.next().value).assertEqual(5); 291 }); 292 293 /** 294 * @tc.name: testSetAll018 295 * @tc.desc: Copy key value pairs from one LightWeightMap to another. 296 */ 297 it("testSetAll018", 0, function () { 298 let lightWeightMap = new LightWeightMap(); 299 lightWeightMap.set(1, "A"); 300 lightWeightMap.set(2, "B"); 301 lightWeightMap.set(3, "C"); 302 lightWeightMap.set(4, "D"); 303 lightWeightMap.set(5, "E"); 304 let lightWeightMap1 = new LightWeightMap(); 305 lightWeightMap1.set(6, "A"); 306 lightWeightMap1.set(7, "B"); 307 lightWeightMap.setAll(lightWeightMap1); 308 for (let i = 1; i < 8; i++) { 309 expect(lightWeightMap.hasKey(i)).assertEqual(true); 310 } 311 }); 312 313 /** 314 * @tc.name: testRemove019 315 * @tc.desc: Delete key value pairs according to key. For example: lightWeightMap.remove(3). 316 */ 317 it("testRemove019", 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.remove(3); 325 expect(res).assertEqual("C"); 326 let res1 = lightWeightMap.hasValue("C"); 327 expect(res1).assertEqual(false); 328 }); 329 330 /** 331 * @tc.name: testRemoveAt020 332 * @tc.desc: Delete key value pairs according to index. For example: lightWeightMap.removeAt(1). 333 */ 334 it("testRemoveAt020", 0, function () { 335 let lightWeightMap = new LightWeightMap(); 336 lightWeightMap.set(1, "A"); 337 lightWeightMap.set(2, "B"); 338 lightWeightMap.set(3, "C"); 339 lightWeightMap.set(4, "D"); 340 lightWeightMap.set(5, "E"); 341 let res = lightWeightMap.removeAt(1); 342 expect(res).assertEqual(true); 343 let res1 = lightWeightMap.hasValue("A"); 344 expect(res1).assertEqual(true); 345 let res2 = lightWeightMap.hasValue("B"); 346 expect(res2).assertEqual(false); 347 let res3 = lightWeightMap.removeAt(10); 348 expect(res3).assertEqual(false); 349 }); 350 351 /** 352 * @tc.name: testClear021 353 * @tc.desc: Clear all key value pairs in LightWeightMap. For example: lightWeightMap.clear(). 354 */ 355 it("testClear021", 0, function () { 356 let lightWeightMap = new LightWeightMap(); 357 lightWeightMap.set(1, "A"); 358 lightWeightMap.set(2, "B"); 359 lightWeightMap.set(3, "C"); 360 lightWeightMap.set(4, "D"); 361 lightWeightMap.set(5, "E"); 362 lightWeightMap.clear(); 363 let res = lightWeightMap.length; 364 expect(res).assertEqual(0); 365 let isEmpty = lightWeightMap.isEmpty(); 366 expect(isEmpty).assertEqual(true); 367 }); 368 369 /** 370 * @tc.name: testSetValueAt022 371 * @tc.desc: Modify the value of the key value pair according to the corresponding index. 372 * For example: setValueAt(0, "a"). 373 */ 374 it("testSetValueAt022", 0, function () { 375 let lightWeightMap = new LightWeightMap(); 376 lightWeightMap.set(1, "A"); 377 lightWeightMap.set(2, "B"); 378 lightWeightMap.set(3, "C"); 379 lightWeightMap.set(4, "D"); 380 lightWeightMap.set(5, "E"); 381 lightWeightMap.setValueAt(0, "a"); 382 let res = lightWeightMap.get(1); 383 expect(res).assertEqual("a"); 384 }); 385 386 /** 387 * @tc.name: testForEach023 388 * @tc.desc: Traverse all key value pairs in the LightWeightMap instance. 389 */ 390 it("testForEach023", 0, function () { 391 let lightWeightMap = new LightWeightMap(); 392 lightWeightMap.set(1, "A"); 393 lightWeightMap.set(2, "B"); 394 lightWeightMap.set(3, "C"); 395 lightWeightMap.set(4, "D"); 396 lightWeightMap.set(5, "E"); 397 let arr = []; 398 lightWeightMap.forEach((value, index) => { 399 arr.push(value); 400 }); 401 let arr1 = ["A", "B", "C", "D", "E"]; 402 for (let i = 0; i < arr1.length; i++) { 403 expect(arr[i]).assertEqual(arr1[i]); 404 } 405 }); 406 407 /** 408 * @tc.name: testToString024 409 * @tc.desc: Use "," to splice the elements in the LightWeightMap instance into a string. 410 * For example: lightWeightMap.toString(). 411 */ 412 it("testToString024", 0, function () { 413 let lightWeightMap = new LightWeightMap(); 414 lightWeightMap.set(1, "A"); 415 lightWeightMap.set(2, "B"); 416 lightWeightMap.set(3, "C"); 417 let res = lightWeightMap.toString(); 418 expect(res).assertEqual("1:A,2:B,3:C"); 419 }); 420 421 /** 422 * @tc.name: testValues025 423 * @tc.desc: Get a collection of all the values in the LightWeightMap. For example: lightWeightMap.values(). 424 */ 425 it("testValues025", 0, function () { 426 let lightWeightMap = new LightWeightMap(); 427 lightWeightMap.set(1, "A"); 428 lightWeightMap.set(2, "B"); 429 lightWeightMap.set(3, "C"); 430 lightWeightMap.set(4, "D"); 431 lightWeightMap.set(5, "E"); 432 let res = lightWeightMap.values(); 433 expect(res.next().value).assertEqual("A"); 434 expect(res.next().value).assertEqual("B"); 435 expect(res.next().value).assertEqual("C"); 436 expect(res.next().value).assertEqual("D"); 437 expect(res.next().value).assertEqual("E"); 438 }); 439 440 /** 441 * @tc.name: testGetValueAt026 442 * @tc.desc: Get the value of the key value pair according to the corresponding index. 443 * For example: lightWeightMap.getValueAt(1). 444 */ 445 it("testGetValueAt026", 0, function () { 446 let lightWeightMap = new LightWeightMap(); 447 lightWeightMap.set(1, "A"); 448 lightWeightMap.set(2, "B"); 449 lightWeightMap.set(3, "C"); 450 lightWeightMap.set(4, "D"); 451 lightWeightMap.set(5, "E"); 452 let res = lightWeightMap.getValueAt(1); 453 expect(res).assertEqual("B"); 454 }); 455 456 /** 457 * @tc.name: testIterator027 458 * @tc.desc: Iterate over all key value pairs in the LightWeightMap. 459 */ 460 it("testIterator027", 0, function () { 461 let lightWeightMap = new LightWeightMap(); 462 lightWeightMap.set(1, "A"); 463 lightWeightMap.set(2, "B"); 464 lightWeightMap.set(3, "C"); 465 lightWeightMap.set(4, "D"); 466 lightWeightMap.set(5, "E"); 467 let iters = lightWeightMap[Symbol.iterator](); 468 let flag = true; 469 for (let i = 0, len = lightWeightMap.length; i < len; i++) { 470 let entry = iters.next().value; 471 let res = lightWeightMap.get(entry[0]); 472 if (res != entry[1]) { 473 flag = false; 474 break; 475 } 476 } 477 expect(flag).assertEqual(true); 478 }); 479 480 /** 481 * @tc.name: testSet028 482 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set(1, null). 483 */ 484 it("testSet028", 0, function () { 485 let lightWeightMap = new LightWeightMap(); 486 lightWeightMap.set(1, null); 487 let res = lightWeightMap.hasValue(null); 488 expect(res).assertEqual(true); 489 let res1 = lightWeightMap.hasKey(1); 490 expect(res1).assertEqual(true); 491 }); 492 493 /** 494 * @tc.name: testSet029 495 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set(null, null). 496 */ 497 it("testSet029", 0, function () { 498 let lightWeightMap = new LightWeightMap(); 499 lightWeightMap.set(null, null); 500 let res = lightWeightMap.hasValue(null); 501 expect(res).assertEqual(true); 502 let res1 = lightWeightMap.hasKey(null); 503 expect(res1).assertEqual(true); 504 }); 505 506 /** 507 * @tc.name: testSet030 508 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set(true, false). 509 */ 510 it("testSet030", 0, function () { 511 let lightWeightMap = new LightWeightMap(); 512 lightWeightMap.set(true, false); 513 let res = lightWeightMap.hasValue(false); 514 expect(res).assertEqual(true); 515 let res1 = lightWeightMap.hasKey(true); 516 expect(res1).assertEqual(true); 517 }); 518 519 /** 520 * @tc.name: testSet031 521 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. For example: lightWeightMap.set(1.23, 321). 522 */ 523 it("testSet031", 0, function () { 524 let lightWeightMap = new LightWeightMap(); 525 lightWeightMap.set(1.23, 321); 526 let res = lightWeightMap.hasValue(321); 527 expect(res).assertEqual(true); 528 let res1 = lightWeightMap.hasKey(1.23); 529 expect(res1).assertEqual(true); 530 }); 531 532 /** 533 * @tc.name: testHasAll032 534 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 535 */ 536 it("testHasAll032", 0, function () { 537 let lightWeightMap = new LightWeightMap(); 538 lightWeightMap.set("a", "A"); 539 lightWeightMap.set("b", "B"); 540 lightWeightMap.set("c", "C"); 541 lightWeightMap.set("d", "D"); 542 lightWeightMap.set("e", "E"); 543 let lightWeightMap1 = new LightWeightMap(); 544 lightWeightMap1.set("a1", "A1"); 545 lightWeightMap1.set("d1", "D1"); 546 let res = lightWeightMap.hasAll(lightWeightMap1); 547 expect(res).assertEqual(false); 548 }); 549 550 /** 551 * @tc.name: testHasAll033 552 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 553 */ 554 it("testHasAll033", 0, function () { 555 let lightWeightMap = new LightWeightMap(); 556 lightWeightMap.set("a", "A"); 557 lightWeightMap.set("b", "B"); 558 lightWeightMap.set("c", "C"); 559 lightWeightMap.set("d", "D"); 560 lightWeightMap.set("e", "E"); 561 let lightWeightMap1 = new LightWeightMap(); 562 lightWeightMap1.set("a", "A1"); 563 lightWeightMap1.set("d", "D1"); 564 let res = lightWeightMap.hasAll(lightWeightMap1); 565 expect(res).assertEqual(false); 566 }); 567 568 /** 569 * @tc.name: testRemove034 570 * @tc.desc: Delete key value pairs according to key. For example: lightWeightMap.remove(3). 571 */ 572 it("testRemove034", 0, function () { 573 let lightWeightMap = new LightWeightMap(); 574 let res = lightWeightMap.remove(3); 575 expect(res).assertEqual(undefined); 576 }); 577 578 /** 579 * @tc.name: testRemoveAt035 580 * @tc.desc: Delete key value pairs according to index. For example: lightWeightMap.removeAt(1). 581 */ 582 it("testRemoveAt035", 0, function () { 583 let lightWeightMap = new LightWeightMap(); 584 let res = lightWeightMap.removeAt(1); 585 expect(res).assertEqual(false); 586 }); 587 588 /** 589 * @tc.name: testIncreaseCapacityTo036 590 * @tc.desc: Expand the LightWeightMap instance capacity to the specified value. 591 * For example: lightWeightMap.increaseCapacityTo(10). 592 */ 593 it("testIncreaseCapacityTo036", 0, function () { 594 let lightWeightMap = new LightWeightMap(); 595 lightWeightMap.set("a", "A"); 596 lightWeightMap.set("b", "B"); 597 lightWeightMap.set("c", "C"); 598 lightWeightMap.set("d", "D"); 599 lightWeightMap.set("e", "E"); 600 let res = lightWeightMap.increaseCapacityTo(10); 601 expect(res).assertEqual(undefined); 602 }); 603 604 /** 605 * @tc.name: testIncreaseCapacityTo037 606 * @tc.desc: Expand the LightWeightMap instance capacity to the specified value. 607 * For example: lightWeightMap.increaseCapacityTo("qwe"). 608 */ 609 it("testIncreaseCapacityTo037", 0, function () { 610 let lightWeightMap = new LightWeightMap(); 611 lightWeightMap.set("a", "A"); 612 lightWeightMap.set("b", "B"); 613 lightWeightMap.set("c", "C"); 614 lightWeightMap.set("d", "D"); 615 lightWeightMap.set("e", "E"); 616 try { 617 lightWeightMap.increaseCapacityTo("qwe"); 618 expect(true).assertEqual(false); 619 } catch (err) { 620 expect(err.name).assertEqual("BusinessError"); 621 expect(err.code).assertEqual(401); 622 expect(err.message).assertEqual(`The type of "minimumCapacity" must be number. Received value is: qwe`); 623 } 624 }); 625 626 /** 627 * @tc.name: testRemoveAt038 628 * @tc.desc: Delete key value pairs according to index. For example: lightWeightMap.removeAt("a"). 629 */ 630 it("testRemoveAt038", 0, function () { 631 let lightWeightMap = new LightWeightMap(); 632 try { 633 let res = lightWeightMap.removeAt("a"); 634 expect(true).assertEqual(false); 635 } catch (err) { 636 expect(err.name).assertEqual("BusinessError"); 637 expect(err.code).assertEqual(401); 638 expect(err.message).assertEqual(`The type of "index" must be number. Received value is: a`); 639 } 640 }); 641 642 /** 643 * @tc.name: testGetValueAt039 644 * @tc.desc: Get the value of the key value pair according to the corresponding index. 645 * For example: lightWeightMap.getValueAt("a"). 646 */ 647 it("testGetValueAt039", 0, function () { 648 let lightWeightMap = new LightWeightMap(); 649 lightWeightMap.set(1, "A"); 650 lightWeightMap.set(2, "B"); 651 lightWeightMap.set(3, "C"); 652 lightWeightMap.set(4, "D"); 653 lightWeightMap.set(5, "E"); 654 try { 655 let res = lightWeightMap.getValueAt("a"); 656 expect(true).assertEqual(false); 657 } catch (err) { 658 expect(err.name).assertEqual("BusinessError"); 659 expect(err.code).assertEqual(401); 660 expect(err.message).assertEqual(`The type of "index" must be number. Received value is: a`); 661 } 662 }); 663 664 /** 665 * @tc.name: testGetKeyAt040 666 * @tc.desc: Find the key of the key value pair according to the corresponding index. 667 * For example: lightWeightMap.getKeyAt("a"). 668 */ 669 it("testGetKeyAt040", 0, function () { 670 let lightWeightMap = new LightWeightMap(); 671 lightWeightMap.set(1, "A"); 672 lightWeightMap.set(2, "B"); 673 lightWeightMap.set(3, "C"); 674 lightWeightMap.set(4, "D"); 675 lightWeightMap.set(5, "E"); 676 try { 677 let res = lightWeightMap.getKeyAt("a"); 678 expect(true).assertEqual(false); 679 } catch (err) { 680 expect(err.name).assertEqual("BusinessError"); 681 expect(err.code).assertEqual(401); 682 expect(err.message).assertEqual(`The type of "index" must be number. Received value is: a`); 683 } 684 }); 685 686 /** 687 * @tc.name: testConstructor001 688 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 689 */ 690 it("testHasAll041", 0, function () { 691 let lightWeightMap = new LightWeightMap(); 692 let lightWeightMap1 = new LightWeightMap(); 693 let res = lightWeightMap.hasAll(lightWeightMap1); 694 expect(res).assertEqual(true); 695 }); 696 697 /** 698 * @tc.name: testSet042 699 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 700 */ 701 it("testSet042", 0, function () { 702 let lightWeightMap = new LightWeightMap(); 703 for (let i = 0; i < 100; i++) { 704 lightWeightMap.set(i, "A"); 705 } 706 let res = lightWeightMap.get(99); 707 expect(res).assertEqual("A"); 708 let res1 = lightWeightMap.length; 709 expect(res1).assertEqual(100); 710 }); 711 712 /** 713 * @tc.name: testSet043 714 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 715 */ 716 it("testSet043", 0, function () { 717 let lightWeightMap = new LightWeightMap(); 718 for (let i = 0; i < 100; i++) { 719 lightWeightMap.set(1, i); 720 } 721 let res = lightWeightMap.get(1); 722 expect(res).assertEqual(99); 723 let res1 = lightWeightMap.length; 724 expect(res1).assertEqual(1); 725 }); 726 727 /** 728 * @tc.name: testSet044 729 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 730 */ 731 it("testSet044", 0, function () { 732 let lightWeightMap = new LightWeightMap(); 733 for (let i = 0; i < 10000; i++) { 734 lightWeightMap.set(i, i); 735 } 736 let res1 = lightWeightMap.length; 737 expect(res1).assertEqual(10000); 738 for (let index = 0; index < 10000; index++) { 739 let resKey = lightWeightMap.hasKey(index); 740 expect(resKey).assertEqual(true); 741 let resValue = lightWeightMap.hasValue(index); 742 expect(resValue).assertEqual(true); 743 } 744 }); 745 746 /** 747 * @tc.name: testSet045 748 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 749 */ 750 it("testSet045", 0, function () { 751 let lightWeightMap = new LightWeightMap(); 752 for (let i = 0; i < 5000; i++) { 753 lightWeightMap.set(i, i); 754 } 755 for (let i = -1; i > -5001; i--) { 756 lightWeightMap.set(i, i); 757 } 758 let res1 = lightWeightMap.length; 759 expect(res1).assertEqual(10000); 760 for (let index = 0; index < 5000; index++) { 761 let resKey = lightWeightMap.hasKey(index); 762 expect(resKey).assertEqual(true); 763 let resValue = lightWeightMap.hasValue(index); 764 expect(resValue).assertEqual(true); 765 } 766 for (let i = -1; i > -5001; i--) { 767 let resKey = lightWeightMap.hasKey(i); 768 expect(resKey).assertEqual(true); 769 let resValue = lightWeightMap.hasValue(i); 770 expect(resValue).assertEqual(true); 771 } 772 }); 773 774 /** 775 * @tc.name: testSet046 776 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 777 */ 778 it("testSet046", 0, function () { 779 let lightWeightMap = new LightWeightMap(); 780 lightWeightMap.set(true, 0.001); 781 let res = lightWeightMap.hasValue(0.001); 782 expect(res).assertEqual(true); 783 let res1 = lightWeightMap.hasKey(true); 784 expect(res1).assertEqual(true); 785 }); 786 787 /** 788 * @tc.name: testSet047 789 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 790 */ 791 it("testSet047", 0, function () { 792 let lightWeightMap = new LightWeightMap(); 793 let a = [1, 2, 3, 4]; 794 lightWeightMap.set(a, 1); 795 let res = lightWeightMap.hasValue(1); 796 expect(res).assertEqual(true); 797 let res1 = lightWeightMap.hasKey(a); 798 expect(res1).assertEqual(true); 799 }); 800 801 /** 802 * @tc.name: testSet048 803 * @tc.desc: Add a pair of key value pairs to the LightWeightMap. 804 */ 805 it("testSet048", 0, function () { 806 let lightWeightMap = new LightWeightMap(); 807 let a = {abc: 1}; 808 lightWeightMap.set(a, ""); 809 let res = lightWeightMap.hasValue(""); 810 expect(res).assertEqual(true); 811 let res1 = lightWeightMap.hasKey(a); 812 expect(res1).assertEqual(true); 813 }); 814 815 /** 816 * @tc.name: testGet049 817 * @tc.desc: Get the corresponding value through the key. For example: lightWeightMap.get(10). 818 */ 819 it("testGet049", 0, function () { 820 let lightWeightMap = new LightWeightMap(); 821 lightWeightMap.set(1, "A"); 822 lightWeightMap.set(2, "B"); 823 lightWeightMap.set(3, "C"); 824 lightWeightMap.set(4, "D"); 825 lightWeightMap.set(5, "E"); 826 let res = lightWeightMap.get(10); 827 expect(res).assertEqual(undefined); 828 }); 829 830 /** 831 * @tc.name: testHasAll050 832 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 833 */ 834 it("testHasAll050", 0, function () { 835 let lightWeightMap = new LightWeightMap(); 836 lightWeightMap.set("a", "A"); 837 lightWeightMap.set("b", "B"); 838 lightWeightMap.set("c", "C"); 839 lightWeightMap.set("d", "D"); 840 lightWeightMap.set("e", "E"); 841 let lightWeightMap1 = new LightWeightMap(); 842 lightWeightMap1.set("a", "A"); 843 lightWeightMap1.set("d", "D1"); 844 let res = lightWeightMap.hasAll(lightWeightMap1); 845 expect(res).assertEqual(false); 846 }); 847 848 /** 849 * @tc.name: testHasAll051 850 * @tc.desc: Judge whether a lightweightmap contains all key value pairs in another lightweightmap. 851 */ 852 it("testHasAll051", 0, function () { 853 let lightWeightMap = new LightWeightMap(); 854 try { 855 lightWeightMap.hasAll([1, 2, 3]); 856 expect(true).assertEqual(false); 857 } catch (err) { 858 expect(err.name).assertEqual("BusinessError"); 859 expect(err.code).assertEqual(401); 860 expect(err.message).assertEqual(`The type of "map" must be LightWeightMap. Received value is: 1,2,3`); 861 } 862 }); 863 864 /** 865 * @tc.name: testGetKeyAt052 866 * @tc.desc: Find the key of the key value pair according to the corresponding index. 867 * For example: lightWeightMap.getKeyAt(6). 868 */ 869 it("testGetKeyAt052", 0, function () { 870 let lightWeightMap = new LightWeightMap(); 871 lightWeightMap.set(1, "A"); 872 lightWeightMap.set(2, "B"); 873 lightWeightMap.set(3, "C"); 874 lightWeightMap.set(4, "D"); 875 lightWeightMap.set(5, "E"); 876 try { 877 let res = lightWeightMap.getKeyAt(6); 878 expect(true).assertEqual(false); 879 } catch (err) { 880 expect(err.name).assertEqual("BusinessError"); 881 expect(err.code).assertEqual(10200001); 882 expect(err.message).assertEqual(`The value of "index" is out of range. It must be >= 0 && <= 4. Received value is: 6`); 883 } 884 }); 885 886 /** 887 * @tc.name: testSetAll053 888 * @tc.desc: Copy key value pairs from one LightWeightMap to another. 889 */ 890 it("testSetAll053", 0, function () { 891 let lightWeightMap = new LightWeightMap(); 892 lightWeightMap.set(1, "A"); 893 lightWeightMap.set(2, "B"); 894 try { 895 lightWeightMap.setAll([1, 2, 3]); 896 expect(true).assertEqual(false); 897 } catch (err) { 898 expect(err.name).assertEqual("BusinessError"); 899 expect(err.code).assertEqual(401); 900 expect(err.message).assertEqual(`The type of "map" must be LightWeightMap. Received value is: 1,2,3`); 901 } 902 }); 903 904 /** 905 * @tc.name: testSetValueAt054 906 * @tc.desc: Modify the value of the key value pair according to the corresponding index. 907 * For example: lightWeightMap.setValueAt("a", "a"). 908 */ 909 it("testSetValueAt054", 0, function () { 910 let lightWeightMap = new LightWeightMap(); 911 lightWeightMap.set(1, "A"); 912 lightWeightMap.set(2, "B"); 913 try { 914 lightWeightMap.setValueAt("a", "a"); 915 expect(true).assertEqual(false); 916 } catch (err) { 917 expect(err.name).assertEqual("BusinessError"); 918 expect(err.code).assertEqual(401); 919 expect(err.message).assertEqual(`The type of "index" must be number. Received value is: a`); 920 } 921 }); 922 923 /** 924 * @tc.name: testSetValueAt055 925 * @tc.desc: Modify the value of the key value pair according to the corresponding index. 926 * For example: lightWeightMap.setValueAt(3, "a"). 927 */ 928 it("testSetValueAt055", 0, function () { 929 let lightWeightMap = new LightWeightMap(); 930 lightWeightMap.set(1, "A"); 931 lightWeightMap.set(2, "B"); 932 try { 933 lightWeightMap.setValueAt(3, "a"); 934 expect(true).assertEqual(false); 935 } catch (err) { 936 expect(err.name).assertEqual("BusinessError"); 937 expect(err.code).assertEqual(10200001); 938 expect(err.message).assertEqual(`The value of "index" is out of range. It must be >= 0 && <= 1. Received value is: 3`); 939 } 940 }); 941 942 /** 943 * @tc.name: testGetValueAt056 944 * @tc.desc: Get the value of the key value pair according to the corresponding index. 945 * For example: lightWeightMap.getValueAt(6). 946 */ 947 it("testGetValueAt056", 0, function () { 948 let lightWeightMap = new LightWeightMap(); 949 lightWeightMap.set(1, "A"); 950 lightWeightMap.set(2, "B"); 951 lightWeightMap.set(3, "C"); 952 lightWeightMap.set(4, "D"); 953 lightWeightMap.set(5, "E"); 954 try { 955 lightWeightMap.getValueAt(6); 956 expect(true).assertEqual(false); 957 } catch (err) { 958 expect(err.name).assertEqual("BusinessError"); 959 expect(err.code).assertEqual(10200001); 960 expect(err.message).assertEqual(`The value of "index" is out of range. It must be >= 0 && <= 4. Received value is: 6`); 961 } 962 }); 963}); 964} 965