• 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 Deque = undefined;
23if (globalThis["ArkPrivate"] != undefined) {
24    Deque = ArkPrivate.Load(ArkPrivate.Deque);
25    let deque = new Deque();
26    let proxy = new Proxy(deque, {});
27    let res = true
28    let testArray = []
29    let map = new Map();
30    for(let i = 0; i < 10; i++) {
31        testArray.push(i)
32    }
33    for(let i = 9; i >= 0; --i) {
34        proxy.insertFront(i)
35    }
36
37    res = true
38    for(let i = 0; i < testArray.length; i++) {
39        if (proxy[i] !== testArray[i]) {
40            res = false
41        }
42    }
43    map.set("test deque insertFront:", res)
44
45    for(let i = 10; i < 20; i++) {
46        proxy.insertEnd(i)
47        testArray.push(i)
48    }
49
50    res = true
51    for(let i = 0; i < testArray.length; i++) {
52        if (proxy[i] !== testArray[i]) {
53            res = false
54        }
55    }
56    map.set("test deque insertEnd:", res)
57
58    res = true
59    proxy.forEach((i, d) => {
60        if (d !== testArray[i]) {
61            res = false
62        }
63    })
64
65    map.set("test deque forEach:", res)
66
67    res = true
68    let j = 0
69    for (const data of proxy) {
70      if (data !== testArray[j]) {
71        res = false
72      }
73      j++;
74    }
75    map.set("test deque for of:", res)
76
77    let itr = proxy[Symbol.iterator]();
78    let tmp = undefined;
79    let testArray1 = []
80    do {
81      tmp = itr.next().value;
82      testArray1.push(tmp);
83    } while (tmp != undefined);
84
85    for (let k = 0; k < proxy.length; k++) {
86      if (testArray1[k] !== testArray[k]) {
87        res = false
88      }
89    }
90    map.set("test deque Symbol.iterator:", res)
91
92    map.set("test deque has:",  proxy.has(7))
93    map.set("test deque popFirst:",  proxy.getFirst() === 0)
94    map.set("test deque popLast:",  proxy.getLast() === 19)
95    map.set("test deque popFirst:",  proxy.popFirst() === 0)
96    map.set("test deque popLast:",  proxy.popLast() === 19)
97
98    let flag = undefined;
99    function elements(value, key, map) {
100        if (!value) {
101            if (!flag) {
102                flag = [];
103            }
104            flag.push(key);
105        }
106    }
107    map.forEach(elements);
108    var arr1 = [];
109    let myTest = new Deque();
110    for (let i = 0; i < 10; i++) {
111        myTest.insertEnd(i);
112    }
113    for (let i = 0; i < 5; i++) {
114        myTest.popFirst();
115    }
116    myTest.forEach(
117        function myFunc(item, index, arr) {
118            arr1.push(item);
119        }
120    );
121    for (let j = 5; j < 10; j++) {
122        if (arr1[j - 5] != j) {
123            print("Deque forEach first argv fail");
124        }
125    }
126
127    let de = new Deque();
128    try {
129        de.forEach(123);
130    } catch(err) {
131        if (err.name != "BusinessError") {
132            print("Deque forEach throw error fail");
133        }
134    }
135    let test1 = new Deque();
136    for (let k = 0; k < 10; k++) {
137        test1.insertEnd(k);
138    }
139    var keyName = "";
140    for (const key in test1) {
141        keyName += key;
142    }
143    if (keyName != "0123456789") {
144        print("Deque for in fail")
145    }
146    if (!flag) {
147        print("Test Deque success!!!");
148    } else {
149        print("Test Deque fail: " + flag);
150    }
151}