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