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 {testdProxyArray1}  from "./utility";
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_civar arrayList = undefined;
254514f5e3Sopenharmony_ciif (globalThis["ArkPrivate"] != undefined) {
264514f5e3Sopenharmony_ci    arrayList = ArkPrivate.Load(ArkPrivate.ArrayList);
274514f5e3Sopenharmony_ci    let arr = new arrayList();
284514f5e3Sopenharmony_ci    arr.add(1);
294514f5e3Sopenharmony_ci    arr.add(2);
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ci    let map = new Map();
324514f5e3Sopenharmony_ci    let flag1 = false;
334514f5e3Sopenharmony_ci    try {
344514f5e3Sopenharmony_ci        arr["aa"] = 3;
354514f5e3Sopenharmony_ci    } catch (e) {
364514f5e3Sopenharmony_ci        flag1 = true;
374514f5e3Sopenharmony_ci    }
384514f5e3Sopenharmony_ci    map.set("flag1", flag1);
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_ci    let flag2 = true;
414514f5e3Sopenharmony_ci    for (let i = 0; i < arr.length; i++) {
424514f5e3Sopenharmony_ci        if (arr[i] != (i + 1)) {
434514f5e3Sopenharmony_ci            flag2 = false;
444514f5e3Sopenharmony_ci            break;
454514f5e3Sopenharmony_ci        }
464514f5e3Sopenharmony_ci    }
474514f5e3Sopenharmony_ci    map.set("flag2", flag2);
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ci    let arr1 = new arrayList();
504514f5e3Sopenharmony_ci    let proxy = new Proxy(arr1, {});
514514f5e3Sopenharmony_ci    let testArray = []
524514f5e3Sopenharmony_ci    let res = true
534514f5e3Sopenharmony_ci    for(let i = 0; i < 10; i++) {
544514f5e3Sopenharmony_ci        proxy.add(i)
554514f5e3Sopenharmony_ci        testArray.push(i)
564514f5e3Sopenharmony_ci    }
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
594514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i]) {
604514f5e3Sopenharmony_ci            res = false
614514f5e3Sopenharmony_ci        }
624514f5e3Sopenharmony_ci    }
634514f5e3Sopenharmony_ci    map.set("test arraylist add:", res)
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ci    map.set("test arraylist has:", proxy.has(2))
664514f5e3Sopenharmony_ci    map.set("test arraylist getCapacity:", proxy.getCapacity() === 15)
674514f5e3Sopenharmony_ci    map.set("test arraylist getLastIndexOf:", proxy.getLastIndexOf(1) === 1)
684514f5e3Sopenharmony_ci    map.set("test arraylist getIndexOf:", proxy.getIndexOf(5) === 5)
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci    let array = proxy.convertToArray()
714514f5e3Sopenharmony_ci    res = true
724514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
734514f5e3Sopenharmony_ci        if (array[i] !== testArray[i]) {
744514f5e3Sopenharmony_ci            res = false
754514f5e3Sopenharmony_ci        }
764514f5e3Sopenharmony_ci    }
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci    map.set("test arraylist convertToArray:", res)
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    let newArrayList = proxy.clone()
814514f5e3Sopenharmony_ci    res = true
824514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
834514f5e3Sopenharmony_ci        if (newArrayList[i] !== testArray[i]) {
844514f5e3Sopenharmony_ci            res = false
854514f5e3Sopenharmony_ci        }
864514f5e3Sopenharmony_ci    }
874514f5e3Sopenharmony_ci    map.set("test arraylist clone:", res)
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ci    proxy.insert(999, 3)
904514f5e3Sopenharmony_ci    testArray.splice(3, 0, 999)
914514f5e3Sopenharmony_ci    res = true
924514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
934514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i]) {
944514f5e3Sopenharmony_ci            res = false
954514f5e3Sopenharmony_ci        }
964514f5e3Sopenharmony_ci    }
974514f5e3Sopenharmony_ci    map.set("test arraylist insert:", res)
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_ci    proxy.removeByIndex(9)
1004514f5e3Sopenharmony_ci    testArray.splice(9, 1)
1014514f5e3Sopenharmony_ci    res = true
1024514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1034514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i]) {
1044514f5e3Sopenharmony_ci            res = false
1054514f5e3Sopenharmony_ci        }
1064514f5e3Sopenharmony_ci    }
1074514f5e3Sopenharmony_ci    map.set("test arraylist removeByIndex:", res)
1084514f5e3Sopenharmony_ci
1094514f5e3Sopenharmony_ci    const removeRes = proxy.remove(7)
1104514f5e3Sopenharmony_ci    testArray.splice(8, 1)
1114514f5e3Sopenharmony_ci    res = true
1124514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1134514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i]) {
1144514f5e3Sopenharmony_ci            res = false
1154514f5e3Sopenharmony_ci        }
1164514f5e3Sopenharmony_ci    }
1174514f5e3Sopenharmony_ci    map.set("test arraylist remove:", res)
1184514f5e3Sopenharmony_ci    map.set("test arraylist removeRes:", removeRes)
1194514f5e3Sopenharmony_ci
1204514f5e3Sopenharmony_ci    proxy.removeByRange(1, 3)
1214514f5e3Sopenharmony_ci    testArray.splice(1, 2)
1224514f5e3Sopenharmony_ci    res = true
1234514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1244514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i]) {
1254514f5e3Sopenharmony_ci            res = false
1264514f5e3Sopenharmony_ci        }
1274514f5e3Sopenharmony_ci    }
1284514f5e3Sopenharmony_ci    map.set("test arraylist removeByRange:", res)
1294514f5e3Sopenharmony_ci
1304514f5e3Sopenharmony_ci    res = true
1314514f5e3Sopenharmony_ci    proxy.forEach((value, index) => {
1324514f5e3Sopenharmony_ci        if (value !== testArray[index]) {
1334514f5e3Sopenharmony_ci            res = false
1344514f5e3Sopenharmony_ci        }
1354514f5e3Sopenharmony_ci    })
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci    map.set("test arraylist forEach:", res);
1384514f5e3Sopenharmony_ci
1394514f5e3Sopenharmony_ci    res = true
1404514f5e3Sopenharmony_ci    let subArrayList = proxy.subArrayList(1, 3)
1414514f5e3Sopenharmony_ci    const newtestArray = testArray.slice(1, 3)
1424514f5e3Sopenharmony_ci    for(let i = 0; i < subArrayList.length; i++) {
1434514f5e3Sopenharmony_ci        if (newtestArray[i] !== subArrayList[i]) {
1444514f5e3Sopenharmony_ci            res =  false
1454514f5e3Sopenharmony_ci        }
1464514f5e3Sopenharmony_ci    }
1474514f5e3Sopenharmony_ci    map.set("test arraylist subArrayList:", res)
1484514f5e3Sopenharmony_ci
1494514f5e3Sopenharmony_ci    res = true
1504514f5e3Sopenharmony_ci    let j = 0
1514514f5e3Sopenharmony_ci    for (const data of proxy) {
1524514f5e3Sopenharmony_ci      if (data !== testArray[j]) {
1534514f5e3Sopenharmony_ci        res = false
1544514f5e3Sopenharmony_ci      }
1554514f5e3Sopenharmony_ci      j++;
1564514f5e3Sopenharmony_ci    }
1574514f5e3Sopenharmony_ci    map.set("test arraylist for of:", res);
1584514f5e3Sopenharmony_ci
1594514f5e3Sopenharmony_ci    testdProxyArray1(proxy, res, testArray);
1604514f5e3Sopenharmony_ci
1614514f5e3Sopenharmony_ci    map.set("test arraylist Symbol.iterator:", res)
1624514f5e3Sopenharmony_ci
1634514f5e3Sopenharmony_ci    proxy.replaceAllElements((item, index) => {
1644514f5e3Sopenharmony_ci        return item * 2
1654514f5e3Sopenharmony_ci    })
1664514f5e3Sopenharmony_ci    res = true
1674514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1684514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i] * 2) {
1694514f5e3Sopenharmony_ci            res = false
1704514f5e3Sopenharmony_ci        }
1714514f5e3Sopenharmony_ci    }
1724514f5e3Sopenharmony_ci    map.set("test arraylist replaceAllElements:", res)
1734514f5e3Sopenharmony_ci
1744514f5e3Sopenharmony_ci    let arr2 = new arrayList();
1754514f5e3Sopenharmony_ci    let proxy1 = new Proxy(arr2, {});
1764514f5e3Sopenharmony_ci    proxy1.add(4);
1774514f5e3Sopenharmony_ci    proxy1.add(3);
1784514f5e3Sopenharmony_ci    proxy1.add(1);
1794514f5e3Sopenharmony_ci    proxy1.add(2);
1804514f5e3Sopenharmony_ci    proxy1.add(0);
1814514f5e3Sopenharmony_ci    proxy1.sort((a,b) => a-b);
1824514f5e3Sopenharmony_ci    res = true
1834514f5e3Sopenharmony_ci    for (let i = 0; i < 5; i++) {
1844514f5e3Sopenharmony_ci        if (proxy1[i] !== i) {
1854514f5e3Sopenharmony_ci            res = false
1864514f5e3Sopenharmony_ci        }
1874514f5e3Sopenharmony_ci    }
1884514f5e3Sopenharmony_ci    map.set("test arraylist sort:", res)
1894514f5e3Sopenharmony_ci
1904514f5e3Sopenharmony_ci    proxy1.clear()
1914514f5e3Sopenharmony_ci    map.set("test arraylist clear:", proxy1.length === 0)
1924514f5e3Sopenharmony_ci    map.set("test arraylist isEmpty:", proxy1.isEmpty())
1934514f5e3Sopenharmony_ci    proxy1.add(4);
1944514f5e3Sopenharmony_ci    proxy1.add(3);
1954514f5e3Sopenharmony_ci    proxy1.add(1);
1964514f5e3Sopenharmony_ci    proxy1.add(2);
1974514f5e3Sopenharmony_ci    proxy1.add(0);
1984514f5e3Sopenharmony_ci    proxy1.sort((a,b) => a-b);
1994514f5e3Sopenharmony_ci    res = true
2004514f5e3Sopenharmony_ci    for (let i = 0; i < 5; i++) {
2014514f5e3Sopenharmony_ci        if (proxy1[i] !== i) {
2024514f5e3Sopenharmony_ci            res = false
2034514f5e3Sopenharmony_ci        }
2044514f5e3Sopenharmony_ci    }
2054514f5e3Sopenharmony_ci    map.set("test arraylist clear and add:", res)
2064514f5e3Sopenharmony_ci
2074514f5e3Sopenharmony_ci    proxy1.insert(50, 0);
2084514f5e3Sopenharmony_ci    map.set("test arraylist insert index === 0:", proxy1[0] === 50)
2094514f5e3Sopenharmony_ci    proxy1.increaseCapacityTo(20)
2104514f5e3Sopenharmony_ci    map.set("test arraylist increaseCapacityTo:", proxy1.getCapacity() === 20)
2114514f5e3Sopenharmony_ci
2124514f5e3Sopenharmony_ci    proxy1.trimToCurrentLength()
2134514f5e3Sopenharmony_ci    map.set("test arraylist trimToCurrentLength:", proxy1.getCapacity() === 6)
2144514f5e3Sopenharmony_ci
2154514f5e3Sopenharmony_ci    let testlist = new arrayList();
2164514f5e3Sopenharmony_ci    try {
2174514f5e3Sopenharmony_ci        testlist.removeByIndex(0);
2184514f5e3Sopenharmony_ci    } catch(err) {
2194514f5e3Sopenharmony_ci        res = (err =="BusinessError: Container is empty")
2204514f5e3Sopenharmony_ci        map.set("test RemoveByIndex exception when arraylist is empty:", res)
2214514f5e3Sopenharmony_ci    }
2224514f5e3Sopenharmony_ci    try {
2234514f5e3Sopenharmony_ci        testlist.removeByRange(0, 1);
2244514f5e3Sopenharmony_ci    } catch(err) {
2254514f5e3Sopenharmony_ci        res = (err =="BusinessError: Container is empty")
2264514f5e3Sopenharmony_ci        map.set("test RemoveByRange exception when arraylist is empty:", res)
2274514f5e3Sopenharmony_ci    }
2284514f5e3Sopenharmony_ci    try {
2294514f5e3Sopenharmony_ci        testlist.subArrayList(0, 1);
2304514f5e3Sopenharmony_ci    } catch(err) {
2314514f5e3Sopenharmony_ci        res = (err =="BusinessError: Container is empty")
2324514f5e3Sopenharmony_ci        map.set("test SubArrayList exception when arraylist is empty:", res)
2334514f5e3Sopenharmony_ci    }
2344514f5e3Sopenharmony_ci    testlist.add(7);
2354514f5e3Sopenharmony_ci    testlist.add(9);
2364514f5e3Sopenharmony_ci    map.set("test arraylist JSAPIArrayList::GetProperty:", testlist[Math.floor(1)] === 9);
2374514f5e3Sopenharmony_ci
2384514f5e3Sopenharmony_ci    try {
2394514f5e3Sopenharmony_ci        let myArrayList = new arrayList();
2404514f5e3Sopenharmony_ci        myArrayList.add(1);
2414514f5e3Sopenharmony_ci        myArrayList[2147483648];
2424514f5e3Sopenharmony_ci    } catch(err) {
2434514f5e3Sopenharmony_ci        let overFlowTest = (err == "BusinessError: The type of \"index\" must be small integer.");
2444514f5e3Sopenharmony_ci        map.set("test ArrayList[i] overFlowTest:", overFlowTest);
2454514f5e3Sopenharmony_ci    }
2464514f5e3Sopenharmony_ci
2474514f5e3Sopenharmony_ci    let flag = undefined;
2484514f5e3Sopenharmony_ci    function elements(value, key, map) {
2494514f5e3Sopenharmony_ci        if (!value) {
2504514f5e3Sopenharmony_ci            if (!flag) {
2514514f5e3Sopenharmony_ci                flag = [];
2524514f5e3Sopenharmony_ci            }
2534514f5e3Sopenharmony_ci            flag.push(key);
2544514f5e3Sopenharmony_ci        }
2554514f5e3Sopenharmony_ci    }
2564514f5e3Sopenharmony_ci    map.forEach(elements);
2574514f5e3Sopenharmony_ci    let de = new arrayList();
2584514f5e3Sopenharmony_ci    try {
2594514f5e3Sopenharmony_ci        de.forEach(123);
2604514f5e3Sopenharmony_ci    } catch(err) {
2614514f5e3Sopenharmony_ci        if (err.name != "BusinessError") {
2624514f5e3Sopenharmony_ci            print("ArrayList forEach throw error fail");
2634514f5e3Sopenharmony_ci        }
2644514f5e3Sopenharmony_ci    }
2654514f5e3Sopenharmony_ci    let test1 = new arrayList();
2664514f5e3Sopenharmony_ci    for (let k = 0; k < 10; k++) {
2674514f5e3Sopenharmony_ci        test1.add(k);
2684514f5e3Sopenharmony_ci    }
2694514f5e3Sopenharmony_ci    var keyName = "";
2704514f5e3Sopenharmony_ci    for (const key in test1) {
2714514f5e3Sopenharmony_ci        keyName += key;
2724514f5e3Sopenharmony_ci    }
2734514f5e3Sopenharmony_ci    if (keyName != "0123456789") {
2744514f5e3Sopenharmony_ci        print("ArrayList for in fail")
2754514f5e3Sopenharmony_ci    }
2764514f5e3Sopenharmony_ci
2774514f5e3Sopenharmony_ci    // Math.foor as index input should not have exception.
2784514f5e3Sopenharmony_ci    let myAl = new arrayList();
2794514f5e3Sopenharmony_ci    myAl.add(1);
2804514f5e3Sopenharmony_ci    myAl.add(2);
2814514f5e3Sopenharmony_ci    myAl.add(3);
2824514f5e3Sopenharmony_ci    myAl.insert(30, Math.floor(1.3));
2834514f5e3Sopenharmony_ci    myAl.removeByIndex(Math.floor(1.3));
2844514f5e3Sopenharmony_ci
2854514f5e3Sopenharmony_ci    const v1 = new arrayList()
2864514f5e3Sopenharmony_ci    v1.add(1)
2874514f5e3Sopenharmony_ci    v1.add(2)
2884514f5e3Sopenharmony_ci    v1.add(3)
2894514f5e3Sopenharmony_ci    let oldLen = v1.length
2904514f5e3Sopenharmony_ci    v1.replaceAllElements((val,index,temp)=>{
2914514f5e3Sopenharmony_ci        temp.add(val)
2924514f5e3Sopenharmony_ci    })
2934514f5e3Sopenharmony_ci    print(oldLen * 2 === v1.length)
2944514f5e3Sopenharmony_ci    const v3 = new arrayList();
2954514f5e3Sopenharmony_ci    const v4 = [arrayList,arrayList];
2964514f5e3Sopenharmony_ci    class C4{
2974514f5e3Sopenharmony_ci    ["100"] = v4
2984514f5e3Sopenharmony_ci    }
2994514f5e3Sopenharmony_ci    const v5 = new C4();
3004514f5e3Sopenharmony_ci    try {
3014514f5e3Sopenharmony_ci        Object.assign(v3,v5)
3024514f5e3Sopenharmony_ci    } catch (error) {
3034514f5e3Sopenharmony_ci        print(error)
3044514f5e3Sopenharmony_ci    }
3054514f5e3Sopenharmony_ci
3064514f5e3Sopenharmony_ci    const v11 = new arrayList()
3074514f5e3Sopenharmony_ci    function f2(a3, a4) {
3084514f5e3Sopenharmony_ci        const o5 = {}
3094514f5e3Sopenharmony_ci        return o5;
3104514f5e3Sopenharmony_ci    }
3114514f5e3Sopenharmony_ci    const o6 = {
3124514f5e3Sopenharmony_ci        "set" : f2
3134514f5e3Sopenharmony_ci    }
3144514f5e3Sopenharmony_ci    try {
3154514f5e3Sopenharmony_ci        const v8Proxy = new Proxy(v11,o6)
3164514f5e3Sopenharmony_ci        v8Proxy[4] = "no"
3174514f5e3Sopenharmony_ci    } catch (error) {
3184514f5e3Sopenharmony_ci        print(error)
3194514f5e3Sopenharmony_ci    }
3204514f5e3Sopenharmony_ci
3214514f5e3Sopenharmony_ci    if (!flag) {
3224514f5e3Sopenharmony_ci        print("Test ArrayList success!!!");
3234514f5e3Sopenharmony_ci    } else {
3244514f5e3Sopenharmony_ci        print("Test ArrayList fail: " + flag);
3254514f5e3Sopenharmony_ci    }
3264514f5e3Sopenharmony_ci}
3274514f5e3Sopenharmony_ciexport let arraylistRes = "Test ArrayList";
328