1/* 2 * Copyright (c) 2023 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:arraySlice 18 * @tc.desc:test array.slice 19 * @tc.type: FUNC 20 * @tc.require: 21 */ 22 23const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; 24 25print(animals.slice(2)); 26// Expected output: Array ["camel", "duck", "elephant"] 27 28print(animals.slice(2, 4)); 29// Expected output: Array ["camel", "duck"] 30 31print(animals.slice(1, 5)); 32// Expected output: Array ["bison", "camel", "duck", "elephant"] 33 34print(animals.slice(-2)); 35// Expected output: Array ["duck", "elephant"] 36 37print(animals.slice(2, -1)); 38// Expected output: Array ["camel", "duck"] 39 40print(animals.slice()); 41// Expected output: Array ["ant", "bison", "camel", "duck", "elephant"] 42 43print([1, 2, , 4, 5].slice(1, 4)); // [2, empty, 4] 44const arrayLike = { 45 length: 3, 46 0: 2, 47 1: 3, 48 2: 4, 49}; 50print(Array.prototype.slice.call(arrayLike, 1, 3)); 51 52const slice = Function.prototype.call.bind(Array.prototype.slice); 53function list() { 54 return slice(arguments); 55} 56 57const list1 = list(1, 2, 3); // [1, 2, 3] 58print(list1); 59 60var srcArr = [0, 1, true, null, new Object(), "five"]; 61srcArr[9999999] = -6.6; 62var resArr = srcArr.slice(1, 3); 63print(resArr); 64 65var A = function(_length) { 66 this.length = 0; 67 Object.preventExtensions(this); 68}; 69var arr = [1]; 70arr.constructor = {}; 71arr.constructor[Symbol.species] = A; 72arr.slice(2); 73print(arr); 74