• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.TreeSet);
25
26    let map = new Map();
27    let set = new fastset();
28    set.add("aa");
29    set.add("bb");
30
31    // test has: true
32    map.set("test has:", set.length == 2 && set.has("aa") && set.has("bb") && !set.has("cc"));
33
34    set.add("cc");
35    // test getFirstKey and getLastKey: true
36    map.set("test getFirstKey and getLastKey:", set.getFirstValue() == "aa" && set.getLastValue() == "cc");
37    // test getLowerValue and getHigherValue out: true
38    map.set("test getLowerValue and getHigherValue", set.getLowerValue("bb") == "aa" &&
39            set.getLowerValue("aa") == undefined && set.getHigherValue("bb") == "cc" &&
40            set.getHigherValue("cc") == undefined);
41
42    // test values: true
43    let iteratorSetValues = set.values();
44    map.set("test values:", iteratorSetValues.next().value == "aa" && iteratorSetValues.next().value == "bb" &&
45            iteratorSetValues.next().value == "cc" && iteratorSetValues.next().value == undefined);
46    // test entries: [cc, cc], undefined
47    let iteratorSetEntries = set.entries();
48    iteratorSetEntries.next().value;
49    iteratorSetEntries.next().value;
50    map.set("test entries1:", iteratorSetEntries.next().value != undefined);
51    map.set("test entries2:", iteratorSetEntries.next().value == undefined);
52
53    // test forof: aa, bb, cc
54    let arr = ["aa", "bb", "cc"];
55    let i = 0;
56    for (const item of set) {
57        map.set(arr[i], item == arr[i]);
58        i++;
59    }
60
61    // test forin:
62    for (const item in set) {
63        map.set("test forin:", item);
64    }
65
66    // test forEach:
67    let setFlag = false;
68    function TestForEach(value, key, set) {
69        setFlag= set.has(key) && set.has(value);
70        map.set("test forEach" + key, setFlag);
71    }
72    set.forEach(TestForEach);
73
74    // test isEmpty: false
75    map.set("test isEmpty:", !set.isEmpty());
76
77    set.add("ee");
78    set.add("dd");
79    // test popFirst and popLast: true
80    map.set("test popFirst and popLast:", set.length == 5 && set.popFirst() == "aa" &&
81          set.popLast() == "ee" && !set.has("aa"));
82    // test remove: true
83    map.set("test remove:", set.remove("bb") && set.length == 2 && !set.has("bb"));
84    // test clear: true
85    set.clear();
86    map.set("test clear:", set.length == 0 && !set.has("cc") && set.isEmpty());
87
88    let flag = false;
89    try {
90        set["aa"] = 3;
91    } catch (e) {
92        flag = true;
93    }
94    map.set("test set throw error", flag);
95
96    let comset =  new fastset((firstValue, secondValue) => {return firstValue < secondValue});
97    comset.add("c");
98    comset.add("a");
99    comset.add("b");
100    comset.add("d");
101    if (comset.length == 4) {
102        comset.remove("a");
103        comset.remove("b");
104        comset.remove("c");
105        comset.remove("d");
106    }
107    map.set("test commpare", comset.length == 0);
108
109    let set1 = new fastset();
110    let proxy = new Proxy(set1, {});
111    proxy.add("aa");
112    proxy.add("bb");
113
114    // test has: true
115    map.set("test has:", proxy.length == 2 && proxy.has("aa") && proxy.has("bb") && !proxy.has("cc"));
116
117    proxy.add("cc");
118    // test getFirstKey and getLastKey: true
119    map.set("test getFirstKey and getLastKey:", proxy.getFirstValue() == "aa" && proxy.getLastValue() == "cc");
120    // test getLowerValue and getHigherValue out: true
121    map.set("test getLowerValue and getHigherValue", proxy.getLowerValue("bb") == "aa" &&
122            proxy.getLowerValue("aa") == undefined && proxy.getHigherValue("bb") == "cc" &&
123            proxy.getHigherValue("cc") == undefined);
124
125    // test values: true
126    let iteratorSetValues1 = proxy.values();
127    map.set("test values:", iteratorSetValues1.next().value == "aa" && iteratorSetValues1.next().value == "bb" &&
128            iteratorSetValues1.next().value == "cc" && iteratorSetValues1.next().value == undefined);
129    // test entries: [cc, cc], undefined
130    let iteratorSetEntries1 = proxy.entries();
131    iteratorSetEntries1.next().value;
132    iteratorSetEntries1.next().value;
133    map.set("test entries1:", iteratorSetEntries1.next().value != undefined);
134    map.set("test entries2:", iteratorSetEntries1.next().value == undefined);
135
136    // test forof: aa, bb, cc
137    let arr1 = ["aa", "bb", "cc"];
138    let j = 0;
139    for (const item of proxy) {
140        map.set(arr1[j], item == arr1[j]);
141        j++;
142    }
143
144    // test forin:
145    for (const item in proxy) {
146        map.set("test forin:", item);
147    }
148
149    // test forEach:
150    let setFlag1 = false;
151    function TestForEach1(value, key, proxy) {
152        setFlag1 = proxy.has(key) && proxy.has(value);
153        map.set("test forEach" + key, setFlag1);
154    }
155    proxy.forEach(TestForEach1);
156
157    // test isEmpty: false
158    map.set("test isEmpty:", !proxy.isEmpty());
159
160    proxy.add("ee");
161    proxy.add("dd");
162    // test popFirst and popLast: true
163    map.set("test popFirst and popLast:", proxy.length == 5 && proxy.popFirst() == "aa" &&
164            proxy.popLast() == "ee" && !proxy.has("aa"));
165    // test remove: true
166    map.set("test remove:", proxy.remove("bb") && proxy.length == 2 && !proxy.has("bb"));
167    // test clear: true
168    proxy.clear();
169    map.set("test clear:", proxy.length == 0 && !proxy.has("cc") && proxy.isEmpty());
170
171    flag = false;
172    try {
173        proxy["aa"] = 3;
174    } catch (e) {
175        flag = true;
176    }
177    map.set("test set throw error", flag);
178
179    flag = undefined;
180    function elements(value, key, map) {
181        if (!value) {
182            if (!flag) {
183                flag = [];
184            }
185            flag.push(key);
186        }
187    }
188    map.forEach(elements);
189
190    let de = new fastset();
191    try {
192        de.forEach(123);
193    } catch(err) {
194        if (err.name != "BusinessError") {
195            print("TreeSet forEach throw error fail");
196        }
197    }
198    if (!flag) {
199        print("Test TreeSet success!!!");
200    } else {
201        print("Test TreeSet fail: " + flag);
202    }
203}
204