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 PlainArray = undefined; 23if (globalThis["ArkPrivate"] != undefined) { 24 PlainArray = ArkPrivate.Load(ArkPrivate.PlainArray); 25 26 let map = new Map(); 27 let plainArray = new PlainArray(); 28 let proxy = new Proxy(plainArray, {}); 29 let testArray = ["0", "1", "2", "3", "4", "5"] 30 let res = true 31 let ret = proxy.add(0, "0") 32 proxy.add(1, "1") 33 proxy.add(2, "2") 34 proxy.add(3, "3") 35 proxy.add(4, "4") 36 proxy.add(5, "5") 37 38 for(let i = 0; i < testArray.length; i++) { 39 if (proxy[i] !== testArray[i]) { 40 res = false 41 } 42 } 43 map.set("test plainarray add:", res) 44 map.set("test plainarray 'add' ret:", ret === undefined) 45 map.set("test plainarray length:", proxy.length === 6) 46 map.set("test plainarray has:", proxy.has(2)) 47 map.set("test plainarray getIndexOfValue:", proxy.getIndexOfValue("1") === 1) 48 map.set("test plainarray getIndexOfKey:", proxy.getIndexOfKey(5) === 5) 49 map.set("test plainarray getKeyAt:", proxy.getKeyAt(1) === 1) 50 map.set("test plainarray getValueAt:", proxy.getValueAt(2) === "2") 51 52 let newPlainArray = proxy.clone() 53 res = true 54 for(let i = 0; i < testArray.length; i++) { 55 if (newPlainArray[i] !== testArray[i]) { 56 res = false 57 } 58 } 59 map.set("test plainarray clone:", res) 60 61 const removeRes = proxy.remove(3) 62 testArray.splice(3, 1) 63 map.set("test plainarray removeRes:", removeRes) 64 65 proxy.removeAt(2) 66 testArray.splice(2, 1) 67 res = true 68 for(let i = 0; i < testArray.length; i++) { 69 if (proxy.get(i) !== testArray[i]) { 70 res = false 71 } 72 } 73 74 newPlainArray = proxy.clone() 75 res = proxy.removeRangeFrom(1, 2) 76 testArray.splice(1, 2) 77 if (res > 0) { 78 res = newPlainArray.removeRangeFrom(0, 100) 79 if (res > 0) { 80 res = newPlainArray.isEmpty() 81 } 82 } 83 map.set("test plainarray removeRangeFrom:", res) 84 85 res = true 86 proxy.forEach((i, d) => { 87 }) 88 89 map.set("test plainarray forEach:", res) 90 91 res = true 92 let testArray3 = [0, 5] 93 let j = 0 94 for (const data of proxy) { 95 } 96 map.set("test plainarray for of:", res) 97 98 res = true 99 let itr = proxy[Symbol.iterator](); 100 let tmp = undefined; 101 let testArray1 = [] 102 do { 103 tmp = itr.next().value; 104 testArray1.push(tmp); 105 } while (tmp != undefined); 106 map.set("test plainarray Symbol.iterator:", res) 107 108 let arr2 = new PlainArray(); 109 let proxy1 = new Proxy(arr2, {}); 110 proxy1.add(0, "0") 111 proxy1.add(1, "1") 112 proxy1.add(2, "2") 113 proxy1.add(3, "3") 114 proxy1.add(4, "4") 115 proxy1.add(5, "5") 116 proxy1.setValueAt(2, "123") 117 map.set("test plainarray setValueAt and get:", proxy1.get(2) === "123") 118 ret = proxy1.clear() 119 map.set("test plainarray clear:", proxy1.length === 0) 120 map.set("test plainarray 'clear' ret:", ret === undefined) 121 map.set("test plainarray isEmpty:", proxy1.isEmpty()) 122 proxy1.add(0, "0") 123 proxy1.add(1, "1") 124 proxy1.add(2, "2") 125 proxy1.add(3, "3") 126 proxy1.add(4, "4") 127 proxy1.add(5, "5") 128 map.set("test plainarray toString:", proxy1.toString() == "0:0,1:1,2:2,3:3,4:4,5:5"); 129 let empty_pa = new PlainArray(); 130 try { 131 empty_pa.getValueAt(0); 132 } catch(err) { 133 res = (err =="BusinessError: Container is empty") 134 map.set("test getValueAt exception when arraylist is empty:", res) 135 } 136 try { 137 empty_pa.removeRangeFrom(0, 1); 138 } catch(err) { 139 res = (err =="BusinessError: Container is empty") 140 map.set("test removeRangeFrom exception when arraylist is empty:", res) 141 } 142 try { 143 empty_pa.setValueAt(0); 144 } catch(err) { 145 res = (err =="BusinessError: Container is empty") 146 map.set("test setValueAt exception when arraylist is empty:", res) 147 } 148 149 try { 150 let myPlainArray = new PlainArray(); 151 myPlainArray.add(1); 152 myPlainArray[2147483648]; 153 } catch(err) { 154 let overFlowTest = (err == "BusinessError: The type of \"index\" must be small integer."); 155 map.set("test PlainArray[i] overFlowTest:", overFlowTest); 156 } 157 158 res = undefined; 159 function elements(value, key, map) { 160 if (!value) { 161 if (!res) { 162 res = []; 163 } 164 res.push(key); 165 } 166 } 167 map.forEach(elements); 168 169 let de = new PlainArray(); 170 try { 171 de.forEach(123); 172 } catch(err) { 173 if (err.name != "BusinessError") { 174 print("PlainArray forEach throw error fail"); 175 } 176 } 177 178 // Math.floor as index input should not throw exception. 179 let myPa = new PlainArray(); 180 myPa.add(0, "a"); 181 myPa.add(Math.floor(1.5), "b"); 182 myPa.has(Math.floor(1.5)); 183 myPa.get(Math.floor(1.5)); 184 myPa.getValueAt(Math.floor(1.5)); 185 myPa.setValueAt(Math.floor(1.5), "c"); 186 myPa.getKeyAt(Math.floor(1.5)); 187 myPa.getIndexOfKey(Math.floor(1.5)); 188 myPa.removeAt(Math.floor(1.5)); 189 190 if (!res) { 191 print("Test PlainArray success!!!"); 192 } else { 193 print("Test PlainArray fail: " + res); 194 } 195 const v6 = new PlainArray() 196 function f2(a3) { 197 return a3 198 } 199 const o5 = { 200 "get" : f2, 201 } 202 const v7 = new Proxy(v6, o5) 203 try { 204 v7[1073741823] 205 } catch (error) { 206 print(error) 207 } 208} 209export let plainarrayRes = "Test PlainArray done"; 210