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 HashSet RBTree 123 let collisionSet = new fastset(); 124 let count = 0; 125 126 collisionSet.add(1224); 127 collisionSet.add(1288); 128 collisionSet.add(1464); 129 collisionSet.add(4312); 130 collisionSet.add(5128); 131 collisionSet.add(5896); 132 collisionSet.add(6600); 133 collisionSet.add(6776); 134 collisionSet.add(8424); 135 collisionSet.add(9400); 136 collisionSet.forEach((value, key, hashMap) => { 137 count += value; 138 }); 139 if (count != 50512) { 140 print("test RBTree forEach fail. count = " + count); 141 } 142 143 // test isEmpty: false 144 map.set("test isEmpty:", !proxy.isEmpty()); 145 146 proxy.add("ee"); 147 proxy.add("dd"); 148 // test remove: true 149 map.set("test remove:", proxy.remove("bb")); 150 // test clear: true 151 proxy.clear(); 152 map.set("test clear:", proxy.length == 0 && !proxy.has("cc") && proxy.isEmpty()); 153 154 flag = false; 155 let seten = new fastset(); 156 seten.add(1); 157 seten.add(2); 158 seten.add(3); 159 seten.add(4); 160 seten.add(5); 161 let iter = seten.entries(); 162 let temp = iter.next(); 163 while(!temp.done) { 164 if ((temp.value[0]) == (temp.value[1])) { 165 flag = true; 166 } 167 temp = iter.next(); 168 } 169 map.set("test entries return type", flag); 170 flag = false; 171 try { 172 proxy["aa"] = 3; 173 } catch (e) { 174 flag = true; 175 } 176 map.set("test set throw error", flag); 177 178 flag = undefined; 179 function elementsHashSet(valueHash, keyHash, map) { 180 if (!valueHash) { 181 if (!flag) { 182 flag = []; 183 } 184 flag.push(keyHash); 185 } 186 } 187 map.forEach(elementsHashSet); 188 let de = new fastset(); 189 try { 190 de.forEach(123); 191 } catch(err) { 192 if (err.name != "BusinessError") { 193 print("HashSet forEach throw error fail"); 194 } 195 } 196 if (!flag) { 197 print("Test HashSet success!!!"); 198 } else { 199 print("Test HashSet fail: " + flag); 200 } 201} 202export let hashsetRes = "Test hashset done"; 203