!1531 [stdlib] Fix TypedArrays.$_get

Merge pull request !1531 from korobkovilya/typed_arrays_uarrays_get
This commit is contained in:
openharmony_ci 2024-04-12 08:56:43 +00:00 committed by Gitee
commit 1474ed2f35
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 946 additions and 1005 deletions

File diff suppressed because it is too large Load Diff

View File

@ -245,7 +245,7 @@ export class Uint8ClampedArray implements Iterable<Number> {
}
}
/**
/**
* Creates an Uint8ClampedArray with respect to length.
*
* @param length data initializer
@ -378,7 +378,7 @@ export class Uint8ClampedArray implements Iterable<Number> {
* @param index index to change
*/
public $_set(index: int, val: number): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -404,12 +404,13 @@ export class Uint8ClampedArray implements Iterable<Number> {
* @param index index to change
*/
public $_set(index: int, val: int): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
this.setUnsafeClamp(index, val)
}
/**
* Makes a copy of internal elements to targetPos from startPos to endPos.
@ -1316,7 +1317,6 @@ export class Uint8ClampedArray implements Iterable<Number> {
/// === with element lambda functions ===
/**
* Checks that all elements of Uint8ClampedArray satisfy the passed function
*
@ -1329,6 +1329,7 @@ export class Uint8ClampedArray implements Iterable<Number> {
(element: number, index: number, array: Uint8ClampedArray): boolean => { return fn(element) }
return this.every(newF)
}
/**
* creates a new Uint8ClampedArray from current Uint8ClampedArray based on a condition fn
*
@ -1407,13 +1408,13 @@ export class Uint8ClampedArray implements Iterable<Number> {
this.forEach(newF)
}
/**
* Creates a new Uint8ClampedArray using fn(arr[i]) over all elements of current Uint8ClampedArray
*
* @param fn a function to apply for each element of current Uint8ClampedArray
*
* @returns a new Uint8ClampedArray where for each element from current Uint8ClampedArray fn was applied
*/
/**
* Creates a new Uint8ClampedArray using fn(arr[i]) over all elements of current Uint8ClampedArray
*
* @param fn a function to apply for each element of current Uint8ClampedArray
*
* @returns a new Uint8ClampedArray where for each element from current Uint8ClampedArray fn was applied
*/
public map(fn: (val: number) => number): Uint8ClampedArray {
let newF: (val: number, index: number) => number =
(val: number, index: number): number => { return fn(val) }
@ -1445,15 +1446,13 @@ export class Uint8ClampedArray implements Iterable<Number> {
for (let i = 0; i < this.lengthInt; i++) {
arr[i] = this.getUnsafe(i)
}
// TODO(ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
/*
let mustPrecede: (a: int, b: int) => boolean =
(a: int, b: int): boolean => { return (fn(a, b) <= 0) }
sort(arr, mustPrecede)
for (let i = 0; i < this.lengthInt; i++) {
this.setUnsafe(i, arr[i])
}
*/
// NOTE (ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
// let mustPrecede: (a: int, b: int) => boolean =
// (a: int, b: int): boolean => { return (fn(a, b) <= 0) }
// sort(arr, mustPrecede)
// for (let i = 0; i < this.lengthInt; i++) {
// this.setUnsafe(i, arr[i])
// }
return this;
}
@ -1641,7 +1640,7 @@ export class Uint8ClampedArray implements Iterable<Number> {
* @param fn the condition to apply for each element
*
* @returns the first element that satisfies fn
* TODO: return int | undefined as in JS
* NOTE: return int | undefined as in JS
*/
public find(fn: (val: number, index: number, array: Uint8ClampedArray) => boolean): number {
for (let i = 0; i < this.lengthInt; i++) {
@ -1795,7 +1794,6 @@ export class Uint8ClampedArray implements Iterable<Number> {
public from(o: Object, mapFn: (e: Object, index: number) => int): Uint8ClampedArray {
throw new Error("Uint8ClampedArray.from: not implemented")
}
/** Byte offset within the underlying Buffer */
public get byteOffset(): number {
return this.byteOffsetInt
@ -1902,6 +1900,7 @@ export class Uint8ClampedArray implements Iterable<Number> {
}
}
class Uint8ArrayIteratorKeys implements IterableIterator<Number> {
private length: int = 0
private idx: int = 0
@ -2126,7 +2125,7 @@ export class Uint8Array implements Iterable<Number> {
}
}
/**
/**
* Creates an Uint8Array with respect to length.
*
* @param length data initializer
@ -2273,7 +2272,7 @@ export class Uint8Array implements Iterable<Number> {
* @param index index to change
*/
public $_set(index: int, val: number): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -2300,13 +2299,14 @@ export class Uint8Array implements Iterable<Number> {
* @param index index to change
*/
public $_set(index: int, val: int): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
let v = this.zeroIfInfinity(val)
this.setUnsafeClamp(index, v)
}
/**
* Makes a copy of internal elements to targetPos from startPos to endPos.
@ -3214,7 +3214,6 @@ export class Uint8Array implements Iterable<Number> {
/// === with element lambda functions ===
/**
* Checks that all elements of Uint8Array satisfy the passed function
*
@ -3227,6 +3226,7 @@ export class Uint8Array implements Iterable<Number> {
(element: number, index: number, array: Uint8Array): boolean => { return fn(element) }
return this.every(newF)
}
/**
* creates a new Uint8Array from current Uint8Array based on a condition fn
*
@ -3305,13 +3305,13 @@ export class Uint8Array implements Iterable<Number> {
this.forEach(newF)
}
/**
* Creates a new Uint8Array using fn(arr[i]) over all elements of current Uint8Array
*
* @param fn a function to apply for each element of current Uint8Array
*
* @returns a new Uint8Array where for each element from current Uint8Array fn was applied
*/
/**
* Creates a new Uint8Array using fn(arr[i]) over all elements of current Uint8Array
*
* @param fn a function to apply for each element of current Uint8Array
*
* @returns a new Uint8Array where for each element from current Uint8Array fn was applied
*/
public map(fn: (val: number) => number): Uint8Array {
let newF: (val: number, index: number) => number =
(val: number, index: number): number => { return fn(val) }
@ -3343,15 +3343,13 @@ export class Uint8Array implements Iterable<Number> {
for (let i = 0; i < this.lengthInt; i++) {
arr[i] = this.getUnsafe(i)
}
// TODO(ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
/*
let mustPrecede: (a: int, b: int) => boolean =
(a: int, b: int): boolean => { return (fn(a, b) <= 0) }
sort(arr, mustPrecede)
for (let i = 0; i < this.lengthInt; i++) {
this.setUnsafe(i, arr[i])
}
*/
// NOTE (ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
// let mustPrecede: (a: int, b: int) => boolean =
// (a: int, b: int): boolean => { return (fn(a, b) <= 0) }
// sort(arr, mustPrecede)
// for (let i = 0; i < this.lengthInt; i++) {
// this.setUnsafe(i, arr[i])
// }
return this;
}
@ -3539,7 +3537,7 @@ export class Uint8Array implements Iterable<Number> {
* @param fn the condition to apply for each element
*
* @returns the first element that satisfies fn
* TODO: return int | undefined as in JS
* NOTE: return int | undefined as in JS
*/
public find(fn: (val: number, index: number, array: Uint8Array) => boolean): number {
for (let i = 0; i < this.lengthInt; i++) {
@ -3693,7 +3691,6 @@ export class Uint8Array implements Iterable<Number> {
public from(o: Object, mapFn: (e: Object, index: number) => int): Uint8Array {
throw new Error("Uint8Array.from: not implemented")
}
/** Byte offset within the underlying Buffer */
public get byteOffset(): number {
return this.byteOffsetInt
@ -3794,6 +3791,7 @@ export class Uint8Array implements Iterable<Number> {
}
}
class Uint16ArrayIteratorKeys implements IterableIterator<Number> {
private length: int = 0
private idx: int = 0
@ -4025,7 +4023,7 @@ export class Uint16Array implements Iterable<Number> {
}
}
/**
/**
* Creates an Uint16Array with respect to length.
*
* @param length data initializer
@ -4172,7 +4170,7 @@ export class Uint16Array implements Iterable<Number> {
* @param index index to change
*/
public $_set(index: int, val: number): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -4199,13 +4197,14 @@ export class Uint16Array implements Iterable<Number> {
* @param index index to change
*/
public $_set(index: int, val: int): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
let v = this.zeroIfInfinity(val)
this.setUnsafeClamp(index, v)
}
/**
* Makes a copy of internal elements to targetPos from startPos to endPos.
@ -5113,7 +5112,6 @@ export class Uint16Array implements Iterable<Number> {
/// === with element lambda functions ===
/**
* Checks that all elements of Uint16Array satisfy the passed function
*
@ -5126,6 +5124,7 @@ export class Uint16Array implements Iterable<Number> {
(element: number, index: number, array: Uint16Array): boolean => { return fn(element) }
return this.every(newF)
}
/**
* creates a new Uint16Array from current Uint16Array based on a condition fn
*
@ -5204,13 +5203,13 @@ export class Uint16Array implements Iterable<Number> {
this.forEach(newF)
}
/**
* Creates a new Uint16Array using fn(arr[i]) over all elements of current Uint16Array
*
* @param fn a function to apply for each element of current Uint16Array
*
* @returns a new Uint16Array where for each element from current Uint16Array fn was applied
*/
/**
* Creates a new Uint16Array using fn(arr[i]) over all elements of current Uint16Array
*
* @param fn a function to apply for each element of current Uint16Array
*
* @returns a new Uint16Array where for each element from current Uint16Array fn was applied
*/
public map(fn: (val: number) => number): Uint16Array {
let newF: (val: number, index: number) => number =
(val: number, index: number): number => { return fn(val) }
@ -5242,15 +5241,13 @@ export class Uint16Array implements Iterable<Number> {
for (let i = 0; i < this.lengthInt; i++) {
arr[i] = this.getUnsafe(i)
}
// TODO(ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
/*
let mustPrecede: (a: int, b: int) => boolean =
(a: int, b: int): boolean => { return (fn(a, b) <= 0) }
sort(arr, mustPrecede)
for (let i = 0; i < this.lengthInt; i++) {
this.setUnsafe(i, arr[i])
}
*/
// NOTE (ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
// let mustPrecede: (a: int, b: int) => boolean =
// (a: int, b: int): boolean => { return (fn(a, b) <= 0) }
// sort(arr, mustPrecede)
// for (let i = 0; i < this.lengthInt; i++) {
// this.setUnsafe(i, arr[i])
// }
return this;
}
@ -5438,7 +5435,7 @@ export class Uint16Array implements Iterable<Number> {
* @param fn the condition to apply for each element
*
* @returns the first element that satisfies fn
* TODO: return int | undefined as in JS
* NOTE: return int | undefined as in JS
*/
public find(fn: (val: number, index: number, array: Uint16Array) => boolean): number {
for (let i = 0; i < this.lengthInt; i++) {
@ -5592,7 +5589,6 @@ export class Uint16Array implements Iterable<Number> {
public from(o: Object, mapFn: (e: Object, index: number) => int): Uint16Array {
throw new Error("Uint16Array.from: not implemented")
}
/** Byte offset within the underlying Buffer */
public get byteOffset(): number {
return this.byteOffsetInt
@ -5693,6 +5689,7 @@ export class Uint16Array implements Iterable<Number> {
}
}
class Uint32ArrayIteratorKeys implements IterableIterator<Number> {
private length: int = 0
private idx: int = 0
@ -5924,7 +5921,7 @@ export class Uint32Array implements Iterable<Number> {
}
}
/**
/**
* Creates an Uint32Array with respect to length.
*
* @param length data initializer
@ -6071,7 +6068,7 @@ export class Uint32Array implements Iterable<Number> {
* @param index index to change
*/
public $_set(index: int, val: number): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -6098,7 +6095,7 @@ export class Uint32Array implements Iterable<Number> {
* @param index index to change
*/
public $_set(index: int, val: long): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -6116,7 +6113,7 @@ export class Uint32Array implements Iterable<Number> {
this.$_set(index as int, val as int)
}
/**
/**
* Assigns val as element on index.
*
* @param val value to set
@ -6124,7 +6121,7 @@ export class Uint32Array implements Iterable<Number> {
* @param index index to change
*/
public $_set(index: int, val: int): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -7038,7 +7035,6 @@ export class Uint32Array implements Iterable<Number> {
/// === with element lambda functions ===
/**
* Checks that all elements of Uint32Array satisfy the passed function
*
@ -7051,6 +7047,7 @@ export class Uint32Array implements Iterable<Number> {
(element: number, index: number, array: Uint32Array): boolean => { return fn(element) }
return this.every(newF)
}
/**
* creates a new Uint32Array from current Uint32Array based on a condition fn
*
@ -7129,13 +7126,13 @@ export class Uint32Array implements Iterable<Number> {
this.forEach(newF)
}
/**
* Creates a new Uint32Array using fn(arr[i]) over all elements of current Uint32Array
*
* @param fn a function to apply for each element of current Uint32Array
*
* @returns a new Uint32Array where for each element from current Uint32Array fn was applied
*/
/**
* Creates a new Uint32Array using fn(arr[i]) over all elements of current Uint32Array
*
* @param fn a function to apply for each element of current Uint32Array
*
* @returns a new Uint32Array where for each element from current Uint32Array fn was applied
*/
public map(fn: (val: number) => number): Uint32Array {
let newF: (val: number, index: number) => number =
(val: number, index: number): number => { return fn(val) }
@ -7167,15 +7164,13 @@ export class Uint32Array implements Iterable<Number> {
for (let i = 0; i < this.lengthInt; i++) {
arr[i] = this.getUnsafe(i)
}
// TODO(ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
/*
let mustPrecede: (a: long, b: long) => boolean =
(a: long, b: long): boolean => { return (fn(a, b) <= 0) }
sort(arr, mustPrecede)
for (let i = 0; i < this.lengthInt; i++) {
this.setUnsafe(i, arr[i])
}
*/
// NOTE (ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
// let mustPrecede: (a: long, b: long) => boolean =
// (a: long, b: long): boolean => { return (fn(a, b) <= 0) }
// sort(arr, mustPrecede)
// for (let i = 0; i < this.lengthInt; i++) {
// this.setUnsafe(i, arr[i])
// }
return this;
}
@ -7363,7 +7358,7 @@ export class Uint32Array implements Iterable<Number> {
* @param fn the condition to apply for each element
*
* @returns the first element that satisfies fn
* TODO: return long | undefined as in JS
* NOTE: return long | undefined as in JS
*/
public find(fn: (val: number, index: number, array: Uint32Array) => boolean): number {
for (let i = 0; i < this.lengthInt; i++) {
@ -7517,7 +7512,6 @@ export class Uint32Array implements Iterable<Number> {
public from(o: Object, mapFn: (e: Object, index: number) => long): Uint32Array {
throw new Error("Uint32Array.from: not implemented")
}
/** Byte offset within the underlying Buffer */
public get byteOffset(): number {
return this.byteOffsetInt
@ -7618,6 +7612,7 @@ export class Uint32Array implements Iterable<Number> {
}
}
class BigUint64ArrayIteratorKeys implements IterableIterator<Number> {
private length: int = 0
private idx: int = 0
@ -7849,7 +7844,7 @@ export class BigUint64Array implements Iterable<BigInt> {
}
}
/**
/**
* Creates an BigUint64Array with respect to length.
*
* @param length data initializer
@ -7996,7 +7991,7 @@ export class BigUint64Array implements Iterable<BigInt> {
* @param index index to change
*/
public $_set(index: int, val: BigInt): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -8023,7 +8018,7 @@ export class BigUint64Array implements Iterable<BigInt> {
* @param index index to change
*/
public $_set(index: int, val: long): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -8041,7 +8036,7 @@ export class BigUint64Array implements Iterable<BigInt> {
this.$_set(index as int, val as int)
}
/**
/**
* Assigns val as element on index.
*
* @param val value to set
@ -8049,7 +8044,7 @@ export class BigUint64Array implements Iterable<BigInt> {
* @param index index to change
*/
public $_set(index: int, val: int): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -8963,7 +8958,6 @@ export class BigUint64Array implements Iterable<BigInt> {
/// === with element lambda functions ===
/**
* Checks that all elements of BigUint64Array satisfy the passed function
*
@ -8976,6 +8970,7 @@ export class BigUint64Array implements Iterable<BigInt> {
(element: BigInt, index: number, array: BigUint64Array): boolean => { return fn(element) }
return this.every(newF)
}
/**
* creates a new BigUint64Array from current BigUint64Array based on a condition fn
*
@ -9054,13 +9049,13 @@ export class BigUint64Array implements Iterable<BigInt> {
this.forEach(newF)
}
/**
* Creates a new BigUint64Array using fn(arr[i]) over all elements of current BigUint64Array
*
* @param fn a function to apply for each element of current BigUint64Array
*
* @returns a new BigUint64Array where for each element from current BigUint64Array fn was applied
*/
/**
* Creates a new BigUint64Array using fn(arr[i]) over all elements of current BigUint64Array
*
* @param fn a function to apply for each element of current BigUint64Array
*
* @returns a new BigUint64Array where for each element from current BigUint64Array fn was applied
*/
public map(fn: (val: BigInt) => BigInt): BigUint64Array {
let newF: (val: BigInt, index: number) => BigInt =
(val: BigInt, index: number): BigInt => { return fn(val) }
@ -9092,15 +9087,13 @@ export class BigUint64Array implements Iterable<BigInt> {
for (let i = 0; i < this.lengthInt; i++) {
arr[i] = this.getUnsafe(i)
}
// TODO(ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
/*
let mustPrecede: (a: long, b: long) => boolean =
(a: long, b: long): boolean => { return (fn(a, b) <= 0) }
sort(arr, mustPrecede)
for (let i = 0; i < this.lengthInt; i++) {
this.setUnsafe(i, arr[i])
}
*/
// NOTE (ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
// let mustPrecede: (a: long, b: long) => boolean =
// (a: long, b: long): boolean => { return (fn(a, b) <= 0) }
// sort(arr, mustPrecede)
// for (let i = 0; i < this.lengthInt; i++) {
// this.setUnsafe(i, arr[i])
// }
return this;
}
@ -9288,7 +9281,7 @@ export class BigUint64Array implements Iterable<BigInt> {
* @param fn the condition to apply for each element
*
* @returns the first element that satisfies fn
* TODO: return long | undefined as in JS
* NOTE: return long | undefined as in JS
*/
public find(fn: (val: BigInt, index: number, array: BigUint64Array) => boolean): BigInt {
for (let i = 0; i < this.lengthInt; i++) {
@ -9442,7 +9435,6 @@ export class BigUint64Array implements Iterable<BigInt> {
public from(o: Object, mapFn: (e: Object, index: number) => long): BigUint64Array {
throw new Error("BigUint64Array.from: not implemented")
}
/** Byte offset within the underlying Buffer */
public get byteOffset(): number {
return this.byteOffsetInt

View File

@ -73,7 +73,6 @@ class {{N}}ArrayIterator implements IterableIterator<{{subsetTypeValues}}> {
}
}
/**
* JS {{N}}Array API-compatible class
*/
@ -303,8 +302,8 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
this.length = other.length
this.byteOffset = 0
}
{%- if (N != 'Float32') and (N != 'Float64') %}
{% if (N != 'Float32') and (N != 'Float64') %}
internal zeroIfInfinity(val: {{elementCompat}}): {{elementCompat}} {
{%- if elementCompat == 'number' %}
if ((val == Infinity) || (val == -Infinity)) {
@ -322,7 +321,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
}
return val
}
{% endif %}
{%- endif %}
/**
* Assigns val as element on index.
@ -343,7 +342,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
* @param index index to change
*/
public $_set(index: int, val: {{elementCompat}}): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.length as int) {
throw new RangeError("invalid index")
}
@ -354,8 +353,8 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
this.setUnsafe(index, val{{fromElementCompat}})
{%- endif %}
}
{%- if T != 'int' %}
/**
* Assigns val as element on index.
*
@ -375,7 +374,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
* @param index index to change
*/
public $_set(index: int, val: int): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.length as int) {
throw new RangeError("invalid index")
}
@ -387,8 +386,8 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
{%- endif %}
}
{%- endif %}
{%- if T != 'double' %}
/**
* Assigns val as element on index.
*
@ -408,7 +407,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
* @param index index to change
*/
public $_set(index: int, val: {{T}}): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.length as int) {
throw new RangeError("invalid index")
}
@ -429,7 +428,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
* @returns a primitive at index
*/
public $_get(index: number): {{elementCompat}} {
this.$_get(index as int)
return this.$_get(index as int)
}
/**
@ -445,8 +444,8 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
}
return {{asElementCompat % 'this.getUnsafe(index)'}}
}
{%- if T != 'long' %}
{% if T != 'long' %}
/**
* Returns an instance of primitive type at passed index.
*
@ -478,6 +477,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
return new Number(this.getUnsafe(k))
}
{%- else %}
/**
* Returns an instance of primitive type at passed index.
*
@ -671,7 +671,6 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
return this.fill(value{{fromElementCompat}}, start as int, end as int)
}
/**
* Fills the {{N}}Array with specified value
*
@ -702,8 +701,8 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
return this
{%- endif %}
}
{%- if N != 'Float64' %}
/**
* Fills the {{N}}Array with specified value
*
@ -726,7 +725,6 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
return this.fill(value, start as int, asIntOrDefault(end, this.length as int));
}
/**
* Fills the {{N}}Array with specified value
*
@ -829,8 +827,8 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
{%- endif %}
}
}
{%- if T != 'double' %}
{% if T != 'double' %}
/**
* Copies all elements of arr to the current {{N}}Array.
*
@ -839,7 +837,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
public set(arr: {{elementCompat}}[]): void {
this.set(arr, 0 as number)
}
{%- endif%}
{%- endif %}
/**
* Copies all elements of arr to the current {{N}}Array.
@ -913,8 +911,8 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
}
return false
}
{%- if T != 'double' %}
{% if T != 'double' %}
/**
* Checks if specified argument is in {{N}}Array
*
@ -974,8 +972,8 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
}
return -1
}
{%- if T != 'double' %}
{% if (T != 'double') %}
/**
* Returns index of specified element
*
@ -1002,7 +1000,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
let res: StringBuilder = new StringBuilder("")
for (let i = 0; i < this.length as int - 1; ++i) {
res.append({{asElementCompat % 'this.getUnsafe(i)'}})
res.append(s!)
res.append(s)
}
if (this.length as int > 0) {
res.append({{asElementCompat % 'this.getUnsafe(this.length as int - 1)'}})
@ -1082,8 +1080,8 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
}
return -1
}
{%- if T != 'double' %}
{% if (T != 'double')%}
/**
* Moves backwards and search val.
*
@ -1166,7 +1164,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
return this.slice(begin as int, end as int)
}
/**
/**
* Creates a slice of current {{N}}Array using range [begin, end)
*
* @param begin start index to be taken into slice
@ -1272,7 +1270,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
return this.subarray(begin as int, end as int)
}
/**
/**
* Creates a {{N}}Array with the same underlying ArrayBufferLike
*
* @param begin start index, inclusive
@ -1441,12 +1439,11 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
}
/// === with element lambda functions ===
{# NOTE: to add overloads with idxType == 'int' we need to enhance generics supports #}
{#- NOTE: to add overloads with idxType == 'int' we need to enhance generics supports #}
{%- for idxType, castToIdx in [('number', ' as number')] %}
{%- set skip = False %}
{%- set elType, castToEl, castFromEl = elementCompat, asElementCompat, fromElementCompat %}
{%- set retType = 'Number | undefined' if elementCompat == 'number' else 'BigInt | undefined' %}
{%- if not skip %}
/**
* Checks that all elements of {{N}}Array satisfy the passed function
@ -1538,13 +1535,13 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
this.forEach(newF)
}
/**
* Creates a new {{N}}Array using fn(arr[i]) over all elements of current {{N}}Array
*
* @param fn a function to apply for each element of current {{N}}Array
*
* @returns a new {{N}}Array where for each element from current {{N}}Array fn was applied
*/
/**
* Creates a new {{N}}Array using fn(arr[i]) over all elements of current {{N}}Array
*
* @param fn a function to apply for each element of current {{N}}Array
*
* @returns a new {{N}}Array where for each element from current {{N}}Array fn was applied
*/
public map(fn: (val: {{elType}}) => {{elType}}): {{N}}Array {
let newF: (val: {{elType}}, index: number) => {{elType}} =
(val: {{elType}}, index: number): {{elType}} => { return fn(val) }
@ -1589,18 +1586,16 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
}
else {
// NOTE(ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
/*
let mustPrecede: (a: {{T}}, b: {{T}}) => boolean =
(a: {{T}}, b: {{T}}): boolean => { return (fn(a, b) <= 0) }
sort(arr, mustPrecede)
for (let i = 0; i < this.length as int; ++i) {
this.set(i, arr[i])
}
*/
// let mustPrecede: (a: {{T}}, b: {{T}}) => boolean =
// (a: {{T}}, b: {{T}}): boolean => { return (fn(a, b) <= 0) }
// sort(arr, mustPrecede)
// for (let i = 0; i < this.length as int; ++i) {
// this.set(i, arr[i])
// }
}
return this
}
{%- endif %} {# comment: end-condition for if not skip #}
{%- endif %}{#- comment: end-condition for if not skip #}
/**
* Checks that at least one element of {{N}}Array satisfies the passed function
@ -1696,12 +1691,12 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
}
/**
* Creates a new {{N}}Array using fn(arr[i]) over all elements of current {{N}}Array.
*
* @param fn a function to apply for each element of current {{N}}Array
*
* @returns a new {{N}}Array where for each element from current {{N}}Array fn was applied
*/
* Creates a new {{N}}Array using fn(arr[i]) over all elements of current {{N}}Array.
*
* @param fn a function to apply for each element of current {{N}}Array
*
* @returns a new {{N}}Array where for each element from current {{N}}Array fn was applied
*/
public map(fn: (val: {{elType}}, index: {{idxType}}) => {{elType}}): {{N}}Array {
let resBuf = new ArrayBuffer(this.length as int * {{N}}Array.BYTES_PER_ELEMENT as int)
let res = new {{N}}Array(resBuf, 0, resBuf.getByteLength() / {{N}}Array.BYTES_PER_ELEMENT as int)
@ -2123,5 +2118,4 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}> {
/** String \"{{N}}Array\" */
public readonly name = "{{N}}Array"
}
{%- endfor %}

View File

@ -272,7 +272,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
}
}
/**
/**
* Creates an {{element['name']}}Array with respect to length.
*
* @param length data initializer
@ -313,9 +313,8 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
this.lengthInt = other.length as int
this.byteOffsetInt = 0
}
{%- if element.get('name') != 'Uint8Clamped' %}{#- comment: Uint8Array and others, except Uint8Clamped #}
{%- if element.get('name') != 'Uint8Clamped' %}
{# comment: Uint8Array and others, except Uint8Clamped #}
internal zeroIfInfinity(val: {{element['subsetType']}}): {{element['subsetType']}} {
{%- if element.get('subsetType') == 'number' %}
if ((val == Infinity) || (val == -Infinity)) {
@ -333,7 +332,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
}
return val
}
{%- endif %}
{%- endif %}
/**
* Iteratorable interface implementation
@ -430,7 +429,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
* @param index index to change
*/
public $_set(index: int, val: {{element['subsetType']}}): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -461,7 +460,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
* @param index index to change
*/
public $_set(index: int, val: {{element['etsType']}}): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -472,9 +471,8 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
this.setUnsafeClamp(index, val)
{%- endif %}
}
{% if element['etsType'] != 'int' -%}
{%- if element['etsType'] != 'int' %}
/**
* Assigns val as element on index.
*
@ -486,7 +484,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
this.$_set(index as int, val as int)
}
/**
/**
* Assigns val as element on index.
*
* @param val value to set
@ -494,7 +492,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
* @param index index to change
*/
public $_set(index: int, val: int): void {
// NOTE (ikorobkov): TS doesn't throw exception. Exception added to avoid event of memory out-range access
// NOTE (ikorobkov): TS doesn't throw exception. Exception was added to avoid memory's out-of-range access
if (index < 0 || index >= this.lengthInt) {
throw new RangeError("invalid index")
}
@ -999,7 +997,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
let res: StringBuilder = new StringBuilder("")
for (let i = 0; i < this.lengthInt - 1; i++) {
res.append({{asElementCompat % 'this.getUnsafe(i)'}})
res.append(s!)
res.append(s)
}
if (this.lengthInt > 0) {
res.append({{asElementCompat % 'this.getUnsafe(this.lengthInt - 1)'}})
@ -1423,7 +1421,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
}
/// === with element lambda functions ===
{# NOTE: to add overloads with idxType == 'int' we need to enhance generics supports #}
{#- NOTE: to add overloads with idxType == 'int' we need to enhance generics supports #}
{%- for isEts in [False] %}
{%- if isEts %}
{%- set idxType, castToIdx = 'int', '' %}
@ -1445,6 +1443,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
(element: {{elType}}, index: number, array: {{element['name']}}Array): boolean => { return fn(element) }
return this.every(newF)
}
/**
* creates a new {{element['name']}}Array from current {{element['name']}}Array based on a condition fn
*
@ -1523,13 +1522,13 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
this.forEach(newF)
}
/**
* Creates a new {{element['name']}}Array using fn(arr[i]) over all elements of current {{element['name']}}Array
*
* @param fn a function to apply for each element of current {{element['name']}}Array
*
* @returns a new {{element['name']}}Array where for each element from current {{element['name']}}Array fn was applied
*/
/**
* Creates a new {{element['name']}}Array using fn(arr[i]) over all elements of current {{element['name']}}Array
*
* @param fn a function to apply for each element of current {{element['name']}}Array
*
* @returns a new {{element['name']}}Array where for each element from current {{element['name']}}Array fn was applied
*/
public map(fn: (val: {{elType}}) => {{elType}}): {{element['name']}}Array {
let newF: (val: {{elType}}, index: number) => {{elType}} =
(val: {{elType}}, index: number): {{elType}} => { return fn(val) }
@ -1561,15 +1560,13 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
for (let i = 0; i < this.lengthInt; i++) {
arr[i] = this.getUnsafe(i)
}
// TODO(ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
/*
let mustPrecede: (a: {{element['etsType']}}, b: {{element['etsType']}}) => boolean =
(a: {{element['etsType']}}, b: {{element['etsType']}}): boolean => { return (fn(a, b) <= 0) }
sort(arr, mustPrecede)
for (let i = 0; i < this.lengthInt; i++) {
this.setUnsafe(i, arr[i])
}
*/
// NOTE (ivan-tyulyandin): unresolved reference i in for loop, blocked by internal issue 12961
// let mustPrecede: (a: {{element['etsType']}}, b: {{element['etsType']}}) => boolean =
// (a: {{element['etsType']}}, b: {{element['etsType']}}): boolean => { return (fn(a, b) <= 0) }
// sort(arr, mustPrecede)
// for (let i = 0; i < this.lengthInt; i++) {
// this.setUnsafe(i, arr[i])
// }
return this;
}
@ -1757,7 +1754,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
* @param fn the condition to apply for each element
*
* @returns the first element that satisfies fn
* TODO: return {{element['etsType']}} | undefined as in JS
* NOTE: return {{element['etsType']}} | undefined as in JS
*/
public find(fn: (val: {{elType}}, index: {{idxType}}, array: {{element['name']}}Array) => boolean): {{elType}} {
for (let i = 0; i < this.lengthInt; i++) {
@ -1911,7 +1908,7 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
public from(o: Object, mapFn: (e: Object, index: {{idxType}}) => {{element['etsType']}}): {{element['name']}}Array {
throw new Error("{{element['name']}}Array.from: not implemented")
}
{%- endfor %}
{% endfor -%}
/** Byte offset within the underlying Buffer */
public get byteOffset(): number {
@ -2022,5 +2019,4 @@ export class {{element['name']}}Array implements Iterable<{{element['subsetTypeV
}
}
}
{%- endfor %}
{% endfor -%}

View File

@ -13,7 +13,8 @@
* limitations under the License.
*/
const success: int = 0;
const fail: int = 1;
function main(): int {
let failures: int = 0;
@ -51,30 +52,29 @@ function main(): int {
failures += check(testSlice0(), "Try to Array slice(0) function");
failures += check(testSlice1(), "Try to Array slice(1) function");
failures += check(testSlice2(), "Try to Array slice(2) function");
failures += check(createArray_Set_And_Get(), "Create Array from source data");
failures += check(createArraySetAndGet(), "Create Array from source data");
{% for ci in copyWithin -%}
failures += check({{.ci.name}}(), "Test262: {{.ci.name}}");
{%+ endfor %}
if (failures > 0) return 1
return 0;
if (failures > 0) { return fail; }
return success;
}
function check(result: int, message: String): int {
if(result == 0) {
return 0;
if (result == 0) {
return success;
}
console.println("\nFAILED: " + message);
return 1;
return fail;
}
function createDefault(): int {
let target: {{.item.objectType}} = new {{.item.objectType}}();
if(target.length as int == 0 && target.byteOffset as int == 0) return 0;
return 1;
if (target.length as int == 0 && target.byteOffset as int == 0) { return success; }
return fail;
}
function createTAFromEmptyArrayBuffer(): int {
@ -83,11 +83,11 @@ function createTAFromEmptyArrayBuffer(): int {
try {
target = new {{.item.objectType}}(ss);
} catch(e) {
return 1;
return fail;
}
if(target.length as int == 0 && target.byteOffset as int == 0) return 0;
return 1;
if (target.length as int == 0 && target.byteOffset as int == 0) return success;
return fail;
}
function createTAFromNonEmptyArrayBuffer(): int {
@ -96,12 +96,12 @@ function createTAFromNonEmptyArrayBuffer(): int {
try {
target = new {{.item.objectType}}(ss);
} catch(e) {
return 1;
return fail;
}
if(target.byteLength as int == 5*{{.item.primitiveSizeBytes}} && target.byteOffset as int == 0) return 0;
if (target.byteLength as int == 5*{{.item.primitiveSizeBytes}} && target.byteOffset as int == 0) { return success; }
console.println("Error: Actual bytes length: " + target.byteLength);
return 1;
return fail;
}
@ -111,11 +111,11 @@ function createTAFromEmptyArrayBufferOneParamNoOffset(): int {
try {
target = new {{.item.objectType}}(ss, 0 as number);
} catch(e) {
return 1;
return fail;
}
if(target.length as int == 0 && target.byteOffset as int == 0) return 0;
return 1;
if (target.length as int == 0 && target.byteOffset as int == 0) { return success; }
return fail;
}
function createTAFromNonEmptyArrayBufferOneParamNoOffset(): int {
@ -124,12 +124,12 @@ function createTAFromNonEmptyArrayBufferOneParamNoOffset(): int {
try {
target = new {{.item.objectType}}(ss, 0 as number);
} catch(e) {
return 1;
return fail;
}
if(target.byteLength as int == 5*{{.item.primitiveSizeBytes}} && target.byteOffset as int == 0) return 0;
if (target.byteLength as int == 5*{{.item.primitiveSizeBytes}} && target.byteOffset as int == 0) { return success; }
console.println("Error: Actual bytes length: " + target.byteLength);
return 1;
return fail;
}
function createTAFromNonEmptyArrayBufferOneParamWithOffset(): int {
@ -139,13 +139,13 @@ function createTAFromNonEmptyArrayBufferOneParamWithOffset(): int {
target = new {{.item.objectType}}(ss, 2*{{.item.primitiveSizeBytes}} as number);
} catch(e) {
console.println(e);
return 1;
return fail;
}
if(target.byteLength as int == 5*{{.item.primitiveSizeBytes}} && target.byteOffset as int == 2*{{.item.primitiveSizeBytes}}) return 0;
if (target.byteLength as int == 5*{{.item.primitiveSizeBytes}} && target.byteOffset as int == 2*{{.item.primitiveSizeBytes}}) { return success; }
console.println("Error: Actual bytes length: " + target.byteLength);
console.println("Error: Actual bytes offset: " + target.byteOffset);
return 1;
return fail;
}
function createTAFromNonEmptyArrayBufferOneParamWithOffsetAndSize(): int {
@ -155,13 +155,13 @@ function createTAFromNonEmptyArrayBufferOneParamWithOffsetAndSize(): int {
target = new {{.item.objectType}}(ss, 2*{{.item.primitiveSizeBytes}} as number, 4 as number);
} catch(e) {
console.println(e);
return 1;
return fail;
}
if(target.byteLength as int == 4*{{.item.primitiveSizeBytes}} && target.byteOffset as int == 2*{{.item.primitiveSizeBytes}} && target.length as int == 4) return 0;
if (target.byteLength as int == 4*{{.item.primitiveSizeBytes}} && target.byteOffset as int == 2*{{.item.primitiveSizeBytes}} && target.length as int == 4) { return success; }
console.println("Error: Actual bytes length: " + target.byteLength);
console.println("Error: Actual bytes offset: " + target.byteOffset);
return 1;
return fail;
}
@ -172,11 +172,11 @@ function createTAFromEmptyArrayBufferTwoParams(): int {
try {
target = new {{.item.objectType}}(ss, 0 as number, 0 as number);
} catch(e) {
return 1;
return fail;
}
if(target.length as int == 0 && target.byteOffset as int == 0) return 0;
return 1;
if (target.length as int == 0 && target.byteOffset as int == 0) { return success; }
return fail;
}
function createTAFromNonEmptyArrayBufferTwoParamsZeroLength(): int {
@ -185,12 +185,12 @@ function createTAFromNonEmptyArrayBufferTwoParamsZeroLength(): int {
try {
target = new {{.item.objectType}}(ss, 0 as number, 0 as number);
} catch(e) {
return 1;
return fail;
}
if(target.byteLength as int == 0 && target.byteOffset as int == 0) return 0;
if (target.byteLength as int == 0 && target.byteOffset as int == 0) { return success; }
console.println("Error: Actual bytes length: " + target.byteLength);
return 1;
return fail;
}
function createTAFromNonEmptyArrayBufferTwoParams(): int {
@ -199,12 +199,12 @@ function createTAFromNonEmptyArrayBufferTwoParams(): int {
try {
target = new {{.item.objectType}}(ss, 0 as number, 3 as number);
} catch(e) {
return 1;
return fail;
}
if(target.byteLength as int == 3*{{.item.primitiveSizeBytes}} && target.byteOffset as int == 0) return 0;
if (target.byteLength as int == 3*{{.item.primitiveSizeBytes}} && target.byteOffset as int == 0) { return success; }
console.println("Error: Actual bytes length: " + target.byteLength);
return 1;
return fail;
}
const source: {{.item.primitiveType}}[] = {{.item.data}};
@ -212,40 +212,65 @@ const source: {{.item.primitiveType}}[] = {{.item.data}};
{% set posInf = item.primitiveType[0].upper() + item.primitiveType[1:] + '.POSITIVE_INFINITY' %}
{% set negInf = '-' + item.primitiveType[0].upper() + item.primitiveType[1:] + '.NEGATIVE_INFINITY' %}
{% set skipInfinity = True if item.primitiveType == 'float' or item.primitiveType == 'double' else False %}
{% set skipInfinity = False %}
function createArray_Set_And_Get(): int {
function createArraySetAndGet(): int {
let ss = new ArrayBuffer(source.length as int * {{.item.primitiveSizeBytes}});
let target: {{.item.objectType}};
try {
target = new {{.item.objectType}}(ss);
} catch(e) {
return 1;
return fail;
}
for(let i:int = 0; i < source.length as int; i++) {
for (let i: int = 0; i < source.length as int; i++) {
try {
target.set(i, source[i] as {{.item.primitiveType}});
} catch(e) {
console.print("Catched exception at insert");
return 1;
console.print("Catched exception at insert via set(int, {{.item.primitiveType}})");
return fail;
}
}
//console.println("Validate....");
let acc: int = 0;
for(let i:int = 0; i < source.length as int; i++) {
let back = target[i] as {{.item.primitiveType}} ;
for (let i: int = 0; i < source.length as int; i++) {
{%- if skipInfinity %}
if ((source[i] == {{.posInf}}) && (source[i] == {{.negInf}})) {
continue;
}
{%- endif %}
if(back != source[i]) {
let back = target[i] as {{.item.primitiveType}};
if (back != source[i]) {
console.println("Data mismatch for {{.item.objectType}} " + " expected: " + source[i] + " at " + i + " but actual: " + back);
acc++;
return fail;
}
if (target[i as int] != target[i as number]) {
console.println("Data mismatch for $_get(int) and $_get(number): " + target[i as int] + " vs " + target[i as number]);
return fail;
}
}
return acc;
// NOTE: comparison of target that filled by set(int, {{.item.primitiveType}}) vs target2.$_set(number, {{.item.primitiveType}})
let target2: {{.item.objectType}};
try {
target2 = new {{.item.objectType}}(new ArrayBuffer(source.length as int * {{.item.primitiveSizeBytes}}));
} catch(e) {
return fail;
}
for (let i: int = 0; i < source.length; i++) {
try {
target2[i as number] = source[i] as {{.item.primitiveType}};
} catch(e) {
console.print("Catched exception at insert via $_set(number, {{.item.primitiveType}})");
return fail;
}
if (target[i] != target2[i]) {
console.print("Data mismatch for $_get(int) vs $_get(int) filled by set(int, {{.item.primitiveType}}) vs $_set(number, {{.item.primitiveType}})");
return fail;
}
if (target[i as number] != target2[i as number]) {
console.print("Data mismatch for $_get(number) vs $_get(number) filled by set(int, {{.item.primitiveType}}) vs $_set(number, {{.item.primitiveType}})");
return fail;
}
}
return success;
}
function createArrayFromArray(): int {
@ -254,16 +279,15 @@ function createArrayFromArray(): int {
try {
target = new {{.item.objectType}}(ss);
} catch(e) {
return 1;
return fail;
}
try {
target.set(source);
} catch(e) {
return 1;
return fail;
}
//console.println("Validate....");
let acc: int = 0;
let sumExp: {{.item.primitiveType}} = 0;
@ -283,14 +307,14 @@ function createArrayFromArray(): int {
acc++;
}
for(let i:int = 0; i < source.length as int; i++) {
let back = target[i] as {{.item.primitiveType}};
for (let i: int = 0; i < source.length as int; i++) {
{%- if skipInfinity %}
if ((source[i] == {{.posInf}}) && (source[i] == {{.negInf}})) {
continue;
}
{%- endif %}
if(back != source[i]) {
let back = target[i] as {{.item.primitiveType}};
if (back != source[i]) {
console.println("Data mismatch for {{.item.objectType}} " + " expected: " + source[i] + " at " + i + " but actual: " + back);
acc++;
}
@ -307,27 +331,25 @@ function createArrayFromArrayWithOffset(): int {
try {
target = new {{.item.objectType}}(ss);
} catch(e) {
return 1;
return fail;
}
try {
target.set(source, offset);
} catch(e) {
console.println("can't set")
return 1;
return fail;
}
//console.println("Validate....");
let acc: int = 0;
for(let i:int = offset; i < source.length as int; i++) {
let back = target[i] as {{.item.primitiveType}};
for (let i: int = offset; i < source.length as int; i++) {
{%- if skipInfinity %}
if ((source[i-offset] == {{.posInf}}) && (source[i-offset] == {{.negInf}})) {
continue;
}
{%- endif %}
if(back != source[i-offset]) {
let back = target[i] as {{.item.primitiveType}};
if (back != source[i-offset]) {
console.println("Data mismatch for {{.item.objectType}} " + " expected: " + source[i] + " at " + i + " but actual: " + back);
acc++;
}
@ -342,14 +364,14 @@ function createFilteredArrayFromGiven(): int {
origin = new {{.item.objectType}}(ss);
origin.set(source);
} catch(e) {
return 1;
return fail;
}
let target: {{.item.objectType}};
try {
target = origin.filter((val: {{.item.type}}, index: number): boolean => { return (val{{.item.cast2primitive}}) > 0});
} catch(e) {
return 1;
return fail;
}
let targetIdx = 0
@ -359,19 +381,19 @@ function createFilteredArrayFromGiven(): int {
}
if (targetIdx >= target.length as int) {
console.println("filter: buffer overflow")
return 1
return fail
}
if (target.at(targetIdx) != origin.at(originIdx)) {
console.println("wrong filtered item " + target.at(targetIdx))
return 1
return fail
}
targetIdx++
}
if (targetIdx != target.length as int) {
console.println("new element " + target.at(targetIdx))
return 1
return fail
}
return 0;
return success;
}
@ -383,15 +405,13 @@ function testLastIndexOfNotFound(): int {
target = new {{.item.objectType}}(ss);
target.set(source);
} catch(e) {
return 1;
return fail;
}
let nf: int = target.lastIndexOf(11 as {{.item.primitiveType}}) as int;
if(nf == -1) return 0;
return 1;
if (nf == -1) { return success; }
return fail;
}
function testLastIndexOf4(): int {
@ -406,13 +426,13 @@ function testLastIndexOf4(): int {
target.set(source);
} catch(e) {
console.println(e);
return 1;
return fail;
}
let result: int = target.lastIndexOf(50 as {{.item.primitiveType}}, 5);
if(result == 4) return 0;
if (result == 4) { return success; }
return 1;
return fail;
}
function testLastIndexOf3(): int {
@ -427,13 +447,13 @@ function testLastIndexOf3(): int {
target.set(source);
} catch(e) {
console.println(e);
return 1;
return fail;
}
let result: int = target.lastIndexOf(50 as {{.item.primitiveType}}, 3);
if(result == 3) return 0;
return 1;
if (result == 3) { return success; }
return fail;
}
@ -450,7 +470,7 @@ function testMap(): int {
origin.set(source);
} catch(e) {
console.println(e);
return 1;
return fail;
}
@ -460,27 +480,27 @@ function testMap(): int {
target = origin.map((val: {{.item.type}}, index: number): {{.item.type}} => { return {{.item.create}}(-val{{.item.cast2primitive}}) });
} catch(e) {
console.println(e);
return 1;
return fail;
}
if(target.length as int != source.length as int) {
if (target.length as int != source.length as int) {
console.println("Array length mismatch");
return 1;
return fail;
}
for(let i:int = 0; i< source.length as int; i++) {
for (let i: int = 0; i < source.length as int; i++) {
{%- if skipInfinity %}
if ((source[i] == {{.posInf}}) && (source[i] == {{.negInf}})) {
continue;
}
{%- endif %}
if(target[i] as {{.item.primitiveType}} != -source[i]) {
if (target[i] as {{.item.primitiveType}} != -source[i]) {
console.println("Array data mismatch");
return 1;
return fail;
}
}
return 0;
return success;
}
@ -496,7 +516,7 @@ function testSlice0(): int {
origin.set(source);
} catch(e) {
console.println(e);
return 1;
return fail;
}
@ -506,25 +526,25 @@ function testSlice0(): int {
target = origin.slice();
} catch(e) {
console.println(e);
return 1;
return fail;
}
if(target.length as int != origin.length as int) {
if (target.length as int != origin.length as int) {
console.println("Array length mismatch on slice");
return 1;
return fail;
}
//Check all the data copied;
for(let i:int = 0; i< origin.length as int; i++) {
for (let i: int = 0; i < origin.length as int; i++) {
let tv = target[i] as {{.item.primitiveType}};
let ov = origin[i] as {{.item.primitiveType}};
console.println(source[i] + "->" + tv + "->" + ov);
if(tv != ov) {
if (tv != ov) {
console.println("Array data mismatch");
return 1;
return fail;
}
}
return 0;
return success;
}
function testSlice1(): int {
@ -539,7 +559,7 @@ function testSlice1(): int {
origin.set(source);
} catch(e) {
console.println(e);
return 1;
return fail;
}
let sliceStart: int = 1;
@ -551,25 +571,25 @@ function testSlice1(): int {
target = origin.slice(sliceStart);
} catch(e) {
console.println(e);
return 1;
return fail;
}
if(target.length as int != origin.length as int - sliceStart) {
if (target.length as int != origin.length as int - sliceStart) {
console.println("Array length mismatch on slice1");
return 1;
return fail;
}
//Check all the data copied;
for(let i:int = sliceStart; i < sliceEnd; i++) {
for (let i: int = sliceStart; i < sliceEnd; i++) {
let tv = target[i - sliceStart] as {{.item.primitiveType}};
let ov = origin[i] as {{.item.primitiveType}};
console.println(source[i] + "->" + tv + "->" + ov);
if(tv != ov) {
if (tv != ov) {
console.println("Array data mismatch");
return 1;
return fail;
}
}
return 0;
return success;
}
function testSlice2(): int {
@ -584,7 +604,7 @@ function testSlice2(): int {
origin.set(source);
} catch(e) {
console.println(e);
return 1;
return fail;
}
let sliceStart: int = 1;
@ -596,30 +616,29 @@ function testSlice2(): int {
target = origin.slice(sliceStart, sliceEnd);
} catch(e) {
console.println(e);
return 1;
return fail;
}
if(target.length as int != sliceEnd - sliceStart) {
if (target.length as int != sliceEnd - sliceStart) {
console.println("Array length mismatch on slice2");
return 1;
return fail;
}
//Check all the data copied;
for(let i:int = sliceStart; i< sliceEnd; i++) {
for (let i: int = sliceStart; i < sliceEnd; i++) {
let tv = target[i - sliceStart] as {{.item.primitiveType}};
let ov = origin[i] as {{.item.primitiveType}};
console.println(source[i] + "->" + tv + "->" + ov);
if(tv != ov) {
if (tv != ov) {
console.println("Array data mismatch");
return 1;
return fail;
}
}
return 0;
return success;
}
function createFromSource(source: {{.item.primitiveType}}[]): {{.item.objectType}} throws {
let ss = new ArrayBuffer(source.length as int * {{.item.primitiveSizeBytes}});
let origin: {{.item.objectType}};
try {
@ -629,36 +648,32 @@ function createFromSource(source: {{.item.primitiveType}}[]): {{.item.objectType
console.println(e);
throw new Exception("Unable to create typed array");
}
return origin;
}
function dump(source: {{.item.primitiveType}}[], origin: {{.item.objectType}}): void {
console.print("Expected: ");
for(let i: int = 0; i< source.length as int; i++) console.print(source[i] + " ");
for (let i: int = 0; i < source.length as int; i++) { console.print(source[i] + " "); }
console.println();
console.print("Passed: ");
for(let i: int = 0; i< origin.length as int; i++) console.print(origin[i] as {{.item.primitiveType}} + " ");
for (let i: int = 0; i < origin.length as int; i++) { console.print(origin[i] as {{.item.primitiveType}} + " "); }
console.println();
}
function assertArrayEqualsToTypedArray(source: {{.item.primitiveType}}[], origin: {{.item.objectType}}): int {
if(origin.length as int != source.length as int) {
return 1;
if (origin.length as int != source.length as int) {
return fail;
}
for(let i: int; i< origin.length; i++) {
for (let i: int; i < origin.length; i++) {
let tv: {{.item.primitiveType}} = origin[i] as {{.item.primitiveType}};
let sv: {{.item.primitiveType}} = source[i] as {{.item.primitiveType}};
if(tv != sv) {
if (tv != sv) {
console.println("Unexpected data changed at [" + i + "] " + sv + "->" + tv);
return 1;
return fail;
}
}
return 0;
return success;
}
const src: {{.item.primitiveType}}[] = [1 as {{.item.primitiveType}},
@ -670,14 +685,12 @@ const src: {{.item.primitiveType}}[] = [1 as {{.item.primitiveType}},
{%+ for ci in copyWithin %}
function {{.ci.name}}(): int {
let origin: {{.item.objectType}};
let expected: {{.item.primitiveType}}[] = [{%+ for ea in ci.expected %}{{.ea}} as {{.item.primitiveType}}, {% endfor %}];
try {
origin = createFromSource(src);
} catch(e) {
console.println(e);
return 1;
return fail;
}
origin.copyWithin({{.ci.params|join(', ')}});
return assertArrayEqualsToTypedArray(expected, origin);