import { describe, test, expect, Result } from './tests.uts' export class TFloat64Array { test() { // #ifndef APP-IOS this.testfloat64(); this.testConstructor(); this.testSet(); this.testCopyWith(); this.testEvery(); this.testFill(); this.testFilter(); this.find(); this.findIndex(); this.foreach(); this.iterator(); this.includes(); this.indexOf(); this.join(); this.keys(); this.map(); this.reduce(); this.reduceRight(); this.reverse(); this.slice(); this.sort(); this.subarray(); this.values(); this.arrayBufferSlice(); // #endif } // #ifndef APP-IOS testfloat64() { let float64 = new Float64Array(2); float64[0] = 42; expect(float64[0]).toEqual(42); expect(float64.length).toEqual(2); expect(Float64Array.BYTES_PER_ELEMENT).toEqual(8); let x = new Float64Array([21, 31, 3]); expect(x[1]).toEqual(31); let y = new Float64Array(x); expect(y[0]).toEqual(21); let buffer = new ArrayBuffer(16); let z = new Float64Array(buffer, 2, 4); expect(z.byteOffset).toEqual(2); expect(z.length).toEqual(4); } testConstructor() { let buffer = new ArrayBuffer(16); let float64 = new Float64Array(buffer); float64[1] = 42; expect(float64.toString()).toEqual("0,42"); } testSet() { // #TEST Float64Array.set let float64 = new Float64Array(8); var array = [1, 2, 3]; float64.set(array, 1); expect(float64.toString()).toEqual("0,1,2,3,0,0,0,0"); // #END } testCopyWith() { // #TEST Float64Array.copyWithin let float64 = new Float64Array(8); float64.set([1, 2, 3], 1); float64.copyWithin(3, 0, 3); expect(float64.toString()).toEqual("0,1,2,0,1,2,0,0"); let initialLargeArray : Array = new Array(10000, 0); for (let i = 0; i < 5000; ++i) { initialLargeArray[i] = i; } let initialLargeFloat64Array = new Float64Array(initialLargeArray); let largeFloat64Array : Float64Array = new Float64Array(initialLargeFloat64Array); largeFloat64Array.copyWithin(5000, 0); for (let i = 0; i < 5000; ++i) { if (largeFloat64Array[i + 5000] !== i) { expect(true).toEqual(false); } } // #END } testEvery() { // #TEST Float64Array.every let result = new Float64Array([-10, -20, -30, -40, -50]).every((value : number, _ : number, _a : Float64Array) : boolean => value < 0); expect(result).toEqual(true); // #END } testFill() { // #TEST Float64Array.fill let float64 = new Float64Array([1, 2, 3]).fill(4); expect(float64.toString()).toEqual("4,4,4"); float64 = new Float64Array([1, 2, 3]).fill(4, 1); expect(float64.toString()).toEqual("1,4,4"); float64 = new Float64Array([1, 2, 3]).fill(4, 1, 2); expect(float64.toString()).toEqual("1,4,3"); float64 = new Float64Array([1, 2, 3]).fill(4, 1, 1); expect(float64.toString()).toEqual("1,2,3"); float64 = new Float64Array([1, 2, 3]).fill(4, -3, -2); expect(float64.toString()).toEqual("4,2,3"); // #END } testFilter() { // #TEST Float64Array.filter let float64 = new Float64Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Float64Array) : boolean => value >= 10); expect(float64.toString()).toEqual("12,44"); // #END } find() { // #TEST Float64Array.find let float64 = new Float64Array([4, 5, 8, 12]); let res = float64.find((value : number, _ : number, _a : Float64Array) : boolean => value > 5); expect(res).toEqual(8); // #END } findIndex() { // #TEST Float64Array.findIndex let float64 = new Float64Array([4, 6, 8, 12]); let res = float64.findIndex((value : number, _ : number, _a : Float64Array) : boolean => value > 100); expect(res).toEqual(-1); let ufloat64 = new Float64Array([4, 6, 7, 120]); res = ufloat64.findIndex((value : number, _ : number, _a : Float64Array) : boolean => value > 100); expect(res).toEqual(3); // #END } foreach() { // #TEST Float64Array.forEach new Float64Array([0, 1, 2, 3]).forEach((value : number, index : number, _ : Float64Array) => { console.log(`a[${index}] = ${value}`); }); // #END } iterator() { // #TEST Float64Array.entries let arr = new Float64Array([10, 20, 30, 40, 50]); let entries = arr.entries(); expect(entries.next().value[1]).toEqual(10); expect(entries.next().value[1]).toEqual(20); // #END } includes() { // #TEST Float64Array.includes let float64 = new Float64Array([1, 2, 3]); let res = float64.includes(2); expect(res).toEqual(true); res = float64.includes(4); expect(res).toEqual(false); res = float64.includes(3, 3); expect(res).toEqual(false); // #END } indexOf() { // #TEST Float64Array.indexOf let float64 = new Float64Array([2, 5, 9]); let res = float64.indexOf(2); expect(res).toEqual(0); res = float64.indexOf(7); expect(res).toEqual(-1); res = float64.indexOf(9, 2); expect(res).toEqual(2); res = float64.indexOf(2, -1); expect(res).toEqual(-1); res = float64.indexOf(2, -3); expect(res).toEqual(0); // #END } join() { // #TEST Float64Array.join let float64 = new Float64Array([1, 2, 3]); let res = float64.join(); expect(res).toEqual("1,2,3"); res = float64.join(" / "); expect(res).toEqual("1 / 2 / 3"); res = float64.join(""); expect(res).toEqual("123"); // #END } keys() { // #TEST Float64Array.keys let arr = new Float64Array([10, 20, 30, 40, 50]); let keys = arr.keys(); expect(keys.next().value).toEqual(0); expect(keys.next().value).toEqual(1); expect(keys.next().value).toEqual(2); expect(keys.next().value).toEqual(3); expect(keys.next().value).toEqual(4); // #END } map() { // #TEST Float64Array.map let numbers = new Float64Array([1, 4, 9]); let doubles = numbers.map((value : number, _ : number, _a : Float64Array) : number => value * 2); expect(numbers.toString()).toEqual("1,4,9"); expect(doubles.toString()).toEqual("2,8,18"); // #END } reduce() { // #TEST Float64Array.reduce let total = new Float64Array([0, 1, 2, 3]); let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue); expect(res).toEqual(6); total = new Float64Array([0, 1, 2, 3]); res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue, 8); expect(res).toEqual(14); // #END } reduceRight() { // #TEST Float64Array.reduceRight let total = new Float64Array([0, 1, 2, 3]); let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue); expect(res).toEqual(6); total = new Float64Array([0, 1, 2, 3]); res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue, 8); expect(res).toEqual(14); // #END } reverse() { // #TEST Float64Array.reverse let float64 = new Float64Array([1, 2, 3]); float64.reverse(); expect(float64.toString()).toEqual("3,2,1"); // #END } slice() { // #TEST Float64Array.slice let float64 = new Float64Array([1, 2, 3]); let res = float64.slice(1); expect(res.toString()).toEqual("2,3"); res = float64.slice(2); expect(res.toString()).toEqual("3"); res = float64.slice(-2); expect(res.toString()).toEqual("2,3"); res = float64.slice(0, 1); expect(res.toString()).toEqual("1"); const size = 1000; const initialFloat64Array = new Float64Array(size); for (let i = 0; i < size; ++i) { initialFloat64Array[i] = Math.random(); } let arr : Float64Array; let new_arr : Float64Array; arr = new Float64Array(initialFloat64Array); new_arr = arr.slice(1, -1); for (let i = 1; i < size - 1; ++i) { if (arr[i] !== new_arr[i - 1]) { expect(true).toEqual(false); break } } // #END } sort() { // #TEST Float64Array.sort let numbers = new Float64Array([40, 1, 5]); numbers.sort(); expect(numbers.toString()).toEqual("1,5,40"); numbers.sort((a, b) : number => a - b); expect(numbers.toString()).toEqual("1,5,40"); // #END } subarray() { // #TEST Float64Array.subarray let buffer = new ArrayBuffer(32); let float64 = new Float64Array(buffer); float64.set([1, 2, 3]); expect(float64.toString()).toEqual("1,2,3,0"); let sub = float64.subarray(0, 4); expect(sub.toString()).toEqual("1,2,3,0"); const size = 1000; const initialFloat64Array = new Float64Array(size); for (let i = 0; i < size; ++i) { initialFloat64Array[i] = Math.random(); } let arr = new Float64Array(initialFloat64Array); let new_arr = arr.subarray(1, size - 1); for (let i = 1; i < size - 1; ++i) { if (arr[i] !== new_arr[i - 1]) { expect(true).toEqual(false); } } // #END } values() { // #TEST Float64Array.values let arr = new Float64Array([1, 2, 3]); let values = arr.values(); expect(values.next().value).toEqual(1); expect(values.next().value).toEqual(2); expect(values.next().value).toEqual(3); // #END } arrayBufferSlice() { // #TEST ArrayBuffer.slice let buffer = new ArrayBuffer(16); let float64 = new Float64Array(buffer); float64[1] = 42; expect(float64.toString()).toEqual("0,42"); let res = buffer.slice(8); let sliced = new Float64Array(res); expect(sliced[0]).toEqual(42); // #END } testSome() { // #TEST Float64Array.some const float64 = new Float64Array([-10, 20, -30, 40, -50]); const positives = new Float64Array([10, 20, 30, 40, 50]); expect(float64.some((element : number, index : number, array : Float64Array) : boolean => element < 0 )).toEqual(true); expect(positives.some((element : number, index : number, array : Float64Array) : boolean => element < 0 )).toEqual(false); // #END } // #endif }