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_ciclass c{
244514f5e3Sopenharmony_ci    n = 0;
254514f5e3Sopenharmony_ci    constructor(a){
264514f5e3Sopenharmony_ci      this.n = a;
274514f5e3Sopenharmony_ci    }
284514f5e3Sopenharmony_ci  }
294514f5e3Sopenharmony_ciif (globalThis["ArkPrivate"] != undefined) {
304514f5e3Sopenharmony_ci    fastset = ArkPrivate.Load(ArkPrivate.TreeSet);
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_ci    let map = new Map();
334514f5e3Sopenharmony_ci    let set = new fastset();
344514f5e3Sopenharmony_ci    set.add("aa");
354514f5e3Sopenharmony_ci    set.add("bb");
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ci    // test has: true
384514f5e3Sopenharmony_ci    map.set("test has:", set.length == 2 && set.has("aa") && set.has("bb") && !set.has("cc"));
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_ci    set.add("cc");
414514f5e3Sopenharmony_ci    // test getFirstKey and getLastKey: true
424514f5e3Sopenharmony_ci    map.set("test getFirstKey and getLastKey:", set.getFirstValue() == "aa" && set.getLastValue() == "cc");
434514f5e3Sopenharmony_ci    // test getLowerValue and getHigherValue out: true
444514f5e3Sopenharmony_ci    map.set("test getLowerValue and getHigherValue", set.getLowerValue("bb") == "aa" &&
454514f5e3Sopenharmony_ci            set.getLowerValue("aa") == undefined && set.getHigherValue("bb") == "cc" &&
464514f5e3Sopenharmony_ci            set.getHigherValue("cc") == undefined);
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci    // test values: true
494514f5e3Sopenharmony_ci    let iteratorSetValues = set.values();
504514f5e3Sopenharmony_ci    map.set("test values:", iteratorSetValues.next().value == "aa" && iteratorSetValues.next().value == "bb" &&
514514f5e3Sopenharmony_ci            iteratorSetValues.next().value == "cc" && iteratorSetValues.next().value == undefined);
524514f5e3Sopenharmony_ci    // test entries: [cc, cc], undefined
534514f5e3Sopenharmony_ci    let iteratorSetEntries = set.entries();
544514f5e3Sopenharmony_ci    iteratorSetEntries.next().value;
554514f5e3Sopenharmony_ci    iteratorSetEntries.next().value;
564514f5e3Sopenharmony_ci    map.set("test entries1:", iteratorSetEntries.next().value != undefined);
574514f5e3Sopenharmony_ci    map.set("test entries2:", iteratorSetEntries.next().value == undefined);
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci    // test forof: aa, bb, cc
604514f5e3Sopenharmony_ci    let arr = ["aa", "bb", "cc"];
614514f5e3Sopenharmony_ci    let i = 0;
624514f5e3Sopenharmony_ci    for (const item of set) {
634514f5e3Sopenharmony_ci        map.set(arr[i], item == arr[i]);
644514f5e3Sopenharmony_ci        i++;
654514f5e3Sopenharmony_ci    }
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    // test forin:
684514f5e3Sopenharmony_ci    for (const item in set) {
694514f5e3Sopenharmony_ci        map.set("test forin:", item);
704514f5e3Sopenharmony_ci    }
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    // test forEach:
734514f5e3Sopenharmony_ci    let setFlag = false;
744514f5e3Sopenharmony_ci    function TestForEach(value, key, set) {
754514f5e3Sopenharmony_ci        setFlag= set.has(key) && set.has(value);
764514f5e3Sopenharmony_ci        map.set("test forEach" + key, setFlag);
774514f5e3Sopenharmony_ci    }
784514f5e3Sopenharmony_ci    set.forEach(TestForEach);
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    // test isEmpty: false
814514f5e3Sopenharmony_ci    map.set("test isEmpty:", !set.isEmpty());
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    set.add("ee");
844514f5e3Sopenharmony_ci    set.add("dd");
854514f5e3Sopenharmony_ci    // test popFirst and popLast: true
864514f5e3Sopenharmony_ci    map.set("test popFirst and popLast:", set.length == 5 && set.popFirst() == "aa" &&
874514f5e3Sopenharmony_ci          set.popLast() == "ee" && !set.has("aa"));
884514f5e3Sopenharmony_ci    // test remove: true
894514f5e3Sopenharmony_ci    map.set("test remove:", set.remove("bb") && set.length == 2 && !set.has("bb"));
904514f5e3Sopenharmony_ci    // test clear: true
914514f5e3Sopenharmony_ci    set.clear();
924514f5e3Sopenharmony_ci    map.set("test clear:", set.length == 0 && !set.has("cc") && set.isEmpty());
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    let flag = false;
954514f5e3Sopenharmony_ci    try {
964514f5e3Sopenharmony_ci        set["aa"] = 3;
974514f5e3Sopenharmony_ci    } catch (e) {
984514f5e3Sopenharmony_ci        flag = true;
994514f5e3Sopenharmony_ci    }
1004514f5e3Sopenharmony_ci    map.set("test set throw error", flag);
1014514f5e3Sopenharmony_ci
1024514f5e3Sopenharmony_ci    let cmmp = new fastset((firstValue, secondValue) => {return firstValue > secondValue});
1034514f5e3Sopenharmony_ci    cmmp.add("aa");
1044514f5e3Sopenharmony_ci    cmmp.add("bb");
1054514f5e3Sopenharmony_ci    map.set("test has undefined", cmmp.has(undefined) == false);
1064514f5e3Sopenharmony_ci    map.set("test has null", cmmp.has(null) == false);
1074514f5e3Sopenharmony_ci    cmmp.add(null, 1);
1084514f5e3Sopenharmony_ci    cmmp.add(undefined, 1);
1094514f5e3Sopenharmony_ci    map.set("test has undefined false", cmmp.has(undefined) == true);
1104514f5e3Sopenharmony_ci    map.set("test has null false", cmmp.has(null) == true);
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ci    let comset =  new fastset((firstValue, secondValue) => {return firstValue < secondValue});
1134514f5e3Sopenharmony_ci    comset.add("c");
1144514f5e3Sopenharmony_ci    comset.add("a");
1154514f5e3Sopenharmony_ci    comset.add("b");
1164514f5e3Sopenharmony_ci    comset.add("d");
1174514f5e3Sopenharmony_ci    if (comset.length == 4) {
1184514f5e3Sopenharmony_ci        comset.remove("a");
1194514f5e3Sopenharmony_ci        comset.remove("b");
1204514f5e3Sopenharmony_ci        comset.remove("c");
1214514f5e3Sopenharmony_ci        comset.remove("d");
1224514f5e3Sopenharmony_ci    }
1234514f5e3Sopenharmony_ci    map.set("test commpare", comset.length == 0);
1244514f5e3Sopenharmony_ci
1254514f5e3Sopenharmony_ci    class Person {
1264514f5e3Sopenharmony_ci        id = 0;
1274514f5e3Sopenharmony_ci        name = '';
1284514f5e3Sopenharmony_ci        constructor(id, name) {
1294514f5e3Sopenharmony_ci            this.id = id;
1304514f5e3Sopenharmony_ci            this.name = name;
1314514f5e3Sopenharmony_ci        }
1324514f5e3Sopenharmony_ci    }
1334514f5e3Sopenharmony_ci
1344514f5e3Sopenharmony_ci    comset =  new fastset((firstValue, secondValue) => {return firstValue.id < secondValue.id});
1354514f5e3Sopenharmony_ci    let personone = new Person(3,'张三');
1364514f5e3Sopenharmony_ci    let persontwo = new Person(1,'李四');
1374514f5e3Sopenharmony_ci    let personsec = new Person(2,'王五');
1384514f5e3Sopenharmony_ci    comset.add(personone);
1394514f5e3Sopenharmony_ci    comset.add(persontwo);
1404514f5e3Sopenharmony_ci    comset.add(personsec);
1414514f5e3Sopenharmony_ci    map.set("test clear and set", comset.getFirstValue().id === 1);
1424514f5e3Sopenharmony_ci    comset.clear();
1434514f5e3Sopenharmony_ci    comset =  new fastset((firstValue, secondValue) => {return firstValue > secondValue});
1444514f5e3Sopenharmony_ci    comset.add("c");
1454514f5e3Sopenharmony_ci    comset.add("a");
1464514f5e3Sopenharmony_ci    comset.add("b");
1474514f5e3Sopenharmony_ci    comset.add("d");
1484514f5e3Sopenharmony_ci    comset.clear();
1494514f5e3Sopenharmony_ci    comset.add("c");
1504514f5e3Sopenharmony_ci    comset.add("a");
1514514f5e3Sopenharmony_ci    comset.add("b");
1524514f5e3Sopenharmony_ci    comset.add("d");
1534514f5e3Sopenharmony_ci    map.set("test clear and set", comset.getFirstValue() === "d");
1544514f5e3Sopenharmony_ci
1554514f5e3Sopenharmony_ci    let set1 = new fastset();
1564514f5e3Sopenharmony_ci    let proxy = new Proxy(set1, {});
1574514f5e3Sopenharmony_ci    proxy.add("aa");
1584514f5e3Sopenharmony_ci    proxy.add("bb");
1594514f5e3Sopenharmony_ci
1604514f5e3Sopenharmony_ci    // test has: true
1614514f5e3Sopenharmony_ci    map.set("test has:", proxy.length == 2 && proxy.has("aa") && proxy.has("bb") && !proxy.has("cc"));
1624514f5e3Sopenharmony_ci
1634514f5e3Sopenharmony_ci    proxy.add("cc");
1644514f5e3Sopenharmony_ci    // test getFirstKey and getLastKey: true
1654514f5e3Sopenharmony_ci    map.set("test getFirstKey and getLastKey:", proxy.getFirstValue() == "aa" && proxy.getLastValue() == "cc");
1664514f5e3Sopenharmony_ci    // test getLowerValue and getHigherValue out: true
1674514f5e3Sopenharmony_ci    map.set("test getLowerValue and getHigherValue", proxy.getLowerValue("bb") == "aa" &&
1684514f5e3Sopenharmony_ci            proxy.getLowerValue("aa") == undefined && proxy.getHigherValue("bb") == "cc" &&
1694514f5e3Sopenharmony_ci            proxy.getHigherValue("cc") == undefined);
1704514f5e3Sopenharmony_ci
1714514f5e3Sopenharmony_ci    // test values: true
1724514f5e3Sopenharmony_ci    let iteratorSetValues1 = proxy.values();
1734514f5e3Sopenharmony_ci    map.set("test values:", iteratorSetValues1.next().value == "aa" && iteratorSetValues1.next().value == "bb" &&
1744514f5e3Sopenharmony_ci            iteratorSetValues1.next().value == "cc" && iteratorSetValues1.next().value == undefined);
1754514f5e3Sopenharmony_ci    // test entries: [cc, cc], undefined
1764514f5e3Sopenharmony_ci    let iteratorSetEntries1 = proxy.entries();
1774514f5e3Sopenharmony_ci    iteratorSetEntries1.next().value;
1784514f5e3Sopenharmony_ci    iteratorSetEntries1.next().value;
1794514f5e3Sopenharmony_ci    map.set("test entries1:", iteratorSetEntries1.next().value != undefined);
1804514f5e3Sopenharmony_ci    map.set("test entries2:", iteratorSetEntries1.next().value == undefined);
1814514f5e3Sopenharmony_ci
1824514f5e3Sopenharmony_ci    // test forof: aa, bb, cc
1834514f5e3Sopenharmony_ci    let arr1 = ["aa", "bb", "cc"];
1844514f5e3Sopenharmony_ci    let j = 0;
1854514f5e3Sopenharmony_ci    for (const item of proxy) {
1864514f5e3Sopenharmony_ci        map.set(arr1[j], item == arr1[j]);
1874514f5e3Sopenharmony_ci        j++;
1884514f5e3Sopenharmony_ci    }
1894514f5e3Sopenharmony_ci
1904514f5e3Sopenharmony_ci    // test forin:
1914514f5e3Sopenharmony_ci    for (const item in proxy) {
1924514f5e3Sopenharmony_ci        map.set("test forin:", item);
1934514f5e3Sopenharmony_ci    }
1944514f5e3Sopenharmony_ci
1954514f5e3Sopenharmony_ci    // test forEach:
1964514f5e3Sopenharmony_ci    let setFlag1 = false;
1974514f5e3Sopenharmony_ci    function TestForEach1(value, key, proxy) {
1984514f5e3Sopenharmony_ci        setFlag1 = proxy.has(key) && proxy.has(value);
1994514f5e3Sopenharmony_ci        map.set("test forEach" + key, setFlag1);
2004514f5e3Sopenharmony_ci    }
2014514f5e3Sopenharmony_ci    proxy.forEach(TestForEach1);
2024514f5e3Sopenharmony_ci
2034514f5e3Sopenharmony_ci    // test isEmpty: false
2044514f5e3Sopenharmony_ci    map.set("test isEmpty:", !proxy.isEmpty());
2054514f5e3Sopenharmony_ci
2064514f5e3Sopenharmony_ci    proxy.add("ee");
2074514f5e3Sopenharmony_ci    proxy.add("dd");
2084514f5e3Sopenharmony_ci    // test popFirst and popLast: true
2094514f5e3Sopenharmony_ci    map.set("test popFirst and popLast:", proxy.length == 5 && proxy.popFirst() == "aa" &&
2104514f5e3Sopenharmony_ci            proxy.popLast() == "ee" && !proxy.has("aa"));
2114514f5e3Sopenharmony_ci    // test remove: true
2124514f5e3Sopenharmony_ci    map.set("test remove:", proxy.remove("bb") && proxy.length == 2 && !proxy.has("bb"));
2134514f5e3Sopenharmony_ci    // test clear: true
2144514f5e3Sopenharmony_ci    proxy.clear();
2154514f5e3Sopenharmony_ci    map.set("test clear:", proxy.length == 0 && !proxy.has("cc") && proxy.isEmpty());
2164514f5e3Sopenharmony_ci
2174514f5e3Sopenharmony_ci    flag = false;
2184514f5e3Sopenharmony_ci    try {
2194514f5e3Sopenharmony_ci        proxy["aa"] = 3;
2204514f5e3Sopenharmony_ci    } catch (e) {
2214514f5e3Sopenharmony_ci        flag = true;
2224514f5e3Sopenharmony_ci    }
2234514f5e3Sopenharmony_ci    map.set("test set throw error", flag);
2244514f5e3Sopenharmony_ci
2254514f5e3Sopenharmony_ci    // test getLower & getHigher when object
2264514f5e3Sopenharmony_ci    let newset = new fastset((x, y)=> x.n < y.n);
2274514f5e3Sopenharmony_ci    newset.add(new c(3));
2284514f5e3Sopenharmony_ci    let tmp_c = new c(5);
2294514f5e3Sopenharmony_ci    newset.add(tmp_c);
2304514f5e3Sopenharmony_ci    newset.add(undefined);
2314514f5e3Sopenharmony_ci    newset.add(new c(1));
2324514f5e3Sopenharmony_ci    newset.add(null);
2334514f5e3Sopenharmony_ci    map.set("test getHigher no.1:", newset.getHigherValue(new c(3)).n == 5);
2344514f5e3Sopenharmony_ci    map.set("test getHigher no.2:", newset.getHigherValue(new c(5)) == null);
2354514f5e3Sopenharmony_ci    map.set("test getHigher no.3:", newset.getHigherValue(null) == undefined);
2364514f5e3Sopenharmony_ci    map.set("test getLower no.1:", newset.getLowerValue(new c(3)).n == 1);
2374514f5e3Sopenharmony_ci    map.set("test getLower no.2:", newset.getLowerValue(undefined) == null);
2384514f5e3Sopenharmony_ci    map.set("test getLower no.3:", newset.getLowerValue(null) == tmp_c);
2394514f5e3Sopenharmony_ci
2404514f5e3Sopenharmony_ci    flag = undefined;
2414514f5e3Sopenharmony_ci    function elementsTreeSet(valueTreeSet, keyTreeSet, map) {
2424514f5e3Sopenharmony_ci        if (!valueTreeSet) {
2434514f5e3Sopenharmony_ci            if (!flag) {
2444514f5e3Sopenharmony_ci                flag = [];
2454514f5e3Sopenharmony_ci            }
2464514f5e3Sopenharmony_ci            flag.push(keyTreeSet);
2474514f5e3Sopenharmony_ci        }
2484514f5e3Sopenharmony_ci    }
2494514f5e3Sopenharmony_ci    map.forEach(elementsTreeSet);
2504514f5e3Sopenharmony_ci
2514514f5e3Sopenharmony_ci    let de = new fastset();
2524514f5e3Sopenharmony_ci    try {
2534514f5e3Sopenharmony_ci        de.forEach(123);
2544514f5e3Sopenharmony_ci    } catch(err) {
2554514f5e3Sopenharmony_ci        if (err.name != "BusinessError") {
2564514f5e3Sopenharmony_ci            print("TreeSet forEach throw error fail");
2574514f5e3Sopenharmony_ci        }
2584514f5e3Sopenharmony_ci    }
2594514f5e3Sopenharmony_ci    if (!flag) {
2604514f5e3Sopenharmony_ci        print("Test TreeSet success!!!");
2614514f5e3Sopenharmony_ci    } else {
2624514f5e3Sopenharmony_ci        print("Test TreeSet fail: " + flag);
2634514f5e3Sopenharmony_ci    }
2644514f5e3Sopenharmony_ci    let treeSet = new fastset((first,second) =>{
2654514f5e3Sopenharmony_ci        return first > second
2664514f5e3Sopenharmony_ci    });
2674514f5e3Sopenharmony_ci    let insertArr = [
2684514f5e3Sopenharmony_ci        643,
2694514f5e3Sopenharmony_ci        811,
2704514f5e3Sopenharmony_ci        807,
2714514f5e3Sopenharmony_ci        378,
2724514f5e3Sopenharmony_ci        226,
2734514f5e3Sopenharmony_ci        195,
2744514f5e3Sopenharmony_ci        599,
2754514f5e3Sopenharmony_ci        641,
2764514f5e3Sopenharmony_ci        494,
2774514f5e3Sopenharmony_ci        964,
2784514f5e3Sopenharmony_ci        156,
2794514f5e3Sopenharmony_ci        419,
2804514f5e3Sopenharmony_ci        977,
2814514f5e3Sopenharmony_ci        20,
2824514f5e3Sopenharmony_ci        788,
2834514f5e3Sopenharmony_ci        596
2844514f5e3Sopenharmony_ci    ]
2854514f5e3Sopenharmony_ci    let addItem = function(obj){
2864514f5e3Sopenharmony_ci        treeSet.add(obj)
2874514f5e3Sopenharmony_ci    }
2884514f5e3Sopenharmony_ci    let removeItem = function(){
2894514f5e3Sopenharmony_ci        const first = treeSet.getFirstValue()
2904514f5e3Sopenharmony_ci        treeSet.remove(first)
2914514f5e3Sopenharmony_ci    }
2924514f5e3Sopenharmony_ci    for(let i = 0;i < insertArr.length;i++) {
2934514f5e3Sopenharmony_ci        addItem(insertArr[i])
2944514f5e3Sopenharmony_ci    }
2954514f5e3Sopenharmony_ci    removeItem()
2964514f5e3Sopenharmony_ci    removeItem()
2974514f5e3Sopenharmony_ci    removeItem()
2984514f5e3Sopenharmony_ci    removeItem()
2994514f5e3Sopenharmony_ci    removeItem()
3004514f5e3Sopenharmony_ci    addItem(664)
3014514f5e3Sopenharmony_ci    removeItem()
3024514f5e3Sopenharmony_ci    removeItem()
3034514f5e3Sopenharmony_ci    removeItem()
3044514f5e3Sopenharmony_ci    removeItem()
3054514f5e3Sopenharmony_ci    removeItem()
3064514f5e3Sopenharmony_ci    removeItem()
3074514f5e3Sopenharmony_ci    removeItem()
3084514f5e3Sopenharmony_ci    removeItem()
3094514f5e3Sopenharmony_ci    let resArr = []
3104514f5e3Sopenharmony_ci    treeSet.forEach(element => {
3114514f5e3Sopenharmony_ci        resArr.push(element)
3124514f5e3Sopenharmony_ci    });
3134514f5e3Sopenharmony_ci    print(resArr)
3144514f5e3Sopenharmony_ci}
3154514f5e3Sopenharmony_ciexport let treesetRes = "Test TreeSet done";