1/*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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:arrayfindlast
18 * @tc.desc:test Array.findLast and Array.findLastIndex
19 * @tc.type: FUNC
20 * @tc.require: issueI7LP2E
21 */
22
23var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
24var result = -1;
25result = arr.findLast((element, index, array) => {
26    array.length = 5;
27    return (element == 5);
28});
29print(result);
30result = arr.findLast((element) => {
31    return element > 2;
32});
33print(result);
34
35result = arr.findLastIndex((element, index, array) => {
36    if (array.length == 5) {
37        array.push(100);
38    }
39    return (element == 100);
40});
41print(result);
42result = arr.findLastIndex((element, index, array) => {
43    return (element == 100);
44});
45print(result);
46
47var arr2 = new Array(1025);
48result = arr2.findLast((element, index, array) => {
49    return (element == undefined);
50});
51print(result);
52
53result = arr2.findLastIndex((element, index, array) => {
54    return (element == undefined);
55});
56print(result);
57
58result = arr2.findLastIndex((element, index, array) => {
59    array[5] = 100;
60    return (element == 100);
61});
62print(result);
63
64var arr3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
65var arr4 = new Array();
66function testFunction(element, index, array) {
67    if (index == 9) {
68        array.length = 6;
69    }
70    return element < 1;
71}
72for (let i = 0; i < 10; i++) arr4[i] = i;
73
74result = arr3.findLast(testFunction);
75print(result);
76result = arr4.findLast(testFunction);
77print(result);
78
79let arr1 = [1, 2, , 7, , undefined];
80arr1.__proto__.push(9);
81arr1.__proto__.push(9);
82arr1.__proto__.push(9);
83function fun1(element){
84    return element === 1;
85}
86print(arr1.findLast(fun1));
87arr1.__proto__.pop(9);
88arr1.__proto__.pop(9);
89arr1.__proto__.pop(9);
90
91//slowPath
92let arr5 = [1, 2, 3, 4, 5, 6, 7, 8];
93function func2(element, index, arr) {
94    return element === 6;
95}
96print(arr5.findLast(func2));
97
98//notFound
99let arr6 = new Array(5, 2, 7);
100function func3(element, index, arr) {
101    arr.length = 3;
102    return element === 100;
103}
104print(arr6.findLast(func3));