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:array 18 * @tc.desc:test Array 19 * @tc.type: FUNC 20 * @tc.require: issueI5NO8G 21 */ 22var arr = new Array(100); 23var a = arr.slice(90, 100); 24assert_equal(a.length, 10); 25 26var arr1 = [1, 1, 1, 1, 1, 1]; 27arr1.fill(0, 2, 4); 28assert_equal(arr1, [1,1,0,0,1,1]); 29 30var arr2 = new Array(100); 31arr2.fill(0, 2, 4); 32var a1 = arr2.slice(0, 5); 33assert_equal(arr2.length, 100); 34assert_equal(a1, [,,0,0,,]); 35 36var arr3 = [1, 2, 3, 4, 5, 6]; 37arr3.pop(); 38assert_equal(arr3.length, 5); 39assert_equal(arr3, [1,2,3,4,5]); 40 41var arr4 = [1, 3, 4]; 42arr4.splice(1, 0, 2); 43assert_equal(arr4.length, 4); 44assert_equal(arr4, [1,2,3,4]); 45// 1, 2, 3, 4 46var arr4 = [1, 2, 3, 4]; 47arr4.splice(3, 1, 3); 48assert_equal(arr4.length, 4); 49assert_equal(arr4, [1,2,3,3]); 50// 1, 2, 3, 3 51 52var arr5 = [1, 2, 3, 4, 5, 6]; 53arr5.shift(); 54assert_equal(arr5.length, 5); 55assert_equal(arr5, [2,3,4,5,6]); 56// 1, 2, 3, 4, 5 57 58var arr6 = [1, 2, 3, 4, 5]; 59arr6.reverse(); 60assert_equal(arr6, [5,4,3,2,1]); 61 62var arr7 = []; 63arr7[2] = 10; 64assert_equal(arr7.pop(), 10); // 10 65assert_equal(arr7.pop(), undefined); // undefined 66assert_equal(arr7.pop(), undefined); // undefined 67 68var arr8 = []; 69arr8[1] = 8; 70assert_equal(arr8.shift(), undefined); // undefined 71assert_equal(arr8.shift(), 8); // 8 72assert_equal(arr8.shift(), undefined); // undefined 73 74// Test on Array::Splice 75var arr9 = new Array(9); 76assert_equal(arr9.length, 9); // 9 77arr9.splice(0, 9); 78assert_equal(arr9.length, 0); // 0 79 80var arr10 = new Array(9); 81assert_equal(arr10.length, 9); // 9 82arr10.splice(0, 8, 1); 83assert_equal(arr10.length, 2); // 2 84assert_equal(arr10[0], 1); 85assert_equal(arr10[1], undefined); 86 87var arr11 = new Array(9); 88assert_equal(arr11.length, 9); // 9 89arr11.splice(1, 8, 1); 90assert_equal(arr11.length, 2); // 2 91assert_equal(arr11[0], undefined); 92assert_equal(arr11[1], 1); 93 94var arr12 = new Array(9); 95assert_equal(arr12.length, 9); // 9 96arr12.splice(0, 4, 1, 2, 3, 4, 5); 97assert_equal(arr12.length, 10); // 10 98for (let i = 0; i < arr12.length; i++) { 99 let v = arr12[i]; 100 if (i < 5) { 101 assert_equal(v, i + 1); 102 } else { 103 assert_equal(v, undefined); 104 } 105} 106 107var arr13 = new Array(9); 108assert_equal(arr13.length, 9); // 9 109arr13.splice(7, 5, 1, 2, 3); 110assert_equal(arr13.length, 10); // 10 111for (var i = 0; i < arr13.length; i++) { 112 let v = arr13[i]; 113 if (i < 7) { 114 assert_equal(v, undefined) 115 } else { 116 assert_equal(v, i - 6); 117 } 118} 119 120var arr14 = Array.apply(null, Array(16)); 121assert_equal(arr14.length, 16); 122var arr15 = Array.apply(null, [1, 2, 3, 4, 5, 6]); 123assert_equal(arr15.length, 6); 124 125var a = '0'; 126assert_equal(Array(5).indexOf(a), -1); 127 128let throwException = false; 129const v3 = new Float32Array(7); 130try { 131 v3.filter(Float32Array) 132} catch (error) { 133 throwException = true; 134} 135assert_true(throwException) 136 137const v20 = new Array(2); 138let v21; 139try { 140 v21 = v20.pop(); 141 assert_equal(v21, undefined); 142} catch (error) { 143 144} 145 146var arr21 = [1, 2, 3, 4, , 6]; 147assert_equal(arr21.at(0), 1); 148assert_equal(arr21.at(5), 6); 149assert_equal(arr21.at(-1), 6); 150assert_equal(arr21.at(6), undefined); 151assert_equal(arr21.at('1.9'), 2); 152assert_equal(arr21.at(true), 2); 153var arr22 = arr21.toReversed(); 154assert_equal(arr22, [6,,4,3,2, 1]) 155assert_equal(arr21, [1,2,3,4,,6]) 156var arr23 = [1, 2, 3, 4, 5]; 157assert_equal(arr23.with(2, 6), [1,2,6,4,5]); // [1, 2, 6, 4, 5] 158assert_equal(arr23, [1,2,3,4,5]); // [1, 2, 3, 4, 5] 159 160const months = ["Mar", "Jan", "Feb", "Dec"]; 161const sortedMonths = months.toSorted(); 162assert_equal(sortedMonths, ['Dec', 'Feb', 'Jan', 'Mar']); // ['Dec', 'Feb', 'Jan', 'Mar'] 163assert_equal(months, ['Mar', 'Jan', 'Feb', 'Dec']); // ['Mar', 'Jan', 'Feb', 'Dec'] 164 165const values = [1, 10, 21, 2]; 166const sortedValues = values.toSorted((a, b) => a - b); 167assert_equal(sortedValues, [1, 2, 10, 21]); // [1, 2, 10, 21] 168assert_equal(values, [1, 10, 21, 2]); // [1, 10, 21, 2] 169 170const arrs = new Array(6); 171for (let i = 0; i < 6; i++) { 172 var str = i.toString(); 173 if (i != 1) { 174 arrs[i] = str; 175 } 176} 177arrs.reverse(); 178assert_equal(arrs, ['5', '4', '3', '2', undefined, '0']); // 5,4,3,2,,0 179 180 181function handleUnexpectedErrorCaught(prompt, e) { 182 if (e instanceof Error) { 183 print(`Unexpected ${e.name} caught in ${prompt}: ${e.message}`); 184 if (typeof e.stack !== 'undefined') { 185 print(`Stacktrace:\n${e.stack}`); 186 } 187 } else { 188 } 189} 190 191// Test cases for reverse() 192try { 193 const arr0 = []; 194 assert_equal(arr0.reverse(), arr0); // true 195 assert_equal(`${arr0.length}`, 0); // 0 196 197 const arr1 = [1]; 198 assert_equal(arr1.reverse(), arr1); // true 199 assert_equal(`${arr1}`, 1); // 1 200 201 const arrWithHoles = []; 202 arrWithHoles[1] = 1; 203 arrWithHoles[4] = 4; 204 arrWithHoles[6] = undefined; 205 arrWithHoles.length = 10; 206 // arrWithHoles = [Hole, 1, Hole, Hole, 4, Hole, undefined, Hole, Hole, Hole] 207 assert_equal(`${arrWithHoles}`, [,1,,,4,,,,,]); // ,1,,,4,,,,, 208 assert_equal(arrWithHoles.reverse(), [,,,,,4,,,1,]); // ,,,,,4,,,1, 209 assert_equal(`${arrWithHoles}`, [,,,,,4,,,1,]); // ,,,,,4,,,1, 210} catch (e) { 211 handleUnexpectedErrorCaught(e); 212} 213 214// Test case for indexOf() and lastIndexOf() 215try { 216 const arr = [0, 10, 20]; 217 arr.length = 10; 218 arr[3] = 80; 219 arr[4] = 40; 220 arr[6] = undefined; 221 arr[7] = 10; 222 arr[8] = "80"; 223 // prompt1, results1, prompt2, results2, ... 224 const resultGroups = [ 225 "Group 1: 0 <= fromIndex < arr.length", [ 226 arr.indexOf(40), // 4 227 arr.indexOf(40, 5), // -1 228 arr.indexOf(10), // 1 229 arr.indexOf(10, 2), // 7 230 arr.lastIndexOf(40), // 4 231 arr.lastIndexOf(40, 3), // -1 232 arr.lastIndexOf(10), // 7 233 arr.lastIndexOf(10, 6), // 1 234 ], 235 "Group 2: -arr.length <= fromIndex < 0", [ 236 arr.indexOf(40, -4), // -1 237 arr.indexOf(40, -8), // 4 238 arr.indexOf(10, -4), // 7 239 arr.indexOf(10, -10), // 1 240 arr.lastIndexOf(40, -4), // 4 241 arr.lastIndexOf(40, -8), // -1 242 arr.lastIndexOf(10, -4), // 1 243 arr.lastIndexOf(10, -10), // -1 244 arr.indexOf(0, -arr.length), // 0 245 arr.indexOf(10, -arr.length), // 1 246 arr.lastIndexOf(0, -arr.length), // 0 247 arr.lastIndexOf(10, -arr.length), // -1 248 ], 249 "Group 3: fromIndex >= arr.length", [ 250 arr.indexOf(0, arr.length), // -1 251 arr.indexOf(0, arr.length + 10), // -1 252 arr.indexOf(10, arr.length), 253 arr.indexOf(10, arr.length + 10), 254 arr.lastIndexOf(0, arr.length), // 0 255 arr.lastIndexOf(0, arr.length + 10), // 0 256 ], 257 "Group 4: fromIndex < -arr.length", [ 258 arr.indexOf(0, -arr.length - 10), // 0 259 arr.lastIndexOf(0, -arr.length - 10) // -1 260 ], 261 "Group 5: fromIndex in [Infinity, -Infinity]", [ 262 arr.indexOf(10, -Infinity), // 1 263 arr.indexOf(10, +Infinity), // -1 264 arr.lastIndexOf(10, -Infinity), // -1 265 arr.lastIndexOf(10, +Infinity) // 7 266 ], 267 "Group 6: fromIndex is NaN", [ 268 arr.indexOf(0, NaN), // 0 269 arr.indexOf(10, NaN), // 1 270 arr.lastIndexOf(0, NaN), // 0 271 arr.lastIndexOf(10, NaN), // -1 272 ], 273 "Group 7: fromIndex is not of type 'number'", [ 274 arr.indexOf(10, '2'), // 7 275 arr.lastIndexOf(10, '2'), // 1 276 arr.indexOf(10, { valueOf() { 277 return 3; 278 } }), // 7 279 arr.indexOf(10, { valueOf() { 280 return 3; 281 } }), // 1 282 ], 283 "Group 8: Strict equality checking", [ 284 arr.indexOf("80"), // 8 285 arr.lastIndexOf(80), // 3 286 ], 287 "Group 9: Searching undefined and null", [ 288 arr.indexOf(), // 6 289 arr.indexOf(undefined), // 6 290 arr.indexOf(null), // -1 291 arr.lastIndexOf(), // 6 292 arr.lastIndexOf(undefined), // 6 293 arr.lastIndexOf(null) // -1 294 ] 295 ]; 296 297 assert_equal(resultGroups[0], [4,-1,1,7,4,-1,7,1]); 298 assert_equal(resultGroups[1], [-1,4,7,1,4,-1,1,-1,0,1,0,-1]); 299 assert_equal(resultGroups[2], [-1,-1,-1,-1,0,0]); 300 assert_equal(resultGroups[3], [0,-1]); 301 assert_equal(resultGroups[4], [ 1,-1,-1,7]); 302 assert_equal(resultGroups[5], [0,1,0,-1]); 303 assert_equal(resultGroups[6], [7,1,7,7]); 304 assert_equal(resultGroups[7], [8, 3]); 305 assert_equal(resultGroups[8], [6,6,-1,6,6,-1]); 306 307 print("Group 10: fromIndex with side effects:"); 308 let accessCount = 0; 309 const arrProxyHandler = { 310 has(target, key) { 311 accessCount += 1; 312 return key in target; 313 } 314 }; 315 // Details on why accessCount = 6 can be seen in ECMAScript specifications: 316 // https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.indexof 317 accessCount = 0; 318 const arr2 = new Proxy([10, 20, 30, 40, 50, 60], arrProxyHandler); 319 const result2 = arr2.indexOf(30, { 320 valueOf() { 321 arr2.length = 2; // Side effects to arr2 322 return 0; 323 } 324 }); // Expected: -1 (with accessCount = 6) 325 assert_equal(`${result2}`, -1); 326 assert_equal(`${accessCount}`, 6); 327 // Details on why accessCount = 6 can be seen in ECMAScript specifications: 328 // https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.lastindexof 329 accessCount = 0; 330 const arr3 = new Proxy([15, 25, 35, 45, 55, 65], arrProxyHandler); 331 const result3 = arr3.lastIndexOf(45, { 332 valueOf() { 333 arr3.length = 2; // Side effects to arr3 334 return 5; 335 } 336 }); // Expected: -1 (with accessCount = 6) 337 assert_equal(`${result3}`, -1); 338 assert_equal(`${accessCount}`, 6); 339 340 let indexOfErrorCount = 0; 341 let lastIndexOfErrorCount = 0; 342 for (let [prompt, fromIndex] of [["bigint", 1n], ["symbol", Symbol()]]) { 343 for (let method of 344 [Array.prototype.indexOf, Array.prototype.lastIndexOf]) { 345 try { 346 const result = method.call(arr, 10, fromIndex); 347 print(`ERROR: Unexpected result (which is ${result}) returned by method '${method.name}': ` + 348 `Expects a TypeError thrown for fromIndex type '${prompt}'.`); 349 } catch (e) { 350 if (method.name == "indexOf") { 351 indexOfErrorCount++; 352 } 353 if (method.name == "lastIndexOf") { 354 lastIndexOfErrorCount++; 355 } 356 } 357 } 358 } 359 assert_equal(indexOfErrorCount, 2); 360 assert_equal(lastIndexOfErrorCount, 2); 361} catch (e) { 362 handleUnexpectedErrorCaught(e); 363} 364 365// Test Array.prototype.filter when callbackFn is not callable 366let callError = false; 367try { 368 [].filter(1); 369} catch (e) { 370 callError = true; 371} 372assert_true(callError); 373 374var getTrap = function (t, key) { 375 if (key === "length") return { [Symbol.toPrimitive]() { 376 return 3 377 } }; 378 if (key === "2") return "baz"; 379 if (key === "3") return "bar"; 380}; 381var target1 = []; 382var obj = new Proxy(target1, { get: getTrap, has: () => true }); 383assert_equal([].concat(obj), [,,"baz"]); 384assert_equal(Array.prototype.concat.apply(obj), [,,"baz"]); 385 386const v55 = new Float64Array(); 387const v98 = [-5.335880620598348e+307, 1.0, 1.0, 2.220446049250313e-16, -1.6304390272817058e+308]; 388 389function f99(a100) { 390} 391 392function f110() { 393 v98[2467] = v55; 394} 395 396Object.defineProperty(f99, Symbol.species, { configurable: true, enumerable: true, value: f110 }); 397v98.constructor = f99; 398let v98Result = JSON.stringify(v98.splice(4)); 399assert_equal(v98Result, "{\"0\":-1.6304390272817058e+308,\"length\":1}"); 400 401var count; 402var params; 403 404class MyObserveArrray extends Array { 405 constructor(...args) { 406 super(...args); 407 params = args; 408 } 409 410 static get [Symbol.species]() { 411 count++; 412 return this; 413 } 414} 415 416count = 0; 417params = undefined; 418new MyObserveArrray().filter(() => { 419}); 420assert_equal(count, 1); 421assert_equal(params, [0]); 422 423count = 0; 424params = undefined; 425new MyObserveArrray().concat(() => { 426}); 427assert_equal(count, 1); 428assert_equal(params, [0]); 429 430count = 0; 431params = undefined; 432new MyObserveArrray().slice(() => { 433}); 434assert_equal(count, 1); 435assert_equal(params, [0]); 436 437count = 0; 438params = undefined; 439new MyObserveArrray().splice(() => { 440}); 441assert_equal(count, 1); 442assert_equal(params, [0]); 443 444new MyObserveArrray([1, 2, 3, 4]).with(0, 0); 445new MyObserveArrray([1, 2, 3, 4]).toReversed(0, 0); 446new MyObserveArrray([1, 2, 3, 4]).toSpliced(0, 0, 0); 447 448arr = new Array(1026); 449arr.fill(100); 450assert_equal(arr.with(0, "n")[0], 'n') 451 452arr = new Array(1026); 453arr.fill(100); 454assert_equal(arr.toReversed()[0], 100) 455 456arr = new Array(1026); 457arr.fill(100); 458assert_equal(arr.toSpliced(0, 0, 0, 0)[0], 0) 459 460var arr25 = [] 461arr25[1025] = 0; 462assert_equal(arr25.includes({}, 414), false); 463assert_equal(arr25.includes(1025,109), false); 464 465var arr26 = [] 466arr25[100] = 0; 467assert_equal(arr25.includes({}, 26), false); 468 469function fun1(obj, name, type) { 470 return typeof type === 'undefined' || typeof desc.value === type; 471} 472 473function fun2(obj, type) { 474 let properties = []; 475 let proto = Object.getPrototypeOf(obj); 476 while (proto && proto != Object.prototype) { 477 Object.getOwnPropertyNames(proto).forEach(name => { 478 if (name !== 'constructor') { 479 if (fun1(proto, name, type)) properties.push(name); 480 } 481 }); 482 proto = Object.getPrototypeOf(proto); 483 } 484 return properties; 485} 486 487function fun4(seed) { 488 let objects = [Object, Error, AggregateError, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, String, BigInt, Function, Number, Boolean, Date, RegExp, Array, ArrayBuffer, DataView, Int8Array, Int16Array, Int32Array, Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array, Set, Map, WeakMap, WeakSet, Symbol, Proxy]; 489 return objects[seed % objects.length]; 490} 491 492function fun8(obj, seed) { 493 let properties = fun2(obj); 494} 495 496fun4(694532)[fun8(fun4(694532), 527224)]; 497Object.freeze(Object.prototype); 498 499Array.prototype.length = 3000; 500assert_equal(Array.prototype.length, 3000) 501 502let unscopables1 = Array.prototype[Symbol.unscopables]; 503let unscopables2 = Array.prototype[Symbol.unscopables]; 504assert_true(unscopables1 == unscopables2) 505 506arr = [] 507let index = 4294967291; 508arr[index] = 0; 509arr.length = 10; 510arr.fill(10); 511assert_equal(arr, [10,10,10,10,10,10,10,10,10,10]); 512 513// Test case for copyWithin() 514var arr_copywithin1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 515var arr_copywithin2 = new Array(); 516for (let i = 0; i < 10; i++) arr_copywithin2[i] = i; 517var arr_copywithin3 = new Array(); 518for (let i = 0; i < 10; i++) { 519 if (i == 2) { 520 continue; 521 } 522 arr_copywithin3[i] = i; 523} 524var arr_copywithin4 = new Array(); 525for (let i = 0; i < 10; i++) arr_copywithin4[i] = i; 526var result_copywithin1 = arr_copywithin1.copyWithin(0, 3, 7); 527assert_equal(result_copywithin1, [3,4,5,6,4,5,6,7,8,9]); 528var result_copywithin2 = arr_copywithin2.copyWithin(0, 3, 7); 529assert_equal(result_copywithin2, [3,4,5,6,4,5,6,7,8,9]); 530var arr_copywithin3 = arr_copywithin3.copyWithin(0, 0, 10); 531assert_equal(arr_copywithin3, [0,1,,3,4,5,6,7,8,9]); 532//elementskind is generic but hclass == generic hclass 533var arr_copywithin4 = arr_copywithin4.copyWithin(3, 0, 6); 534assert_equal(arr_copywithin4, [0,1,2,0,1,2,3,4,5,9]); 535 536const ArraySize = 10; 537const QuarterSize = ArraySize / 4; 538let result; 539let arr_copywithin5 = []; 540let arr_copywithin6 = []; 541arr_copywithin6.proto = arr_copywithin4; 542for (let i = 0; i < ArraySize; ++i) arr_copywithin5[i] = i; 543for (let i = 0; i < ArraySize; ++i) arr_copywithin6[i] = i; 544 545for (let i = 0; i < ArraySize; i++) { 546 //elementskind is not generic 547 result = arr_copywithin5.copyWithin(0, QuarterSize * 2, QuarterSize * 3); 548} 549assert_equal(result, [ 5,6,2,3,4,5,6,7,8,9]); 550 551for (let i = 0; i < ArraySize; i++) { 552 //elementskind is generic but hclass != generic hclass 553 result = arr_copywithin6.copyWithin(0, QuarterSize * 2, QuarterSize * 3); 554} 555assert_equal(result, [5,6,2,3,4,5,6,7,8,9]); 556 557// Test case for every() 558var arr_every1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 559var arr_every2 = new Array(); 560 561function testEvery(element, index, array) { 562 if (index == 0) { 563 array.length = 6; 564 } 565 return element < 6; 566} 567 568function testEvery4(element, index, array) { 569 array.pop(); 570 array.pop(); 571 array.pop(); 572 return element < 6; 573} 574 575for (let i = 0; i < 10; i++) arr_every2[i] = i; 576var arr_every3 = new Array(); 577for (let i = 0; i < 10; i++) { 578 if (i == 2) { 579 continue; 580 } 581 arr_every3[i] = i; 582} 583var arr_every4 = new Array(); 584for (let i = 0; i < 10; i++) arr_every4[i] = i; 585var result_every1 = arr_every1.every(testEvery); 586assert_true(result_every1); 587var result_every2 = arr_every2.every(testEvery); 588assert_true(result_every2); 589var result_every3 = arr_every3.every(testEvery); 590assert_true(result_every3); 591var result_every4 = arr_every4.every(testEvery4); 592assert_true(result_every4); 593 594// Test case for reduceRight() 595var arr_reduceRight1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 596var arr_reduceRight2 = new Array(); 597 598function testReduceRight(accumulator, element, index, array) { 599 if (index == 0) { 600 array.length = 6; 601 } 602 return accumulator + element; 603} 604 605function testReduceRight4(accumulator, element, index, array) { 606 array.pop(); 607 array.pop(); 608 return accumulator + element; 609} 610 611for (let i = 0; i < 10; i++) arr_reduceRight2[i] = i; 612var arr_reduceRight3 = new Array(); 613for (let i = 0; i < 10; i++) { 614 if (i < 9) { 615 continue; 616 } 617 arr_reduceRight3[i] = i; 618} 619var arr_reduceRight4 = new Array(); 620for (let i = 0; i < 10; i++) arr_reduceRight4[i] = i; 621var result_reduceRight1 = arr_reduceRight1.reduceRight(testReduceRight, 100); 622assert_equal(result_reduceRight1, 145); 623var result_reduceRight2 = arr_reduceRight2.reduceRight(testReduceRight, 100); 624assert_equal(result_reduceRight2, 145); 625var result_reduceRight3 = arr_reduceRight3.reduceRight(testReduceRight, 100); 626assert_equal(result_reduceRight3, 109); 627var result_reduceRight4 = arr_reduceRight4.reduceRight(testReduceRight4, 100); 628assert_equal(result_reduceRight4, 125); 629 630// Test case for some() 631var arr_some1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 632var arr_some2 = new Array(); 633 634function testSome(element, index, array) { 635 if (index == 0) { 636 array.length = 6; 637 } 638 return element > 8; 639} 640 641for (let i = 0; i < 10; i++) arr_some2[i] = i; 642var arr_some3 = new Array(); 643for (let i = 0; i < 10; i++) { 644 if (i < 9) { 645 continue; 646 } 647 arr_some3[i] = i; 648} 649var result_some1 = arr_some1.some(testSome); 650assert_equal(result_some1, false); 651var result_some2 = arr_some2.some(testSome); 652assert_equal(result_some2, false); 653var result_some3 = arr_some3.some(testSome); 654assert_equal(result_some3, true); 655 656// Test case for unshift() 657var arr_unshift1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 658var arr_unshift2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 659 660var result_unshift1 = arr_unshift1.unshift(1, 2, 3, 4, 5); 661assert_equal(result_unshift1, 15); 662var result_unshift2 = arr_unshift2.unshift(1, 2, 3); 663assert_equal(result_unshift2, 13); 664 665class C3 { 666 constructor(a5) { 667 try { 668 a5(this, this, ..."895053461", ..."p"); 669 } catch (e) { 670 } 671 } 672 673 a; 674 675 valueOf(a9) { 676 const v10 = a9?.[this]; 677 -718821.501160539 !== a9; 678 const t6 = "895053461"; 679 t6[a9] = "895053461"; 680 return v10; 681 } 682} 683 684const v12 = new C3("p"); 685new C3(v12); 686new C3(C3); 687 688// Test case for toSorted() 689var arr_toSorted1 = ["Mar", "Jan", "Feb", "Dec"]; 690var arr_toSorted2 = new Array(); 691arr_toSorted2[0] = "Mar"; 692arr_toSorted2[1] = "Jan"; 693arr_toSorted2[2] = "Feb"; 694arr_toSorted2[3] = "Dec"; 695var result_toSorted1 = arr_toSorted1.toSorted(); 696assert_equal(result_toSorted1, ["Dec","Feb","Jan","Mar"]); 697var result_toSorted2 = arr_toSorted2.toSorted(); 698assert_equal(result_toSorted2, ["Dec","Feb","Jan","Mar"]); 699 700const v0 = [0, 1]; 701const mapEd = v0.map(() => v0["pop"]()); 702assert_equal(new Uint16Array(v0).length, 1); 703 704let errorCount1 = 0; 705try { 706 const v2 = new ArrayBuffer(103); 707 708 function F3(a5, a6) { 709 if (!new.target) { 710 throw 'must be called with new'; 711 } 712 713 function f7(a8, a9, a10) { 714 return v2; 715 } 716 717 const v13 = new BigUint64Array(35); 718 const o14 = { 719 ...v13, 720 }; 721 Object.defineProperty(o14, 4, { set: f7 }); 722 } 723 724 new F3(); 725 new F3(); 726 v2(); 727} catch (err) { 728 errorCount1++; 729} 730assert_equal(errorCount1, 1); 731 732try { 733 const v3 = new ArrayBuffer(45); 734 const v0 = new Boolean; 735 736 function F4(a6, a7) { 737 if (!new.target) { 738 throw 'must be called with new'; 739 } 740 741 function f8(a9, a10, a11) { 742 return F4; 743 } 744 745 const v14 = new BigUint64Array(15); 746 const o20 = { 747 [v0](a16, a17, a18, a19) { 748 return a6; 749 }, 750 ...v14, 751 }; 752 Object.defineProperty(o20, 4, { set: f8 }); 753 } 754 755 new F4(v0, 101); 756 new F4(); 757 v3.transfer(); 758} catch (err) { 759 errorCount1++; 760} 761assert_equal(errorCount1, 2); 762 763try { 764 765 function F6(a8, a9, a10) { 766 if (!new.target) { 767 throw 'must be called with new'; 768 } 769 const v14 = new Date(-10, 16); 770 v14.toDateString(); 771 } 772 773 const v16 = new F6(11); 774 new F6(2064); 775 776 function f18() { 777 return v16; 778 } 779 780 new BigUint64Array(); 781 const v23 = [-42, 4, 4]; 782 const o24 = {}; 783 const v25 = [5.0, -5.12, 1e-15, -0.05, 5.0, 2.22e-38, -10.0, 10.0, 5.0, -5.0]; 784 785 function f26() { 786 return v25; 787 } 788 789 class C28 extends Date { 790 constructor(a30, a31) { 791 super(); 792 } 793 } 794 795 new Int8Array(); 796 new Int8Array(); 797 v23.filter(Array); 798 new ArrayBuffer(26); 799 const v44 = new Uint8Array(); 800 v44.toString(); 801 const o46 = {}; 802 for (let i49 = -51, i50 = 10; i49 < i50; i50--) { 803 o46[i50] >>= i50; 804 } 805 /c/iusg; 806 const v58 = new Int16Array(); 807 v58.forEach(); 808} catch (err) { 809 errorCount1++; 810} 811assert_equal(errorCount1, 3); 812 813try { 814 const v3 = new ArrayBuffer(19); 815 816 function F4(a6, a7) { 817 if (!new.target) { 818 throw 'must be called with new'; 819 } 820 const v10 = new BigUint64Array(34); 821 const o11 = { 822 ...v10, 823 }; 824 Object.defineProperty(o11, 4, { set: a6 }); 825 } 826 827 const v12 = new F4(); 828 new F4(v12, v3); 829} catch (err) { 830 errorCount1++; 831} 832assert_equal(errorCount1, 4); 833 834try { 835 const v3 = new ArrayBuffer(10); 836 837 function F4(a6, a7) { 838 if (!new.target) { 839 throw 'must be called with new'; 840 } 841 842 function f8(a9, a10, a11) { 843 return a11; 844 } 845 846 const v14 = new BigUint64Array(34); 847 const o15 = { 848 ...v14, 849 }; 850 Object.defineProperty(o15, 4, { set: f8 }); 851 } 852 853 const v16 = new F4(); 854 new F4(v16, v3); 855 Int32Array(); 856} catch (err) { 857 errorCount1++; 858} 859assert_equal(errorCount1, 5); 860 861try { 862 const v2 = new ArrayBuffer(10); 863 864 function F3(a5, a6) { 865 if (!new.target) { 866 throw 'must be called with new'; 867 } 868 869 function f7(a8, a9, a10) { 870 return a9; 871 } 872 873 const v13 = new BigUint64Array(35); 874 const o14 = { 875 ...v13, 876 }; 877 Object.defineProperty(o14, 4, { set: f7 }); 878 } 879 880 new F3(ArrayBuffer, v2); 881 new F3(10, F3); 882 v2.transfer(); 883} catch (err) { 884 errorCount1++; 885} 886assert_equal(errorCount1, 6); 887 888 889try { 890 const v1 = new Uint16Array(); 891 for (let i4 = 0, i5 = 10; i5; i5--) { 892 } 893 undefined instanceof v1.values(); 894} catch (error) { 895 errorCount1++; 896} 897assert_equal(errorCount1, 7); 898 899/* 900 * @tc.name:ArrayConstructor 901 * @tc.desc:test Array 902 * @tc.type: FUNC 903 */ 904const originalArrays = [ 905 [1, 2, 3], 906 ["apple", "banana", "orange"], 907 [true, false, true], 908 [{ name: "John" }, { name: "Doe" }], 909 [NaN, NaN, NaN], 910 [Infinity, -Infinity], 911 [RegExp("pattern1"), RegExp("pattern2")], 912 [new Map(), new Map()], 913 [new Set(), new Set()], 914 [Array.from([1, 2, 3]), Array.from([4, 5, 6])], 915 ["ark_unicodeValue ", "ark_unicodeValue "], 916]; 917 918assert_equal(originalArrays[0], [1, 2, 3]); 919assert_equal(originalArrays[1], ["apple", "banana", "orange"]); 920assert_equal(originalArrays[2], [true, false, true]); 921assert_equal(originalArrays[5], [Infinity, -Infinity]); 922 923errorCount1 = 0; 924try { 925 const arr1 = [1, 2, 3]; 926 assert_equal(arr1[10], undefined); 927} catch (error) { 928 errorCount1++; 929} 930assert_equal(errorCount1, 0); 931 932try { 933} catch (error) { 934 print("Caught an error: ", error); 935} 936 937let errorStr = ""; 938try { 939 const arr2 = new Array(-1); 940} catch (error) { 941 errorStr = error.toString(); 942} 943assert_equal(errorStr, "RangeError: Invalid array length"); 944 945/* 946 * @tc.name:from 947 * @tc.desc:test Array 948 * @tc.type: FUNC 949 */ 950const newArray = Array.from(originalArrays); 951newArray.forEach((array, index) => { 952 assert_equal(array, originalArrays[index]) 953}); 954 955try { 956 Array.from([1, 2, 3], "not a function"); 957} catch (error) { 958 errorStr = error.toString(); 959} 960assert_equal(errorStr, "TypeError: the mapfn is not callable."); 961 962try { 963 Array.from([1, 2, 3], () => { throw new Error("Something went wrong"); }); 964} catch (error) { 965 errorStr = error.toString(); 966} 967assert_equal(errorStr, "Error: Something went wrong"); 968 969try { 970 Array.from(123); 971 Array.from({}); 972 Array.from([1, 2, 3], () => {}, 123); 973} catch (error) { 974 errorStr = error.toString(); 975} 976assert_equal(errorStr, "Error: Something went wrong"); 977 978/* 979 * @tc.name:isArray 980 * @tc.desc:test Array 981 * @tc.type: FUNC 982 */ 983originalArrays.forEach((array, index) => { 984 assert_true(Array.isArray(array)); 985}); 986 987try { 988 assert_true(!Array.isArray()); 989 assert_true(!Array.isArray(123)); 990 assert_true(!Array.isArray("not an array")); 991 assert_true(!Array.isArray(null)); 992 assert_true(!Array.isArray(undefined)); 993} catch (error) { 994 errorStr = error.toString(); 995} 996 997/* 998 * @tc.name:Of 999 * @tc.desc:test Array 1000 * @tc.type: FUNC 1001 */ 1002const ArrayOf = Array.of(...originalArrays); 1003assert_equal(ArrayOf[1], ["apple", "banana", "orange"]); 1004 1005try { 1006 const arr1 = Array.of(undefined); 1007 const arr2 = Array.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 1008 assert_equal(arr1, [undefined]); 1009 assert_equal(arr2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); 1010} catch (error) { 1011 print("Caught an error: ", error); 1012} 1013 1014/* 1015 * @tc.name:At 1016 * @tc.desc:test Array 1017 * @tc.type: FUNC 1018 */ 1019assert_equal(originalArrays.at(1), ['apple', 'banana', 'orange']); 1020assert_equal(originalArrays.at(-1), ["ark_unicodeValue ", "ark_unicodeValue "]); 1021assert_equal(originalArrays.at(-2), [[1,2,3],[4,5,6]]); 1022 1023try { 1024 assert_equal(originalArrays.at(), [1,2,3]); 1025 assert_equal(originalArrays.at(100), undefined); 1026 assert_equal(originalArrays.at(null), [1,2,3]); 1027 assert_equal(originalArrays.at(undefined), [1,2,3]); 1028 assert_equal(originalArrays.at(new Map()), [1,2,3]); 1029} catch (error) { 1030 print("Caught an error: ", error); 1031} 1032 1033/* 1034 * @tc.name:concat 1035 * @tc.desc:test Array 1036 * @tc.type: FUNC 1037 */ 1038const array1 = [1, "two", true]; 1039const e1 = { key: "value" }; 1040const array2 = [null, undefined, e1]; 1041const array3 = []; 1042 1043const concatenatedArray = array1.concat(array2, array3); 1044assert_equal(concatenatedArray, [1, "two", true, null, undefined, e1]); 1045 1046const nestedArray = [[1, 2], ["a", "b"], [true, false]]; 1047const nestedConcatenatedArray = array1.concat(nestedArray, array2); 1048assert_equal(nestedConcatenatedArray, 1049 [1, "two", true, [1, 2], ["a", "b"], [true, false], null, undefined, e1]); 1050 1051const e2 = { prop: "value" }; 1052const mixedConcatenatedArray = array1.concat(4, "five", e2); 1053assert_equal(mixedConcatenatedArray, [1, "two", true, 4, "five", e2]); 1054 1055const spreadConcatenatedArray = [...array1, ...array2, ...array3]; 1056assert_equal(spreadConcatenatedArray, [1, "two", true, null, undefined, e1]); 1057 1058/* 1059 * @tc.name:CopyWithin,Entries 1060 * @tc.desc:test Array 1061 * @tc.type: FUNC 1062 */ 1063const copiedArray1 = originalArrays[0].slice().copyWithin(0, 2); 1064const copiedArray2 = originalArrays[1].slice().copyWithin(1, 0, 2); 1065const copiedArray3 = originalArrays[2].slice().copyWithin(1, -2); 1066const copiedArray4 = originalArrays[3].slice().copyWithin(-1); 1067const copiedArray5 = originalArrays[4].slice().copyWithin(0); 1068 1069assert_equal(copiedArray1, [3, 2, 3]); 1070assert_equal(copiedArray2, ['apple', 'apple', 'banana']); 1071assert_equal(copiedArray3, [true, false, true]); 1072assert_equal(copiedArray4.toString(), '[object Object],[object Object]'); 1073assert_equal(copiedArray5.toString(), 'NaN,NaN,NaN'); 1074 1075for (const [index, value] of originalArrays.entries()) { 1076 assert_equal(originalArrays[index], value); 1077} 1078 1079/* 1080 * @tc.name:Every 1081 * @tc.desc:test Array 1082 * @tc.type: FUNC 1083 */ 1084const numbers1 = [2, 4, 6, 8, 10]; 1085const allEven = numbers1.every(num => num % 2 === 0); 1086assert_true(allEven); 1087 1088const numbers2 = [1, 2, 3, 4, 5]; 1089const allEven2 = numbers2.every(num => num % 2 === 0); 1090assert_true(!allEven2); 1091 1092const emptyArray1 = []; 1093const allEmpty = emptyArray1.every(num => num % 2 === 0); 1094assert_true(allEmpty); 1095 1096const emptyArray2 = []; 1097const allEmpty2 = emptyArray2.every(() => true); 1098assert_true(allEmpty2); 1099 1100const mixedArray = [2, 4, "hello", 8, 10]; 1101const allNumbers = mixedArray.every(num => typeof num === "number"); 1102assert_true(!allNumbers); 1103 1104const emptyArray3 = []; 1105const allNonNegative = emptyArray3.every(num => num >= 0); 1106assert_true(allNonNegative); 1107 1108try { 1109 const arr = [1, 2, 3]; 1110 const result = arr.every("not a function"); 1111} catch (error) { 1112 errorStr = error.toString(); 1113} 1114assert_equal(errorStr, "TypeError: the callbackfun is not callable.") 1115 1116try { 1117 const arr = [1, 2, 3]; 1118 const result = arr.every(num => num < undefinedVariable); 1119} catch (error) { 1120 errorStr = error.toString(); 1121} 1122assert_equal(errorStr, "ReferenceError: undefinedVariable is not defined"); 1123 1124/* 1125 * @tc.name:Fill 1126 * @tc.desc:test Array 1127 * @tc.type: FUNC 1128 */ 1129{ 1130 const array1 = [1, 2, 3, 4, 5]; 1131 assert_equal(array1.fill(0), [0,0,0,0,0]); 1132 1133 const array2 = [1, 2, 3, 4, 5]; 1134 assert_equal(array2.fill(0, 2), [1,2,0,0,0]); 1135 1136 const array3 = [1, 2, 3, 4, 5]; 1137 assert_equal(array3.fill(0, 1, 3), [1,0,0,4,5]); 1138 1139 const array4 = new Array(5); 1140 assert_equal(array4.fill(0), [0,0,0,0,0]); 1141 1142 const array5 = Array.from({ length: 5 }, (_, index) => index + 1); 1143 assert_equal(array5.fill(0, 2), [1,2,0,0,0]); 1144 1145 const array6 = Array.from({ length: 5 }, (_, index) => index + 1); 1146 assert_equal(array6.fill(0, 1, 3), [1,0,0,4,5]); 1147 1148 const array7 = Array(5).fill("hello"); 1149 assert_equal(array7, ['hello', 'hello', 'hello', 'hello', 'hello']); 1150 1151 const array8 = [1, 2, 3]; 1152 assert_equal(array8.fill(0, -2), [1,0,0]); 1153 1154 const array9 = [1, 2, 3]; 1155 assert_equal(array9.fill(0, -2, -1), [1,0,3]); 1156} 1157 1158try { 1159 const arr = [1, 2, 3]; 1160 arr.fill(0, 1.5); 1161} catch (error) { 1162 print("Caught an error: ", error); 1163} 1164 1165try { 1166 const arr = [1, 2, 3]; 1167 arr.fill(0, NaN); 1168} catch (error) { 1169 print("Caught an error: ", error); 1170} 1171 1172/* 1173 * @tc.name:Filter 1174 * @tc.desc:test Array 1175 * @tc.type: FUNC 1176 */ 1177{ 1178 const numbers = [1, 2, 3, 4, 5]; 1179 1180 const evenNumbers = numbers.filter(num => num % 2 === 0); 1181 assert_equal(evenNumbers, [2,4]); 1182 1183 const greaterThanTwo = numbers.filter(num => num > 2); 1184 assert_equal(greaterThanTwo, [3,4,5]); 1185 1186 const lessThanTen = numbers.filter(num => num < 10); 1187 assert_equal(lessThanTen, [1,2,3,4,5]); 1188 1189 const divisibleByThree = numbers.filter(num => num % 3 === 0); 1190 assert_equal(divisibleByThree, [3]); 1191 1192 const words = ["apple", "banana", "pear", "orange"]; 1193 const longWords = words.filter(word => word.length >= 5); 1194 assert_equal(longWords, ['apple', 'banana', 'orange']); 1195 1196 const persons = [ 1197 { name: "Alice", age: 25 }, 1198 { name: "Bob", age: 17 }, 1199 { name: "Charlie", age: 30 } 1200 ]; 1201 const adults = persons.filter(person => person.age > 18); 1202 assert_equal(adults.toString(), '[object Object],[object Object]'); 1203} 1204 1205 1206try { 1207 const arr = [1, 2, 3]; 1208 const result = arr.filter("not a function"); 1209} catch (error) { 1210 errorStr = error.toString(); 1211} 1212assert_equal(errorStr, 'TypeError: the callbackfun is not callable.'); 1213 1214try { 1215 const obj = { a: 1, b: 2 }; 1216 const result = obj.filter(() => true); 1217} catch (error) { 1218 errorStr = error.toString(); 1219} 1220assert_equal(errorStr, 'TypeError: undefined is not callable'); 1221 1222/* 1223 * @tc.name:Find 1224 * @tc.desc:test Array 1225 * @tc.type: FUNC 1226 */ 1227{ 1228 const array = [6, 7, 8, 9, 10, NaN, undefined, null, "", false, {name: "John"}, [1, 2, 3]]; 1229 assert_equal(array.find(item => item === 5), undefined); 1230 assert_equal(array.find(item => item === 11), undefined); 1231 assert_equal(array.find(item => item > 5), 6); 1232 assert_equal(array.find(item => item < 0), undefined); 1233 assert_equal(array.find(item => typeof item === 'string'), ""); 1234 assert_equal(array.find(item => typeof item === 'object'), null); 1235 assert_equal(array.find(item => Array.isArray(item)), [1,2,3]); 1236 assert_equal(array.find(item => item), 6); 1237 assert_equal(array.find(item => item === null), null); 1238 assert_equal(array.find(item => item === undefined), undefined); 1239 assert_equal(array.find(item => isNaN(item)).toString(), 'NaN'); 1240 assert_equal(array.find(item => item === false), false); 1241} 1242 1243try { 1244 const array = [1, 2, 3, 4, 5]; 1245 array.find(); 1246} catch (error) { 1247 errorStr = error.toString(); 1248} 1249assert_equal(errorStr, 'TypeError: the predicate is not callable.'); 1250 1251try { 1252 const array = [1, 2, 3, 4, 5]; 1253 array.find("not a function"); 1254} catch (error) { 1255 errorStr = error.toString(); 1256} 1257assert_equal(errorStr, 'TypeError: the predicate is not callable.'); 1258 1259try { 1260 let array = null; 1261 array.find(item => item === 1); 1262} catch (error) { 1263 errorStr = error.toString(); 1264} 1265assert_equal(errorStr, 'TypeError: Cannot read property find of null'); 1266 1267try { 1268 array = undefined; 1269 array.find(item => item === 1); 1270} catch (error) { 1271 errorStr = error.toString(); 1272} 1273assert_equal(errorStr, 'ReferenceError: array is not defined'); 1274 1275/* 1276 * @tc.name:FindIndex 1277 * @tc.desc:test Array 1278 * @tc.type: FUNC 1279 */ 1280{ 1281 const array = [6, 7, 8, 9, 10, NaN, undefined, null, "", false, {name: "John"}, [1, 2, 3]]; 1282 assert_equal(array.findIndex(item => item === 5), -1); 1283 assert_equal(array.findIndex(item => item === 11), -1); 1284 assert_equal(array.findIndex(item => item > 5), 0); 1285 assert_equal(array.findIndex(item => item < 0), -1); 1286 assert_equal(array.findIndex(item => typeof item === 'string'), 8); 1287 assert_equal(array.findIndex(item => typeof item === 'object'), 7); 1288 assert_equal(array.findIndex(item => Array.isArray(item)), 11); 1289 assert_equal(array.findIndex(item => item), 0); 1290 assert_equal(array.findIndex(item => item === null), 7); 1291 assert_equal(array.findIndex(item => item === undefined), 6); 1292 assert_equal(array.findIndex(item => isNaN(item)), 5); 1293 assert_equal(array.findIndex(item => item === false), 9); 1294} 1295 1296try { 1297 const array = [1, 2, 3, 4, 5]; 1298 array.findIndex(); 1299} catch (error) { 1300 errorStr = error.toString(); 1301} 1302assert_equal(errorStr, 'TypeError: the predicate is not callable.'); 1303 1304try { 1305 const array = [1, 2, 3, 4, 5]; 1306 array.findIndex("not a function"); 1307} catch (error) { 1308 errorStr = error.toString(); 1309} 1310assert_equal(errorStr, 'TypeError: the predicate is not callable.'); 1311 1312try { 1313 let array = null; 1314 array.findIndex(item => item === 1); 1315} catch (error) { 1316 errorStr = error.toString(); 1317} 1318assert_equal(errorStr, 'TypeError: Cannot read property findIndex of null'); 1319 1320try { 1321 array = undefined; 1322 array.findIndex(item => item === 1); 1323} catch (error) { 1324 errorStr = error.toString(); 1325} 1326assert_equal(errorStr, 'ReferenceError: array is not defined'); 1327 1328/* 1329 * @tc.name:FindLast 1330 * @tc.desc:test Array 1331 * @tc.type: FUNC 1332 */ 1333{ 1334 const array = [6, 7, 8, 9, 10, NaN, undefined, null, "", false, {name: "John"}, [1, 2, 3]]; 1335 assert_equal(array.findLast(item => item === 5), undefined); 1336 assert_equal(array.findLast(item => item === 11), undefined); 1337 assert_equal(array.findLast(item => item > 5), 10); 1338 assert_equal(array.findLast(item => item < 0), undefined); 1339 assert_equal(array.findLast(item => typeof item === 'string'), ""); 1340 assert_equal(array.findLast(item => typeof item === 'object'), [1,2,3]); 1341 assert_equal(array.findLast(item => Array.isArray(item)), [1,2,3]); 1342 assert_equal(array.findLast(item => item), [1,2,3]); 1343 assert_equal(array.findLast(item => item === null), null); 1344 assert_equal(array.findLast(item => item === undefined), undefined); 1345 assert_equal(array.findLast(item => isNaN(item)), [1,2,3]); 1346 assert_equal(array.findLast(item => item === false), false); 1347} 1348 1349try { 1350 const array = [1, 2, 3, 4, 5]; 1351 array.findLast(); 1352} catch (error) { 1353 errorStr = error.toString(); 1354} 1355assert_equal(errorStr, 'TypeError: the predicate is not callable.'); 1356 1357try { 1358 const array = [1, 2, 3, 4, 5]; 1359 array.findLast("not a function"); 1360} catch (error) { 1361 errorStr = error.toString(); 1362} 1363assert_equal(errorStr, 'TypeError: the predicate is not callable.'); 1364 1365try { 1366 let array = null; 1367 array.findLast(item => item === 1); 1368} catch (error) { 1369 errorStr = error.toString(); 1370} 1371assert_equal(errorStr, 'TypeError: Cannot read property findLast of null'); 1372 1373try { 1374 array = undefined; 1375 array.findLast(item => item === 1); 1376} catch (error) { 1377 errorStr = error.toString(); 1378} 1379assert_equal(errorStr, 'ReferenceError: array is not defined'); 1380 1381/* 1382 * @tc.name:FindLastIndex 1383 * @tc.desc:test Array 1384 * @tc.type: FUNC 1385 */ 1386{ 1387 const array = [6, 7, 8, 9, 10, NaN, undefined, null, "", false, {name: "John"}, [1, 2, 3]]; 1388 assert_equal(array.findLastIndex(item => item === 5), -1); 1389 assert_equal(array.findLastIndex(item => item === 11), -1); 1390 assert_equal(array.findLastIndex(item => item > 5), 4); 1391 assert_equal(array.findLastIndex(item => item < 0), -1); 1392 assert_equal(array.findLastIndex(item => typeof item === 'string'), 8); 1393 assert_equal(array.findLastIndex(item => typeof item === 'object'), 11); 1394 assert_equal(array.findLastIndex(item => Array.isArray(item)), 11); 1395 assert_equal(array.findLastIndex(item => item), 11); 1396 assert_equal(array.findLastIndex(item => item === null), 7); 1397 assert_equal(array.findLastIndex(item => item === undefined), 6); 1398 assert_equal(array.findLastIndex(item => isNaN(item)), 11); 1399 assert_equal(array.findLastIndex(item => item === false), 9); 1400} 1401 1402try { 1403 const array = [1, 2, 3, 4, 5]; 1404 array.findLastIndex(); 1405} catch (error) { 1406 errorStr = error.toString(); 1407} 1408assert_equal(errorStr, 'TypeError: the predicate is not callable.'); 1409 1410try { 1411 const array = [1, 2, 3, 4, 5]; 1412 array.findLastIndex("not a function"); 1413} catch (error) { 1414 errorStr = error.toString(); 1415} 1416assert_equal(errorStr, 'TypeError: the predicate is not callable.'); 1417 1418try { 1419 let array = null; 1420 array.findLastIndex(item => item === 1); 1421} catch (error) { 1422 errorStr = error.toString(); 1423} 1424assert_equal(errorStr, 'TypeError: Cannot read property findLastIndex of null'); 1425 1426try { 1427 array = undefined; 1428 array.findLastIndex(item => item === 1); 1429} catch (error) { 1430 errorStr = error.toString(); 1431} 1432assert_equal(errorStr, 'ReferenceError: array is not defined'); 1433 1434/* 1435 * @tc.name:Flat 1436 * @tc.desc:test Array 1437 * @tc.type: FUNC 1438 */ 1439{ 1440 const array = [1, 2, [3, 4, [5, 6]], [], [[7], 8], 9, [10]]; 1441 1442 assert_equal(array.flat().toString(), '1,2,3,4,5,6,7,8,9,10'); 1443 assert_equal(array.flat(2), [1,2,3,4,5,6,7,8,9,10]); 1444 1445 const deeplyNestedArray = [1, [2, [3, [4, [5]]]]]; 1446 assert_equal(deeplyNestedArray.flat(Infinity), [1,2,3,4,5]); 1447 1448 const emptyArray = [1, [2, [], [3, []]]]; 1449 assert_equal(emptyArray.flat().toString(), '1,2,,3,'); 1450 1451 const sparseArray = [1, 2, , 3, 4, [5, , 6]]; 1452 assert_equal(sparseArray.flat(), [1,2,3,4,5,6]); 1453 1454 const irregularArray = [1, [2, 3, [4, [5]]]]; 1455 assert_equal(irregularArray.flat().toString(), '1,2,3,4,5'); 1456 1457 const arrayWithNonArrays = [1, 'string', {name: 'John'}, null, undefined]; 1458 assert_equal(arrayWithNonArrays.flat().toString(), '1,string,[object Object],,'); 1459 1460 const arrayWithNaNAndInfinity = [1, [NaN, Infinity], [2, [3, NaN]]]; 1461 assert_equal(arrayWithNaNAndInfinity.flat().toString(), '1,NaN,Infinity,2,3,NaN'); 1462} 1463 1464errorStr = ""; 1465try { 1466 const array = [1, 2, [3, 4]]; 1467 array.flat('string'); 1468} catch (error) { 1469 errorStr = error.toString(); 1470} 1471assert_equal(errorStr, ''); 1472 1473errorStr = ""; 1474try { 1475 const array = [1, 2, [3, 4]]; 1476 array.flat(-1); 1477} catch (error) { 1478 errorStr = error.toString(); 1479} 1480assert_equal(errorStr, ''); 1481 1482/* 1483 * @tc.name:FlatMap 1484 * @tc.desc:test Array 1485 * @tc.type: FUNC 1486 */ 1487{ 1488 const array = [1, 2, 3]; 1489 assert_equal(array.flatMap(x => [x, x * 2]), [1,2,2,4,3,6]); 1490 assert_equal(array.flatMap(x => []), []); 1491 assert_equal(array.flatMap(x => x * 2), [2,4,6]); 1492 assert_equal(array.flatMap((x, index) => [x, index]).toString(), '1,0,2,1,3,2'); 1493 const sparseArray = [1, , 2, , 3]; 1494 assert_equal(sparseArray.flatMap(x => [x]).toString(), '1,2,3'); 1495 const nestedArray = [[1, 2], [3, 4], [5, 6]]; 1496 assert_equal(nestedArray.flatMap(arr => arr).toString(), '1,2,3,4,5,6'); 1497 const arrayWithEmptyArrays = [1, [], [2, 3], [], 4]; 1498 assert_equal(arrayWithEmptyArrays.flatMap(x => x).toString(), '1,2,3,4'); 1499 assert_equal(array.flatMap(x => x % 2 === 0 ? [x, x * 2] : x).toString(), '1,2,4,3'); 1500} 1501 1502/* 1503 * @tc.name:ForEach 1504 * @tc.desc:test Array 1505 * @tc.type: FUNC 1506 */ 1507 1508try { 1509 const array = [1, 2, 3]; 1510 array.forEach('not a function'); 1511} catch (error) { 1512 errorStr = error.toString(); 1513} 1514assert_equal(errorStr, 'TypeError: the callbackfun is not callable.') 1515 1516/* 1517 * @tc.name:Includes 1518 * @tc.desc:test Array 1519 * @tc.type: FUNC 1520 */ 1521const testCases = [ 1522 { array: [1, 2, 3, 4, 5], target: 3 }, 1523 { array: [1, 2, 3, 4, 5], target: 6 }, 1524 { array: [NaN, 2, 3], target: NaN }, 1525 { array: [undefined, 2, 3], target: undefined }, 1526 { array: ["apple", "banana", "orange"], target: "banana" }, 1527 { array: ["apple", "banana", "orange"], target: "grape" }, 1528 { array: [], target: 1 }, 1529 { array: [true, false, true], target: true }, 1530 { array: [true, false, true], target: false }, 1531 { array: [Infinity, -Infinity], target: Infinity }, 1532 { array: [Infinity, -Infinity], target: -Infinity }, 1533 { array: [new Map(), new Map()], target: new Map() }, 1534 { array: [new Set(), new Set()], target: new Set() }, 1535]; 1536 1537/* 1538 * @tc.name:IndexOf 1539 * @tc.desc:test Array 1540 * @tc.type: FUNC 1541 */ 1542{ 1543 let arr = [1, 2, 3, 4, 5]; 1544 assert_equal(arr.indexOf(3), 2); 1545 assert_equal(arr.indexOf(1), 0); 1546 assert_equal(arr.indexOf(5), 4); 1547 assert_equal([].indexOf(1), -1); 1548 let arr2 = ["apple", "banana", "cherry"]; 1549 assert_equal(arr2.indexOf("banana"), 1) 1550 let arr3 = [1, 2, 2, 3, 4, 2]; 1551 assert_equal(arr3.indexOf(2), 1); 1552 assert_equal(arr.indexOf(10), -1); 1553 let arr4 = [{id: 1}, {id: 2}, {id: 3}]; 1554 assert_equal(arr4.indexOf({id: 2}), -1); 1555 assert_equal(arr4.findIndex(item => item.id === 2), 1); 1556 assert_equal("not an array".indexOf(1), -1); 1557} 1558 1559/* 1560 * @tc.name:Join 1561 * @tc.desc:test Array 1562 * @tc.type: FUNC 1563 */ 1564{ 1565 let arr = ["apple", "banana", "cherry"]; 1566 assert_equal(arr.join(), 'apple,banana,cherry'); 1567 assert_equal(arr.join(", "), 'apple, banana, cherry'); 1568 let emptyArr = []; 1569 assert_equal(emptyArr.join(), ""); 1570 let singleElementArr = ["apple"]; 1571 assert_equal(singleElementArr.join(), 'apple'); 1572 let mixedArr = ["apple", 1, {name: "John"}]; 1573 assert_equal(mixedArr.join(), 'apple,1,[object Object]'); 1574 let customSeparatorArr = ["apple", "banana", "cherry"]; 1575 assert_equal(customSeparatorArr.join(" + "), 'apple + banana + cherry'); 1576} 1577 1578/* 1579 * @tc.name:Keys 1580 * @tc.desc:test Array 1581 * @tc.type: FUNC 1582 */ 1583{ 1584 let arr = ["apple", "banana", "cherry"]; 1585 let keysIter = arr.keys(); 1586 for (let key of keysIter) { 1587 assert_true(key == 0 || key == 1 || key == 2); 1588 } 1589 1590 let emptyArr = []; 1591 let emptyKeysIter = emptyArr.keys(); 1592 assert_equal(emptyKeysIter.next().done, true); 1593 1594 let singleElementArr = ["apple"]; 1595 let singleElementKeysIter = singleElementArr.keys(); 1596 assert_equal(singleElementKeysIter.next().value, 0); 1597 assert_equal(singleElementKeysIter.next().done, true); 1598 1599 let multiDimArr = [["apple", "banana"], ["cherry", "date"]]; 1600 let multiDimKeysIter = multiDimArr.keys(); 1601 for (let key of multiDimKeysIter) { 1602 assert_true(key == 0 || key == 1); 1603 } 1604 1605 let sparseArr = [1, , 3]; 1606 let sparseKeysIter = sparseArr.keys(); 1607 for (let key of sparseKeysIter) { 1608 assert_true(key == 0 || key == 1 || key == 2); 1609 } 1610} 1611 1612/* 1613 * @tc.name:LastIndexOf 1614 * @tc.desc:test Array 1615 * @tc.type: FUNC 1616 */ 1617{ 1618 let arr = [1, 2, 3, 4, 2, 5]; 1619 assert_equal(arr.lastIndexOf(2), 4); 1620 1621 assert_equal(arr.lastIndexOf(1), 0); 1622 assert_equal(arr.lastIndexOf(5), 5); 1623 assert_equal(arr.lastIndexOf(6), -1); 1624 1625 let emptyArr = []; 1626 assert_equal(emptyArr.lastIndexOf(1), -1); 1627 1628 let arrWithNaN = [1, 2, NaN, 4, NaN]; 1629 assert_equal(arrWithNaN.lastIndexOf(NaN), -1); 1630 1631 let arrWithUndefined = [1, 2, undefined, 4]; 1632 assert_equal(arrWithUndefined.lastIndexOf(undefined), 2); 1633} 1634 1635/* 1636 * @tc.name:Map 1637 * @tc.desc:test Array 1638 * @tc.type: FUNC 1639 */ 1640{ 1641 let arr = [1, 2, 3, 4, 5]; 1642 let mappedArr = arr.map(num => num * 2); 1643 assert_equal(mappedArr, [2,4,6,8,10]); 1644 1645 let emptyArr = []; 1646 let mappedEmptyArr = emptyArr.map(item => item * 2); 1647 assert_equal(mappedEmptyArr, []); 1648 1649 let arrWithNaN = [1, 2, NaN, 4, NaN]; 1650 let mappedArrWithNaN = arrWithNaN.map(num => num * 2); 1651 assert_equal(mappedArrWithNaN.toString(), '2,4,NaN,8,NaN'); 1652 1653 let sparseArr = [1, , 3]; 1654 let mappedSparseArr = sparseArr.map(num => num * 2); 1655 assert_equal(mappedSparseArr, [2,,6]); 1656 1657 let objArr = [{id: 1}, {id: 2}, {id: 3}]; 1658 let mappedObjArr = objArr.map(obj => obj.id); 1659 assert_equal(mappedObjArr, [1,2,3]); 1660 1661 let multiDimArr = [[1, 2], [3, 4], [5, 6]]; 1662 let mappedMultiDimArr = multiDimArr.map(innerArr => innerArr.map(num => num * 2)); 1663 assert_equal(mappedMultiDimArr.toString(), '2,4,6,8,10,12'); 1664} 1665 1666/* 1667 * @tc.name:Pop 1668 * @tc.desc:test Array 1669 * @tc.type: FUNC 1670 */ 1671{ 1672 let arr = [1, 2, 3, 4, 5]; 1673 let poppedElement = arr.pop(); 1674 assert_equal(poppedElement, 5); 1675 assert_equal(arr, [1,2,3,4]); 1676 1677 let emptyArr = []; 1678 let poppedEmptyElement = emptyArr.pop(); 1679 assert_equal(poppedEmptyElement, undefined); 1680 assert_equal(emptyArr, []); 1681 1682 let singleElementArr = [1]; 1683 let poppedSingleElement = singleElementArr.pop(); 1684 assert_equal(poppedSingleElement, 1); 1685 assert_equal(singleElementArr, []); 1686 1687 let anotherSingleElementArr = ["apple"]; 1688 let poppedAnotherSingleElement = anotherSingleElementArr.pop(); 1689 assert_equal(poppedAnotherSingleElement, 'apple'); 1690} 1691 1692/* 1693 * @tc.name:Push 1694 * @tc.desc:test Array 1695 * @tc.type: FUNC 1696 */ 1697{ 1698 let arr = [1, 2, 3]; 1699 arr.push(4); 1700 assert_equal(arr, [1,2,3,4]); 1701 1702 arr.push(5, 6); 1703 assert_equal(arr, [1,2,3,4,5,6]); 1704 1705 let emptyArr = []; 1706 emptyArr.push(1); 1707 assert_equal(emptyArr, [1]); 1708 1709 let objArr = [{ id: 1 }]; 1710 objArr.push({ id: 2 }); 1711 assert_equal(objArr.toString(), '[object Object],[object Object]'); 1712 1713 let nestedArr = [1, 2]; 1714 nestedArr.push([3, 4]); 1715 assert_equal(nestedArr, [1,2,[3,4]]); 1716 1717 let arrWithUndefined = [1, 2, 3]; 1718 arrWithUndefined.push(undefined); 1719 assert_equal(arrWithUndefined, [1,2,3,undefined]); 1720 1721 let singleElementArr = [1]; 1722 singleElementArr.push(2); 1723 assert_equal(singleElementArr, [1,2]); 1724 1725 var ve = ['']; 1726 try { 1727 for (var item in ve) { 1728 var vee = ve[item]; 1729 Array.prototype.push.call(vee[item],[,,]); 1730 } 1731 } catch(e) { errorStr = e.toString(); } 1732 assert_equal(errorStr, 'TypeError: Cannot convert a UNDEFINED value to a JSObject'); 1733} 1734 1735/* 1736 * @tc.name:Reduce 1737 * @tc.desc:test Array 1738 * @tc.type: FUNC 1739 */ 1740{ 1741 let arr = [1, 2, 3, 4, 5]; 1742 let sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0); 1743 assert_equal(sum, 15); 1744 1745 let emptyArr = []; 1746 let sumOfEmptyArr = emptyArr.reduce((accumulator, currentValue) => accumulator + currentValue, 0); 1747 assert_equal(sumOfEmptyArr, 0); 1748 1749 let singleArr = [1]; 1750 let sumOfSingleArr = singleArr.reduce((accumulator, currentValue) => accumulator + currentValue, 0); 1751 assert_equal(sumOfSingleArr, 1); 1752 1753 let arrNaN = [1, 2, NaN, 4]; 1754 let sumOfArrNaN = arrNaN.reduce((accumulator, currentValue) => accumulator + currentValue, 0); 1755 assert_equal(sumOfArrNaN.toString(), 'NaN'); 1756} 1757 1758/* 1759 * @tc.name:ReduceRight 1760 * @tc.desc:test Array 1761 * @tc.type: FUNC 1762 */ 1763{ 1764 let array1 = ["a", "b", "c", "d", "e"]; 1765 let result1 = array1.reduceRight((acc, curr) => acc + curr); 1766 assert_equal(result1, 'edcba'); 1767 1768 let array2 = []; 1769 let result2 = array2.reduceRight((acc, curr) => acc + curr, "initialValue"); 1770 assert_equal(result2, 'initialValue'); 1771 1772 let array3 = ["a"]; 1773 let result3 = array3.reduceRight((acc, curr) => acc + curr); 1774 assert_equal(result3, 'a'); 1775 1776 let array4 = ["a", "b", undefined, "d"]; 1777 let result4 = array4.reduceRight((acc, curr) => acc + curr); 1778 assert_equal(result4, 'dundefinedba'); 1779} 1780 1781/* 1782 * @tc.name:Reverse 1783 * @tc.desc:test Array 1784 * @tc.type: FUNC 1785 */ 1786{ 1787 let array1 = ["a", "b", "c", "d", "e"]; 1788 array1.reverse(); 1789 assert_equal(array1, ['e', 'd', 'c', 'b', 'a']); 1790 1791 let emptyArray = []; 1792 emptyArray.reverse(); 1793 assert_equal(emptyArray, []); 1794 1795 let singleElementArray = ["a"]; 1796 singleElementArray.reverse(); 1797 assert_equal(singleElementArray, ['a']); 1798 1799 let arrayWithUndefined = ["a", "b", undefined, "d"]; 1800 arrayWithUndefined.reverse(); 1801 assert_equal(arrayWithUndefined, ['d', undefined, 'b', 'a']); 1802} 1803 1804/* 1805 * @tc.name:Shift 1806 * @tc.desc:test Array 1807 * @tc.type: FUNC 1808 */ 1809{ 1810 let array1 = ["a", "b", "c", "d", "e"]; 1811 let shiftedElement1 = array1.shift(); 1812 assert_equal(shiftedElement1, 'a'); 1813 assert_equal(array1, ['b', 'c', 'd', 'e']); 1814 1815 let emptyArray = []; 1816 let shiftedElement2 = emptyArray.shift(); 1817 assert_equal(shiftedElement2, undefined); 1818 assert_equal(emptyArray, []); 1819 1820 let singleElementArray = ["a"]; 1821 let shiftedElement3 = singleElementArray.shift(); 1822 assert_equal(shiftedElement3, 'a'); 1823 assert_equal(singleElementArray, []); 1824 1825 let arrayWithUndefined = ["a", undefined, "b", "c"]; 1826 let shiftedElement4 = arrayWithUndefined.shift(); 1827 assert_equal(shiftedElement4, 'a'); 1828 assert_equal(arrayWithUndefined, [undefined, 'b', 'c']); 1829} 1830 1831/* 1832 * @tc.name:Slice 1833 * @tc.desc:test Array 1834 * @tc.type: FUNC 1835 */ 1836{ 1837 let array1 = ["a", "b", "c", "d", "e"]; 1838 let slicedArray1 = array1.slice(1, 3); 1839 assert_equal(slicedArray1, ['b', 'c']); 1840 1841 let emptyArray = []; 1842 let slicedEmptyArray = emptyArray.slice(1, 3); 1843 assert_equal(slicedEmptyArray, []); 1844 1845 let singleElementArray = ["a"]; 1846 let slicedSingleElementArray = singleElementArray.slice(0, 1); 1847 assert_equal(slicedSingleElementArray, ['a']); 1848 1849 let arrayWithUndefined = ["a", undefined, "b", "c"]; 1850 let slicedArrayWithUndefined = arrayWithUndefined.slice(1, 3); 1851 assert_equal(slicedArrayWithUndefined, [undefined, 'b']); 1852} 1853 1854/* 1855 * @tc.name:Some 1856 * @tc.desc:test Array 1857 * @tc.type: FUNC 1858 */ 1859{ 1860 let array1 = [1, 2, 3, 4, 5]; 1861 let isSomeEven = array1.some(num => num % 2 === 0); 1862 assert_true(isSomeEven); 1863 1864 let array2 = [1, 3, 5, 7, 9]; 1865 let isSomeEven2 = array2.some(num => num % 2 === 0); 1866 assert_true(!isSomeEven2); 1867 1868 let emptyArray = []; 1869 let isSomeEmpty = emptyArray.some(num => num > 0); 1870 assert_true(!isSomeEmpty); 1871 1872 let singleElementArray = [1]; 1873 let isSomeSingleElement = singleElementArray.some(num => num > 0); 1874 assert_true(isSomeSingleElement); 1875 1876 let arrayWithUndefined = [1, undefined, 3, 5]; 1877 let isSomeUndefined = arrayWithUndefined.some(num => num === undefined); 1878 assert_true(isSomeUndefined); 1879} 1880 1881/* 1882 * @tc.name:Sort 1883 * @tc.desc:test Array 1884 * @tc.type: FUNC 1885 */ 1886{ 1887 let array1 = [3, 1, 4, 1, 5, 9, 2, 6, 5]; 1888 array1.sort(); 1889 assert_equal(array1, [1,1,2,3,4,5,5,6,9]); 1890 1891 let emptyArray = []; 1892 emptyArray.sort(); 1893 assert_equal(emptyArray, []); 1894 1895 let singleElementArray = [1]; 1896 singleElementArray.sort(); 1897 assert_equal(singleElementArray, [1]); 1898 1899 let arrayWithUndefined = [1, undefined, 3, 5]; 1900 arrayWithUndefined.sort(); 1901 assert_equal(arrayWithUndefined, [1,3,5,undefined]); 1902 1903 let arrayWithStrings = ["banana", "apple", "cherry"]; 1904 arrayWithStrings.sort(); 1905 assert_equal(arrayWithStrings, ['apple', 'banana', 'cherry']); 1906} 1907 1908/* 1909 * @tc.name:Splice 1910 * @tc.desc:test Array 1911 * @tc.type: FUNC 1912 */ 1913{ 1914 let array1 = ["a", "b", "c", "d", "e"]; 1915 let removedElements1 = array1.splice(2, 2, "x", "y"); 1916 assert_equal(removedElements1, ['c', 'd']); 1917 assert_equal(array1, ['a', 'b', 'x', 'y', 'e']); 1918 1919 let emptyArray = []; 1920 let removedElements2 = emptyArray.splice(0, 0, "x", "y"); 1921 assert_equal(removedElements2, []); 1922 assert_equal(emptyArray, ['x', 'y']); 1923 1924 let singleElementArray = ["a"]; 1925 let removedElements3 = singleElementArray.splice(0, 1, "x", "y"); 1926 assert_equal(removedElements3, ['a']); 1927 assert_equal(singleElementArray, ['x', 'y']); 1928 1929 let arrayWithUndefined = [1, undefined, 3, 5]; 1930 let removedElements4 = arrayWithUndefined.splice(1, 2); 1931 assert_equal(removedElements4, [undefined, 3]); 1932 assert_equal(arrayWithUndefined, [1, 5]); 1933} 1934 1935/* 1936 * @tc.name:toString 1937 * @tc.desc:test Array 1938 * @tc.type: FUNC 1939 */ 1940{ 1941 let array = ["apple", "banana", "cherry"]; 1942 let string = array.toString(); 1943 assert_equal(string, 'apple,banana,cherry'); 1944 1945 let numbers = [1, 2, 3, 4, 5]; 1946 let string2 = numbers.toString(); 1947 assert_equal(string2, '1,2,3,4,5'); 1948 1949 let mixed = [1, "two", true]; 1950 let string3 = mixed.toString(); 1951 assert_equal(string3, "1,two,true"); 1952} 1953 1954/* 1955 * @tc.name:Unshift 1956 * @tc.desc:test Array 1957 * @tc.type: FUNC 1958 */ 1959{ 1960 let array1 = ["a", "b", "c"]; 1961 let newLength1 = array1.unshift("x", "y"); 1962 assert_equal(newLength1, 5); 1963 assert_equal(array1, ['x', 'y', 'a', 'b', 'c']); 1964 1965 let emptyArray = []; 1966 let newLength2 = emptyArray.unshift("x", "y"); 1967 assert_equal(newLength2, 2); 1968 assert_equal(emptyArray, ['x', 'y']); 1969 1970 let singleElementArray = ["a"]; 1971 let newLength3 = singleElementArray.unshift("x"); 1972 assert_equal(newLength3, 2); 1973 assert_equal(singleElementArray, ['x', 'a']); 1974 1975 let arrayWithUndefined = [1, 2, undefined]; 1976 let newLength4 = arrayWithUndefined.unshift("x"); 1977 assert_equal(newLength4, 4); 1978 assert_equal(arrayWithUndefined, ['x', 1, 2, undefined]); 1979 1980 let arrayWithHole = [1, 2, ,4]; 1981 let newLength5 = arrayWithHole.unshift(5, 6, 7); 1982 assert_equal(newLength5, 7); 1983 assert_equal(arrayWithHole, [5,6,7,1,2,,4]); 1984 1985 let arrayWithHole1 = [1, 2, ,4]; 1986 let newLength6 = arrayWithHole1.unshift(5, 6, 7, 8); 1987 assert_equal(newLength6, 8); 1988 assert_equal(arrayWithHole1, [5,6,7,8,1,2,,4]); 1989} 1990 1991/* 1992 * @tc.name:ToReversed ToSorted ToSpliced With 1993 * @tc.desc:test Array 1994 * @tc.type: FUNC 1995 */ 1996{ 1997 let array1 = ["a", "b", "c", "d", "e"]; 1998 assert_equal(array1.toReversed(), ['e', 'd', 'c', 'b', 'a']); 1999 2000 let emptyArray = []; 2001 assert_equal(emptyArray.toReversed(), []); 2002 2003 let singleElementArray = ["a"]; 2004 assert_equal(singleElementArray.toReversed(), ['a']); 2005 2006 let arrayWithUndefined = ["a", "b", undefined, "d"]; 2007 assert_equal(arrayWithUndefined.toReversed(), ['d', undefined, 'b', 'a']); 2008} 2009 2010{ 2011 let array1 = [3, 1, 4, 1, 5, 9, 2, 6, 5]; 2012 assert_equal(array1.toSorted(), [1,1,2,3,4,5,5,6,9]); 2013 2014 let emptyArray = []; 2015 assert_equal(emptyArray.toSorted(), []); 2016 2017 let singleElementArray = [1]; 2018 assert_equal(singleElementArray.toSorted(), [1]); 2019 2020 let arrayWithUndefined = [1, undefined, 3, 5]; 2021 assert_equal(arrayWithUndefined.toSorted(), [1,3,5,undefined]); 2022 2023 let arrayWithStrings = ["banana", "apple", "cherry"]; 2024 assert_equal(arrayWithStrings.toSorted(), ['apple', 'banana', 'cherry']); 2025} 2026 2027{ 2028 let array1 = ["a", "b", "c", "d", "e"]; 2029 let removedElements1 = array1.toSpliced(2, 2, "x", "y"); 2030 assert_equal(removedElements1, ['a', 'b', 'x', 'y', 'e']); 2031 assert_equal(array1, ['a', 'b', 'c', 'd', 'e']); 2032 2033 let emptyArray = []; 2034 let removedElements2 = emptyArray.toSpliced(0, 0, "x", "y"); 2035 assert_equal(removedElements2, ['x', 'y']); 2036 assert_equal(emptyArray, []); 2037 2038 let singleElementArray = ["a"]; 2039 let removedElements3 = singleElementArray.toSpliced(0, 1, "x", "y"); 2040 assert_equal(removedElements3, ['x', 'y']); 2041 assert_equal(singleElementArray, ['a']); 2042 2043 let arrayWithUndefined = [1, undefined, 3, 5]; 2044 let removedElements4 = arrayWithUndefined.toSpliced(1, 2); 2045 assert_equal(removedElements4, [1,5]); 2046 assert_equal(arrayWithUndefined, [1,undefined,3,5]); 2047} 2048 2049/* 2050 * @tc.name:IsArray 2051 * @tc.desc:test Array 2052 * @tc.type: FUNC 2053 */ 2054{ 2055 // print true 2056 assert_true(Array.isArray([])); 2057 assert_true(Array.isArray([1])); 2058 assert_true(Array.isArray(new Array())); 2059 assert_true(Array.isArray(new Array("a", "b", "c", "d"))); 2060 assert_true(Array.isArray(new Array(3))); 2061 assert_true(Array.isArray(Array.prototype)); 2062 2063 // print false 2064 assert_true(!Array.isArray()); 2065 assert_true(!Array.isArray({})); 2066 assert_true(!Array.isArray(null)); 2067 assert_true(!Array.isArray(undefined)); 2068 assert_true(!Array.isArray(17)); 2069 assert_true(!Array.isArray("Array")); 2070 assert_true(!Array.isArray(true)); 2071 assert_true(!Array.isArray(false)); 2072 assert_true(!Array.isArray(new Uint8Array(32))); 2073 assert_true(!Array.isArray({ __proto__: Array.prototype })); 2074} 2075 2076var arr_push = []; 2077Object.defineProperty(arr_push, "length", { writable : false}); 2078try { 2079 arr_push.push(3); 2080} catch (e) { 2081 assert_true(e instanceof TypeError); 2082} 2083 2084// find index object 2085let findIndexArray = new Array(4); 2086const obj0 = { ["obj0"]: 0 }; 2087const obj1 = { ["obj1"]: 1 }; 2088const obj2 = { ["obj2"]: 2 }; 2089const obj3 = { ["obj3"]: 3 }; 2090 2091findIndexArray[0] = obj0; 2092findIndexArray[1] = obj1; 2093findIndexArray[2] = obj2; 2094findIndexArray[3] = obj3; 2095 2096assert_equal(findIndexArray.findIndex(element => element == obj1), 1); 2097assert_equal(findIndexArray.findIndex(element => element == { ["obj3"]: 3 }), -1); 2098 2099// find index bigint 2100findIndexArray = new Array(4); 2101findIndexArray[0] = 1n; 2102findIndexArray[1] = 2n; 2103findIndexArray[2] = 3n; 2104findIndexArray[3] = 4n; 2105assert_equal(findIndexArray.findIndex(element => element == 2n), 1); 2106assert_equal(findIndexArray.findIndex(element => element == 0n), -1); 2107 2108 2109// find index string 2110findIndexArray = new Array(4); 2111findIndexArray[0] = "a"; 2112findIndexArray[1] = "b"; 2113findIndexArray[2] = "c"; 2114findIndexArray[3] = "d"; 2115assert_equal(findIndexArray.findIndex(element => element == "b"), 1); 2116assert_equal(findIndexArray.findIndex(element => element == "e"), -1); 2117 2118{ 2119 Array.prototype.__proto__ = null; 2120 var vp = Array.prototype; 2121 vp.map(function(vp) {}); 2122 vp.filter(function(vp) {}); 2123 Array.prototype.__proto__ = Object.prototype; 2124} 2125// slice 2126let sliceArray = [0, 1, 2, 3, 4, 5]; 2127assert_equal(sliceArray.slice(0), [0,1,2,3,4,5]); 2128assert_equal(sliceArray.slice(-1), [5]); 2129assert_equal(sliceArray.slice(1, 4), [1,2,3]); 2130assert_equal(sliceArray.slice(1, 10), [1,2,3,4,5]); 2131 2132// Test ArrayIteratorNext for arraylike object 2133{ 2134 const arrayLike = { 2135 get length() { 2136 if (this._canAccessLength) { 2137 return 2; 2138 } else { 2139 throw new Error("Access to length is denied"); 2140 } 2141 }, 2142 0: 'a', 2143 1: 'b' 2144 }; 2145 2146 const iterator = Array.prototype[Symbol.iterator].call(arrayLike); 2147 let hasException = false; 2148 try { 2149 iterator.next() 2150 } catch { 2151 hasException = true; 2152 } 2153 assert_true(hasException); 2154} 2155 2156// Test ArrayIteratorNext for arraylike object 2157{ 2158 const arrayLike = { 2159 get length() { 2160 return 2; 2161 }, 2162 0: 'a', 2163 1: 'b' 2164 }; 2165 2166 // Values Iterator 2167 const valuesIterator = Array.prototype[Symbol.iterator].call(arrayLike); 2168 let result; 2169 while (!(result = valuesIterator.next()).done) { 2170 let v = result.value; 2171 let d = result.done; 2172 assert_true((v == 'a' && d == false) || (v == 'b' && d == false)); 2173 2174 } 2175 assert_true((result.value == undefined) && (result.done == true)); 2176 2177 // Keys Iterator 2178 const keysIterator = Array.prototype.keys.call(arrayLike); 2179 while (!(result = keysIterator.next()).done) { 2180 let v = result.value; 2181 let d = result.done; 2182 assert_true((v == 0 && d == false) || (v == 1 && d == false)); 2183 } 2184 assert_true((result.value == undefined) && (result.done == true)); 2185 2186 // Entries Iterator 2187 const entriesIterator = Array.prototype.entries.call(arrayLike); 2188 while (!(result = entriesIterator.next()).done) { 2189 let v = result.value.toString(); 2190 let d = result.done; 2191 assert_true((v == '0,a' && d == false) || (v == '1,b' && d == false)); 2192 } 2193 assert_true((result.value == undefined) && (result.done == true)); 2194} 2195 2196// Test ArrayIteratorNext for non-jsArrayIterator 2197{ 2198 const nonArrayIterator = { 2199 next: function () { 2200 return { done: true, value: undefined }; 2201 } 2202 }; 2203 2204 try { 2205 for (const item of nonArrayIterator) { 2206 throw new Error(); 2207 } 2208 } catch (error) { 2209 if (error instanceof TypeError) { 2210 assert_true(true); 2211 } else { 2212 assert_true(false); 2213 } 2214 } 2215} 2216 2217 2218let x1 = new Array(10); 2219let x2 = new Array(1,2,3,undefined); 2220let x3 = new Array(); 2221 2222x1.constructor = null 2223 2224assert_equal(ArkTools.hasConstructor(x2), false); 2225assert_equal(ArkTools.hasConstructor(x3), false); 2226// pop 2227var y = new Array(10) 2228for (let i = 0; i < y.length; i++) { 2229 y[i] = i 2230} 2231assert_equal(y.pop(), 9); //: 9 2232assert_equal(y.pop(), 8); //: 8 2233assert_equal(y.pop(), 7); //: 7 2234assert_equal(y.pop(), 6); //: 6 2235assert_equal(y.pop(), 5); //: 5 2236assert_equal(y, [0,1,2,3,4]); 2237// shift 2238var shiftArray = new Array(10) 2239for (let i = 0; i < shiftArray.length; i++) { 2240 shiftArray[i] = i 2241} 2242assert_equal(shiftArray.shift(), 0); //: 0 2243assert_equal(shiftArray.shift(), 1); //: 1 2244assert_equal(shiftArray.shift(), 2); //: 2 2245assert_equal(shiftArray.shift(), 3); //: 3 2246assert_equal(shiftArray.shift(), 4); //: 4 2247assert_equal(shiftArray, [5,6,7,8,9]); 2248// copyWithin 2249let copyWithInArray = new Array(0, 1, 2, 3, 4); 2250assert_equal(copyWithInArray.copyWithin(2,1,3), [0,1,1,2,4]) 2251 2252for (let i = 0; i < 2; i++) { 2253 let copyWithInArray1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 2254 copyWithInArray1.copyWithin(1,2,3); 2255 if (i == 0) { 2256 assert_equal(copyWithInArray1, [1,3,3,4,5,6,7,8,9]); 2257 } 2258 copyWithInArray1.copyWithin(1,3,2); 2259 if (i == 0) { 2260 assert_equal(copyWithInArray1, [1,3,3,4,5,6,7,8,9]); 2261 } 2262 copyWithInArray1.copyWithin(2,1,3); 2263 if (i == 0) { 2264 assert_equal(copyWithInArray1, [1,3,3,3,5,6,7,8,9]); 2265 } 2266 copyWithInArray1.copyWithin(2,3,1); 2267 if (i == 0) { 2268 assert_equal(copyWithInArray1, [1,3,3,3,5,6,7,8,9]); 2269 } 2270 copyWithInArray1.copyWithin(3,1,2); 2271 if (i == 0) { 2272 assert_equal(copyWithInArray1, [1,3,3,3,5,6,7,8,9]); 2273 } 2274 copyWithInArray1.copyWithin(3,2,1); 2275 if (i == 0) { 2276 assert_equal(copyWithInArray1, [1,3,3,3,5,6,7,8,9]); 2277 } 2278} 2279assert_equal([0, 1, 2, 3].copyWithin(0, 1, -10), [0,1,2,3]); 2280 2281var arr1 = new Array(1); 2282var arr2 = arr1.splice(16310, -2, '9'); 2283arr2.length = 60627; 2284arr2.fill(51442, 53470); 2285var fn = function() 2286{ 2287 arr2.length = 23670; 2288 arr2.length = 53125; 2289 arr2.fill(53470, 44166, 127); 2290} 2291assert_equal(arr2.reduce(fn, 44166), undefined); 2292 2293test_end();