• 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:
21 */
22var Queue = undefined;
23if (globalThis["ArkPrivate"] != undefined) {
24    Queue = ArkPrivate.Load(ArkPrivate.Queue);
25    let queue = new Queue();
26    let proxy = new Proxy(queue, {});
27    let res = true
28    let testArray = []
29    let map = new Map();
30    for(let i = 0; i < 10; i++) {
31        proxy.add(i)
32        testArray.push(i)
33    }
34
35    res = true
36    for(let i = 0; i < testArray.length; i++) {
37        if (proxy[i] !== testArray[i]) {
38            res = false
39        }
40    }
41    map.set("test queue add:", res)
42
43    res = true
44    proxy.forEach((i, d) => {
45        if (d !== testArray[i]) {
46            res = false
47        }
48    })
49
50    map.set("test queue forEach:", res)
51
52    res = true
53    let j = 0
54    for (const data of proxy) {
55      if (data !== testArray[j]) {
56        res = false
57      }
58      j++;
59    }
60    map.set("test queue for of:", res)
61
62    let itr = proxy[Symbol.iterator]();
63    let tmp = undefined;
64    let testArray1 = []
65    do {
66      tmp = itr.next().value;
67      testArray1.push(tmp);
68    } while (tmp != undefined);
69
70    for (let k = 0; k < proxy.length; k++) {
71      if (testArray1[k] !== testArray[k]) {
72        res = false
73      }
74    }
75    map.set("test queue Symbol.iterator:", res)
76
77    map.set("test queue popFirst:",  proxy.getFirst() === 0)
78    map.set("test queue pop:",  proxy.pop() === 0)
79
80    let flag = undefined;
81    function elements(value, key, map) {
82        if (!value) {
83            if (!flag) {
84                flag = [];
85            }
86            flag.push(key);
87        }
88    }
89
90    let myTest = new Queue();
91    var arr1 = [];
92    for (var i = 0; i < 10; i++) {
93        myTest.add(i);
94    }
95    for (var i = 0; i < 5; i++) {
96        myTest.pop();
97    }
98    myTest.forEach(
99        function myFunc(item, index, arr) {
100            arr1.push(item);
101        }
102    );
103    for (let j = 5; j < 10; j++) {
104        if (arr1[j - 5] != j) {
105            print("Queue forEach first argv fail");
106        }
107    }
108
109    let de = new Queue();
110    try {
111        de.forEach(123);
112    } catch(err) {
113        if (err.name != "BusinessError") {
114            print("Queue forEach throw error fail");
115        }
116    }
117
118    map.forEach(elements);
119    if (!flag) {
120        print("Test Queue success!!!");
121    } else {
122        print("Test Queue fail: " + flag);
123    }
124}