14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci/*
174514f5e3Sopenharmony_ci * @tc.name:container
184514f5e3Sopenharmony_ci * @tc.desc:test container
194514f5e3Sopenharmony_ci * @tc.type: FUNC
204514f5e3Sopenharmony_ci * @tc.require: issueI5NO8G
214514f5e3Sopenharmony_ci */
224514f5e3Sopenharmony_ciimport {testCommon}  from "./utility";
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_civar fastmap = undefined;
254514f5e3Sopenharmony_ciif (globalThis["ArkPrivate"] != undefined) {
264514f5e3Sopenharmony_ci    fastmap = ArkPrivate.Load(ArkPrivate.LightWeightMap);
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_ci    let res = new Map();
294514f5e3Sopenharmony_ci    let map = new fastmap();
304514f5e3Sopenharmony_ci    let proxy = new Proxy(map, {});
314514f5e3Sopenharmony_ci    // test isEmpty: true
324514f5e3Sopenharmony_ci    res.set("test isEmpty:", proxy.isEmpty());
334514f5e3Sopenharmony_ci    proxy.set("a", "aa");
344514f5e3Sopenharmony_ci    proxy.set("b", "bb");
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ci    // test get: true
374514f5e3Sopenharmony_ci    res.set("test get:", proxy.length == 2 && proxy.get("a") == "aa" && proxy.get("b") == "bb");
384514f5e3Sopenharmony_ci    // test hasKey and hasValue: true
394514f5e3Sopenharmony_ci    res.set("test hasKey and hasValue:", proxy.hasKey("a") && proxy.hasKey("b") && proxy.hasValue("aa") &&
404514f5e3Sopenharmony_ci            proxy.hasValue("bb") && !proxy.hasKey("c") && !proxy.hasValue("cc"));
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci    proxy.set("c", "cc");
434514f5e3Sopenharmony_ci    // test getIndexOfKey and getIndexOfValue: true
444514f5e3Sopenharmony_ci    res.set("test getIndexOfKey and getIndexOfValue:", proxy.getIndexOfKey("a") === 0 && proxy.getIndexOfValue("bb") === 1);
454514f5e3Sopenharmony_ci    // test getKeyAt: true
464514f5e3Sopenharmony_ci    res.set("test getKeyAt-1:", proxy.getKeyAt(1) == "b");
474514f5e3Sopenharmony_ci    res.set("test getKeyAt-2:", proxy.getKeyAt(Math.floor(1.3)) == "b");
484514f5e3Sopenharmony_ci    testCommon(proxy, res);
494514f5e3Sopenharmony_ci
504514f5e3Sopenharmony_ci    // test forEach:
514514f5e3Sopenharmony_ci    let flag = false;
524514f5e3Sopenharmony_ci    function TestForEach(value, key, proxy) {
534514f5e3Sopenharmony_ci        flag = proxy.get(key) === value;
544514f5e3Sopenharmony_ci        res.set("test forEach" + key, flag)
554514f5e3Sopenharmony_ci    }
564514f5e3Sopenharmony_ci    proxy.forEach(TestForEach);
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci    let dmap = new fastmap();
594514f5e3Sopenharmony_ci    let dProxy = new Proxy(dmap, {});
604514f5e3Sopenharmony_ci    dProxy.set("a", "aa");
614514f5e3Sopenharmony_ci    dProxy.set("b", "bb");
624514f5e3Sopenharmony_ci    dProxy.set("c", "cc");
634514f5e3Sopenharmony_ci    dProxy.set("d", "dd");
644514f5e3Sopenharmony_ci    dProxy.set("e", "ee");
654514f5e3Sopenharmony_ci    // test setAll:
664514f5e3Sopenharmony_ci    dProxy.setAll(proxy);
674514f5e3Sopenharmony_ci    res.set("test setAll:", dProxy.length === 5);
684514f5e3Sopenharmony_ci    res.set("test hasAll:", dProxy.hasAll(proxy));
694514f5e3Sopenharmony_ci    // test remove: true
704514f5e3Sopenharmony_ci    res.set("test remove:", dProxy.remove("a") == "aa" && dProxy.length == 4);
714514f5e3Sopenharmony_ci    // test removeAt: true
724514f5e3Sopenharmony_ci    res.set("test removeAt-1:", dProxy.removeAt(dProxy.getIndexOfKey("b")) && dProxy.length == 3);
734514f5e3Sopenharmony_ci    // test setValueAt: true
744514f5e3Sopenharmony_ci    res.set("test setValueAt-1:", dProxy.setValueAt(dProxy.getIndexOfKey("d"), "ee"));
754514f5e3Sopenharmony_ci    // test getValueAt: true
764514f5e3Sopenharmony_ci    res.set("test getValueAt:", dProxy.getValueAt(dProxy.getIndexOfKey("d")) === "ee");
774514f5e3Sopenharmony_ci    res.set("test getValueAt:", dProxy.getValueAt(Math.floor(0)) === "cc");
784514f5e3Sopenharmony_ci    res.set("test getValueAt:", dProxy.getValueAt(Math.floor(1)) === "ee");
794514f5e3Sopenharmony_ci    res.set("test setValueAt-2:", dProxy.setValueAt(Math.floor(1.3), "ff"));
804514f5e3Sopenharmony_ci    res.set("test getValueAt-2:", dProxy.getValueAt(Math.floor(1.3)) === "ff");
814514f5e3Sopenharmony_ci    // test toString: true
824514f5e3Sopenharmony_ci    res.set("test toString:", dProxy.toString() === "c:cc,d:ff,e:ee");
834514f5e3Sopenharmony_ci    res.set("test removeAt-2:", dProxy.removeAt(Math.floor(1.3)) && dProxy.length == 2);
844514f5e3Sopenharmony_ci    // test increaseCapacityTo: true
854514f5e3Sopenharmony_ci    dProxy.increaseCapacityTo(20)
864514f5e3Sopenharmony_ci    dProxy.increaseCapacityTo(Math.floor(1.3));
874514f5e3Sopenharmony_ci    res.set("test increaseCapacityTo:", true);
884514f5e3Sopenharmony_ci    // test clear: 0
894514f5e3Sopenharmony_ci    let ret = dProxy.clear();
904514f5e3Sopenharmony_ci    res.set("test clear:", dProxy.length == 0);
914514f5e3Sopenharmony_ci    res.set("test 'clear' ret:", ret === undefined);
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_ci    let empty_fm = new fastmap();
944514f5e3Sopenharmony_ci    try {
954514f5e3Sopenharmony_ci        empty_fm.getKeyAt(0);
964514f5e3Sopenharmony_ci    } catch(err) {
974514f5e3Sopenharmony_ci        res.set("test GetKeyAt exception when arraylist is empty:", err == "BusinessError: Container is empty")
984514f5e3Sopenharmony_ci    }
994514f5e3Sopenharmony_ci    try {
1004514f5e3Sopenharmony_ci        empty_fm.getValueAt(0);
1014514f5e3Sopenharmony_ci    } catch(err) {
1024514f5e3Sopenharmony_ci        res.set("test GetValueAt exception when arraylist is empty:", err == "BusinessError: Container is empty")
1034514f5e3Sopenharmony_ci    }
1044514f5e3Sopenharmony_ci    try {
1054514f5e3Sopenharmony_ci        empty_fm.setValueAt(0);
1064514f5e3Sopenharmony_ci    } catch(err) {
1074514f5e3Sopenharmony_ci        res.set("test SetValueAt exception when arraylist is empty:", err == "BusinessError: Container is empty")
1084514f5e3Sopenharmony_ci    }
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    flag = false;
1114514f5e3Sopenharmony_ci    try {
1124514f5e3Sopenharmony_ci        proxy["aa"] = 3;
1134514f5e3Sopenharmony_ci    } catch (e) {
1144514f5e3Sopenharmony_ci        flag = true;
1154514f5e3Sopenharmony_ci    }
1164514f5e3Sopenharmony_ci    res.set("test map throw error", flag);
1174514f5e3Sopenharmony_ci    flag = undefined;
1184514f5e3Sopenharmony_ci    function elements(value, key, res) {
1194514f5e3Sopenharmony_ci        if (!value) {
1204514f5e3Sopenharmony_ci            if (!flag) {
1214514f5e3Sopenharmony_ci                flag = [];
1224514f5e3Sopenharmony_ci            }
1234514f5e3Sopenharmony_ci            flag.push(key);
1244514f5e3Sopenharmony_ci        }
1254514f5e3Sopenharmony_ci    }
1264514f5e3Sopenharmony_ci    res.forEach(elements);
1274514f5e3Sopenharmony_ci
1284514f5e3Sopenharmony_ci    let de = new fastmap();
1294514f5e3Sopenharmony_ci    try {
1304514f5e3Sopenharmony_ci        de.forEach(123);
1314514f5e3Sopenharmony_ci    } catch(err) {
1324514f5e3Sopenharmony_ci        if (err.name != "BusinessError") {
1334514f5e3Sopenharmony_ci            print("LightWeightMap forEach throw error fail");
1344514f5e3Sopenharmony_ci        }
1354514f5e3Sopenharmony_ci    }
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci    // Math.floor as index input should not throw exception.
1384514f5e3Sopenharmony_ci    let myLm = new fastmap();
1394514f5e3Sopenharmony_ci    myLm.set("a", "aa");
1404514f5e3Sopenharmony_ci    myLm.set("b", "bb");
1414514f5e3Sopenharmony_ci    myLm.getKeyAt(Math.floor(1.5));
1424514f5e3Sopenharmony_ci    myLm.getValueAt(Math.floor(1.5));
1434514f5e3Sopenharmony_ci    myLm.setValueAt(Math.floor(1.5), "cc");
1444514f5e3Sopenharmony_ci    myLm.removeAt(Math.floor(1.5));
1454514f5e3Sopenharmony_ci
1464514f5e3Sopenharmony_ci    if (!flag) {
1474514f5e3Sopenharmony_ci        print("Test LightWeightMap success!!!");
1484514f5e3Sopenharmony_ci    } else {
1494514f5e3Sopenharmony_ci        print("Test LightWeightMap fail: " + flag);
1504514f5e3Sopenharmony_ci    }
1514514f5e3Sopenharmony_ci}
1524514f5e3Sopenharmony_ciexport let lightweightmapRes = "Test LightWeightMap done";
153