From b22beded7465c9ea8edeeefe848b6c8c3021c803 Mon Sep 17 00:00:00 2001 From: mahaifeng Date: Tue, 3 Sep 2024 15:09:38 +0800 Subject: [PATCH] =?UTF-8?q?[array]=E5=8E=BB=E9=99=A4=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uts-tests/utssdk/Array.uts | 136 +++++++++++++++++++++---- 1 file changed, 118 insertions(+), 18 deletions(-) diff --git a/uni_modules/uts-tests/utssdk/Array.uts b/uni_modules/uts-tests/utssdk/Array.uts index 1e5dd2f..73a1c07 100644 --- a/uni_modules/uts-tests/utssdk/Array.uts +++ b/uni_modules/uts-tests/utssdk/Array.uts @@ -47,6 +47,7 @@ export function testArray() : Result { }) test('convert-native', () => { + // #TEST Array.toKotlinList // #ifdef APP-ANDROID let utsArray = ["1", 2, 3.0] let javaArray = utsArray.toTypedArray(); @@ -54,6 +55,10 @@ export function testArray() : Result { let convertArrayFromJava = Array.fromNative(javaArray); let convertArrayFromKotlin = Array.fromNative(kotlinArray); + console.log(convertArrayFromJava[0] == convertArrayFromKotlin[0])//true + + console.log(convertArrayFromJava[0])//"1" + // #END expect(convertArrayFromJava[0] == convertArrayFromKotlin[0]).toEqual(true); expect(convertArrayFromJava[0]).toEqual("1"); // #endif @@ -124,16 +129,27 @@ export function testArray() : Result { expect(ret4).toEqual([1, 2, 3, 3, 1]); }) test("every", () => { + // #TEST Array.every,Array.every_1,Array.every_2,Array.every_3 const isBelowThreshold = (currentValue : number) : boolean => currentValue < 40; const array1 : number[] = [1, 30, 39, 29, 10, 13]; + console.log(array1.every(isBelowThreshold));// true + const array2 : number[] = [1, 30, 39, 29, 10, 13, 41]; - expect(array1.every(isBelowThreshold)).toEqual(true); - expect(array2.every(isBelowThreshold)).toEqual(false); + console.log(array2.every(isBelowThreshold));// false + const array3 : number[] = [1, 2, 3]; + array3.every((element : number, index : number, array : number[]) : boolean => { + console.log(array[index])//1=>2->3 + return true; + }) + + // #END array3.every((element : number, index : number, array : number[]) : boolean => { expect(array[index]).toEqual(element); return true; }) + expect(array1.every(isBelowThreshold)).toEqual(true); + expect(array2.every(isBelowThreshold)).toEqual(false); }) test("fill", () => { // #TEST Array.fill @@ -155,9 +171,11 @@ export function testArray() : Result { expect(array3.fill(1, 0, 1.5)).toEqual([1, 1]); }) test("filter", () => { + // #TEST Array.filter,Array.filter_1,Array.filter_2,Array.filter_3 const words : string[] = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter((word : string) : boolean => word.length > 6); - expect(result).toEqual(["exuberant", "destruction", "present"]); + console.log(result);// ["exuberant", "destruction", "present"] + const array1 : number[] = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; const isPrime = array1.filter((num : number) : boolean => { for (let i = 2; num > i; i++) { @@ -168,12 +186,21 @@ export function testArray() : Result { } return num > 1; }) - expect(isPrime).toEqual([2, 3, 5, 7, 11, 13]); + console.log(isPrime)//[2, 3, 5, 7, 11, 13] + const array2 : number[] = [1, 2, 3]; array2.filter((element : number, index : number, array : number[]) : boolean => { expect(array[index]).toEqual(element); + console.log(array[index])//1=>2=>3 return true; }) + // #END + array2.filter((element : number, index : number, array : number[]) : boolean => { + expect(array[index]).toEqual(element); + return true; + }) + expect(result).toEqual(["exuberant", "destruction", "present"]); + expect(isPrime).toEqual([2, 3, 5, 7, 11, 13]); }) test("find", () => { // #TEST Array.find_2 @@ -232,20 +259,29 @@ export function testArray() : Result { expect(arr3.flat(2)).toEqual([1, 2, 3, 4, 5, 6]); }) test("forEach", () => { + // #TEST Array.forEach,Array.forEach_1,Array.forEach_2 const array1 : string[] = ['a', 'b', 'c']; - array1.forEach((element : string, index : number) => { - expect(array1[index]).toEqual(element) - }); - + array1.forEach(element => console.log(element)); + // expected output: "a" + // expected output: "b" + // expected output: "c" const items : string[] = ['item1', 'item2', 'item3']; const copyItems : string[] = []; items.forEach((item : string) => { copyItems.push(item); }); + console.log(copyItems)//['item1', 'item2', 'item3'] + // #END + array1.forEach((element : string, index : number) => { + expect(array1[index]).toEqual(element) + }); expect(copyItems).toEqual(items) }) test("includes", () => { + // #TEST Array.includes const array1 : number[] = [1, 2, 3]; + console.log(array1.includes(2))//true + // #END expect(array1.includes(2)).toEqual(true); const pets : string[] = ['cat', 'dog', 'bat']; expect(pets.includes('cat')).toEqual(true); @@ -282,7 +318,17 @@ export function testArray() : Result { arr.push(raw); expect(arr.indexOf(raw)).toEqual(2); + // #TEST Array.indexOf const beasts : string[] = ['ant', 'bison', 'camel', 'duck', 'bison']; + + console.log(beasts.indexOf('bison')); // 1 + + + console.log(beasts.indexOf('bison', 2));// 2 + + console.log(beasts.indexOf('giraffe'));// -1 + // #END + expect(beasts.indexOf('bison')).toEqual(1); expect(beasts.indexOf('bison', 2)).toEqual(4); expect(beasts.indexOf('giraffe')).toEqual(-1); @@ -318,8 +364,12 @@ export function testArray() : Result { arr.push(raw); expect(arr.lastIndexOf(raw)).toEqual(2); - + // #TEST Array.lastIndexOf const animals : string[] = ['Dodo', 'Tiger', 'Penguin', 'Dodo']; + console.log(animals.lastIndexOf('Dodo'));//3 + console.log(animals.lastIndexOf('Tiger'));//1 + // #END + expect(animals.lastIndexOf('Dodo')).toEqual(3); expect(animals.lastIndexOf('Tiger')).toEqual(1); const array : number[] = [2, 5, 9, 2]; @@ -337,19 +387,26 @@ export function testArray() : Result { }) test("map", () => { + // #TEST Array.map,Array.map_1,Array.map_2 const array1 : number[] = [1, 4, 9, 16]; const map1 = array1.map((x : number) : number => x * 2); - expect(map1).toEqual([2, 8, 18, 32]); - + console.log(map1); + // expected output: Array [2, 8, 18, 32] const numbers : number[] = [1, 4, 9]; const roots = numbers.map((num : number) : number => num + 1); - expect(numbers).toEqual([1, 4, 9]); - expect(roots).toEqual([2, 5, 10]); + const array2 : number[] = [1, 2, 3]; + array2.map((element : number, index : number, array : number[]) => { + console.log(array[index]) //1=>2=>3 + }) + // #END array2.map((element : number, index : number, array : number[]) => { expect(array[index]).toEqual(element); }) + expect(map1).toEqual([2, 8, 18, 32]); + expect(numbers).toEqual([1, 4, 9]); + expect(roots).toEqual([2, 5, 10]); }) test("pop", () => { // #TEST Array.pop @@ -378,12 +435,15 @@ export function testArray() : Result { }) test("reduce", () => { + // #TEST Array.reduce,Array.reduce_1,Array.reduce_2,Array.reduce_3,Array.reduce_4,Array.reduce_5 const array1 : number[] = [1, 2, 3, 4]; const initialValue : number = 0; const sumWithInitial = array1.reduce( (previousValue : number, currentValue : number) : number => previousValue + currentValue, initialValue ); + console.log(sumWithInitial)//10 + // #END expect(sumWithInitial).toEqual(10); }) test("shift", () => { @@ -429,8 +489,12 @@ export function testArray() : Result { expect(animals.slice()).toEqual(["ant", "bison", "camel", "duck", "elephant"]); }) test("some", () => { + // #TEST Array.some const array : number[] = [1, 2, 3, 4, 5]; const even = (element : number) : boolean => element % 2 == 0; + console.log(array.some(even));//true + // #END + expect(array.some(even)).toEqual(true); const isBiggerThan10 = (element : number) : boolean => element > 10; expect([2, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(false); @@ -490,7 +554,15 @@ export function testArray() : Result { }) test("unshift", () => { - const array1 : number[] = [1, 2, 3]; + // #TEST Array.unshift + const array1 = [1, 2, 3]; + + console.log(array1.unshift(4, 5)); + // 5 + + console.log(array1); + // [4, 5, 1, 2, 3] + // #END expect(array1.unshift(4, 5)).toEqual(5); expect(array1).toEqual([4, 5, 1, 2, 3]); }) @@ -518,19 +590,31 @@ export function testArray() : Result { // expect(array2).toEqual([5, 4, 3, 2, 1]); }) test("reduceRight", () => { + // #TEST Array.reduceRight,Array.reduceRight_1,Array.reduceRight_2,Array.reduceRight_3,Array.reduceRight_4,Array.reduceRight_5,Array.reduceRight_6 const array1 : number[][] = [[0, 1], [2, 3], [4, 5]]; const result1 = array1.reduceRight((accumulator : number[], currentValue : number[]) : number[] => accumulator.concat(currentValue)); - expect(result1).toEqual([4, 5, 2, 3, 0, 1]); + console.log(result1) //[4, 5, 2, 3, 0, 1] + const array2 : number[] = [1, 2, 3, 4]; - const result2 = array2.reduceRight((acc : number, cur : number, index : number, array : number[]) : number => { - expect(array[index]).toEqual(cur); + let result2 = array2.reduceRight((acc : number, cur : number, index : number, array : number[]) : number => { return acc + cur; }); - expect(result2).toEqual(10); + + console.log(result2) //10 + const result3 = array2.reduceRight((acc : number, cur : number) : number => acc + cur, 5); + console.log(result3) //15 + // #END + expect(result3).toEqual(15); + expect(result1).toEqual([4, 5, 2, 3, 0, 1]); + result2 = array2.reduceRight((acc : number, cur : number, index : number, array : number[]) : number => { + expect(array[index]).toEqual(cur); + return acc + cur; + }); + expect(result2).toEqual(10); }) test("flatMap", () => { const arr : number[] = [1, 2, 3]; @@ -571,6 +655,22 @@ export function testArray() : Result { // expect(iterator1.next().value).toEqual(2); // expect(iterator1.next().done).toEqual(true); }) + test("isArray", () => { + // #TEST Array.isArray + + console.log(Array.isArray([1, 3, 5])); + // Expected output: true + + console.log(Array.isArray('[]')); + // Expected output: false + + console.log(Array.isArray(new Array(5))); + // Expected output: true + + console.log(Array.isArray(new Int16Array([15, 33]))); + // Expected output: false + // #END + }) }) } \ No newline at end of file -- GitLab