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 FastVector = undefined;
234514f5e3Sopenharmony_ciif (globalThis["ArkPrivate"] != undefined) {
244514f5e3Sopenharmony_ci    FastVector = ArkPrivate.Load(ArkPrivate.Vector);
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_ci    let map = new Map();
274514f5e3Sopenharmony_ci    let vector = new FastVector();
284514f5e3Sopenharmony_ci    vector.add(4); // index is 0
294514f5e3Sopenharmony_ci    vector.add(3);
304514f5e3Sopenharmony_ci    vector.add(1);
314514f5e3Sopenharmony_ci    vector.add(5);
324514f5e3Sopenharmony_ci    vector.add(14);
334514f5e3Sopenharmony_ci    let res = vector.toString();
344514f5e3Sopenharmony_ci    map.set("test add and toString:", res);
354514f5e3Sopenharmony_ci    // test insert, length, get, getIndexOf
364514f5e3Sopenharmony_ci    vector.insert(2, 2);
374514f5e3Sopenharmony_ci    map.set("test length:", vector.length == 6);
384514f5e3Sopenharmony_ci    map.set("test get(index is 2):", vector.get(2) == 2);
394514f5e3Sopenharmony_ci    map.set("test get(index is 3):", vector.get(3) !== 3); // false
404514f5e3Sopenharmony_ci    map.set("test getIndexOf(target is 3):", vector.getIndexOf(3) == 1); // true
414514f5e3Sopenharmony_ci    map.set("test getIndexOf(target is 2):", vector.getIndexOf(2) !== 5); // false
424514f5e3Sopenharmony_ci    // test isEmpty
434514f5e3Sopenharmony_ci    map.set("test isEmpty:", !vector.isEmpty());
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    let vec = vector.clone();
464514f5e3Sopenharmony_ci    // test clear
474514f5e3Sopenharmony_ci    vector.clear();
484514f5e3Sopenharmony_ci    map.set("test clear:", vector.isEmpty());
494514f5e3Sopenharmony_ci    // // test set, clone
504514f5e3Sopenharmony_ci    vec.set(2, 8);
514514f5e3Sopenharmony_ci    map.set("test set:", vec.get(2) == 8 && vec.length == 6);
524514f5e3Sopenharmony_ci    // trimToCurrentLength
534514f5e3Sopenharmony_ci    vec.trimToCurrentLength();
544514f5e3Sopenharmony_ci    map.set("test trimToCurrentLength1:", vec.getCapacity() === 6);
554514f5e3Sopenharmony_ci    vec.trimToCurrentLength();
564514f5e3Sopenharmony_ci    map.set("test trimToCurrentLength2:", vec.getCapacity() === 6);
574514f5e3Sopenharmony_ci    // test subvector
584514f5e3Sopenharmony_ci    let subVec = vec.subVector(0, 3);
594514f5e3Sopenharmony_ci    map.set("test subVector and tostring:", subVec.toString());
604514f5e3Sopenharmony_ci    // test replaceAllElements
614514f5e3Sopenharmony_ci    subVec.replaceAllElements((item, index) => {
624514f5e3Sopenharmony_ci        return (item = 2 * item);
634514f5e3Sopenharmony_ci    });
644514f5e3Sopenharmony_ci    map.set("test replaceAllElements:", subVec.toString() == "8,6,16");
654514f5e3Sopenharmony_ci    // GetFirstElement
664514f5e3Sopenharmony_ci    map.set("test GetFirstElement:", subVec.getFirstElement() == 8 &&
674514f5e3Sopenharmony_ci            vec.getFirstElement() == 4);
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ci    let arr = [4, 3, 8, 1, 5, 14];
704514f5e3Sopenharmony_ci    for (let i = 0; i < vector.length; i++) {
714514f5e3Sopenharmony_ci        map.set("for of " + arr[i], vec.get(i) == arr[i]);
724514f5e3Sopenharmony_ci    }
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    let flag = false;
754514f5e3Sopenharmony_ci    try {
764514f5e3Sopenharmony_ci        vec["aa"] = 3;
774514f5e3Sopenharmony_ci    } catch (e) {
784514f5e3Sopenharmony_ci        flag = true;
794514f5e3Sopenharmony_ci    }
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    map.set("test vector throw error", flag);
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    let vector1 = new FastVector();
844514f5e3Sopenharmony_ci    let proxy = new Proxy(vector1, {});
854514f5e3Sopenharmony_ci    let testArray = []
864514f5e3Sopenharmony_ci    res = true
874514f5e3Sopenharmony_ci    for(let i = 0; i<10; i++) {
884514f5e3Sopenharmony_ci        proxy.add(i)
894514f5e3Sopenharmony_ci        testArray.push(i)
904514f5e3Sopenharmony_ci    }
914514f5e3Sopenharmony_ci
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 vector add:", res)
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_ci    map.set("test vector has:", proxy.has(2))
1004514f5e3Sopenharmony_ci    map.set("test vector getCapacity:", proxy.getCapacity() === 10)
1014514f5e3Sopenharmony_ci    map.set("test vector getLastIndexOf:", proxy.getLastIndexOf(1) === 1)
1024514f5e3Sopenharmony_ci    map.set("test vector getIndexOf:", proxy.getIndexOf(5) === 5)
1034514f5e3Sopenharmony_ci    map.set("test vector getFirstElement:", proxy.getFirstElement() === 0)
1044514f5e3Sopenharmony_ci    map.set("test vector getLastElement:", proxy.getLastElement() === 9)
1054514f5e3Sopenharmony_ci    map.set("test vector getLastIndexFrom:", proxy.getLastIndexFrom(1, 5) === 1)
1064514f5e3Sopenharmony_ci    map.set("test vector getIndexFrom:", proxy.getIndexFrom(5, 1) === 5)
1074514f5e3Sopenharmony_ci
1084514f5e3Sopenharmony_ci    let array = proxy.convertToArray()
1094514f5e3Sopenharmony_ci    res = true
1104514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1114514f5e3Sopenharmony_ci        if (array[i] !== testArray[i]) {
1124514f5e3Sopenharmony_ci            res = false
1134514f5e3Sopenharmony_ci        }
1144514f5e3Sopenharmony_ci    }
1154514f5e3Sopenharmony_ci    map.set("test vector convertToArray:", res)
1164514f5e3Sopenharmony_ci
1174514f5e3Sopenharmony_ci    let newVector = proxy.clone()
1184514f5e3Sopenharmony_ci    res = true
1194514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1204514f5e3Sopenharmony_ci        if (newVector[i] !== testArray[i]) {
1214514f5e3Sopenharmony_ci            res = false
1224514f5e3Sopenharmony_ci        }
1234514f5e3Sopenharmony_ci    }
1244514f5e3Sopenharmony_ci    map.set("test vector clone:", res)
1254514f5e3Sopenharmony_ci
1264514f5e3Sopenharmony_ci    proxy.insert(999, 3)
1274514f5e3Sopenharmony_ci    testArray.splice(3, 0, 999)
1284514f5e3Sopenharmony_ci    res = true
1294514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1304514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i]) {
1314514f5e3Sopenharmony_ci            res = false
1324514f5e3Sopenharmony_ci        }
1334514f5e3Sopenharmony_ci    }
1344514f5e3Sopenharmony_ci    map.set("test vector insert:", res)
1354514f5e3Sopenharmony_ci
1364514f5e3Sopenharmony_ci    proxy.removeByIndex(9)
1374514f5e3Sopenharmony_ci    testArray.splice(9, 1)
1384514f5e3Sopenharmony_ci    res = true
1394514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1404514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i]) {
1414514f5e3Sopenharmony_ci            res = false
1424514f5e3Sopenharmony_ci        }
1434514f5e3Sopenharmony_ci    }
1444514f5e3Sopenharmony_ci    map.set("test vector removeByIndex:", res)
1454514f5e3Sopenharmony_ci
1464514f5e3Sopenharmony_ci    const removeRes = proxy.remove(6)
1474514f5e3Sopenharmony_ci    testArray.splice(7, 1)
1484514f5e3Sopenharmony_ci    res = true
1494514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1504514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i]) {
1514514f5e3Sopenharmony_ci            res = false
1524514f5e3Sopenharmony_ci        }
1534514f5e3Sopenharmony_ci    }
1544514f5e3Sopenharmony_ci    map.set("test vector remove:", res)
1554514f5e3Sopenharmony_ci    map.set("test vector removeRes:", removeRes)
1564514f5e3Sopenharmony_ci
1574514f5e3Sopenharmony_ci    proxy.removeByRange(1, 3)
1584514f5e3Sopenharmony_ci    testArray.splice(1, 2)
1594514f5e3Sopenharmony_ci    res = true
1604514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1614514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i]) {
1624514f5e3Sopenharmony_ci            res = false
1634514f5e3Sopenharmony_ci        }
1644514f5e3Sopenharmony_ci    }
1654514f5e3Sopenharmony_ci    map.set("test vector removeByRange:", res)
1664514f5e3Sopenharmony_ci
1674514f5e3Sopenharmony_ci    res = true
1684514f5e3Sopenharmony_ci    proxy.forEach((value, index) => {
1694514f5e3Sopenharmony_ci        if (value !== testArray[index]) {
1704514f5e3Sopenharmony_ci            res = false
1714514f5e3Sopenharmony_ci        }
1724514f5e3Sopenharmony_ci    })
1734514f5e3Sopenharmony_ci
1744514f5e3Sopenharmony_ci    map.set("test vector forEach:", res)
1754514f5e3Sopenharmony_ci
1764514f5e3Sopenharmony_ci    proxy.replaceAllElements((item, index) => {
1774514f5e3Sopenharmony_ci        return item * 2
1784514f5e3Sopenharmony_ci    })
1794514f5e3Sopenharmony_ci    res = true
1804514f5e3Sopenharmony_ci    for(let i = 0; i < testArray.length; i++) {
1814514f5e3Sopenharmony_ci        if (proxy[i] !== testArray[i] * 2) {
1824514f5e3Sopenharmony_ci            res = false
1834514f5e3Sopenharmony_ci        }
1844514f5e3Sopenharmony_ci    }
1854514f5e3Sopenharmony_ci    map.set("test vector replaceAllElements:", res)
1864514f5e3Sopenharmony_ci
1874514f5e3Sopenharmony_ci    res = true
1884514f5e3Sopenharmony_ci    let subVector = proxy.subVector(1, 3)
1894514f5e3Sopenharmony_ci    const newtestArray = testArray.slice(1, 3)
1904514f5e3Sopenharmony_ci    for(let i = 0; i < subVector.length; i++) {
1914514f5e3Sopenharmony_ci        if (newtestArray[i] * 2 !== subVector[i]) {
1924514f5e3Sopenharmony_ci            res =  false
1934514f5e3Sopenharmony_ci        }
1944514f5e3Sopenharmony_ci    }
1954514f5e3Sopenharmony_ci    map.set("test vector subVector:", res)
1964514f5e3Sopenharmony_ci
1974514f5e3Sopenharmony_ci    res = true
1984514f5e3Sopenharmony_ci    let j = 0
1994514f5e3Sopenharmony_ci    for (const data of proxy) {
2004514f5e3Sopenharmony_ci      if (data !== testArray[j] * 2) {
2014514f5e3Sopenharmony_ci        res = false
2024514f5e3Sopenharmony_ci      }
2034514f5e3Sopenharmony_ci      j++;
2044514f5e3Sopenharmony_ci    }
2054514f5e3Sopenharmony_ci    map.set("test vector for of:", res)
2064514f5e3Sopenharmony_ci
2074514f5e3Sopenharmony_ci    let itr = proxy[Symbol.iterator]();
2084514f5e3Sopenharmony_ci    let tmp = undefined;
2094514f5e3Sopenharmony_ci    let testArray1 = []
2104514f5e3Sopenharmony_ci    do {
2114514f5e3Sopenharmony_ci      tmp = itr.next().value;
2124514f5e3Sopenharmony_ci      testArray1.push(tmp);
2134514f5e3Sopenharmony_ci    } while (tmp != undefined);
2144514f5e3Sopenharmony_ci
2154514f5e3Sopenharmony_ci    for (let k = 0; k < proxy.length; k++) {
2164514f5e3Sopenharmony_ci      if (testArray1[k] !== testArray[k] * 2) {
2174514f5e3Sopenharmony_ci        res = false
2184514f5e3Sopenharmony_ci      }
2194514f5e3Sopenharmony_ci    }
2204514f5e3Sopenharmony_ci    map.set("test vector Symbol.iterator:", res)
2214514f5e3Sopenharmony_ci
2224514f5e3Sopenharmony_ci    let vector2 = new FastVector();
2234514f5e3Sopenharmony_ci    let proxy1 = new Proxy(vector2, {});
2244514f5e3Sopenharmony_ci    proxy1.add(4);
2254514f5e3Sopenharmony_ci    proxy1.add(3);
2264514f5e3Sopenharmony_ci    proxy1.add(1);
2274514f5e3Sopenharmony_ci    proxy1.add(2);
2284514f5e3Sopenharmony_ci    proxy1.add(0);
2294514f5e3Sopenharmony_ci    proxy1.sort((a,b) => a-b);
2304514f5e3Sopenharmony_ci    res = true
2314514f5e3Sopenharmony_ci    for (let i = 0; i < 5; i++) {
2324514f5e3Sopenharmony_ci        if (proxy1[i] !== i) {
2334514f5e3Sopenharmony_ci            res = false
2344514f5e3Sopenharmony_ci        }
2354514f5e3Sopenharmony_ci    }
2364514f5e3Sopenharmony_ci    map.set("test vector sort:", res)
2374514f5e3Sopenharmony_ci
2384514f5e3Sopenharmony_ci    map.set("test vector get:", proxy1.get(2) === 2)
2394514f5e3Sopenharmony_ci    proxy1.set(3, 7)
2404514f5e3Sopenharmony_ci    map.set("test vector set and get:", proxy1.get(3) === 7)
2414514f5e3Sopenharmony_ci
2424514f5e3Sopenharmony_ci    proxy1.clear()
2434514f5e3Sopenharmony_ci    map.set("test vector clear:", proxy1.length === 0)
2444514f5e3Sopenharmony_ci    map.set("test vector isEmpty:", proxy1.isEmpty())
2454514f5e3Sopenharmony_ci    proxy1.add(4);
2464514f5e3Sopenharmony_ci    proxy1.add(3);
2474514f5e3Sopenharmony_ci    proxy1.add(1);
2484514f5e3Sopenharmony_ci    proxy1.add(2);
2494514f5e3Sopenharmony_ci    proxy1.add(0);
2504514f5e3Sopenharmony_ci    map.set("test vector toString:", proxy1.toString() == "4,3,1,2,0");
2514514f5e3Sopenharmony_ci
2524514f5e3Sopenharmony_ci    res = true
2534514f5e3Sopenharmony_ci    let arr1 = [-1, -1, -1, -1, -1];
2544514f5e3Sopenharmony_ci    proxy1.copyToArray(arr1);
2554514f5e3Sopenharmony_ci    let a = [4, 3, 1, 2, 0];
2564514f5e3Sopenharmony_ci    for (let i = 0; i < a.length; i++) {
2574514f5e3Sopenharmony_ci        if (arr1[i] !== a[i]) {
2584514f5e3Sopenharmony_ci            res = false
2594514f5e3Sopenharmony_ci        }
2604514f5e3Sopenharmony_ci    }
2614514f5e3Sopenharmony_ci    map.set("test vector copyToArray:", res);
2624514f5e3Sopenharmony_ci
2634514f5e3Sopenharmony_ci    proxy1.sort((a,b) => a-b);
2644514f5e3Sopenharmony_ci    res = true
2654514f5e3Sopenharmony_ci    for (let i = 0; i < 5; i++) {
2664514f5e3Sopenharmony_ci        if (proxy1[i] !== i) {
2674514f5e3Sopenharmony_ci            res = false
2684514f5e3Sopenharmony_ci        }
2694514f5e3Sopenharmony_ci    }
2704514f5e3Sopenharmony_ci    map.set("test vector sort:", res)
2714514f5e3Sopenharmony_ci
2724514f5e3Sopenharmony_ci    proxy1.increaseCapacityTo(20)
2734514f5e3Sopenharmony_ci    map.set("test vector increaseCapacityTo:", proxy1.getCapacity() === 20)
2744514f5e3Sopenharmony_ci
2754514f5e3Sopenharmony_ci    proxy1.trimToCurrentLength()
2764514f5e3Sopenharmony_ci    map.set("test vector trimToCurrentLength:", proxy1.getCapacity() === 5)
2774514f5e3Sopenharmony_ci
2784514f5e3Sopenharmony_ci    let v = new FastVector();
2794514f5e3Sopenharmony_ci    for (let i = 0; i < 10; i++) {
2804514f5e3Sopenharmony_ci        v.add(i);
2814514f5e3Sopenharmony_ci    }
2824514f5e3Sopenharmony_ci    v.replaceAllElements((value, key, t) => {
2834514f5e3Sopenharmony_ci        if (key == 8) {
2844514f5e3Sopenharmony_ci            t.clear();
2854514f5e3Sopenharmony_ci            t.trimToCurrentLength();
2864514f5e3Sopenharmony_ci            return 0;
2874514f5e3Sopenharmony_ci        }
2884514f5e3Sopenharmony_ci        return value;
2894514f5e3Sopenharmony_ci    }, v);
2904514f5e3Sopenharmony_ci    map.set("test vector replaceAllElements redefine:", v.length == 0);
2914514f5e3Sopenharmony_ci
2924514f5e3Sopenharmony_ci    flag = undefined;
2934514f5e3Sopenharmony_ci    function elements(value, key, map) {
2944514f5e3Sopenharmony_ci        if (!value) {
2954514f5e3Sopenharmony_ci            if (!flag) {
2964514f5e3Sopenharmony_ci                flag = [];
2974514f5e3Sopenharmony_ci            }
2984514f5e3Sopenharmony_ci            flag.push(key);
2994514f5e3Sopenharmony_ci        }
3004514f5e3Sopenharmony_ci    }
3014514f5e3Sopenharmony_ci
3024514f5e3Sopenharmony_ci    let de = new FastVector();
3034514f5e3Sopenharmony_ci    try {
3044514f5e3Sopenharmony_ci        de.forEach(123);
3054514f5e3Sopenharmony_ci    } catch(err) {
3064514f5e3Sopenharmony_ci        if (err.name != "TypeError") {
3074514f5e3Sopenharmony_ci            print("Vector forEach throw error fail");
3084514f5e3Sopenharmony_ci        }
3094514f5e3Sopenharmony_ci    }
3104514f5e3Sopenharmony_ci    map.forEach(elements);
3114514f5e3Sopenharmony_ci    let test1 = new FastVector();
3124514f5e3Sopenharmony_ci    for (let k = 0; k < 10; k++) {
3134514f5e3Sopenharmony_ci        test1.add(k);
3144514f5e3Sopenharmony_ci    }
3154514f5e3Sopenharmony_ci    var keyName = "";
3164514f5e3Sopenharmony_ci    for (const key in test1) {
3174514f5e3Sopenharmony_ci        keyName += key;
3184514f5e3Sopenharmony_ci    }
3194514f5e3Sopenharmony_ci    if (keyName != "0123456789") {
3204514f5e3Sopenharmony_ci        print("Vector for in fail")
3214514f5e3Sopenharmony_ci    }
3224514f5e3Sopenharmony_ci    if (!flag) {
3234514f5e3Sopenharmony_ci        print("Test Vector success!!!");
3244514f5e3Sopenharmony_ci    } else {
3254514f5e3Sopenharmony_ci        print("Test Vector fail: " + flag);
3264514f5e3Sopenharmony_ci    }
3274514f5e3Sopenharmony_ci}
3284514f5e3Sopenharmony_ciexport let vectorRes = "Test Vector done";