1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/*
17 * @tc.name:container
18 * @tc.desc:test container
19 * @tc.type: FUNC
20 * @tc.require: issueI5NO8G
21 */
22var fastset = undefined;
23if (globalThis["ArkPrivate"] != undefined) {
24    fastset = ArkPrivate.Load(ArkPrivate.LightWeightSet);
25
26    let res = new Map();
27    let set = new fastset();
28    let proxy = new Proxy(set, {});
29    // test isEmpty: true
30    res.set("test isEmpty:", proxy.isEmpty());
31    proxy.add(1);
32    proxy.add(2);
33    proxy.add(3);
34    // test has: true
35    res.set("test has 1:", proxy.has(1));
36    res.set("test has 2:", proxy.has(2));
37    res.set("test has 3:", proxy.has(3));
38    // test has: false
39    res.set("test has 4:", proxy.has(4) == false);
40    // test values: true
41    let iteratorValues1 = proxy.values();
42    res.set("test values:", iteratorValues1.next().value == 1 && iteratorValues1.next().value == 2 &&
43            iteratorValues1.next().value == 3 && iteratorValues1.next().value == undefined);
44    // test entries: [c,cc], undefined
45    let iteratorEntries1 = proxy.entries();
46    iteratorEntries1.next().value;
47    iteratorEntries1.next().value;
48    res.set("test entries1:", iteratorEntries1.next().value != undefined);
49    res.set("itest entries2:", iteratorEntries1.next().value == undefined);
50
51    // test forof
52    let arr1 = [1, 2, 3];
53    let j = 0;
54    for (const item of proxy) {
55    }
56    // test forin:
57    for (const item in proxy) {
58        res.set("test forin", true);
59    }
60    // test forEach:
61    let flag = false;
62    function TestForEach1(value, key, proxy) {
63        flag = proxy.has(key) && proxy.has(value);
64        res.set("test forEach" + key, flag);
65    }
66    proxy.forEach(TestForEach1);
67
68    let dset = new fastset();
69    let dProxy = new Proxy(dset, {});
70    dProxy.add(4);
71    dProxy.add(5);
72    dProxy.add(6);
73    dProxy.add(7);
74    dProxy.add(8);
75    dProxy.add(9);
76    res.set("test addAll:", dProxy.addAll(proxy));
77    res.set("test hasAll:", dProxy.hasAll(proxy));
78    let obj = ["a", "b"]
79    res.set("test equal:", !dProxy.equal(obj));
80    // test remove: true
81    res.set("test remove:", dProxy.remove(1) === 1 && dProxy.length === 8);
82    // test removeAt: true
83    res.set("test removeAt:", dProxy.removeAt(3) && dProxy.length === 7);
84    // test setValueAt: true
85    res.set("test getValueAt:", dProxy.getValueAt(3) === 6);
86    // test setValueAt: true
87    res.set("test getIndexOf:", dProxy.getIndexOf(2) === 0);
88    // test toString: true
89    res.set("test toString:", dProxy.toString() === "2,3,4,6,7,8,9");
90    let arr = dProxy.toArray()
91    res.set("test toArray:", true);
92    // test increaseCapacityTo: true
93    dProxy.increaseCapacityTo(20)
94    res.set("test increaseCapacityTo:", true);
95    // test clear: 0
96    let ret = dProxy.clear();
97    res.set("test clear:", dProxy.length == 0);
98    res.set("test 'clear' ret:", ret === undefined)
99
100    // test COW
101    let LOOP_COUNT = 5;
102    let myTest = new fastset();
103
104    for (var i = 0; i < LOOP_COUNT; i++) {
105        myTest.add(i);
106    }
107
108    let a = myTest.toArray();
109    res.set("test COW - create a :", a.length == LOOP_COUNT);
110    myTest.add(10);
111    res.set("test COW - check a.length :", a.length == LOOP_COUNT);
112    let b = myTest.toArray();
113    res.set("test COW - check b.length :", b.length == (LOOP_COUNT + 1));
114
115    flag = false;
116    let seten = new fastset();
117    seten.add(1);
118    seten.add(2);
119    seten.add(3);
120    seten.add(4);
121    seten.add(5);
122    let iter = seten.entries();
123    let temp = iter.next();
124    while(!temp.done) {
125        if ((temp.value[0]) == (temp.value[1])) {
126            flag = true;
127        }
128        temp = iter.next();
129    }
130    res.set("test entries return type", flag);
131
132    flag = false;
133    try {
134        proxy["aa"] = 3;
135    } catch (e) {
136        flag = true;
137    }
138    res.set("test map throw error", flag);
139
140    flag = undefined;
141    function elements(value, key, res) {
142        if (!value) {
143            if (!flag) {
144                flag = [];
145            }
146            flag.push(key);
147        }
148    }
149    res.forEach(elements);
150
151    let de = new fastset();
152    try {
153        de.forEach(123);
154    } catch(err) {
155        if (err.name != "BusinessError") {
156            print("LightWeightSet forEach throw error fail");
157        }
158    }
159
160    // Math.floor be index input should not throw exception.
161    let myLs = new fastset();
162    myLs.add("a");
163    myLs.add("b");
164    myLs.getValueAt(Math.floor(1.5));
165    myLs.removeAt(Math.floor(1.5));
166
167    if (!flag) {
168        print("Test LightWeightSet success!!!");
169    } else {
170        print("Test LightWeightSet fail: " + flag);
171    }
172}
173export let lightweightsetRes = "Test LightWeightSet done";
174