• 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;
23class c{
24    n = 0;
25    constructor(a){
26      this.n = a;
27    }
28  }
29if (globalThis["ArkPrivate"] != undefined) {
30    fastset = ArkPrivate.Load(ArkPrivate.TreeSet);
31
32    let map = new Map();
33    let set = new fastset();
34    set.add("aa");
35    set.add("bb");
36
37    // test has: true
38    map.set("test has:", set.length == 2 && set.has("aa") && set.has("bb") && !set.has("cc"));
39
40    set.add("cc");
41    // test getFirstKey and getLastKey: true
42    map.set("test getFirstKey and getLastKey:", set.getFirstValue() == "aa" && set.getLastValue() == "cc");
43    // test getLowerValue and getHigherValue out: true
44    map.set("test getLowerValue and getHigherValue", set.getLowerValue("bb") == "aa" &&
45            set.getLowerValue("aa") == undefined && set.getHigherValue("bb") == "cc" &&
46            set.getHigherValue("cc") == undefined);
47
48    // test values: true
49    let iteratorSetValues = set.values();
50    map.set("test values:", iteratorSetValues.next().value == "aa" && iteratorSetValues.next().value == "bb" &&
51            iteratorSetValues.next().value == "cc" && iteratorSetValues.next().value == undefined);
52    // test entries: [cc, cc], undefined
53    let iteratorSetEntries = set.entries();
54    iteratorSetEntries.next().value;
55    iteratorSetEntries.next().value;
56    map.set("test entries1:", iteratorSetEntries.next().value != undefined);
57    map.set("test entries2:", iteratorSetEntries.next().value == undefined);
58
59    // test forof: aa, bb, cc
60    let arr = ["aa", "bb", "cc"];
61    let i = 0;
62    for (const item of set) {
63        map.set(arr[i], item == arr[i]);
64        i++;
65    }
66
67    // test forin:
68    for (const item in set) {
69        map.set("test forin:", item);
70    }
71
72    // test forEach:
73    let setFlag = false;
74    function TestForEach(value, key, set) {
75        setFlag= set.has(key) && set.has(value);
76        map.set("test forEach" + key, setFlag);
77    }
78    set.forEach(TestForEach);
79
80    // test isEmpty: false
81    map.set("test isEmpty:", !set.isEmpty());
82
83    set.add("ee");
84    set.add("dd");
85    // test popFirst and popLast: true
86    map.set("test popFirst and popLast:", set.length == 5 && set.popFirst() == "aa" &&
87          set.popLast() == "ee" && !set.has("aa"));
88    // test remove: true
89    map.set("test remove:", set.remove("bb") && set.length == 2 && !set.has("bb"));
90    // test clear: true
91    set.clear();
92    map.set("test clear:", set.length == 0 && !set.has("cc") && set.isEmpty());
93
94    let flag = false;
95    try {
96        set["aa"] = 3;
97    } catch (e) {
98        flag = true;
99    }
100    map.set("test set throw error", flag);
101
102    let cmmp = new fastset((firstValue, secondValue) => {return firstValue > secondValue});
103    cmmp.add("aa");
104    cmmp.add("bb");
105    map.set("test has undefined", cmmp.has(undefined) == false);
106    map.set("test has null", cmmp.has(null) == false);
107    cmmp.add(null, 1);
108    cmmp.add(undefined, 1);
109    map.set("test has undefined false", cmmp.has(undefined) == true);
110    map.set("test has null false", cmmp.has(null) == true);
111
112    let comset =  new fastset((firstValue, secondValue) => {return firstValue < secondValue});
113    comset.add("c");
114    comset.add("a");
115    comset.add("b");
116    comset.add("d");
117    if (comset.length == 4) {
118        comset.remove("a");
119        comset.remove("b");
120        comset.remove("c");
121        comset.remove("d");
122    }
123    map.set("test commpare", comset.length == 0);
124
125    class Person {
126        id = 0;
127        name = '';
128        constructor(id, name) {
129            this.id = id;
130            this.name = name;
131        }
132    }
133
134    comset =  new fastset((firstValue, secondValue) => {return firstValue.id < secondValue.id});
135    let personone = new Person(3,'张三');
136    let persontwo = new Person(1,'李四');
137    let personsec = new Person(2,'王五');
138    comset.add(personone);
139    comset.add(persontwo);
140    comset.add(personsec);
141    map.set("test clear and set", comset.getFirstValue().id === 1);
142    comset.clear();
143    comset =  new fastset((firstValue, secondValue) => {return firstValue > secondValue});
144    comset.add("c");
145    comset.add("a");
146    comset.add("b");
147    comset.add("d");
148    comset.clear();
149    comset.add("c");
150    comset.add("a");
151    comset.add("b");
152    comset.add("d");
153    map.set("test clear and set", comset.getFirstValue() === "d");
154
155    let set1 = new fastset();
156    let proxy = new Proxy(set1, {});
157    proxy.add("aa");
158    proxy.add("bb");
159
160    // test has: true
161    map.set("test has:", proxy.length == 2 && proxy.has("aa") && proxy.has("bb") && !proxy.has("cc"));
162
163    proxy.add("cc");
164    // test getFirstKey and getLastKey: true
165    map.set("test getFirstKey and getLastKey:", proxy.getFirstValue() == "aa" && proxy.getLastValue() == "cc");
166    // test getLowerValue and getHigherValue out: true
167    map.set("test getLowerValue and getHigherValue", proxy.getLowerValue("bb") == "aa" &&
168            proxy.getLowerValue("aa") == undefined && proxy.getHigherValue("bb") == "cc" &&
169            proxy.getHigherValue("cc") == undefined);
170
171    // test values: true
172    let iteratorSetValues1 = proxy.values();
173    map.set("test values:", iteratorSetValues1.next().value == "aa" && iteratorSetValues1.next().value == "bb" &&
174            iteratorSetValues1.next().value == "cc" && iteratorSetValues1.next().value == undefined);
175    // test entries: [cc, cc], undefined
176    let iteratorSetEntries1 = proxy.entries();
177    iteratorSetEntries1.next().value;
178    iteratorSetEntries1.next().value;
179    map.set("test entries1:", iteratorSetEntries1.next().value != undefined);
180    map.set("test entries2:", iteratorSetEntries1.next().value == undefined);
181
182    // test forof: aa, bb, cc
183    let arr1 = ["aa", "bb", "cc"];
184    let j = 0;
185    for (const item of proxy) {
186        map.set(arr1[j], item == arr1[j]);
187        j++;
188    }
189
190    // test forin:
191    for (const item in proxy) {
192        map.set("test forin:", item);
193    }
194
195    // test forEach:
196    let setFlag1 = false;
197    function TestForEach1(value, key, proxy) {
198        setFlag1 = proxy.has(key) && proxy.has(value);
199        map.set("test forEach" + key, setFlag1);
200    }
201    proxy.forEach(TestForEach1);
202
203    // test isEmpty: false
204    map.set("test isEmpty:", !proxy.isEmpty());
205
206    proxy.add("ee");
207    proxy.add("dd");
208    // test popFirst and popLast: true
209    map.set("test popFirst and popLast:", proxy.length == 5 && proxy.popFirst() == "aa" &&
210            proxy.popLast() == "ee" && !proxy.has("aa"));
211    // test remove: true
212    map.set("test remove:", proxy.remove("bb") && proxy.length == 2 && !proxy.has("bb"));
213    // test clear: true
214    proxy.clear();
215    map.set("test clear:", proxy.length == 0 && !proxy.has("cc") && proxy.isEmpty());
216
217    flag = false;
218    try {
219        proxy["aa"] = 3;
220    } catch (e) {
221        flag = true;
222    }
223    map.set("test set throw error", flag);
224
225    // test getLower & getHigher when object
226    let newset = new fastset((x, y)=> x.n < y.n);
227    newset.add(new c(3));
228    let tmp_c = new c(5);
229    newset.add(tmp_c);
230    newset.add(undefined);
231    newset.add(new c(1));
232    newset.add(null);
233    map.set("test getHigher no.1:", newset.getHigherValue(new c(3)).n == 5);
234    map.set("test getHigher no.2:", newset.getHigherValue(new c(5)) == null);
235    map.set("test getHigher no.3:", newset.getHigherValue(null) == undefined);
236    map.set("test getLower no.1:", newset.getLowerValue(new c(3)).n == 1);
237    map.set("test getLower no.2:", newset.getLowerValue(undefined) == null);
238    map.set("test getLower no.3:", newset.getLowerValue(null) == tmp_c);
239{
240    let temp = ArkTools.getAPIVersion();
241    ArkTools.setAPIVersion(20);
242    class A {
243        time = 0;
244        constructor(time) {
245            this.time = time;
246        }
247    }
248    let set = new fastset((first, second) => {
249        return first.time - second.time;
250    });
251    const a1 = new A(1);
252    const a2 = new A(2);
253    const a3 = new A(3);
254    const a4 = new A(4);
255    const a5 = new A(5);
256    set.add(a1);
257    set.add(a2);
258    set.add(a3);
259    set.add(a4);
260    set.add(a5);
261    for (let i = 0; i < 5; i++) {
262        set.remove(a1);
263        let ok = set.has(a1);
264        map.set("test add and remove(after fixed) failed, expect:" + false + ", output:" + ok, ok === false);
265        set.add(a1);
266    }
267    ArkTools.setAPIVersion(temp);
268}
269
270{
271    class A {
272        time = 0;
273        constructor(time) {
274            this.time = time;
275        }
276    }
277    let set = new fastset((first, second) => {
278        return first.time - second.time;
279    });
280    const a1 = new A(1);
281    const a2 = new A(2);
282    const a3 = new A(3);
283    const a4 = new A(4);
284    const a5 = new A(5);
285    set.add(a1);
286    set.add(a2);
287    set.add(a3);
288    set.add(a4);
289    set.add(a5);
290    let res = [false, false, false, true, true];
291    for (let i = 0; i < 5; i++) {
292        set.remove(a1);
293        let ok = set.has(a1);
294        map.set("test add and remove(before fixed) failed, expect:" + res[i] + ", output:" + ok, ok === res[i]);
295        set.add(a1);
296    }
297}
298
299    flag = undefined;
300    function elementsTreeSet(valueTreeSet, keyTreeSet, map) {
301        if (!valueTreeSet) {
302            if (!flag) {
303                flag = [];
304            }
305            flag.push(keyTreeSet);
306        }
307    }
308    map.forEach(elementsTreeSet);
309
310    let de = new fastset();
311    try {
312        de.forEach(123);
313    } catch(err) {
314        if (err.name != "BusinessError") {
315            print("TreeSet forEach throw error fail");
316        }
317    }
318    if (!flag) {
319        print("Test TreeSet success!!!");
320    } else {
321        print("Test TreeSet fail: " + flag);
322    }
323    let treeSet = new fastset((first,second) =>{
324        return first > second
325    });
326    let insertArr = [
327        643,
328        811,
329        807,
330        378,
331        226,
332        195,
333        599,
334        641,
335        494,
336        964,
337        156,
338        419,
339        977,
340        20,
341        788,
342        596
343    ]
344    let addItem = function(obj){
345        treeSet.add(obj)
346    }
347    let removeItem = function(){
348        const first = treeSet.getFirstValue()
349        treeSet.remove(first)
350    }
351    for(let i = 0;i < insertArr.length;i++) {
352        addItem(insertArr[i])
353    }
354    removeItem()
355    removeItem()
356    removeItem()
357    removeItem()
358    removeItem()
359    addItem(664)
360    removeItem()
361    removeItem()
362    removeItem()
363    removeItem()
364    removeItem()
365    removeItem()
366    removeItem()
367    removeItem()
368    let resArr = []
369    treeSet.forEach(element => {
370        resArr.push(element)
371    });
372    print(resArr)
373}
374export let treesetRes = "Test TreeSet done";