import { describe, test, expect, Result } from './tests.uts' export class TUint16Array { test() { // #ifdef (UNI-APP-X && APP-ANDROID) || WEB this.testuint16(); 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 } // #ifdef (UNI-APP-X && APP-ANDROID) || WEB testuint16() { let uint16 = new Uint16Array(2); uint16[0] = 42; expect(uint16[0]).toEqual(42); expect(uint16.length).toEqual(2); expect(Uint16Array.BYTES_PER_ELEMENT).toEqual(4); let x = new Uint16Array([21, 31, 3]); expect(x[1]).toEqual(31); let y = new Uint16Array(x); expect(y[0]).toEqual(21); let buffer = new ArrayBuffer(16); let z = new Uint16Array(buffer, 2, 4); expect(z.byteOffset).toEqual(2); expect(z.length).toEqual(4); } testConstructor() { let buffer = new ArrayBuffer(16); let uint16 = new Uint16Array(buffer); uint16[1] = 42; expect(uint16.toString()).toEqual("0,42,0,0,0,0,0,0"); } testSet() { // #TEST Uint16Array.set let uint16 = new Uint16Array(8); var array = [1, 2, 3] uint16.set(array, 1); expect(uint16.toString()).toEqual("0,1,2,3,0,0,0,0"); // #END } testCopyWith() { // #TEST Uint16Array.copyWith console.log("testCopyWith 1") let uint16 = new Uint16Array(8); uint16.set([1, 2, 3], 1); console.log("testCopyWith 2") uint16.copyWithin(3, 0, 3); console.log("testCopyWith 3") expect(uint16.toString()).toEqual("0,1,2,0,1,2,0,0"); // #END } testEvery() { // #TEST Uint16Array.every // const isBelowThreshold = (currentValue: number, index: number, array: Uint16Array): boolean => currentValue < 40; let result = new Uint16Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Uint16Array) : boolean => value < 40); expect(result).toEqual(false); // #END } testFill() { // #TEST Uint16Array.fill let uint16 = new Uint16Array([1, 2, 3]).fill(4); expect(uint16.toString()).toEqual("4,4,4"); uint16 = new Uint16Array([1, 2, 3]).fill(4, 1); expect(uint16.toString()).toEqual("1,4,4"); uint16 = new Uint16Array([1, 2, 3]).fill(4, 1, 2); expect(uint16.toString()).toEqual("1,4,3"); uint16 = new Uint16Array([1, 2, 3]).fill(4, 1, 1); expect(uint16.toString()).toEqual("1,2,3"); uint16 = new Uint16Array([1, 2, 3]).fill(4, -3, -2); expect(uint16.toString()).toEqual("4,2,3"); // #END } testFilter() { // #TEST Uint16Array.filter // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10; let uint16 = new Uint16Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Uint16Array) : boolean => value >= 10); expect(uint16.toString()).toEqual("12,44"); // #END } find() { // #TEST Uint16Array.find let uint16 = new Uint16Array([4, 5, 8, 12]); let res = uint16.find((value : number, _ : number, _a : Uint16Array) : boolean => value > 5); expect(res).toEqual(8); // #END } findIndex() { // #TEST Uint16Array.findIndex let uint16 = new Uint16Array([4, 6, 8, 12]); let res = uint16.findIndex((value : number, _ : number, _a : Uint16Array) : boolean => value > 100); expect(res).toEqual(-1); let uuint16 = new Uint16Array([4, 6, 7, 120]); res = uuint16.findIndex((value : number, _ : number, _a : Uint16Array) : boolean => value > 100); expect(res).toEqual(3); // #END } foreach() { // #TEST Uint16Array.forEach new Uint16Array([0, 1, 2, 3]).forEach((value : number, index : number, _a : Uint16Array) => { console.log(`a[${index}] = ${value}`); }); // #END } iterator() { // #TEST Uint16Array.entries let arr = new Uint16Array([10, 20, 30, 40, 50]); let entries = arr.entries(); expect(entries.next().value[1]).toEqual(10); // #END } includes() { // #TEST Uint16Array.includes let uint16 = new Uint16Array([1, 2, 3]); let res = uint16.includes(2); expect(res).toEqual(true); res = uint16.includes(4); expect(res).toEqual(false); res = uint16.includes(3, 3); expect(res).toEqual(false); // #END } indexOf() { // #TEST Uint16Array.indexOf let uint16 = new Uint16Array([2, 5, 9]); let res = uint16.indexOf(2); expect(res).toEqual(0); res = uint16.indexOf(7); expect(res).toEqual(-1); res = uint16.indexOf(9, 2); expect(res).toEqual(2); res = uint16.indexOf(2, -1); expect(res).toEqual(-1); res = uint16.indexOf(2, -3); expect(res).toEqual(0); // #END } join() { // #TEST Uint16Array.join let uint16 = new Uint16Array([1, 2, 3]); let res = uint16.join(); expect(res).toEqual("1,2,3"); res = uint16.join(" / "); expect(res).toEqual("1 / 2 / 3"); res = uint16.join(""); expect(res).toEqual("123"); // #END } keys() { // #TEST Uint16Array.keys let arr = new Uint16Array([10, 20, 30, 40, 50]); let keys = arr.keys(); expect(keys.next().value).toEqual(0); // #END } map() { // #TEST Uint16Array.map let numbers = new Uint16Array([1, 4, 9]); let doubles = numbers.map((value : number, _ : number, _a : Uint16Array) : number => value * 2); expect(numbers.toString()).toEqual("1,4,9"); expect(doubles.toString()).toEqual("2,8,18"); // #END } reduce() { // #TEST Uint16Array.reduce let total = new Uint16Array([0, 1, 2, 3]); let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number => accumulator + currentValue); expect(res).toEqual(6); total = new Uint16Array([0, 1, 2, 3]); res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number => accumulator + currentValue, 8); expect(res).toEqual(14); // #END } reduceRight() { // #TEST Uint16Array.reduceRight let total = new Uint16Array([0, 1, 2, 3]); let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number => accumulator + currentValue); expect(res).toEqual(6); total = new Uint16Array([0, 1, 2, 3]); res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number => accumulator + currentValue, 8); expect(res).toEqual(14); // #END } reverse() { // #TEST Uint16Array.reverse let uint16 = new Uint16Array([1, 2, 3]); uint16.reverse(); expect(uint16.toString()).toEqual("3,2,1"); // #END } slice() { // #TEST Uint16Array.slice let uint16 = new Uint16Array([1, 2, 3]); let res = uint16.slice(1); expect(res.toString()).toEqual("2,3"); res = uint16.slice(2); expect(res.toString()).toEqual("3"); res = uint16.slice(-2); expect(res.toString()).toEqual("2,3"); res = uint16.slice(0, 1); expect(res.toString()).toEqual("1"); // #END } sort() { // #TEST Uint16Array.sort let numbers = new Uint16Array([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 Uint16Array.subarray let buffer = new ArrayBuffer(16); let uint16 = new Uint16Array(buffer); uint16.set([1, 2, 3]); expect(uint16.toString()).toEqual("1,2,3,0,0,0,0,0"); let sub = uint16.subarray(0, 4); expect(sub.toString()).toEqual("1,2,3,0"); // #END } values() { // #TEST Uint16Array.values let arr = new Uint16Array([1, 2, 3]); let values = arr.values(); expect(values.next().value).toEqual(1); // #END } arrayBufferSlice() { // #TEST Uint16Array.arrayBufferSlice let buffer = new ArrayBuffer(16); let uint16 = new Uint16Array(buffer); uint16[4] = 42; expect(uint16.toString()).toEqual("0,0,0,0,42,0,0,0"); let res = buffer.slice(8); let sliced = new Uint16Array(res); expect(sliced[0]).toEqual(42); // #END } testSome() { // #TEST Uint16Array.some const uint16 = new Uint16Array([8, 20, 30, 40, 50]); const positives = new Uint16Array([10, 20, 30, 40, 50]); expect(uint16.some((element : number, index : number, array : Uint16Array) : boolean => element < 10 )).toEqual(true); expect(positives.some((element : number, index : number, array : Uint16Array) : boolean => element < 0 )).toEqual(false); // #END } // #endif }