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_civar fastset = undefined;
234514f5e3Sopenharmony_ciif (globalThis["ArkPrivate"] != undefined) {
244514f5e3Sopenharmony_ci    fastset = ArkPrivate.Load(ArkPrivate.LightWeightSet);
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_ci    let res = new Map();
274514f5e3Sopenharmony_ci    let set = new fastset();
284514f5e3Sopenharmony_ci    let proxy = new Proxy(set, {});
294514f5e3Sopenharmony_ci    // test isEmpty: true
304514f5e3Sopenharmony_ci    res.set("test isEmpty:", proxy.isEmpty());
314514f5e3Sopenharmony_ci    proxy.add(1);
324514f5e3Sopenharmony_ci    proxy.add(2);
334514f5e3Sopenharmony_ci    proxy.add(3);
344514f5e3Sopenharmony_ci    // test has: true
354514f5e3Sopenharmony_ci    res.set("test has 1:", proxy.has(1));
364514f5e3Sopenharmony_ci    res.set("test has 2:", proxy.has(2));
374514f5e3Sopenharmony_ci    res.set("test has 3:", proxy.has(3));
384514f5e3Sopenharmony_ci    // test has: false
394514f5e3Sopenharmony_ci    res.set("test has 4:", proxy.has(4) == false);
404514f5e3Sopenharmony_ci    // test values: true
414514f5e3Sopenharmony_ci    let iteratorValues1 = proxy.values();
424514f5e3Sopenharmony_ci    res.set("test values:", iteratorValues1.next().value == 1 && iteratorValues1.next().value == 2 &&
434514f5e3Sopenharmony_ci            iteratorValues1.next().value == 3 && iteratorValues1.next().value == undefined);
444514f5e3Sopenharmony_ci    // test entries: [c,cc], undefined
454514f5e3Sopenharmony_ci    let iteratorEntries1 = proxy.entries();
464514f5e3Sopenharmony_ci    iteratorEntries1.next().value;
474514f5e3Sopenharmony_ci    iteratorEntries1.next().value;
484514f5e3Sopenharmony_ci    res.set("test entries1:", iteratorEntries1.next().value != undefined);
494514f5e3Sopenharmony_ci    res.set("itest entries2:", iteratorEntries1.next().value == undefined);
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    // test forof
524514f5e3Sopenharmony_ci    let arr1 = [1, 2, 3];
534514f5e3Sopenharmony_ci    let j = 0;
544514f5e3Sopenharmony_ci    for (const item of proxy) {
554514f5e3Sopenharmony_ci    }
564514f5e3Sopenharmony_ci    // test forin:
574514f5e3Sopenharmony_ci    for (const item in proxy) {
584514f5e3Sopenharmony_ci        res.set("test forin", true);
594514f5e3Sopenharmony_ci    }
604514f5e3Sopenharmony_ci    // test forEach:
614514f5e3Sopenharmony_ci    let flag = false;
624514f5e3Sopenharmony_ci    function TestForEach1(value, key, proxy) {
634514f5e3Sopenharmony_ci        flag = proxy.has(key) && proxy.has(value);
644514f5e3Sopenharmony_ci        res.set("test forEach" + key, flag);
654514f5e3Sopenharmony_ci    }
664514f5e3Sopenharmony_ci    proxy.forEach(TestForEach1);
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    let dset = new fastset();
694514f5e3Sopenharmony_ci    let dProxy = new Proxy(dset, {});
704514f5e3Sopenharmony_ci    dProxy.add(4);
714514f5e3Sopenharmony_ci    dProxy.add(5);
724514f5e3Sopenharmony_ci    dProxy.add(6);
734514f5e3Sopenharmony_ci    dProxy.add(7);
744514f5e3Sopenharmony_ci    dProxy.add(8);
754514f5e3Sopenharmony_ci    dProxy.add(9);
764514f5e3Sopenharmony_ci    res.set("test addAll:", dProxy.addAll(proxy));
774514f5e3Sopenharmony_ci    res.set("test hasAll:", dProxy.hasAll(proxy));
784514f5e3Sopenharmony_ci    let obj = ["a", "b"]
794514f5e3Sopenharmony_ci    res.set("test equal:", !dProxy.equal(obj));
804514f5e3Sopenharmony_ci    // test remove: true
814514f5e3Sopenharmony_ci    res.set("test remove:", dProxy.remove(1) === 1 && dProxy.length === 8);
824514f5e3Sopenharmony_ci    // test removeAt: true
834514f5e3Sopenharmony_ci    res.set("test removeAt:", dProxy.removeAt(3) && dProxy.length === 7);
844514f5e3Sopenharmony_ci    // test setValueAt: true
854514f5e3Sopenharmony_ci    res.set("test getValueAt:", dProxy.getValueAt(3) === 6);
864514f5e3Sopenharmony_ci    // test setValueAt: true
874514f5e3Sopenharmony_ci    res.set("test getIndexOf:", dProxy.getIndexOf(2) === 0);
884514f5e3Sopenharmony_ci    // test toString: true
894514f5e3Sopenharmony_ci    res.set("test toString:", dProxy.toString() === "2,3,4,6,7,8,9");
904514f5e3Sopenharmony_ci    let arr = dProxy.toArray()
914514f5e3Sopenharmony_ci    res.set("test toArray:", true);
924514f5e3Sopenharmony_ci    // test increaseCapacityTo: true
934514f5e3Sopenharmony_ci    dProxy.increaseCapacityTo(20)
944514f5e3Sopenharmony_ci    res.set("test increaseCapacityTo:", true);
954514f5e3Sopenharmony_ci    // test clear: 0
964514f5e3Sopenharmony_ci    let ret = dProxy.clear();
974514f5e3Sopenharmony_ci    res.set("test clear:", dProxy.length == 0);
984514f5e3Sopenharmony_ci    res.set("test 'clear' ret:", ret === undefined)
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ci    // test COW
1014514f5e3Sopenharmony_ci    let LOOP_COUNT = 5;
1024514f5e3Sopenharmony_ci    let myTest = new fastset();
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_ci    for (var i = 0; i < LOOP_COUNT; i++) {
1054514f5e3Sopenharmony_ci        myTest.add(i);
1064514f5e3Sopenharmony_ci    }
1074514f5e3Sopenharmony_ci
1084514f5e3Sopenharmony_ci    let a = myTest.toArray();
1094514f5e3Sopenharmony_ci    res.set("test COW - create a :", a.length == LOOP_COUNT);
1104514f5e3Sopenharmony_ci    myTest.add(10);
1114514f5e3Sopenharmony_ci    res.set("test COW - check a.length :", a.length == LOOP_COUNT);
1124514f5e3Sopenharmony_ci    let b = myTest.toArray();
1134514f5e3Sopenharmony_ci    res.set("test COW - check b.length :", b.length == (LOOP_COUNT + 1));
1144514f5e3Sopenharmony_ci
1154514f5e3Sopenharmony_ci    flag = false;
1164514f5e3Sopenharmony_ci    let seten = new fastset();
1174514f5e3Sopenharmony_ci    seten.add(1);
1184514f5e3Sopenharmony_ci    seten.add(2);
1194514f5e3Sopenharmony_ci    seten.add(3);
1204514f5e3Sopenharmony_ci    seten.add(4);
1214514f5e3Sopenharmony_ci    seten.add(5);
1224514f5e3Sopenharmony_ci    let iter = seten.entries();
1234514f5e3Sopenharmony_ci    let temp = iter.next();
1244514f5e3Sopenharmony_ci    while(!temp.done) {
1254514f5e3Sopenharmony_ci        if ((temp.value[0]) == (temp.value[1])) {
1264514f5e3Sopenharmony_ci            flag = true;
1274514f5e3Sopenharmony_ci        }
1284514f5e3Sopenharmony_ci        temp = iter.next();
1294514f5e3Sopenharmony_ci    }
1304514f5e3Sopenharmony_ci    res.set("test entries return type", flag);
1314514f5e3Sopenharmony_ci
1324514f5e3Sopenharmony_ci    flag = false;
1334514f5e3Sopenharmony_ci    try {
1344514f5e3Sopenharmony_ci        proxy["aa"] = 3;
1354514f5e3Sopenharmony_ci    } catch (e) {
1364514f5e3Sopenharmony_ci        flag = true;
1374514f5e3Sopenharmony_ci    }
1384514f5e3Sopenharmony_ci    res.set("test map throw error", flag);
1394514f5e3Sopenharmony_ci
1404514f5e3Sopenharmony_ci    flag = undefined;
1414514f5e3Sopenharmony_ci    function elements(value, key, res) {
1424514f5e3Sopenharmony_ci        if (!value) {
1434514f5e3Sopenharmony_ci            if (!flag) {
1444514f5e3Sopenharmony_ci                flag = [];
1454514f5e3Sopenharmony_ci            }
1464514f5e3Sopenharmony_ci            flag.push(key);
1474514f5e3Sopenharmony_ci        }
1484514f5e3Sopenharmony_ci    }
1494514f5e3Sopenharmony_ci    res.forEach(elements);
1504514f5e3Sopenharmony_ci
1514514f5e3Sopenharmony_ci    let de = new fastset();
1524514f5e3Sopenharmony_ci    try {
1534514f5e3Sopenharmony_ci        de.forEach(123);
1544514f5e3Sopenharmony_ci    } catch(err) {
1554514f5e3Sopenharmony_ci        if (err.name != "BusinessError") {
1564514f5e3Sopenharmony_ci            print("LightWeightSet forEach throw error fail");
1574514f5e3Sopenharmony_ci        }
1584514f5e3Sopenharmony_ci    }
1594514f5e3Sopenharmony_ci
1604514f5e3Sopenharmony_ci    // Math.floor be index input should not throw exception.
1614514f5e3Sopenharmony_ci    let myLs = new fastset();
1624514f5e3Sopenharmony_ci    myLs.add("a");
1634514f5e3Sopenharmony_ci    myLs.add("b");
1644514f5e3Sopenharmony_ci    myLs.getValueAt(Math.floor(1.5));
1654514f5e3Sopenharmony_ci    myLs.removeAt(Math.floor(1.5));
1664514f5e3Sopenharmony_ci
1674514f5e3Sopenharmony_ci    if (!flag) {
1684514f5e3Sopenharmony_ci        print("Test LightWeightSet success!!!");
1694514f5e3Sopenharmony_ci    } else {
1704514f5e3Sopenharmony_ci        print("Test LightWeightSet fail: " + flag);
1714514f5e3Sopenharmony_ci    }
1724514f5e3Sopenharmony_ci}
1734514f5e3Sopenharmony_ciexport let lightweightsetRes = "Test LightWeightSet done";
174