Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
Hello UTS
提交
6d66d416
H
Hello UTS
项目概览
DCloud
/
Hello UTS
通知
1595
Star
27
Fork
9
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
Hello UTS
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
6d66d416
编写于
8月 30, 2024
作者:
M
mahaifeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[array]自动生成文档代码
上级
2a8fa841
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
484 addition
and
444 deletion
+484
-444
uni_modules/uts-tests/utssdk/Array.uts
uni_modules/uts-tests/utssdk/Array.uts
+484
-444
未找到文件。
uni_modules/uts-tests/utssdk/Array.uts
浏览文件 @
6d66d416
import { describe, test, expect, Result } from './tests.uts'
import { describe, test, expect, Result } from './tests.uts'
export function testArray(): Result {
export function testArray() : Result {
return describe("Array", () => {
return describe("Array", () => {
test('constructor', () => {
test('constructor', () => {
// 构造器测试
// 构造器测试
let a1 = [1,2,3]
// #TEST Array.Constructor
expect(a1).toEqual([1,2,3]);
let a1 = [1, 2, 3]
let a2 = [1,'2',3]
let a2 = [1, '2', 3]
expect(a2).toEqual([1,'2',3]);
console.log(a1) //[1, 2, 3]
let a3 = new Array(1,2,3);
console.log(a2) // [1, '2', 3]
// #END
// swift 中字面量创建数组,仅支持同一类型的元素
expect(a1).toEqual([1, 2, 3]);
// #ifndef APP-IOS
expect(a2).toEqual([1, '2', 3]);
expect(a3).toEqual(new Array(1,2,3));
let a3 = new Array(1, 2, 3);
let a4 = new Array<any>(1,'2',3);
expect(a4).toEqual(new Array<any>(1,'2',3));
// swift 中字面量创建数组,仅支持同一类型的元素
let a5 = Array(1,2,3);
// #ifndef APP-IOS
expect(a5).toEqual(Array(1,2,3));
expect(a3).toEqual(new Array(1, 2, 3));
let a6 = Array<any>(1,'2','3')
let a4 = new Array<any>(1, '2', 3);
expect(a6).toEqual(Array<any>(1,'2','3'));
expect(a4).toEqual(new Array<any>(1, '2', 3));
// #endif
let a5 = Array(1, 2, 3);
expect(a5).toEqual(Array(1, 2, 3));
})
let a6 = Array<any>(1, '2', '3')
expect(a6).toEqual(Array<any>(1, '2', '3'));
test('equals', () => {
// #endif
// 构造器测试
let a1 = [1,2,3]
})
let a2 = [1,2,3]
let equalsRet = (a1 == a2)
test('equals', () => {
console.log(equalsRet)
// 构造器测试
// #ifndef APP-IOS
let a1 = [1, 2, 3]
expect(equalsRet).toEqual(false);
let a2 = [1, 2, 3]
// #endif
let equalsRet = (a1 == a2)
// #ifdef APP-IOS
console.log(equalsRet)
expect(equalsRet).toEqual(true);
// #ifndef APP-IOS
// #endif
expect(equalsRet).toEqual(false);
// #endif
// #ifdef APP-IOS
})
expect(equalsRet).toEqual(true);
// #endif
test('length', () => {
const arr = ['shoes', 'shirts', 'socks', 'sweaters'];
expect(arr.length).toEqual(4);
})
expect(arr[0]).toEqual('shoes');
expect(arr[1]).toEqual('shirts');
test('convert-native', () => {
// expect(arr[4]).toEqual(null);
// #ifdef APP-ANDROID
const numbers: number[] = [1, 2, 3, 4, 5];
let utsArray = ["1", 2, 3.0]
if (numbers.length > 3) {
let javaArray = utsArray.toTypedArray();
numbers.length = 3;
let kotlinArray = utsArray.toKotlinList()
}
expect(numbers.length).toEqual(3);
let convertArrayFromJava = Array.fromNative(javaArray);
expect(numbers).toEqual([1, 2, 3]);
let convertArrayFromKotlin = Array.fromNative(kotlinArray);
expect([].length).toEqual(0);
expect(convertArrayFromJava[0] == convertArrayFromKotlin[0]).toEqual(true);
expect(convertArrayFromJava[0]).toEqual("1");
// 1. web: 最大长度 2^32-1
// #endif
// 超出边界报错: RangeError: Invalid array length
// 2. kotlin: 最大长度 2^31-1
})
// 超出边界报错: Error: targetMethod error::java.lang.OutOfMemoryError: Failed to allocate a 420546432 byte allocation with 6291456 free bytes and 300MB until OOM, target footprint 295113520, growth limit 603979776
// 3. swift: 最大长度和内存有关
test('length', () => {
// 超出边界没有返回信息
// #TEST Array.length
})
const arr = ['shoes', 'shirts', 'socks', 'sweaters'];
test("concat", () => {
console.log(arr.length)//4
expect(['a', 'b', 'c'].concat(['d', 'e', 'f'])).toEqual(["a", "b", "c", "d", "e", "f"]);
console.log(arr[1])//'shoes'
expect([1, 2, 3].concat([4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6]);
console.log(arr[1])//'shirts'
expect([''].concat([''])).toEqual(["", ""]);
// #END
const num1 = [1, 2, 3];
expect(arr.length).toEqual(4);
const num2 = [4, 5, 6];
expect(arr[0]).toEqual('shoes');
const num3 = [7, 8, 9];
expect(arr[1]).toEqual('shirts');
const numbers = num1.concat(num2, num3);
// expect(arr[4]).toEqual(null);
expect(numbers).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
const numbers : number[] = [1, 2, 3, 4, 5];
})
if (numbers.length > 3) {
test("copyWithin", () => {
numbers.length = 3;
const arr = ['a', 'b', 'c', 'd', 'e'];
}
expect(arr.copyWithin(0, 3, 4)).toEqual(["d", "b", "c", "d", "e"]);
expect(numbers.length).toEqual(3);
expect(arr.copyWithin(1, 3)).toEqual(["d", "d", "e", "d", "e"]);
expect(numbers).toEqual([1, 2, 3]);
const arr2 = [1, 2, 3, 4, 5];
expect([].length).toEqual(0);
expect(arr2.copyWithin(-2)).toEqual([1, 2, 3, 1, 2]);
expect(arr2.copyWithin(-2, -3, -1)).toEqual([1, 2, 3, 3, 1]);
// 1. web: 最大长度 2^32-1
})
// 超出边界报错: RangeError: Invalid array length
test("every", () => {
// 2. kotlin: 最大长度 2^31-1
const isBelowThreshold = (currentValue:number):boolean=> currentValue < 40;
// 超出边界报错: Error: targetMethod error::java.lang.OutOfMemoryError: Failed to allocate a 420546432 byte allocation with 6291456 free bytes and 300MB until OOM, target footprint 295113520, growth limit 603979776
const array1: number[] = [1, 30, 39, 29, 10, 13];
// 3. swift: 最大长度和内存有关
const array2: number[] = [1, 30, 39, 29, 10, 13, 41];
// 超出边界没有返回信息
expect(array1.every(isBelowThreshold)).toEqual(true);
})
expect(array2.every(isBelowThreshold)).toEqual(false);
test("concat", () => {
const array3: number[] = [1, 2, 3];
expect(['a', 'b', 'c'].concat(['d', 'e', 'f'])).toEqual(["a", "b", "c", "d", "e", "f"]);
array3.every((element:number, index:number, array:number[]):boolean => {
expect([1, 2, 3].concat([4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6]);
expect(array[index]).toEqual(element);
expect([''].concat([''])).toEqual(["", ""]);
return true;
const num1 = [1, 2, 3];
})
const num2 = [4, 5, 6];
})
const num3 = [7, 8, 9];
test("fill", () => {
const numbers = num1.concat(num2, num3);
const array1: number[] = [1, 2, 3, 4];
expect(numbers).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(array1.fill(0, 2, 4)).toEqual([1, 2, 0, 0]);
})
expect(array1.fill(5, 1)).toEqual([1, 5, 5, 5]);
test("copyWithin", () => {
expect(array1.fill(6)).toEqual([6, 6, 6, 6]);
const arr = ['a', 'b', 'c', 'd', 'e'];
const array2: number[]= [1, 2, 3]
expect(arr.copyWithin(0, 3, 4)).toEqual(["d", "b", "c", "d", "e"]);
expect(array2.fill(4)).toEqual([4, 4, 4]);
expect(arr.copyWithin(1, 3)).toEqual(["d", "d", "e", "d", "e"]);
const array3: number[]= [0, 0]
const arr2 = [1, 2, 3, 4, 5];
expect(array3.fill(1, null)).toEqual([1, 1]);
expect(arr2.copyWithin(-2)).toEqual([1, 2, 3, 1, 2]);
expect(array3.fill(1, 0, 1.5)).toEqual([1, 1]);
expect(arr2.copyWithin(-2, -3, -1)).toEqual([1, 2, 3, 3, 1]);
})
})
test("filter", () => {
test("every", () => {
const words: string[] = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const isBelowThreshold = (currentValue : number) : boolean => currentValue < 40;
const result = words.filter((word:string):boolean => word.length > 6);
const array1 : number[] = [1, 30, 39, 29, 10, 13];
expect(result).toEqual(["exuberant", "destruction", "present"]);
const array2 : number[] = [1, 30, 39, 29, 10, 13, 41];
const array1: number[] = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
expect(array1.every(isBelowThreshold)).toEqual(true);
const isPrime = array1.filter((num:number):boolean => {
expect(array2.every(isBelowThreshold)).toEqual(false);
for (let i = 2; num > i; i++) {
const array3 : number[] = [1, 2, 3];
// swift里,基础类型暂不支持!==,===对比
array3.every((element : number, index : number, array : number[]) : boolean => {
if (num % i == 0) {
expect(array[index]).toEqual(element);
return false;
return true;
}
})
}
})
return num > 1;
test("fill", () => {
})
const array1 : number[] = [1, 2, 3, 4];
expect(isPrime).toEqual([2, 3, 5, 7, 11, 13]);
expect(array1.fill(0, 2, 4)).toEqual([1, 2, 0, 0]);
const array2: number[] = [1, 2, 3];
expect(array1.fill(5, 1)).toEqual([1, 5, 5, 5]);
array2.filter((element:number, index:number, array:number[]):boolean => {
expect(array1.fill(6)).toEqual([6, 6, 6, 6]);
expect(array[index]).toEqual(element);
const array2 : number[] = [1, 2, 3]
return true;
expect(array2.fill(4)).toEqual([4, 4, 4]);
})
const array3 : number[] = [0, 0]
})
expect(array3.fill(1, null)).toEqual([1, 1]);
test("find", () => {
expect(array3.fill(1, 0, 1.5)).toEqual([1, 1]);
const array1: number[] = [5, 12, 8, 130, 44];
})
const found1 = array1.find((element:number):boolean => element > 10);
test("filter", () => {
expect(found1).toEqual(12);
const words : string[] = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const found2 = array1.find((element:number):boolean => element < 5);
const result = words.filter((word : string) : boolean => word.length > 6);
expect(found2).toEqual(null);
expect(result).toEqual(["exuberant", "destruction", "present"]);
const array2: number[] = [1, 2, 3];
const array1 : number[] = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
array2.find((element:number, index:number, array:number[]):boolean => {
const isPrime = array1.filter((num : number) : boolean => {
expect(array[index]).toEqual(element);
for (let i = 2; num > i; i++) {
return true;
// swift里,基础类型暂不支持!==,===对比
})
if (num % i == 0) {
})
return false;
test("findIndex", () => {
}
const array1: number[] = [5, 12, 8, 130, 44];
}
const isLargeNumber = (element:number):boolean => element > 13;
return num > 1;
expect(array1.findIndex(isLargeNumber)).toEqual(3);
})
const array2: number[] = [10, 11, 12];
expect(isPrime).toEqual([2, 3, 5, 7, 11, 13]);
expect(array2.findIndex(isLargeNumber)).toEqual(-1);
const array2 : number[] = [1, 2, 3];
const array3: number[] = [1, 2, 3];
array2.filter((element : number, index : number, array : number[]) : boolean => {
array3.findIndex((element:number, index:number, array:number[]):boolean => {
expect(array[index]).toEqual(element);
expect(array[index]).toEqual(element);
return true;
return true;
})
})
})
})
test("find", () => {
test("flat", () => {
// #TEST Array.find_1
const arr1: any[] = [0, 1, 2, [3, 4]];
const array1 : number[] = [5, 12, 8, 130, 44];
expect(arr1.flat()).toEqual([0, 1, 2, 3, 4]);
const found1 = array1.find((element : number) : boolean => element > 10);
const arr2: any[] = [0, 1, 2, [[[3, 4]]]];
console.log(found1) //12
expect(arr2.flat(2)).toEqual([0, 1, 2, [3, 4]]);
// #END
const arr3: any[] = [1, 2, [3, 4, [5, 6]]];
expect(found1).toEqual(12);
expect(arr3.flat(2)).toEqual([1, 2, 3, 4, 5, 6]);
// #TEST Array.find_1
})
const array3 : number[] = [5, 12, 8, 130, 44];
test("forEach", () => {
const found2 = array3.find((element : number, index : number) : boolean => element < 5);
const array1: string[] = ['a', 'b', 'c'];
console.log(found2) // null
array1.forEach((element:string, index:number) => {
// #END
expect(array1[index]).toEqual(element)
expect(found2).toEqual(null);
});
// #TEST Array.find
let array2 : number[] = [1, 2, 3];
const items: string[] = ['item1', 'item2', 'item3'];
array2.find((element : number, index : number, array : number[]) : boolean => {
const copyItems: string[] = [];
console.log(array[index]) //1=>2=>3
items.forEach((item:string) => {
return true;
copyItems.push(item);
})
});
// #END
expect(copyItems).toEqual(items)
array2 = [1, 2, 3];
})
array2.find((element : number, index : number, array : number[]) : boolean => {
test("includes", () => {
expect(array[index]).toEqual(element);
const array1: number[] = [1, 2, 3];
return true;
expect(array1.includes(2)).toEqual(true);
})
const pets: string[] = ['cat', 'dog', 'bat'];
})
expect(pets.includes('cat')).toEqual(true);
test("findIndex", () => {
expect(pets.includes('at')).toEqual(false);
const array1 : number[] = [5, 12, 8, 130, 44];
const array2: string[] = ['a', 'b', 'c'];
const isLargeNumber = (element : number) : boolean => element > 13;
expect(array2.includes('c', 3)).toEqual(false);
expect(array1.findIndex(isLargeNumber)).toEqual(3);
expect(array2.includes('c', 100)).toEqual(false);
const array2 : number[] = [10, 11, 12];
expect(array2.findIndex(isLargeNumber)).toEqual(-1);
type P = {
const array3 : number[] = [1, 2, 3];
x : number
array3.findIndex((element : number, index : number, array : number[]) : boolean => {
y : number
expect(array[index]).toEqual(element);
}
return true;
})
// #ifndef APP-IOS
})
const s = JSON.parse<P[]>(JSON.stringify([{ x: 0, y: 0 }])) as P[]
test("flat", () => {
s[0].x += 0;
const arr1 : any[] = [0, 1, 2, [3, 4]];
const clearList = s.map((v : P, _, _a) : number => v.x)
expect(arr1.flat()).toEqual([0, 1, 2, 3, 4]);
expect(clearList.includes(0)).toEqual(true);
const arr2 : any[] = [0, 1, 2, [[[3, 4]]]];
// #endif
expect(arr2.flat(2)).toEqual([0, 1, 2, [3, 4]]);
// #ifdef APP-IOS
const arr3 : any[] = [1, 2, [3, 4, [5, 6]]];
const s = JSON.parse<P[]>(JSON.stringify([{ x: 0, y: 0 }])!) as P[]
expect(arr3.flat(2)).toEqual([1, 2, 3, 4, 5, 6]);
s[0].x += 0;
})
const clearList = s.map((v : P, index: number, _a) : number => v.x)
test("forEach", () => {
expect(clearList.includes(0)).toEqual(true);
const array1 : string[] = ['a', 'b', 'c'];
// #endif
array1.forEach((element : string, index : number) => {
expect(array1[index]).toEqual(element)
})
});
test("indexOf", () => {
const items : string[] = ['item1', 'item2', 'item3'];
let raw = {}
const copyItems : string[] = [];
let arr = new Array<UTSJSONObject>()
items.forEach((item : string) => {
arr.push({});
copyItems.push(item);
arr.push({});
});
arr.push(raw);
expect(copyItems).toEqual(items)
expect(arr.indexOf(raw)).toEqual(2);
})
test("includes", () => {
const beasts: string[] = ['ant', 'bison', 'camel', 'duck', 'bison'];
const array1 : number[] = [1, 2, 3];
expect(beasts.indexOf('bison')).toEqual(1);
expect(array1.includes(2)).toEqual(true);
expect(beasts.indexOf('bison', 2)).toEqual(4);
const pets : string[] = ['cat', 'dog', 'bat'];
expect(beasts.indexOf('giraffe')).toEqual(-1);
expect(pets.includes('cat')).toEqual(true);
expect(pets.includes('at')).toEqual(false);
const indices: number[] = [];
const array2 : string[] = ['a', 'b', 'c'];
const array: string[] = ['a', 'b', 'a', 'c', 'a', 'd'];
expect(array2.includes('c', 3)).toEqual(false);
const element = 'a';
expect(array2.includes('c', 100)).toEqual(false);
let idx = array.indexOf(element);
// swift里,基础类型暂不支持!==,===对比
type P = {
while (idx != -1) {
x : number
indices.push(idx);
y : number
idx = array.indexOf(element, idx + 1);
}
}
expect(indices).toEqual([0, 2, 4]);
// #ifndef APP-IOS
})
const s = JSON.parse<P[]>(JSON.stringify([{ x: 0, y: 0 }])) as P[]
test("join", () => {
s[0].x += 0;
const elements: string[] = ['Fire', 'Air', 'Water'];
const clearList = s.map((v : P, _, _a) : number => v.x)
expect(elements.join()).toEqual("Fire,Air,Water");
expect(clearList.includes(0)).toEqual(true);
expect(elements.join('')).toEqual("FireAirWater");
// #endif
expect(elements.join('-')).toEqual("Fire-Air-Water");
// #ifdef APP-IOS
})
const s = JSON.parse<P[]>(JSON.stringify([{ x: 0, y: 0 }])!) as P[]
test("lastIndexOf", () => {
s[0].x += 0;
const clearList = s.map((v : P, index : number, _a) : number => v.x)
let raw = {}
expect(clearList.includes(0)).toEqual(true);
let arr = new Array<UTSJSONObject>()
// #endif
arr.push({});
arr.push({});
})
arr.push(raw);
test("indexOf", () => {
expect(arr.lastIndexOf(raw)).toEqual(2);
let raw = {}
let arr = new Array<UTSJSONObject>()
const animals: string[] = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
arr.push({});
expect(animals.lastIndexOf('Dodo')).toEqual(3);
arr.push({});
expect(animals.lastIndexOf('Tiger')).toEqual(1);
arr.push(raw);
const array: number[] = [2, 5, 9, 2];
expect(arr.indexOf(raw)).toEqual(2);
let index = array.lastIndexOf(2);
expect(index).toEqual(3);
const beasts : string[] = ['ant', 'bison', 'camel', 'duck', 'bison'];
index = array.lastIndexOf(7);
expect(beasts.indexOf('bison')).toEqual(1);
expect(index).toEqual(-1);
expect(beasts.indexOf('bison', 2)).toEqual(4);
index = array.lastIndexOf(2, 3);
expect(beasts.indexOf('giraffe')).toEqual(-1);
expect(index).toEqual(3);
index = array.lastIndexOf(2, 2);
const indices : number[] = [];
expect(index).toEqual(0);
const array : string[] = ['a', 'b', 'a', 'c', 'a', 'd'];
const element = 'a';
let idx = array.indexOf(element);
// swift里,基础类型暂不支持!==,===对比
while (idx != -1) {
})
indices.push(idx);
test("map", () => {
idx = array.indexOf(element, idx + 1);
const array1: number[] = [1, 4, 9, 16];
}
const map1 = array1.map((x:number):number => x * 2);
expect(indices).toEqual([0, 2, 4]);
expect(map1).toEqual([2, 8, 18, 32]);
})
test("join", () => {
const numbers: number[] = [1, 4, 9];
const elements : string[] = ['Fire', 'Air', 'Water'];
const roots = numbers.map((num:number):number => num + 1);
expect(elements.join()).toEqual("Fire,Air,Water");
expect(numbers).toEqual([1, 4, 9]);
expect(elements.join('')).toEqual("FireAirWater");
expect(roots).toEqual([2, 5, 10]);
expect(elements.join('-')).toEqual("Fire-Air-Water");
})
const array2: number[] = [1, 2, 3];
test("lastIndexOf", () => {
array2.map((element:number, index:number, array:number[]) => {
expect(array[index]).toEqual(element);
let raw = {}
})
let arr = new Array<UTSJSONObject>()
})
arr.push({});
test("pop", () => {
arr.push({});
const plants: string[] = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
arr.push(raw);
expect(plants.pop()).toEqual("tomato");
expect(arr.lastIndexOf(raw)).toEqual(2);
expect(plants).toEqual(["broccoli", "cauliflower", "cabbage", "kale"]);
plants.pop();
expect(plants).toEqual(["broccoli", "cauliflower", "cabbage"]);
const animals : string[] = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
})
expect(animals.lastIndexOf('Dodo')).toEqual(3);
test("push", () => {
expect(animals.lastIndexOf('Tiger')).toEqual(1);
const animals: string[] = ['pigs', 'goats', 'sheep'];
const array : number[] = [2, 5, 9, 2];
const count = animals.push('cows');
let index = array.lastIndexOf(2);
expect(count).toEqual(4);
expect(index).toEqual(3);
expect(animals).toEqual(['pigs', 'goats', 'sheep', 'cows']);
index = array.lastIndexOf(7);
animals.push('chickens', 'cats', 'dogs');
expect(index).toEqual(-1);
expect(animals).toEqual(["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]);
index = array.lastIndexOf(2, 3);
})
expect(index).toEqual(3);
test("reduce", () => {
index = array.lastIndexOf(2, 2);
const array1: number[] = [1, 2, 3, 4];
expect(index).toEqual(0);
const initialValue:number = 0;
const sumWithInitial = array1.reduce(
(previousValue:number, currentValue:number):number => previousValue + currentValue,
initialValue
);
})
expect(sumWithInitial).toEqual(10);
test("map", () => {
})
const array1 : number[] = [1, 4, 9, 16];
test("shift", () => {
const map1 = array1.map((x : number) : number => x * 2);
const array1: number[] = [1, 2, 3];
expect(map1).toEqual([2, 8, 18, 32]);
const firstElement = array1.shift();
expect(firstElement).toEqual(1);
const numbers : number[] = [1, 4, 9];
expect(array1).toEqual([2, 3]);
const roots = numbers.map((num : number) : number => num + 1);
})
expect(numbers).toEqual([1, 4, 9]);
test("slice", () => {
expect(roots).toEqual([2, 5, 10]);
const animals: string[] = ['ant', 'bison', 'camel', 'duck', 'elephant'];
expect(animals.slice(2)).toEqual(["camel", "duck", "elephant"]);
const array2 : number[] = [1, 2, 3];
expect(animals.slice(2, 4)).toEqual(["camel", "duck"]);
array2.map((element : number, index : number, array : number[]) => {
expect(animals.slice(1, 5)).toEqual(["bison", "camel", "duck", "elephant"]);
expect(array[index]).toEqual(element);
expect(animals.slice(-2)).toEqual(["duck", "elephant"]);
})
expect(animals.slice(2, -1)).toEqual(["camel", "duck"]);
})
expect(animals.slice()).toEqual(["ant", "bison", "camel", "duck", "elephant"]);
test("pop", () => {
})
const plants : string[] = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
test("some", () => {
expect(plants.pop()).toEqual("tomato");
const array: number[] = [1, 2, 3, 4, 5];
expect(plants).toEqual(["broccoli", "cauliflower", "cabbage", "kale"]);
const even = (element:number):boolean=> element % 2 == 0;
plants.pop();
expect(array.some(even)).toEqual(true);
expect(plants).toEqual(["broccoli", "cauliflower", "cabbage"]);
const isBiggerThan10 = (element:number):boolean=> element > 10;
})
expect([2, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(false);
test("push", () => {
expect([12, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(true);
const animals : string[] = ['pigs', 'goats', 'sheep'];
})
const count = animals.push('cows');
test("splice", () => {
expect(count).toEqual(4);
const months: string[] = ['Jan', 'March', 'April', 'June'];
expect(animals).toEqual(['pigs', 'goats', 'sheep', 'cows']);
months.splice(1, 0, 'Feb');
animals.push('chickens', 'cats', 'dogs');
expect(months).toEqual(["Jan", "Feb", "March", "April", "June"]);
expect(animals).toEqual(["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]);
months.splice(4, 1, 'May');
})
expect(months).toEqual(["Jan", "Feb", "March", "April", "May"]);
test("reduce", () => {
})
const array1 : number[] = [1, 2, 3, 4];
test('sort', () => {
const initialValue : number = 0;
const months = ['March', 'Jan', 'Feb', 'Dec'];
const sumWithInitial = array1.reduce(
months.sort();
(previousValue : number, currentValue : number) : number => previousValue + currentValue,
expect(months).toEqual(["Dec", "Feb", "Jan", "March"]);
initialValue
);
const array1 = [1, 30, 4, 21, 100000];
expect(sumWithInitial).toEqual(10);
array1.sort();
})
expect(array1).toEqual([1, 100000, 21, 30, 4]);
test("shift", () => {
const array1 : number[] = [1, 2, 3];
const array2 = [5, 1, 4, 2, 3];
const firstElement = array1.shift();
array2.sort((a, b):number => a - b);
expect(firstElement).toEqual(1);
expect(array2).toEqual([1, 2, 3, 4, 5]);
expect(array1).toEqual([2, 3]);
})
// const array3 = [5, "banana", 4, "apple", 3, "cherry", 2, "date", 1];
test("slice", () => {
// array3.sort();
const animals : string[] = ['ant', 'bison', 'camel', 'duck', 'elephant'];
// expect(array3).toEqual([1, 2, 3, 4, 5, "apple", "banana", "cherry", "date"]);
expect(animals.slice(2)).toEqual(["camel", "duck", "elephant"]);
expect(animals.slice(2, 4)).toEqual(["camel", "duck"]);
const array4 = [
expect(animals.slice(1, 5)).toEqual(["bison", "camel", "duck", "elephant"]);
{ name: "John", age: 24 },
expect(animals.slice(-2)).toEqual(["duck", "elephant"]);
{ name: "Sarah", age: 19 },
expect(animals.slice(2, -1)).toEqual(["camel", "duck"]);
{ name: "Bob", age: 27 },
expect(animals.slice()).toEqual(["ant", "bison", "camel", "duck", "elephant"]);
{ name: "Alice", age: 21 }
})
];
test("some", () => {
// 先强转类型,解决编译报错
const array : number[] = [1, 2, 3, 4, 5];
array4.sort((a, b):number => (a['age'] as number) - (b['age'] as number));
const even = (element : number) : boolean => element % 2 == 0;
expect(array.some(even)).toEqual(true);
// #ifndef APP-IOS
const isBiggerThan10 = (element : number) : boolean => element > 10;
expect(array4).toEqual([{ name: "Sarah", age: 19 }, { name: "Alice", age: 21 }, { name: "John", age: 24 }, { name: "Bob", age: 27 }]);
expect([2, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(false);
// #endif
expect([12, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(true);
})
// #ifdef APP-IOS
test("splice", () => {
const arr = array4.map((value: UTSJSONObject): number => { return value["age"] as number })
const months : string[] = ['Jan', 'March', 'April', 'June'];
expect(arr).toEqual([19, 21, 24, 27])
months.splice(1, 0, 'Feb');
// #endif
expect(months).toEqual(["Jan", "Feb", "March", "April", "June"]);
months.splice(4, 1, 'May');
expect(months).toEqual(["Jan", "Feb", "March", "April", "May"]);
})
test('sort', () => {
})
const months = ['March', 'Jan', 'Feb', 'Dec'];
test("unshift", () => {
months.sort();
const array1: number[] = [1, 2, 3];
expect(months).toEqual(["Dec", "Feb", "Jan", "March"]);
expect(array1.unshift(4, 5)).toEqual(5);
expect(array1).toEqual([4, 5, 1, 2, 3]);
const array1 = [1, 30, 4, 21, 100000];
})
array1.sort();
test("toString", () => {
expect(array1).toEqual([1, 100000, 21, 30, 4]);
const array1: number[] = [1, 2, 3];
expect(array1.toString()).toEqual("1,2,3");
const array2 = [5, 1, 4, 2, 3];
const array2 = new Array<string>()
array2.sort((a, b) : number => a - b);
array2.push("a")
expect(array2).toEqual([1, 2, 3, 4, 5]);
array2.push("b")
array2.push("c")
// const array3 = [5, "banana", 4, "apple", 3, "cherry", 2, "date", 1];
expect(array2.toString()).toEqual("a,b,c");
// array3.sort();
})
// expect(array3).toEqual([1, 2, 3, 4, 5, "apple", "banana", "cherry", "date"]);
test('reverse', () => {
// const array1: string[] = ['one', 'two', 'three'];
const array4 = [
// const reversed1: string[] = array1.reverse();
{ name: "John", age: 24 },
// expect(reversed1).toEqual(["three", "two", "one"]);
{ name: "Sarah", age: 19 },
// expect(array1).toEqual(["three", "·two", "one"]);
{ name: "Bob", age: 27 },
{ name: "Alice", age: 21 }
// const array2 = [1, 2, 3, 4, 5];
];
// const reversed2 = array2.reverse();
// 先强转类型,解决编译报错
// expect(reversed2).toEqual([5, 4, 3, 2, 1]);
array4.sort((a, b) : number => (a['age'] as number) - (b['age'] as number));
// expect(array2).toEqual([5, 4, 3, 2, 1]);
})
// #ifndef APP-IOS
test("reduceRight", () => {
expect(array4).toEqual([{ name: "Sarah", age: 19 }, { name: "Alice", age: 21 }, { name: "John", age: 24 }, { name: "Bob", age: 27 }]);
const array1: number[][] = [[0, 1], [2, 3], [4, 5]];
// #endif
const result1 = array1.reduceRight((accumulator: number[], currentValue: number[]): number[] => accumulator.concat(currentValue));
expect(result1).toEqual([4, 5, 2, 3, 0, 1]);
// #ifdef APP-IOS
const arr = array4.map((value : UTSJSONObject) : number => { return value["age"] as number })
const array2: number[] = [1, 2, 3, 4];
expect(arr).toEqual([19, 21, 24, 27])
const result2 = array2.reduceRight((acc: number, cur: number, index: number, array: number[]): number => {
// #endif
expect(array[index]).toEqual(cur);
return acc + cur;
});
expect(result2).toEqual(10);
})
const result3 = array2.reduceRight((acc: number, cur: number): number => acc + cur, 5);
test("unshift", () => {
expect(result3).toEqual(15);
const array1 : number[] = [1, 2, 3];
})
expect(array1.unshift(4, 5)).toEqual(5);
test("flatMap", () => {
expect(array1).toEqual([4, 5, 1, 2, 3]);
const arr: number[] = [1, 2, 3];
})
const result = arr.flatMap((x: number):number[] => [x, x * 2]);
test("toString", () => {
expect(result).toEqual([1, 2, 2, 4, 3, 6]);
// #TEST Array.length
const array1 : number[] = [1, 2, 3];
const arr1: number[] = [1, 2, 3, 4];
console.log(array1.toString()) //"1,2,3"
const result1 = arr1.flatMap((num: number, index: number, array: number[]): number[] => {
// #END
expect(array[index]).toEqual(num);
expect(array1.toString()).toEqual("1,2,3");
if (num % 2 == 0) {
const array2 = new Array<string>()
return [num * 2];
array2.push("a")
}
array2.push("b")
return [];
array2.push("c")
});
expect(array2.toString()).toEqual("a,b,c");
expect(result1).toEqual([4, 8]);
})
})
test('reverse', () => {
test("entries", () => {
// const array1: string[] = ['one', 'two', 'three'];
// const array1 = ['a', 'b', 'c'];
// const reversed1: string[] = array1.reverse();
// const iterator1 = array1.entries();
// expect(reversed1).toEqual(["three", "two", "one"]);
// expect(iterator1.next().value).toEqual([0, "a"]);
// expect(array1).toEqual(["three", "·two", "one"]);
// expect(iterator1.next().value).toEqual([1, "b"]);
// expect(iterator1.next().value).toEqual([2, "c"]);
// const array2 = [1, 2, 3, 4, 5];
// expect(iterator1.next().done).toEqual(true);
// const reversed2 = array2.reverse();
// expect(reversed2).toEqual([5, 4, 3, 2, 1]);
// const array2: any[] = [1, 2, 'hello', true, { name: 'john', age: 30 }, [4, 5]];
// expect(array2).toEqual([5, 4, 3, 2, 1]);
// let count = 0;
// for (const [index, element] of array2.entries()) {
// count++;
// expect(element).toEqual(array2[index]);
// }
// expect(count).toEqual(array2.length);
})
test("keys", () => {
// const array1 = ['a', 'b', 'c'];
// const iterator1 = array1.keys();
// expect(iterator1.next().value).toEqual(0);
// expect(iterator1.next().value).toEqual(1);
// expect(iterator1.next().value).toEqual(2);
// expect(iterator1.next().done).toEqual(true);
})
})
})
test("reduceRight", () => {
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]);
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);
return acc + cur;
});
expect(result2).toEqual(10);
const result3 = array2.reduceRight((acc : number, cur : number) : number => acc + cur, 5);
expect(result3).toEqual(15);
})
test("flatMap", () => {
const arr : number[] = [1, 2, 3];
const result = arr.flatMap((x : number) : number[] => [x, x * 2]);
expect(result).toEqual([1, 2, 2, 4, 3, 6]);
const arr1 : number[] = [1, 2, 3, 4];
const result1 = arr1.flatMap((num : number, index : number, array : number[]) : number[] => {
expect(array[index]).toEqual(num);
if (num % 2 == 0) {
return [num * 2];
}
return [];
});
expect(result1).toEqual([4, 8]);
})
test("entries", () => {
// const array1 = ['a', 'b', 'c'];
// const iterator1 = array1.entries();
// expect(iterator1.next().value).toEqual([0, "a"]);
// expect(iterator1.next().value).toEqual([1, "b"]);
// expect(iterator1.next().value).toEqual([2, "c"]);
// expect(iterator1.next().done).toEqual(true);
// const array2: any[] = [1, 2, 'hello', true, { name: 'john', age: 30 }, [4, 5]];
// let count = 0;
// for (const [index, element] of array2.entries()) {
// count++;
// expect(element).toEqual(array2[index]);
// }
// expect(count).toEqual(array2.length);
})
test("keys", () => {
// const array1 = ['a', 'b', 'c'];
// const iterator1 = array1.keys();
// expect(iterator1.next().value).toEqual(0);
// expect(iterator1.next().value).toEqual(1);
// expect(iterator1.next().value).toEqual(2);
// expect(iterator1.next().done).toEqual(true);
})
})
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录