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