1/* 2 * Copyright (c) 2021 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:arraytospliced 18 * @tc.desc:test Array.toSpliced 19 * @tc.type: FUNC 20 * @tc.require: issueI5NO8G 21 */ 22 23const arr0 = [1, , 3, 4, , 6, undefined]; 24print(arr0.indexOf(undefined)); 25const arr1 = arr0.toSpliced(1, 2); 26print(arr0.indexOf(undefined)); 27print(arr1.indexOf(undefined)); 28 29const arr2 = new Array(1025); 30for(let i = 0; i < 1025; i = i + 1) 31 arr2[i] = i; 32const arr3 = arr2.toSpliced(0, 0); 33 34var arr4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 35var arr5 = new Array(); 36for (let i = 0; i < 10; i++) arr5[i] = i; 37 38var result1 = arr4.toSpliced(0, 2, 6); 39print(result1); 40var result2 = arr5.toSpliced(0, 2, 6); 41print(result2); 42 43let outputs = [] 44// 用例 1: 字符串、数字混合数组 45const mixedArray = [1, "two", 3, "four"]; 46const newArray1 = mixedArray.toSpliced(1, 2, "new", 5); 47outputs.push(newArray1); 48 49// 用例 2: 插入布尔值、对象和函数 50const complexArray = [true, { key: "value" }, () => "hello"]; 51const newArray2 = complexArray.toSpliced(2, 0, false, { anotherKey: 42 }, () => "world"); 52outputs.push(newArray2); 53 54// 用例 3: 替换 undefined 和 null 55const arrayWithUndefinedNull = [undefined, null, 3, 4]; 56const newArray3 = arrayWithUndefinedNull.toSpliced(0, 2, "replaced"); 57outputs.push(newArray3); 58 59// 用例 4: 处理稀疏数组 60const sparseArray = [1, , 3, , 5]; 61const newArray4 = sparseArray.toSpliced(1, 1, "new"); 62outputs.push(newArray4); 63if (newArray4[3] !== undefined) { 64 print("fail: hole should be replaced by undefined"); 65} 66 67// 用例 5: 在空数组中添加元素 68const emptyArray = []; 69const newArray5 = emptyArray.toSpliced(0, 0, "first", 2, "third"); 70outputs.push(newArray5); 71 72// 用例 6: 带有符号类型和 Symbol 类型的数组 73const arrayWithSpecialTypes = [Symbol("id"), BigInt(123), "hello"]; 74const newArray6 = arrayWithSpecialTypes.toSpliced(1, 1, "new value"); 75outputs.push(newArray6); 76 77// 用例 7: 从数组中移除所有元素 78const fullArray = [1, 2, 3, 4]; 79const emptyArray2 = fullArray.toSpliced(0, fullArray.length); 80outputs.push(emptyArray2); 81 82print(outputs.map(out => JSON.stringify(out)).join('\n')); 83