mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 06:21:29 +00:00
Implement TypedArray#every/some/findIndex and improve map/reduce/reduceRight (#433)
This commit is contained in:
File diff suppressed because one or more lines are too long
@ -237,7 +237,7 @@ assert(multisubarr3.byteLength === 3);
|
||||
* tests work.
|
||||
*/
|
||||
|
||||
function testReduce<ArrayType, T>(): void {
|
||||
function testReduce<ArrayType extends TypedArray<T>, T extends number>(): void {
|
||||
var array: ArrayType = instantiate<ArrayType>(3);
|
||||
array[0] = <T>1;
|
||||
array[1] = <T>2;
|
||||
@ -247,8 +247,8 @@ function testReduce<ArrayType, T>(): void {
|
||||
// assert(testindex == index);
|
||||
// assert(array == self);
|
||||
// --testindex;
|
||||
return acc + val;
|
||||
}, 0);
|
||||
return <T>(acc + val);
|
||||
}, <T>0);
|
||||
assert(result == <T>6);
|
||||
}
|
||||
|
||||
@ -264,7 +264,7 @@ testReduce<Uint64Array, u64>();
|
||||
testReduce<Float32Array, f32>();
|
||||
testReduce<Float64Array, f64>();
|
||||
|
||||
function testReduceRight<ArrayType, T>(): void {
|
||||
function testReduceRight<ArrayType extends TypedArray<T>, T extends number>(): void {
|
||||
var array: ArrayType = instantiate<ArrayType>(3);
|
||||
array[0] = <T>1;
|
||||
array[1] = <T>2;
|
||||
@ -274,8 +274,8 @@ function testReduceRight<ArrayType, T>(): void {
|
||||
// assert(testindex == index);
|
||||
// assert(array == self);
|
||||
// --testindex;
|
||||
return acc + val;
|
||||
}, 0);
|
||||
return <T>(acc + val);
|
||||
}, <T>0);
|
||||
assert(result == <T>6);
|
||||
}
|
||||
|
||||
@ -291,7 +291,7 @@ testReduceRight<Uint64Array, u64>();
|
||||
testReduceRight<Float32Array, f32>();
|
||||
testReduceRight<Float64Array, f64>();
|
||||
|
||||
function testArrayMap<ArrayType extends TypedArray<T>, T>(): void {
|
||||
function testArrayMap<ArrayType extends TypedArray<T>, T extends number>(): void {
|
||||
var source: ArrayType = instantiate<ArrayType>(3);
|
||||
source[0] = <T>1;
|
||||
source[1] = <T>2;
|
||||
@ -301,7 +301,7 @@ function testArrayMap<ArrayType extends TypedArray<T>, T>(): void {
|
||||
// assert(self == source);
|
||||
// assert(testIndex == testIndex);
|
||||
// testIndex++;
|
||||
return value * value;
|
||||
return <T>(value * value);
|
||||
});
|
||||
assert(result[0] == <T>1);
|
||||
assert(result[1] == <T>4);
|
||||
@ -319,3 +319,93 @@ testArrayMap<Int64Array, i64>();
|
||||
testArrayMap<Uint64Array, u64>();
|
||||
testArrayMap<Float32Array, f32>();
|
||||
testArrayMap<Float64Array, f64>();
|
||||
|
||||
function testArraySome<ArrayType extends TypedArray<T>, T extends number>(): void {
|
||||
var source: ArrayType = instantiate<ArrayType>(3);
|
||||
source[0] = <T>2;
|
||||
source[1] = <T>4;
|
||||
source[2] = <T>6;
|
||||
// var testIndex: i32 = 0;
|
||||
var result: bool = source.some((value: T, index: i32, self: ArrayType): bool => {
|
||||
// assert(self == source);
|
||||
// assert(testIndex == testIndex);
|
||||
// testIndex++;
|
||||
return value == <T>2;
|
||||
});
|
||||
assert(result);
|
||||
var failResult = source.some((value: T, index: i32, self: ArrayType): bool => value == <T>0);
|
||||
|
||||
assert(!failResult);
|
||||
}
|
||||
|
||||
testArraySome<Int8Array, i8>();
|
||||
testArraySome<Uint8Array, u8>();
|
||||
testArraySome<Uint8ClampedArray, u8>();
|
||||
testArraySome<Int16Array, i16>();
|
||||
testArraySome<Uint16Array, u16>();
|
||||
testArraySome<Int32Array, i32>();
|
||||
testArraySome<Uint32Array, u32>();
|
||||
testArraySome<Int64Array, i64>();
|
||||
testArraySome<Uint64Array, u64>();
|
||||
testArraySome<Float32Array, f32>();
|
||||
testArraySome<Float64Array, f64>();
|
||||
|
||||
function testArrayFindIndex<ArrayType extends TypedArray<T>, T extends number>(): void {
|
||||
var source: ArrayType = instantiate<ArrayType>(3);
|
||||
source[0] = <T>1;
|
||||
source[1] = <T>2;
|
||||
source[2] = <T>3;
|
||||
// var testIndex: i32 = 0;
|
||||
var result = source.findIndex((value: T, index: i32, self: ArrayType): bool => {
|
||||
// assert(self == source);
|
||||
// assert(testIndex == testIndex);
|
||||
// testIndex++;
|
||||
return value == <T>2;
|
||||
});
|
||||
assert(result == 1);
|
||||
var failResult = source.findIndex((value: T, index: i32, self: ArrayType): bool => value == <T>4);
|
||||
|
||||
assert(failResult == -1);
|
||||
}
|
||||
|
||||
testArrayFindIndex<Int8Array, i8>();
|
||||
testArrayFindIndex<Uint8Array, u8>();
|
||||
testArrayFindIndex<Uint8ClampedArray, u8>();
|
||||
testArrayFindIndex<Int16Array, i16>();
|
||||
testArrayFindIndex<Uint16Array, u16>();
|
||||
testArrayFindIndex<Int32Array, i32>();
|
||||
testArrayFindIndex<Uint32Array, u32>();
|
||||
testArrayFindIndex<Int64Array, i64>();
|
||||
testArrayFindIndex<Uint64Array, u64>();
|
||||
testArrayFindIndex<Float32Array, f32>();
|
||||
testArrayFindIndex<Float64Array, f64>();
|
||||
|
||||
function testArrayEvery<ArrayType extends TypedArray<T>, T extends number>(): void {
|
||||
var source: ArrayType = instantiate<ArrayType>(3);
|
||||
source[0] = <T>2;
|
||||
source[1] = <T>4;
|
||||
source[2] = <T>6;
|
||||
// var testIndex: i32 = 0;
|
||||
var result: bool = source.every((value: T, index: i32, self: ArrayType): bool => {
|
||||
// assert(self == source);
|
||||
// assert(testIndex == testIndex);
|
||||
// testIndex++;
|
||||
return (value % <T>2) == <T>0;
|
||||
});
|
||||
assert(result);
|
||||
var failResult = source.every((value: T, index: i32, self: ArrayType): bool => value == <T>2);
|
||||
|
||||
assert(!failResult);
|
||||
}
|
||||
|
||||
testArrayEvery<Int8Array, i8>();
|
||||
testArrayEvery<Uint8Array, u8>();
|
||||
testArrayEvery<Uint8ClampedArray, u8>();
|
||||
testArrayEvery<Int16Array, i16>();
|
||||
testArrayEvery<Uint16Array, u16>();
|
||||
testArrayEvery<Int32Array, i32>();
|
||||
testArrayEvery<Uint32Array, u32>();
|
||||
testArrayEvery<Int64Array, i64>();
|
||||
testArrayEvery<Uint64Array, u64>();
|
||||
testArrayEvery<Float32Array, f32>();
|
||||
testArrayEvery<Float64Array, f64>();
|
||||
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user