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 fastset = undefined; 23if (globalThis["ArkPrivate"] != undefined) { 24 fastset = ArkPrivate.Load(ArkPrivate.HashSet); 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 36 // test values: true 37 let iteratorSetValues = set.values(); 38 map.set("test values:", iteratorSetValues.next().value == "aa" && iteratorSetValues.next().value == "bb" && 39 iteratorSetValues.next().value == "cc" && iteratorSetValues.next().value == undefined); 40 // test entries: [cc, cc], undefined 41 let iteratorSetEntries = set.entries(); 42 iteratorSetEntries.next().value; 43 iteratorSetEntries.next().value; 44 map.set("test entries1:", iteratorSetEntries.next().value != undefined); 45 map.set("test entries2:", iteratorSetEntries.next().value == undefined); 46 47 // test forof: aa, bb, cc 48 let arr = ["aa", "bb", "cc"]; 49 let i = 0; 50 for (const item of set) { 51 map.set(arr[i], item == arr[i]); 52 i++; 53 } 54 55 // test forin: 56 for (const item in set) { 57 map.set("test forin:", item); 58 } 59 60 // test forEach: 61 set.forEach((i, d) => { 62 }); 63 map.set("test forEach:", true); 64 // test isEmpty: false 65 map.set("test isEmpty:", !set.isEmpty()); 66 67 set.add("ee"); 68 set.add("dd"); 69 // test remove: true 70 map.set("test remove:", set.remove("bb")); 71 // test clear: true 72 set.clear(); 73 map.set("test clear:", set.length == 0 && !set.has("cc") && set.isEmpty()); 74 75 let flag = false; 76 try { 77 set["aa"] = 3; 78 } catch (e) { 79 flag = true; 80 } 81 map.set("test set throw error", flag); 82 83 let set1 = new fastset(); 84 let proxy = new Proxy(set1, {}); 85 proxy.add("aa"); 86 proxy.add("bb"); 87 88 // test has: true 89 map.set("test has:", proxy.length == 2 && proxy.has("aa") && proxy.has("bb") && !proxy.has("cc")); 90 91 proxy.add("cc"); 92 93 // test values: true 94 let iteratorSetValues1 = proxy.values(); 95 map.set("test values:", iteratorSetValues1.next().value == "aa" && iteratorSetValues1.next().value == "bb" && 96 iteratorSetValues1.next().value == "cc" && iteratorSetValues1.next().value == undefined); 97 // test entries: [cc, cc], undefined 98 let iteratorSetEntries1 = proxy.entries(); 99 iteratorSetEntries1.next().value; 100 iteratorSetEntries1.next().value; 101 map.set("test entries1:", iteratorSetEntries1.next().value != undefined); 102 map.set("test entries2:", iteratorSetEntries1.next().value == undefined); 103 104 // test forof: aa, bb, cc 105 let arr1 = ["aa", "bb", "cc"]; 106 let j = 0; 107 for (const item of proxy) { 108 map.set(arr1[j], item == arr1[j]); 109 j++; 110 } 111 112 // test forin: 113 for (const item in proxy) { 114 map.set("test forin:", item); 115 } 116 117 // test forEach: 118 proxy.forEach((i, d) => { 119 }) 120 map.set("test forEach:", true); 121 122 // test isEmpty: false 123 map.set("test isEmpty:", !proxy.isEmpty()); 124 125 proxy.add("ee"); 126 proxy.add("dd"); 127 // test remove: true 128 map.set("test remove:", proxy.remove("bb")); 129 // test clear: true 130 proxy.clear(); 131 map.set("test clear:", proxy.length == 0 && !proxy.has("cc") && proxy.isEmpty()); 132 133 flag = false; 134 let seten = new fastset(); 135 seten.add(1); 136 seten.add(2); 137 seten.add(3); 138 seten.add(4); 139 seten.add(5); 140 let iter = seten.entries(); 141 let temp = iter.next(); 142 while(!temp.done) { 143 if ((temp.value[0]) == (temp.value[1])) { 144 flag = true; 145 } 146 temp = iter.next(); 147 } 148 map.set("test entries return type", flag); 149 flag = false; 150 try { 151 proxy["aa"] = 3; 152 } catch (e) { 153 flag = true; 154 } 155 map.set("test set throw error", flag); 156 157 flag = undefined; 158 function elementsHashSet(valueHash, keyHash, map) { 159 if (!valueHash) { 160 if (!flag) { 161 flag = []; 162 } 163 flag.push(keyHash); 164 } 165 } 166 map.forEach(elementsHashSet); 167 let de = new fastset(); 168 try { 169 de.forEach(123); 170 } catch(err) { 171 if (err.name != "BusinessError") { 172 print("HashSet forEach throw error fail"); 173 } 174 } 175 if (!flag) { 176 print("Test HashSet success!!!"); 177 } else { 178 print("Test HashSet fail: " + flag); 179 } 180} 181export let hashsetRes = "Test hashset done";