diff --git a/changelog.md b/changelog.md index 6ef841ffdedc0d648fe1ab7f2b1c434c067f97e0..06890faa7409a516e4ee6c3052a667fd01317245 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,15 @@ +## 1.0.17 +* update 4.22.2024062415-alpha + +## 1.0.16 +* update 4.21.2024061818-alpha + +## 1.0.15 +* update 4.19.2024060704-alpha + +## 1.0.14 +* update 4.18.2024060311-alpha + ## 1.0.13 * update 4.17.2024051110-alpha diff --git a/package.json b/package.json index 838d493574edf02199686f035257daa84bee64c9..a475669133718f2f4b94345d70fb3f5368c6da71 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "id": "hello-uts-alpha", "name": "hello-uts-alpha", "displayName": "hello-uts-alpha", - "version": "1.0.13", + "version": "1.0.18", "description": "UTS插件示例项目", "repository": "https://gitcode.net/dcloud/hello-uts", "keywords": [ diff --git a/uni_modules/uts-tests/utssdk/ArrayBuffer.uts b/uni_modules/uts-tests/utssdk/ArrayBuffer.uts new file mode 100644 index 0000000000000000000000000000000000000000..d44d51eadda2acaf3535762e36e535cb23e53a75 --- /dev/null +++ b/uni_modules/uts-tests/utssdk/ArrayBuffer.uts @@ -0,0 +1,725 @@ +import { describe, test, expect, Result } from './tests.uts' +import { TDataView } from './TDataView.js' +import { TFloat32Array } from './TFloat32Array.js' +import { TFloat64Array } from './TFloat64Array.js' +import { TInt8Array } from './TInt8Array.js' +import { TInt16Array } from './TInt16Array.js' +import { TInt32Array } from './TInt32Array.js' +import { TUint8Array } from './TUint8Array.js' +import { TUint8ClampedArray } from './TUint8ClampedArray.js' +import { TUint16Array } from './TUint16Array.js' +import { TUint32Array } from './TUint32Array.js' + +const tDataView = new TDataView(); +const float32 = new TFloat32Array(); +const float64 = new TFloat64Array() +const int8 = new TInt8Array() +const int32 = new TInt32Array() +const uint8 = new TUint8Array() +const uint8Clamped = new TUint8ClampedArray() +const uint16 = new TUint16Array() +const uint32 = new TUint32Array() +const int16 = new TInt16Array() + +export function testArrayBuffer() : Result { + return describe("ArrayBuffer", () => { + //dataview start + test('dataview_constructor', () => { + tDataView.setConstructor() + }) + test('dataview_setInt8', () => { + tDataView.setInt8() + }) + test('dataview_setFloat32', () => { + tDataView.setFloat32() + }) + test('dataview_setFloat64', () => { + tDataView.setFloat64() + }) + test('dataview_setInt16', () => { + tDataView.setInt16() + }) + test('dataview_setInt32', () => { + tDataView.setInt32() + }) + test('dataview_setUint16', () => { + tDataView.setUint16() + }) + test('dataview_setUint32', () => { + tDataView.setUint32() + }) + test('dataview_setUint8', () => { + tDataView.setUint8() + }) + test('doTestDataViewBigEndian', () => { + tDataView.doTestDataViewBigEndian() + }) + test('doTestDataViewLittleEndian', () => { + tDataView.doTestDataViewLittleEndian() + }) + test('doTestTypedArrayBigEndian', () => { + tDataView.doTestTypedArrayBigEndian() + }) + test('doTestTypedArrayLittleEndian', () => { + tDataView.doTestTypedArrayLittleEndian() + }) + test('doTestDataViewFloats', () => { + tDataView.doTestDataViewFloats() + }) + //dataview end + + //float32array start + test('float32_testConstructor', () => { + float32.testConstructor() + }) + test('float32_testSet', () => { + float32.testSet() + }) + test('float32_testCopyWith', () => { + float32.testCopyWith() + }) + test('float32_testEvery', () => { + float32.testEvery() + }) + test('float32_testFill', () => { + float32.testFill() + }) + test('float32_testFilter', () => { + float32.testFilter() + }) + test('float32_find', () => { + float32.find() + }) + test('float32_findIndex', () => { + float32.findIndex() + }) + test('float32_foreach', () => { + float32.foreach() + }) + test('float32_iterator', () => { + float32.iterator() + }) + test('float32_includes', () => { + float32.includes() + }) + test('float32_indexOf', () => { + float32.indexOf() + }) + test('float32_join', () => { + float32.join() + }) + test('float32_keys', () => { + float32.keys() + }) + test('float32_map', () => { + float32.map() + }) + test('float32_reduce', () => { + float32.reduce() + }) + test('float32_reduceRight', () => { + float32.reduceRight() + }) + test('float32_reverse', () => { + float32.reverse() + }) + test('float32_slice', () => { + float32.slice() + }) + test('float32_sort', () => { + float32.sort() + }) + test('float32_subarray', () => { + float32.subarray() + }) + test('float32_values', () => { + float32.values() + }) + test('float32_arrayBufferSlice', () => { + float32.arrayBufferSlice() + }) + //float32array end + //float64array start + test('float64_testConstructor', () => { + float64.testConstructor() + }) + test('float64_testSet', () => { + float64.testSet() + }) + test('float64_testCopyWith', () => { + float64.testCopyWith() + }) + test('float64_testEvery', () => { + float64.testEvery() + }) + test('float64_testFill', () => { + float64.testFill() + }) + test('float64_testFilter', () => { + float64.testFilter() + }) + test('float64_find', () => { + float64.find() + }) + test('float64_findIndex', () => { + float64.findIndex() + }) + test('float64_foreach', () => { + float64.foreach() + }) + test('float64_iterator', () => { + float64.iterator() + }) + test('float64_includes', () => { + float64.includes() + }) + test('float64_indexOf', () => { + float64.indexOf() + }) + test('float64_join', () => { + float64.join() + }) + test('float64_keys', () => { + float64.keys() + }) + test('float64_map', () => { + float64.map() + }) + test('float64_reduce', () => { + float64.reduce() + }) + test('float64_reduceRight', () => { + float64.reduceRight() + }) + test('float64_reverse', () => { + float64.reverse() + }) + test('float64_slice', () => { + float64.slice() + }) + test('float64_sort', () => { + float64.sort() + }) + test('float64_subarray', () => { + float64.subarray() + }) + test('float64_values', () => { + float64.values() + }) + test('float64_arrayBufferSlice', () => { + float64.arrayBufferSlice() + }) + + //float64 end + //int8 start + test('int8_testConstructor', () => { + int8.testConstructor() + }) + test('int8_testSet', () => { + int8.testSet() + }) + test('int8_testCopyWith', () => { + int8.testCopyWith() + }) + test('int8_testEvery', () => { + int8.testEvery() + }) + test('int8_testFill', () => { + int8.testFill() + }) + test('int8_testFilter', () => { + int8.testFilter() + }) + test('int8_find', () => { + int8.find() + }) + test('int8_findIndex', () => { + int8.findIndex() + }) + test('int8_foreach', () => { + int8.foreach() + }) + test('int8_iterator', () => { + int8.iterator() + }) + test('int8_includes', () => { + int8.includes() + }) + test('int8_indexOf', () => { + int8.indexOf() + }) + test('int8_join', () => { + int8.join() + }) + test('int8_keys', () => { + int8.keys() + }) + test('int8_map', () => { + int8.map() + }) + test('int8_reduce', () => { + int8.reduce() + }) + test('int8_reduceRight', () => { + int8.reduceRight() + }) + test('int8_reverse', () => { + int8.reverse() + }) + test('int8_slice', () => { + int8.slice() + }) + test('int8_sort', () => { + int8.sort() + }) + test('int8_subarray', () => { + int8.subarray() + }) + test('int8_values', () => { + int8.values() + }) + test('int8_arrayBufferSlice', () => { + int8.arrayBufferSlice() + }) + + // //int8 end + + // //int16 start + test('int16_testConstructor', () => { + int16.testConstructor() + }) + test('int16_testSet', () => { + int16.testSet() + }) + test('int16_testCopyWith', () => { + int16.testCopyWith() + }) + test('int16_testEvery', () => { + int16.testEvery() + }) + test('int16_testFill', () => { + int16.testFill() + }) + test('int16_testFilter', () => { + int16.testFilter() + }) + test('int16_find', () => { + int16.find() + }) + test('int16_findIndex', () => { + int16.findIndex() + }) + test('int16_foreach', () => { + int16.foreach() + }) + test('int16_iterator', () => { + int16.iterator() + }) + test('int16_includes', () => { + int16.includes() + }) + test('int16_indexOf', () => { + int16.indexOf() + }) + test('int16_join', () => { + int16.join() + }) + test('int16_keys', () => { + int16.keys() + }) + test('int16_map', () => { + int16.map() + }) + test('int16_reduce', () => { + int16.reduce() + }) + test('int16_reduceRight', () => { + int16.reduceRight() + }) + test('int16_reverse', () => { + int16.reverse() + }) + test('int16_slice', () => { + int16.slice() + }) + test('int16_sort', () => { + int16.sort() + }) + test('int16_subarray', () => { + int16.subarray() + }) + test('int16_values', () => { + int16.values() + }) + test('int16_arrayBufferSlice', () => { + int16.arrayBufferSlice() + }) + + //int16 end + + //int32 start + test('int32_testConstructor', () => { + int32.testConstructor() + }) + test('int32_testSet', () => { + int32.testSet() + }) + test('int32_testCopyWith', () => { + int32.testCopyWith() + }) + test('int32_testEvery', () => { + int32.testEvery() + }) + test('int32_testFill', () => { + int32.testFill() + }) + test('int32_testFilter', () => { + int32.testFilter() + }) + test('int32_find', () => { + int32.find() + }) + test('int32_findIndex', () => { + int32.findIndex() + }) + test('int32_foreach', () => { + int32.foreach() + }) + test('int32_iterator', () => { + int32.iterator() + }) + test('int32_includes', () => { + int32.includes() + }) + test('int32_indexOf', () => { + int32.indexOf() + }) + test('int32_join', () => { + int32.join() + }) + test('int32_keys', () => { + int32.keys() + }) + test('int32_map', () => { + int32.map() + }) + test('int32_reduce', () => { + int32.reduce() + }) + test('int32_reduceRight', () => { + int32.reduceRight() + }) + test('int32_reverse', () => { + int32.reverse() + }) + test('int32_slice', () => { + int32.slice() + }) + test('int32_sort', () => { + int32.sort() + }) + test('int32_subarray', () => { + int32.subarray() + }) + test('int32_values', () => { + int32.values() + }) + test('int32_arrayBufferSlice', () => { + int32.arrayBufferSlice() + }) + + //int32 end + + //uint8 start + test('uint8_testConstructor', () => { + uint8.testConstructor() + }) + test('uint8_testSet', () => { + uint8.testSet() + }) + test('uint8_testCopyWith', () => { + uint8.testCopyWith() + }) + test('uint8_testEvery', () => { + uint8.testEvery() + }) + test('uint8_testFill', () => { + uint8.testFill() + }) + test('uint8_testFilter', () => { + uint8.testFilter() + }) + test('uint8_find', () => { + uint8.find() + }) + test('uint8_findIndex', () => { + uint8.findIndex() + }) + test('uint8_foreach', () => { + uint8.foreach() + }) + test('uint8_iterator', () => { + uint8.iterator() + }) + test('uint8_includes', () => { + uint8.includes() + }) + test('uint8_indexOf', () => { + uint8.indexOf() + }) + test('uint8_join', () => { + uint8.join() + }) + test('uint8_keys', () => { + uint8.keys() + }) + test('uint8_map', () => { + uint8.map() + }) + test('uint8_reduce', () => { + uint8.reduce() + }) + test('uint8_reduceRight', () => { + uint8.reduceRight() + }) + test('uint8_reverse', () => { + uint8.reverse() + }) + test('uint8_slice', () => { + uint8.slice() + }) + test('uint8_sort', () => { + uint8.sort() + }) + test('uint8_subarray', () => { + uint8.subarray() + }) + test('uint8_values', () => { + uint8.values() + }) + test('uint8_arrayBufferSlice', () => { + uint8.arrayBufferSlice() + }) + + //uint8 end + + //uint8Clamped start + test('uint8Clamped_testConstructor', () => { + uint8Clamped.testConstructor() + }) + test('uint8Clamped_testSet', () => { + uint8Clamped.testSet() + }) + test('uint8Clamped_testCopyWith', () => { + uint8Clamped.testCopyWith() + }) + test('uint8Clamped_testEvery', () => { + uint8Clamped.testEvery() + }) + test('uint8Clamped_testFill', () => { + uint8Clamped.testFill() + }) + test('uint8Clamped_testFilter', () => { + uint8Clamped.testFilter() + }) + test('uint8Clamped_find', () => { + uint8Clamped.find() + }) + test('uint8Clamped_findIndex', () => { + uint8Clamped.findIndex() + }) + test('uint8Clamped_foreach', () => { + uint8Clamped.foreach() + }) + test('uint8Clamped_iterator', () => { + uint8Clamped.iterator() + }) + test('uint8Clamped_includes', () => { + uint8Clamped.includes() + }) + test('uint8Clamped_indexOf', () => { + uint8Clamped.indexOf() + }) + test('uint8Clamped_join', () => { + uint8Clamped.join() + }) + test('uint8Clamped_keys', () => { + uint8Clamped.keys() + }) + test('uint8Clamped_map', () => { + uint8Clamped.map() + }) + test('uint8Clamped_reduce', () => { + uint8Clamped.reduce() + }) + test('uint8Clamped_reduceRight', () => { + uint8Clamped.reduceRight() + }) + test('uint8Clamped_reverse', () => { + uint8Clamped.reverse() + }) + test('uint8Clamped_slice', () => { + uint8Clamped.slice() + }) + test('uint8Clamped_sort', () => { + uint8Clamped.sort() + }) + test('uint8Clamped_subarray', () => { + uint8Clamped.subarray() + }) + test('uint8Clamped_values', () => { + uint8Clamped.values() + }) + test('uint8Clamped_arrayBufferSlice', () => { + uint8Clamped.arrayBufferSlice() + }) + + //uint8Clamped end + + //uint16 start + test('uint16_testConstructor', () => { + uint16.testConstructor() + }) + test('uint16_testSet', () => { + uint16.testSet() + }) + test('uint16_testCopyWith', () => { + uint16.testCopyWith() + }) + test('uint16_testEvery', () => { + uint16.testEvery() + }) + test('uint16_testFill', () => { + uint16.testFill() + }) + test('uint16_testFilter', () => { + uint16.testFilter() + }) + test('uint16_find', () => { + uint16.find() + }) + test('uint16_findIndex', () => { + uint16.findIndex() + }) + test('uint16_foreach', () => { + uint16.foreach() + }) + test('uint16_iterator', () => { + uint16.iterator() + }) + test('uint16_includes', () => { + uint16.includes() + }) + test('uint16_indexOf', () => { + uint16.indexOf() + }) + test('uint16_join', () => { + uint16.join() + }) + test('uint16_keys', () => { + uint16.keys() + }) + test('uint16_map', () => { + uint16.map() + }) + test('uint16_reduce', () => { + uint16.reduce() + }) + test('uint16_reduceRight', () => { + uint16.reduceRight() + }) + test('uint16_reverse', () => { + uint16.reverse() + }) + test('uint16_slice', () => { + uint16.slice() + }) + test('uint16_sort', () => { + uint16.sort() + }) + test('uint16_subarray', () => { + uint16.subarray() + }) + test('uint16_values', () => { + uint16.values() + }) + test('uint16_arrayBufferSlice', () => { + uint16.arrayBufferSlice() + }) + + //uint16 end + + //uint32 start + test('uint32_testConstructor', () => { + uint32.testConstructor() + }) + test('uint32_testSet', () => { + uint32.testSet() + }) + test('uint32_testCopyWith', () => { + uint32.testCopyWith() + }) + test('uint32_testEvery', () => { + uint32.testEvery() + }) + test('uint32_testFill', () => { + uint32.testFill() + }) + test('uint32_testFilter', () => { + uint32.testFilter() + }) + test('uint32_find', () => { + uint32.find() + }) + test('uint32_findIndex', () => { + uint32.findIndex() + }) + test('uint32_foreach', () => { + uint32.foreach() + }) + test('uint32_iterator', () => { + uint32.iterator() + }) + test('uint32_includes', () => { + uint32.includes() + }) + test('uint32_indexOf', () => { + uint32.indexOf() + }) + test('uint32_join', () => { + uint32.join() + }) + test('uint32_keys', () => { + uint32.keys() + }) + test('uint32_map', () => { + uint32.map() + }) + test('uint32_reduce', () => { + uint32.reduce() + }) + test('uint32_reduceRight', () => { + uint32.reduceRight() + }) + test('uint32_reverse', () => { + uint32.reverse() + }) + test('uint32_slice', () => { + uint32.slice() + }) + test('uint32_sort', () => { + uint32.sort() + }) + test('uint32_subarray', () => { + uint32.subarray() + }) + test('uint32_values', () => { + uint32.values() + }) + test('uint32_arrayBufferSlice', () => { + uint32.arrayBufferSlice() + }) + + //uint32 end + }) +} \ No newline at end of file diff --git a/uni_modules/uts-tests/utssdk/TDataView.js b/uni_modules/uts-tests/utssdk/TDataView.js new file mode 100644 index 0000000000000000000000000000000000000000..25cbcdd64ac3edec6accc01a559b7da1ae9da9bf --- /dev/null +++ b/uni_modules/uts-tests/utssdk/TDataView.js @@ -0,0 +1,329 @@ +import { + describe, + test, + expect, + Result +} from './tests.uts' + +export class TDataView { + test() { + // this.setDv(); + // this.setInt8(); + // this.setFloat32(); + // this.setFloat64(); + // this.setInt16(); + // this.setInt32(); + // this.setUint16(); + // this.setUint32(); + // this.setUint8(); + // this.testMix(); + } + setConstructor() { + let buffer = new ArrayBuffer(16); + let dataview = new DataView(buffer); + dataview.setInt8(1, 12); + dataview.setFloat32(1, 34); + expect(dataview.getInt8(1)).toEqual(66); + expect(this.numberEquals(34, dataview.getFloat32(1))).toEqual(true); + + buffer = new ArrayBuffer(16); + let view = new DataView(buffer, 1, 4); + view.setInt16(1, 42); // 42 + expect(view.getInt16(1)).toEqual(42); + } + + setInt8() { + // Create an ArrayBuffer with a size in bytes + let buffer = new ArrayBuffer(16); + let view = new DataView(buffer); + view.setInt8(1, 127); // Max signed 8-bit integer + expect(view.getInt8(1)).toEqual(127); + } + + setFloat32() { + console.log('setFloat32', 'start') + // Create an ArrayBuffer with a size in bytes + let buffer = new ArrayBuffer(16); + let view = new DataView(buffer); + view.setFloat32(1, Math.PI.toFloat()); + expect(this.numberEquals(3.1415927, view.getFloat32(1).toFloat())).toEqual(true); + } + + setFloat64() { + // Create an ArrayBuffer with a size in bytes + let buffer = new ArrayBuffer(16); + let view = new DataView(buffer); + view.setFloat64(1, Math.PI); + expect(this.numberEquals(3.141592653589793, view.getFloat64(1))).toEqual(true); + } + + setInt16() { + // Create an ArrayBuffer with a size in bytes + let buffer = new ArrayBuffer(16); + let view = new DataView(buffer); + view.setInt16(1, 32767); // Max signed 16-bit integer + expect(view.getInt16(1)).toEqual(32767); + } + + setInt32() { + // Create an ArrayBuffer with a size in bytes + let buffer = new ArrayBuffer(16); + let view = new DataView(buffer); + view.setInt32(1, 2147483647); // Max signed 32-bit integer + expect(this.numberEquals(2147483647, view.getInt32(1))).toEqual(true); + } + + setUint16() { + // Create an ArrayBuffer with a size in bytes + let buffer = new ArrayBuffer(16); + let view = new DataView(buffer); + view.setUint16(1, 65535); // Max unsigned 16-bit integer + expect(this.numberEquals(65535, view.getUint16(1))).toEqual(true); + } + + setUint32() { + // Create an ArrayBuffer with a size in bytes + let buffer = new ArrayBuffer(16); + let view = new DataView(buffer); + view.setUint32(1, 4294967295); // Max unsigned 32-bit integer + expect(this.numberEquals(4294967295, view.getUint32(1))).toEqual(true); + } + + setUint8() { + // Create an ArrayBuffer with a size in bytes + let buffer = new ArrayBuffer(16); + let view = new DataView(buffer); + view.setUint8(1, 255); // Max unsigned 8-bit integer + expect(this.numberEquals(255, view.getUint8(1))).toEqual(true); + } + + numberEquals(a: Number, b: Number): Boolean { + return a == b + } + + + doTestDataViewBigEndian() { + this.doIterations(false, true); + } + + doTestDataViewLittleEndian() { + this.doIterations(true, true); + } + + doTestTypedArrayBigEndian() { + this.doIterations(false, false); + } + + doTestTypedArrayLittleEndian() { + this.doIterations(true, false); + } + + doTestDataViewFloats() { + this.doFloatIterations(true); + } + + doTestTypedArrayFloats() { + this.doFloatIterations(false); + } + + doIterations(littleEndian: boolean, dataView: boolean) { + var buffer = this.makeBuffer(1000, littleEndian); + var iterations = 10; + if (dataView) { + for (var i = 0; i < iterations; i++) + this.doOneIterationDV(buffer, littleEndian); + } else { + for (var i = 0; i < iterations; i++) + this.doOneIterationJS(buffer, littleEndian); + } + } + + makeBuffer(size: Int, littleEndian: boolean): ArrayBuffer { + var buffer = new ArrayBuffer(size * 14); + var view = new DataView(buffer); + for (var i = 0; i < size; ++i) { + view.setInt8(i * 14, i); + view.setUint8(i * 14 + 1, i); + view.setInt16(i * 14 + 2, i * i, littleEndian); + view.setUint16(i * 14 + 4, i * i, littleEndian); + view.setInt32(i * 14 + 6, i * i * i, littleEndian); + view.setUint32(i * 14 + 10, i * i * i, littleEndian); + } + return buffer; + } + + doOneIterationDV(buffer: ArrayBuffer, littleEndian: boolean) { + var xor = 0; + var view = new DataView(buffer); + for (var i = 0; i < view.byteLength; i += 14) { + xor ^= view.getInt8(i); + xor ^= view.getUint8(i + 1); + xor ^= view.getInt16(i + 2, littleEndian); + xor ^= view.getUint16(i + 4, littleEndian); + xor ^= view.getInt32(i + 6, littleEndian); + xor ^= view.getUint32(i + 10, littleEndian); + } + expect(xor).toEqual(0); + } + + doOneIterationJS(buffer: ArrayBuffer, littleEndian: boolean) { + var xor = 0; + if (littleEndian) { + var reader = new LittleEndian(buffer); + for (var i = 0; i < buffer.byteLength; i += 14) { + xor ^= reader.getInt8(i); + xor ^= reader.getUint8(i + 1); + xor ^= reader.getInt16(i + 2); + xor ^= reader.getUint16(i + 4); + xor ^= reader.getInt32(i + 6); + xor ^= reader.getUint32(i + 10); + } + expect(xor).toEqual(0); + } else { + var reader = new BigEndian(buffer); + for (var i = 0; i < buffer.byteLength; i += 14) { + xor ^= reader.getInt8(i); + xor ^= reader.getUint8(i + 1); + xor ^= reader.getInt16(i + 2); + xor ^= reader.getUint16(i + 4); + xor ^= reader.getInt32(i + 6); + xor ^= reader.getUint32(i + 10); + } + } + + } + + doFloatIterations(dataView: boolean) { + var buffer = makeFloatBuffer(1000); + var iterations = 10; + if (dataView) { + for (var i = 0; i < iterations; i++) + this.doOneFloatIterationDV(buffer); + } else { + for (var i = 0; i < iterations; i++) + this.doOneFloatIterationJS(buffer); + } + } + + makeFloatBuffer(size: Int): ArrayBuffer { + var buffer = new ArrayBuffer(size * 16); + var view = new DataView(buffer); + for (var i = 0; i < size; i++) { + view.setFloat64(i * 16, Math.log10(i + 1)); + view.setFloat32(i * 16 + 8, Math.sqrt(i)); + view.setFloat32(i * 16 + 12, Math.cos(i)); + } + return buffer; + } + + doOneFloatIterationDV(buffer: ArrayBuffer) { + var sum = 0; + var view = new DataView(buffer); + for (var i = 0; i < view.byteLength; i += 16) { + sum += view.getFloat64(i); + sum += view.getFloat32(i + 8); + sum += view.getFloat32(i + 12); + } + expect(sum).toEqual(23634.413356150446); + } + + doOneFloatIterationJS(buffer: ArrayBuffer) { + var sum = 0; + var float32array = new Float32Array(buffer); + var float64array = new Float64Array(buffer); + for (var i = 0; i < buffer.byteLength; i += 16) { + sum += float64array[i / 8]; + sum += float32array[i / 4 + 2]; + sum += float32array[i / 4 + 3]; + } + } +} +class BigEndian { + private uint8View_: Uint8Array; + private int8View_: Int8Array; + + constructor(buffer: ArrayBuffer, ) { + const offset = 0; + this.uint8View_ = new Uint8Array(buffer, offset); + this.int8View_ = new Int8Array(buffer, offset); + } + + getInt8(byteOffset: number): number { + return this.int8View_[byteOffset]; + } + + getUint8(byteOffset: number): number { + return this.uint8View_[byteOffset]; + } + + getInt16(byteOffset: number): number { + return this.uint8View_[byteOffset + 1] | (this.int8View_[byteOffset] << 8); + } + + getUint16(byteOffset: number): number { + return this.uint8View_[byteOffset + 1] | (this.uint8View_[byteOffset] << 8); + } + + getInt32(byteOffset: number): number { + return ( + this.uint8View_[byteOffset + 3] | + (this.uint8View_[byteOffset + 2] << 8) | + (this.uint8View_[byteOffset + 1] << 16) | + (this.int8View_[byteOffset] << 24) + ); + } + + getUint32(byteOffset: number): number { + return ( + this.uint8View_[byteOffset + 3] + + (this.uint8View_[byteOffset + 2] << 8) + + (this.uint8View_[byteOffset + 1] << 16) + + this.uint8View_[byteOffset] * (1 << 24) + ); + } +} + +class LittleEndian { + private uint8View_: Uint8Array; + private int8View_: Int8Array; + + constructor(buffer: ArrayBuffer) { + const offset = 0; + this.uint8View_ = new Uint8Array(buffer, offset); + this.int8View_ = new Int8Array(buffer, offset); + } + + getInt8(byteOffset: number): number { + return this.int8View_[byteOffset]; + } + + getUint8(byteOffset: number): number { + return this.uint8View_[byteOffset]; + } + + getInt16(byteOffset: number): number { + return this.uint8View_[byteOffset] | (this.int8View_[byteOffset + 1] << 8); + } + + getUint16(byteOffset: number): number { + return this.uint8View_[byteOffset] | (this.uint8View_[byteOffset + 1] << 8); + } + + getInt32(byteOffset: number): number { + return ( + this.uint8View_[byteOffset] | + (this.uint8View_[byteOffset + 1] << 8) | + (this.uint8View_[byteOffset + 2] << 16) | + (this.int8View_[byteOffset + 3] << 24) + ); + } + + getUint32(byteOffset: number): number { + return ( + this.uint8View_[byteOffset] + + (this.uint8View_[byteOffset + 1] << 8) + + (this.uint8View_[byteOffset + 2] << 16) + + this.uint8View_[byteOffset + 3] * (1 << 24) + ); + } +} \ No newline at end of file diff --git a/uni_modules/uts-tests/utssdk/TFloat32Array.js b/uni_modules/uts-tests/utssdk/TFloat32Array.js new file mode 100644 index 0000000000000000000000000000000000000000..f53690af56dbcad65c9fc0df0a023e0ecb17fb94 --- /dev/null +++ b/uni_modules/uts-tests/utssdk/TFloat32Array.js @@ -0,0 +1,284 @@ +import { + describe, + test, + expect, + Result +} from './tests.uts' + +export class TFloat32Array { + test() { + this.testfloat32(); + 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(); + } + + testfloat32() { + let float32 = new Float32Array(2); + float32[0] = 42; + expect(float32[0]).toEqual(42); + expect(float32.length).toEqual(2); + expect(Float32Array.BYTES_PER_ELEMENT).toEqual(4); + + let x = new Float32Array([21, 31, 3]); + expect(x[1]).toEqual(31); + + let y = new Float32Array(x); + expect(y[0]).toEqual(21); + + let buffer = new ArrayBuffer(16); + let z = new Float32Array(buffer, 2, 4); + expect(z.byteOffset).toEqual(2); + expect(z.length).toEqual(4); + + var k = Float32Array.of(1, 2, 3) + expect(k.toString()).toEqual("1,2,3"); + } + + testConstructor() { + let buffer = new ArrayBuffer(16); + let float32 = new Float32Array(buffer); + float32[1] = 42; + expect(float32.toString()).toEqual("0,42,0,0"); + } + + testSet() { + let float32 = new Float32Array(8); + var array = [1, 2, 3] + float32.set(array, 1); + expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0"); + } + + testCopyWith() { + console.log("testCopyWith 1") + let float32 = new Float32Array(8); + float32.set([1, 2, 3], 1); + console.log("testCopyWith 1") + float32.copyWithin(3, 0, 3); + console.log("testCopyWith 1") + expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0"); + } + + testEvery() { + // const isBelowThreshold = (currentValue: number, index: number, array:Float32Array): boolean => currentValue < 40; + let result = new Float32Array([12, 5, 8, 130, 44]).every((value: number, index: number, array: Float32Array): + boolean => value < 40); // + expect(result).toEqual(false); + } + + testFill() { + let float32 = new Float32Array([1, 2, 3]).fill(4); + expect(float32.toString()).toEqual("4,4,4"); + + float32 = new Float32Array([1, 2, 3]).fill(4, 1); + expect(float32.toString()).toEqual("1,4,4"); + + float32 = new Float32Array([1, 2, 3]).fill(4, 1, 2); + expect(float32.toString()).toEqual("1,4,3"); + + float32 = new Float32Array([1, 2, 3]).fill(4, 1, 1); + expect(float32.toString()).toEqual("1,2,3"); + + float32 = new Float32Array([1, 2, 3]).fill(4, -3, -2); + expect(float32.toString()).toEqual("4,2,3"); + } + + testFilter() { + // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10; + + let float32 = new Float32Array([12, 5, 8, 44]).filter((value: number, index: number, array: Float32Array): + boolean => value >= 10); + expect(float32.toString()).toEqual("12,44"); + } + + find() { + let float32 = new Float32Array([4, 5, 8, 12]); + let res = float32.find((value: number, index: number, obj: Float32Array): boolean => value > 5); + expect(res).toEqual(8); + } + + findIndex() { + let float32 = new Float32Array([4, 6, 8, 12]); + let res = float32.findIndex((value: number, index: number, obj: Float32Array): boolean => value > 100); + expect(res).toEqual(-1); + + let ufloat32 = new Float32Array([4, 6, 7, 120]); + res = ufloat32.findIndex((value: number, index: number, obj: Float32Array): boolean => value > 100); + expect(res).toEqual(3); + } + + foreach() { + new Float32Array([0, 1, 2, 3]).forEach((value: number, index: number, array: Float32Array) => { + console.log(`a[${index}] = ${value}`); + }); + } + + iterator() { + let arr = new Float32Array([10, 20, 30, 40, 50]); + let entries = arr.entries(); + expect(entries.next().value[1]).toEqual(10); + expect(entries.next().value[1]).toEqual(20); + } + + includes() { + let float32 = new Float32Array([1, 2, 3]); + let res = float32.includes(2.0); + expect(res).toEqual(true); + + res = float32.includes(4.0); + expect(res).toEqual(false); + + res = float32.includes(3.0, 3); + expect(res).toEqual(false); + } + + indexOf() { + let float32 = new Float32Array([2, 5, 9]); + let res = float32.indexOf(2); + expect(res).toEqual(0); + + res = float32.indexOf(7); + expect(res).toEqual(-1); + + res = float32.indexOf(9, 2); + expect(res).toEqual(2); + + res = float32.indexOf(2, -1); + expect(res).toEqual(-1); + + res = float32.indexOf(2, -3); + expect(res).toEqual(0); + } + + join() { + let float32 = new Float32Array([1, 2, 3]); + let res = float32.join(); + expect(res).toEqual("1,2,3"); + + res = float32.join(" / "); + expect(res).toEqual("1 / 2 / 3"); + + res = float32.join(""); + expect(res).toEqual("123"); + } + + keys() { + let arr = new Float32Array([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); + } + + map() { + let numbers = new Float32Array([1, 4, 9]); + let doubles = numbers.map((value: number, index: number, array: Float32Array): number => value * 2); + expect(numbers.toString()).toEqual("1,4,9"); + expect(doubles.toString()).toEqual("2,8,18"); + } + + reduce() { + let total = new Float32Array([0, 1, 2, 3]); + let res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Float32Array): + number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Float32Array([0, 1, 2, 3]); + res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Float32Array): + number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reduceRight() { + let total = new Float32Array([0, 1, 2, 3]); + let res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: + Float32Array): number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Float32Array([0, 1, 2, 3]); + res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: Float32Array): + number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reverse() { + let float32 = new Float32Array([1, 2, 3]); + float32.reverse(); + expect(float32.toString()).toEqual("3,2,1"); + } + + slice() { + let float32 = new Float32Array([1, 2, 3]); + let res = float32.slice(1); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(2); + expect(res.toString()).toEqual("3"); + + res = float32.slice(-2); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(0, 1); + expect(res.toString()).toEqual("1"); + } + + sort() { + let numbers = new Float32Array([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"); + } + + subarray() { + let buffer = new ArrayBuffer(16); + let float32 = new Float32Array(buffer); + float32.set([1, 2, 3]); + expect(float32.toString()).toEqual("1,2,3,0"); + + let sub = float32.subarray(0, 4); + expect(sub.toString()).toEqual("1,2,3,0"); + } + + values() { + let arr = new Float32Array([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); + } + + arrayBufferSlice() { + let buffer = new ArrayBuffer(16); + let float32 = new Float32Array(buffer); + float32[3] = 42; + expect(float32.toString()).toEqual("0,0,0,42"); + + let res = buffer.slice(8); + let sliced = new Float32Array(res); + expect(sliced[1]).toEqual(42); + } +} \ No newline at end of file diff --git a/uni_modules/uts-tests/utssdk/TFloat64Array.js b/uni_modules/uts-tests/utssdk/TFloat64Array.js new file mode 100644 index 0000000000000000000000000000000000000000..170a0e6930dc904a9deb3909c563f0d86892c217 --- /dev/null +++ b/uni_modules/uts-tests/utssdk/TFloat64Array.js @@ -0,0 +1,279 @@ +import { + describe, + test, + expect, + Result +} from './tests.uts' + +export class TFloat64Array { + test() { + 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(); + } + + testfloat64() { + let float32 = new Float64Array(2); + float32[0] = 42; + expect(float32[0]).toEqual(42); + expect(float32.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 float32 = new Float64Array(buffer); + float32[1] = 42; + expect(float32.toString()).toEqual("0,42"); + } + + testSet() { + let float32 = new Float64Array(8); + var array = [1, 2, 3] + float32.set(array, 1); + expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0"); + } + + testCopyWith() { + let float32 = new Float64Array(8); + float32.set([1, 2, 3], 1); + float32.copyWithin(3, 0, 3); + expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0"); + } + + testEvery() { + // const isBelowThreshold = (currentValue: number, index: number, array:Float64Array): boolean => currentValue < 40; + let result = new Float64Array([-10, -20, -30, -40, -50]).every((value: number, index: number, array: + Float64Array): boolean => value < 0); // + expect(result).toEqual(true); + } + + testFill() { + let float32 = new Float64Array([1, 2, 3]).fill(4); + expect(float32.toString()).toEqual("4,4,4"); + + float32 = new Float64Array([1, 2, 3]).fill(4, 1); + expect(float32.toString()).toEqual("1,4,4"); + + float32 = new Float64Array([1, 2, 3]).fill(4, 1, 2); + expect(float32.toString()).toEqual("1,4,3"); + + float32 = new Float64Array([1, 2, 3]).fill(4, 1, 1); + expect(float32.toString()).toEqual("1,2,3"); + + float32 = new Float64Array([1, 2, 3]).fill(4, -3, -2); + expect(float32.toString()).toEqual("4,2,3"); + } + + testFilter() { + // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10; + + let float32 = new Float64Array([12, 5, 8, 44]).filter((value: number, index: number, array: Float64Array): + boolean => value >= 10); + expect(float32.toString()).toEqual("12,44"); + } + + find() { + let float32 = new Float64Array([4, 5, 8, 12]); + let res = float32.find((value: number, index: number, obj: Float64Array): boolean => value > 5); + expect(res).toEqual(8); + } + + findIndex() { + let float32 = new Float64Array([4, 6, 8, 12]); + let res = float32.findIndex((value: number, index: number, obj: Float64Array): boolean => value > 100); + expect(res).toEqual(-1); + + let ufloat32 = new Float64Array([4, 6, 7, 120]); + res = ufloat32.findIndex((value: number, index: number, obj: Float64Array): boolean => value > 100); + expect(res).toEqual(3); + } + + foreach() { + new Float64Array([0, 1, 2, 3]).forEach((value: number, index: number, array: Float64Array) => { + console.log(`a[${index}] = ${value}`); + }); + } + + iterator() { + 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); + } + + includes() { + let float32 = new Float64Array([1, 2, 3]); + let res = float32.includes(2); + expect(res).toEqual(true); + + res = float32.includes(4); + expect(res).toEqual(false); + + res = float32.includes(3, 3); + expect(res).toEqual(false); + } + + indexOf() { + let float32 = new Float64Array([2, 5, 9]); + let res = float32.indexOf(2); + expect(res).toEqual(0); + + res = float32.indexOf(7); + expect(res).toEqual(-1); + + res = float32.indexOf(9, 2); + expect(res).toEqual(2); + + res = float32.indexOf(2, -1); + expect(res).toEqual(-1); + + res = float32.indexOf(2, -3); + expect(res).toEqual(0); + } + + join() { + let float32 = new Float64Array([1, 2, 3]); + let res = float32.join(); + expect(res).toEqual("1,2,3"); + + res = float32.join(" / "); + expect(res).toEqual("1 / 2 / 3"); + + res = float32.join(""); + expect(res).toEqual("123"); + } + + keys() { + //todo 有差异 js 是keys.next().value + 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); + } + + map() { + let numbers = new Float64Array([1, 4, 9]); + let doubles = numbers.map((value: number, index: number, array: Float64Array): number => value * 2); + expect(numbers.toString()).toEqual("1,4,9"); + expect(doubles.toString()).toEqual("2,8,18"); + } + + reduce() { + let total = new Float64Array([0, 1, 2, 3]); + let res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Float64Array): + number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Float64Array([0, 1, 2, 3]); + res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Float64Array): + number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reduceRight() { + let total = new Float64Array([0, 1, 2, 3]); + let res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: + Float64Array): number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Float64Array([0, 1, 2, 3]); + res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: Float64Array): + number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reverse() { + let float32 = new Float64Array([1, 2, 3]); + float32.reverse(); + expect(float32.toString()).toEqual("3,2,1"); + } + + slice() { + let float32 = new Float64Array([1, 2, 3]); + let res = float32.slice(1); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(2); + expect(res.toString()).toEqual("3"); + + res = float32.slice(-2); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(0, 1); + expect(res.toString()).toEqual("1"); + } + + 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"); + } + + subarray() { + let buffer = new ArrayBuffer(32); + let float32 = new Float64Array(buffer); + float32.set([1, 2, 3]); + expect(float32.toString()).toEqual("1,2,3,0"); + + let sub = float32.subarray(0, 4); + expect(sub.toString()).toEqual("1,2,3,0"); + } + + 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); + } + + arrayBufferSlice() { + let buffer = new ArrayBuffer(16); + let float32 = new Float64Array(buffer); + float32[1] = 42; + expect(float32.toString()).toEqual("0,42"); + + let res = buffer.slice(8); + let sliced = new Float64Array(res); + expect(sliced[0]).toEqual(42); + } +} \ No newline at end of file diff --git a/uni_modules/uts-tests/utssdk/TInt16Array.js b/uni_modules/uts-tests/utssdk/TInt16Array.js new file mode 100644 index 0000000000000000000000000000000000000000..90c2f31a3bc19129b9cfebadcbf332252409cd01 --- /dev/null +++ b/uni_modules/uts-tests/utssdk/TInt16Array.js @@ -0,0 +1,253 @@ +import { + describe, + test, + expect, + Result +} from './tests.uts' + +export class TInt16Array { + test() { + 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(); + } + + + testConstructor() { + let buffer = new ArrayBuffer(16); + let float32 = new Int16Array(buffer); + float32[5] = 42; + expect(float32.toString()).toEqual("0,0,0,0,0,42,0,0"); + } + + testSet() { + let float32 = new Int16Array(8); + var array =[1, 2, 3] + float32.set(array, 1); + expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0"); + } + + testCopyWith() { + console.log("testCopyWith 1") + let float32 = new Int16Array(8); + float32.set([1, 2, 3], 1); + console.log("testCopyWith 1") + float32.copyWithin(3, 0, 3); + console.log("testCopyWith 1") + expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0"); + } + + testEvery() { + // const isBelowThreshold = (currentValue: number, index: number, array:Int16Array): boolean => currentValue < 40; + let result = new Int16Array([12, 5, 8, 130, 44]).every((value:number,index : number, array : Int16Array): boolean => value< 40); // + expect(result).toEqual(false); + } + + testFill() { + let float32 = new Int16Array([1, 2, 3]).fill(4); + expect(float32.toString()).toEqual("4,4,4"); + + float32 = new Int16Array([1, 2, 3]).fill(4, 1); + expect(float32.toString()).toEqual("1,4,4"); + + float32 = new Int16Array([1, 2, 3]).fill(4, 1, 2); + expect(float32.toString()).toEqual("1,4,3"); + + float32 = new Int16Array([1, 2, 3]).fill(4, 1, 1); + expect(float32.toString()).toEqual("1,2,3"); + + float32 = new Int16Array([1, 2, 3]).fill(4, -3, -2); + expect(float32.toString()).toEqual("4,2,3"); + } + + testFilter() { + // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10; + + let float32 = new Int16Array([12, 5, 8, 44]).filter((value : number, index : number, array : Int16Array): boolean => value>= 10); + expect(float32.toString()).toEqual("12,44"); + } + + find() { + let float32 = new Int16Array([4, 5, 8, 12]); + let res = float32.find((value : number, index : number, obj : Int16Array): boolean => value > 5); + expect(res).toEqual(8); + } + + findIndex() { + let float32 = new Int16Array([4, 6, 8, 12]); + let res = float32.findIndex((value : number, index : number, obj : Int16Array): boolean => value > 100); + expect(res).toEqual(-1); + + let ufloat32 = new Int16Array([4, 6, 7, 120]); + res = ufloat32.findIndex((value : number, index : number, obj : Int16Array): boolean => value > 100); + expect(res).toEqual(3); + } + + foreach() { + new Int16Array([0, 1, 2, 3]).forEach((value : number, index : number, array : Int16Array) => { + console.log(`a[${index}] = ${value}`); + }); + } + + iterator() { + let arr = new Int16Array([10, 20, 30, 40, 50]); + let entries = arr.entries(); + expect(entries.next().value[1]).toEqual(10); + } + + includes() { + let float32 = new Int16Array([1, 2, 3]); + let res = float32.includes(2); + expect(res).toEqual(true); + + res = float32.includes(4); + expect(res).toEqual(false); + + res = float32.includes(3, 3); + expect(res).toEqual(false); + } + + indexOf() { + let float32 = new Int16Array([2, 5, 9]); + let res = float32.indexOf(2); + expect(res).toEqual(0); + + res = float32.indexOf(7); + expect(res).toEqual(-1); + + res = float32.indexOf(9, 2); + expect(res).toEqual(2); + + res = float32.indexOf(2, -1); + expect(res).toEqual(-1); + + res = float32.indexOf(2, -3); + expect(res).toEqual(0); + } + + join() { + let float32 = new Int16Array([1, 2, 3]); + let res = float32.join(); + expect(res).toEqual("1,2,3"); + + res = float32.join(" / "); + expect(res).toEqual("1 / 2 / 3"); + + res = float32.join(""); + expect(res).toEqual("123"); + } + + keys() { + let arr = new Int16Array([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); + } + + map() { + let numbers = new Int16Array([1, 4, 9]); + let doubles = numbers.map((value : number, index : number, array : Int16Array): number => value * 2); + expect(numbers.toString()).toEqual("1,4,9"); + expect(doubles.toString()).toEqual("2,8,18"); + } + + reduce() { + let total = new Int16Array([0, 1, 2, 3]); + let res = total.reduce((accumulator : number, currentValue : number, currentIndex : number, array : Int16Array): number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Int16Array([0, 1, 2, 3]); + res = total.reduce((accumulator : number, currentValue : number, currentIndex : number, array : Int16Array): number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reduceRight() { + let total = new Int16Array([0, 1, 2, 3]); + let res = total.reduceRight((accumulator: number, currentValue : number, currentIndex : number, array : Int16Array): number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Int16Array([0, 1, 2, 3]); + res = total.reduceRight((accumulator: number, currentValue : number, currentIndex : number, array : Int16Array): number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reverse() { + let float32 = new Int16Array([1, 2, 3]); + float32.reverse(); + expect(float32.toString()).toEqual("3,2,1"); + } + + slice() { + let float32 = new Int16Array([1, 2, 3]); + let res = float32.slice(1); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(2); + expect(res.toString()).toEqual("3"); + + res = float32.slice(-2); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(0, 1); + expect(res.toString()).toEqual("1"); + } + + sort() { + let numbers = new Int16Array([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"); + } + + subarray() { + let buffer = new ArrayBuffer(16); + let float32 = new Int16Array(buffer); + float32.set([1, 2, 3]); + expect(float32.toString()).toEqual("1,2,3,0,0,0,0,0"); + + let sub = float32.subarray(0, 4); + expect(sub.toString()).toEqual("1,2,3,0"); + } + + values() { + let arr = new Int16Array([1, 2, 3]); + let values = arr.values(); + expect(values.next().value).toEqual(1); + } + + arrayBufferSlice() { + let buffer = new ArrayBuffer(16); + let float32 = new Int16Array(buffer); + float32[4] = 42; + expect(float32.toString()).toEqual("0,0,0,0,42,0,0,0"); + + let res = buffer.slice(8,12); + let sliced = new Int16Array(res); + expect(sliced[0]).toEqual(42); + } +} diff --git a/uni_modules/uts-tests/utssdk/TInt32Array.js b/uni_modules/uts-tests/utssdk/TInt32Array.js new file mode 100644 index 0000000000000000000000000000000000000000..b7b14567fe11b690b0b3d18e6a20297f15cf37ef --- /dev/null +++ b/uni_modules/uts-tests/utssdk/TInt32Array.js @@ -0,0 +1,269 @@ +import { + describe, + test, + expect, + Result +} from './tests.uts' + +export class TInt32Array { + test() { + this.testfloat32(); + 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(); + } + + testfloat32() { + let float32 = new Int32Array(2); + float32[0] = 42; + expect(float32[0]).toEqual(42); + expect(float32.length).toEqual(2); + expect(Int32Array.BYTES_PER_ELEMENT).toEqual(4); + + let x = new Int32Array([21, 31, 3]); + expect(x[1]).toEqual(31); + + let y = new Int32Array(x); + expect(y[0]).toEqual(21); + + let buffer = new ArrayBuffer(16); + let z = new Int32Array(buffer, 2, 4); + expect(z.byteOffset).toEqual(2); + expect(z.length).toEqual(4); + } + + testConstructor() { + let buffer = new ArrayBuffer(16); + let float32 = new Int32Array(buffer); + float32[1] = 42; + expect(float32.toString()).toEqual("0,42,0,0"); + } + + testSet() { + let float32 = new Int32Array(8); + var array =[1, 2, 3] + float32.set(array, 1); + expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0"); + } + + testCopyWith() { + console.log("testCopyWith 1") + let float32 = new Int32Array(8); + float32.set([1, 2, 3], 1); + console.log("testCopyWith 1") + float32.copyWithin(3, 0, 3); + console.log("testCopyWith 1") + expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0"); + } + + testEvery() { + // const isBelowThreshold = (currentValue: number, index: number, array:Int32Array): boolean => currentValue < 40; + let result = new Int32Array([12, 5, 8, 130, 44]).every((value:number,index : number, array : Int32Array): boolean => value< 40); // + expect(result).toEqual(false); + } + + testFill() { + let float32 = new Int32Array([1, 2, 3]).fill(4); + expect(float32.toString()).toEqual("4,4,4"); + + float32 = new Int32Array([1, 2, 3]).fill(4, 1); + expect(float32.toString()).toEqual("1,4,4"); + + float32 = new Int32Array([1, 2, 3]).fill(4, 1, 2); + expect(float32.toString()).toEqual("1,4,3"); + + float32 = new Int32Array([1, 2, 3]).fill(4, 1, 1); + expect(float32.toString()).toEqual("1,2,3"); + + float32 = new Int32Array([1, 2, 3]).fill(4, -3, -2); + expect(float32.toString()).toEqual("4,2,3"); + } + + testFilter() { + // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10; + + let float32 = new Int32Array([12, 5, 8, 44]).filter((value : number, index : number, array : Int32Array): boolean => value>= 10); + expect(float32.toString()).toEqual("12,44"); + } + + find() { + let float32 = new Int32Array([4, 5, 8, 12]); + let res = float32.find((value : number, index : number, obj : Int32Array): boolean => value > 5); + expect(res).toEqual(8); + } + + findIndex() { + let float32 = new Int32Array([4, 6, 8, 12]); + let res = float32.findIndex((value : number, index : number, obj : Int32Array): boolean => value > 100); + expect(res).toEqual(-1); + + let ufloat32 = new Int32Array([4, 6, 7, 120]); + res = ufloat32.findIndex((value : number, index : number, obj : Int32Array): boolean => value > 100); + expect(res).toEqual(3); + } + + foreach() { + new Int32Array([0, 1, 2, 3]).forEach((value : number, index : number, array : Int32Array) => { + console.log(`a[${index}] = ${value}`); + }); + } + + iterator() { + let arr = new Int32Array([10, 20, 30, 40, 50]); + let entries = arr.entries(); + expect(entries.next().value[1]).toEqual(10); + + } + + includes() { + let float32 = new Int32Array([1, 2, 3]); + let res = float32.includes(2); + expect(res).toEqual(true); + + res = float32.includes(4); + expect(res).toEqual(false); + + res = float32.includes(3, 3); + expect(res).toEqual(false); + } + + indexOf() { + let float32 = new Int32Array([2, 5, 9]); + let res = float32.indexOf(2); + expect(res).toEqual(0); + + res = float32.indexOf(7); + expect(res).toEqual(-1); + + res = float32.indexOf(9, 2); + expect(res).toEqual(2); + + res = float32.indexOf(2, -1); + expect(res).toEqual(-1); + + res = float32.indexOf(2, -3); + expect(res).toEqual(0); + } + + join() { + let float32 = new Int32Array([1, 2, 3]); + let res = float32.join(); + expect(res).toEqual("1,2,3"); + + res = float32.join(" / "); + expect(res).toEqual("1 / 2 / 3"); + + res = float32.join(""); + expect(res).toEqual("123"); + } + + keys() { + let arr = new Int32Array([10, 20, 30, 40, 50]); + let keys = arr.keys(); + expect(keys.next().value).toEqual(0); + } + + map() { + let numbers = new Int32Array([1, 4, 9]); + let doubles = numbers.map((value : number, index : number, array : Int32Array): number => value * 2); + expect(numbers.toString()).toEqual("1,4,9"); + expect(doubles.toString()).toEqual("2,8,18"); + } + + reduce() { + let total = new Int32Array([0, 1, 2, 3]); + let res = total.reduce((accumulator : number, currentValue : number, currentIndex : number, array : Int32Array): number => accumulator + currentValue); + expect(res).toEqual(6); + + total = new Int32Array([0, 1, 2, 3]); + res = total.reduce((accumulator : number, currentValue : number, currentIndex : number, array : Int32Array): number => accumulator + currentValue, 8); + expect(res).toEqual(14); + } + + reduceRight() { + let total = new Int32Array([0, 1, 2, 3]); + let res = total.reduceRight((accumulator: number, currentValue : number, currentIndex : number, array : Int32Array): number => accumulator + currentValue); + expect(res).toEqual(6); + + total = new Int32Array([0, 1, 2, 3]); + res = total.reduceRight((accumulator: number, currentValue : number, currentIndex : number, array : Int32Array): number => accumulator + currentValue, 8); + expect(res).toEqual(14); + } + + reverse() { + let float32 = new Int32Array([1, 2, 3]); + float32.reverse(); + expect(float32.toString()).toEqual("3,2,1"); + } + + slice() { + let float32 = new Int32Array([1, 2, 3]); + let res = float32.slice(1); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(2); + expect(res.toString()).toEqual("3"); + + res = float32.slice(-2); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(0, 1); + expect(res.toString()).toEqual("1"); + } + + sort() { + let numbers = new Int32Array([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"); + } + + subarray() { + let buffer = new ArrayBuffer(16); + let float32 = new Int32Array(buffer); + float32.set([1, 2, 3]); + expect(float32.toString()).toEqual("1,2,3,0"); + + let sub = float32.subarray(0, 4); + expect(sub.toString()).toEqual("1,2,3,0"); + } + + values() { + let arr = new Int32Array([1, 2, 3]); + let values = arr.values(); + expect(values.next().value).toEqual(1); + } + + arrayBufferSlice() { + let buffer = new ArrayBuffer(16); + let float32 = new Int32Array(buffer); + float32[3] = 42; + expect(float32.toString()).toEqual("0,0,0,42"); + + let res = buffer.slice(8); + let sliced = new Int32Array(res); + expect(sliced[1]).toEqual(42); + } +} diff --git a/uni_modules/uts-tests/utssdk/TInt8Array.js b/uni_modules/uts-tests/utssdk/TInt8Array.js new file mode 100644 index 0000000000000000000000000000000000000000..3bc37bba975f14801b2df9b60e77f5d91b337b5d --- /dev/null +++ b/uni_modules/uts-tests/utssdk/TInt8Array.js @@ -0,0 +1,263 @@ +import { + describe, + test, + expect, + Result +} from './tests.uts' + +export class TInt8Array { + test() { + 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(); + } + + + testConstructor() { + let buffer = new ArrayBuffer(16); + let int8View = new Int8Array(buffer); + int8View[5] = 42; + expect(int8View.toString()).toEqual("0,0,0,0,0,42,0,0,0,0,0,0,0,0,0,0"); + } + + testSet() { + let float32 = new Int8Array(8); + var array = [1, 2, 3] + float32.set(array, 1); + expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0"); + } + + testCopyWith() { + console.log("testCopyWith 1") + let float32 = new Int8Array(8); + float32.set([1, 2, 3], 1); + console.log("testCopyWith 1") + float32.copyWithin(3, 0, 3); + console.log("testCopyWith 1") + expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0"); + } + + testEvery() { + // const isBelowThreshold = (currentValue: number, index: number, array:Int8Array): boolean => currentValue < 40; + let result = new Int8Array([12, 5, 8, 130, 44]).every((value: number, index: number, array: Int8Array): boolean => + value < 40); // + expect(result).toEqual(false); + } + + testFill() { + let float32 = new Int8Array([1, 2, 3]).fill(4); + expect(float32.toString()).toEqual("4,4,4"); + + float32 = new Int8Array([1, 2, 3]).fill(4, 1); + expect(float32.toString()).toEqual("1,4,4"); + + float32 = new Int8Array([1, 2, 3]).fill(4, 1, 2); + expect(float32.toString()).toEqual("1,4,3"); + + float32 = new Int8Array([1, 2, 3]).fill(4, 1, 1); + expect(float32.toString()).toEqual("1,2,3"); + + float32 = new Int8Array([1, 2, 3]).fill(4, -3, -2); + expect(float32.toString()).toEqual("4,2,3"); + } + + testFilter() { + // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10; + + let float32 = new Int8Array([12, 5, 8, 44]).filter((value: number, index: number, array: Int8Array): boolean => + value >= 10); + expect(float32.toString()).toEqual("12,44"); + } + + find() { + let float32 = new Int8Array([4, 5, 8, 12]); + let res = float32.find((value: number, index: number, obj: Int8Array): boolean => value > 5); + expect(res).toEqual(8); + } + + findIndex() { + let float32 = new Int8Array([4, 6, 8, 12]); + let res = float32.findIndex((value: number, index: number, obj: Int8Array): boolean => value > 100); + expect(res).toEqual(-1); + + let ufloat32 = new Int8Array([4, 6, 7, 120]); + res = ufloat32.findIndex((value: number, index: number, obj: Int8Array): boolean => value > 100); + expect(res).toEqual(3); + } + + foreach() { + new Int8Array([0, 1, 2, 3]).forEach((value: number, index: number, array: Int8Array) => { + console.log(`a[${index}] = ${value}`); + }); + } + + iterator() { + let arr = new Int8Array([10, 20, 30, 40, 50]); + let entries = arr.entries(); + expect(entries.next().value[1]).toEqual(10); + expect(entries.next().value[1]).toEqual(20); + } + + includes() { + let float32 = new Int8Array([1, 2, 3]); + let res = float32.includes(2); + expect(res).toEqual(true); + + res = float32.includes(4); + expect(res).toEqual(false); + + res = float32.includes(3, 3); + expect(res).toEqual(false); + } + + indexOf() { + let float32 = new Int8Array([2, 5, 9]); + let res = float32.indexOf(2); + expect(res).toEqual(0); + + res = float32.indexOf(7); + expect(res).toEqual(-1); + + res = float32.indexOf(9, 2); + expect(res).toEqual(2); + + res = float32.indexOf(2, -1); + expect(res).toEqual(-1); + + res = float32.indexOf(2, -3); + expect(res).toEqual(0); + } + + join() { + let float32 = new Int8Array([1, 2, 3]); + let res = float32.join(); + expect(res).toEqual("1,2,3"); + + res = float32.join(" / "); + expect(res).toEqual("1 / 2 / 3"); + + res = float32.join(""); + expect(res).toEqual("123"); + } + + keys() { + let arr = new Int8Array([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); + } + + map() { + let numbers = new Int8Array([1, 4, 9]); + let doubles = numbers.map((value: number, index: number, array: Int8Array): number => value * 2); + expect(numbers.toString()).toEqual("1,4,9"); + expect(doubles.toString()).toEqual("2,8,18"); + } + + reduce() { + let total = new Int8Array([0, 1, 2, 3]); + let res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Int8Array): + number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Int8Array([0, 1, 2, 3]); + res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Int8Array): number => + accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reduceRight() { + let total = new Int8Array([0, 1, 2, 3]); + let res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: Int8Array): + number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Int8Array([0, 1, 2, 3]); + res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: Int8Array): + number => accumulator + currentValue, 8); + + expect(res).toEqual(14.0); + } + + reverse() { + let float32 = new Int8Array([1, 2, 3]); + float32.reverse(); + expect(float32.toString()).toEqual("3,2,1"); + } + + slice() { + let float32 = new Int8Array([1, 2, 3]); + let res = float32.slice(1); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(2); + expect(res.toString()).toEqual("3"); + + res = float32.slice(-2); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(0, 1); + expect(res.toString()).toEqual("1"); + } + + sort() { + let numbers = new Int8Array([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"); + } + + subarray() { + let buffer = new ArrayBuffer(8); + let float32 = new Int8Array(buffer); + float32.set([1, 2, 3]); + expect(float32.toString()).toEqual("1,2,3,0,0,0,0,0"); + + let sub = float32.subarray(0, 4); + expect(sub.toString()).toEqual("1,2,3,0"); + } + + values() { + let arr = new Int8Array([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); + } + + arrayBufferSlice() { + let buffer = new ArrayBuffer(16); + let float32 = new Int8Array(buffer); + float32[4] = 42; + expect(float32.toString()).toEqual("0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"); + + let res = buffer.slice(4, 12); + let sliced = new Int8Array(res); + expect(sliced[0]).toEqual(42); + } +} \ No newline at end of file diff --git a/uni_modules/uts-tests/utssdk/TUInt8Array.js b/uni_modules/uts-tests/utssdk/TUInt8Array.js new file mode 100644 index 0000000000000000000000000000000000000000..65c0ebf30bd92163859dd464524b6ee4cf9b9760 --- /dev/null +++ b/uni_modules/uts-tests/utssdk/TUInt8Array.js @@ -0,0 +1,256 @@ +import { + describe, + test, + expect, + Result +} from './tests.uts' + +export class TUint8Array { + test() { + this.testMAX(); + 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(); + } + + testMAX() { + let float32 = new Uint8Array(16); + float32[0] = 255; + expect(float32[0]).toEqual(255); + } + + testConstructor() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint8Array(buffer); + float32[5] = 42; + expect(float32.toString()).toEqual("0,0,0,0,0,42,0,0,0,0,0,0,0,0,0,0"); + } + + testSet() { + let float32 = new Uint8Array(8); + var array =[1, 2, 3] + float32.set(array, 1); + expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0"); + } + + testCopyWith() { + console.log("testCopyWith 1") + let float32 = new Uint8Array(8); + float32.set([1, 2, 3], 1); + float32.copyWithin(3, 0, 3); + + expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0"); + } + + testEvery() { + // const isBelowThreshold = (currentValue: number, index: number, array:Uint8Array): boolean => currentValue < 40; + let result = new Uint8Array([12, 5, 8, 130, 44]).every((value:number,index : number, array : Uint8Array): boolean => value< 40); // + expect(result).toEqual(false); + } + + testFill() { + let float32 = new Uint8Array([1, 2, 3]).fill(4); + expect(float32.toString()).toEqual("4,4,4"); + + float32 = new Uint8Array([1, 2, 3]).fill(4, 1); + expect(float32.toString()).toEqual("1,4,4"); + + float32 = new Uint8Array([1, 2, 3]).fill(4, 1, 2); + expect(float32.toString()).toEqual("1,4,3"); + + float32 = new Uint8Array([1, 2, 3]).fill(4, 1, 1); + expect(float32.toString()).toEqual("1,2,3"); + + float32 = new Uint8Array([1, 2, 3]).fill(4, -3, -2); + expect(float32.toString()).toEqual("4,2,3"); + } + + testFilter() { + // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10; + + let float32 = new Uint8Array([12, 5, 8, 44]).filter((value : number, index : number, array : Uint8Array): boolean => value>= 10); + expect(float32.toString()).toEqual("12,44"); + } + + find() { + let float32 = new Uint8Array([4, 5, 8, 12]); + let res = float32.find((value : number, index : number, obj : Uint8Array): boolean => value > 5); + expect(res).toEqual(8); + } + + findIndex() { + let float32 = new Uint8Array([4, 6, 8, 12]); + let res = float32.findIndex((value : number, index : number, obj : Uint8Array): boolean => value > 100); + expect(res).toEqual(-1); + + let ufloat32 = new Uint8Array([4, 6, 7, 120]); + res = ufloat32.findIndex((value : number, index : number, obj : Uint8Array): boolean => value > 100); + expect(res).toEqual(3); + } + + foreach() { + new Uint8Array([0, 1, 2, 3]).forEach((value : number, index : number, array : Uint8Array) => { + console.log(`a[${index}] = ${value}`); + }); + } + + iterator() { + let arr = new Uint8Array([10, 20, 30, 40, 50]); + let entries = arr.entries(); + expect(entries.next().value[1]).toEqual(10); + + } + + includes() { + let float32 = new Uint8Array([1, 2, 3]); + let res = float32.includes(2.0); + expect(res).toEqual(true); + + res = float32.includes(4.0); + expect(res).toEqual(false); + + res = float32.includes(3.0, 3); + expect(res).toEqual(false); + } + + indexOf() { + let float32 = new Uint8Array([2, 5, 9]); + let res = float32.indexOf(2); + expect(res).toEqual(0); + + res = float32.indexOf(7); + expect(res).toEqual(-1); + + res = float32.indexOf(9, 2); + expect(res).toEqual(2); + + res = float32.indexOf(2, -1); + expect(res).toEqual(-1); + + res = float32.indexOf(2, -3); + expect(res).toEqual(0); + } + + join() { + let float32 = new Uint8Array([1, 2, 3]); + let res = float32.join(); + expect(res).toEqual("1,2,3"); + + res = float32.join(" / "); + expect(res).toEqual("1 / 2 / 3"); + + res = float32.join(""); + expect(res).toEqual("123"); + } + + keys() { + let arr = new Uint8Array([10, 20, 30, 40, 50]); + let keys = arr.keys(); + expect(keys.next().value).toEqual(0); + } + + map() { + let numbers = new Uint8Array([1, 4, 9]); + let doubles = numbers.map((value : number, index : number, array : Uint8Array): number => value * 2); + expect(numbers.toString()).toEqual("1,4,9"); + expect(doubles.toString()).toEqual("2,8,18"); + } + + reduce() { + let total = new Uint8Array([0, 1, 2, 3]); + let res = total.reduce((accumulator : number, currentValue : number, currentIndex : number, array : Uint8Array): number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Uint8Array([0, 1, 2, 3]); + res = total.reduce((accumulator : number, currentValue : number, currentIndex : number, array : Uint8Array): number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reduceRight() { + let total = new Uint8Array([0, 1, 2, 3]); + let res = total.reduceRight((accumulator: number, currentValue : number, currentIndex : number, array : Uint8Array): number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Uint8Array([0, 1, 2, 3]); + res = total.reduceRight((accumulator: number, currentValue : number, currentIndex : number, array : Uint8Array): number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reverse() { + let float32 = new Uint8Array([1, 2, 3]); + float32.reverse(); + expect(float32.toString()).toEqual("3,2,1"); + } + + slice() { + let float32 = new Uint8Array([1, 2, 3]); + let res = float32.slice(1); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(2); + expect(res.toString()).toEqual("3"); + + res = float32.slice(-2); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(0, 1); + expect(res.toString()).toEqual("1"); + } + + sort() { + let numbers = new Uint8Array([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"); + } + + subarray() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint8Array(buffer); + float32.set([1, 2, 3]); + expect(float32.toString()).toEqual("1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0"); + + let sub = float32.subarray(0, 4); + expect(sub.toString()).toEqual("1,2,3,0"); + } + + values() { + let arr = new Uint8Array([1, 2, 3]); + let values = arr.values(); + expect(values.next().value).toEqual(1); + + } + + arrayBufferSlice() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint8Array(buffer); + float32[4] = 42; + expect(float32.toString()).toEqual("0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"); + + let res = buffer.slice(4, 12); + let sliced = new Uint8Array(res); + expect(sliced[0]).toEqual(42); + } +} diff --git a/uni_modules/uts-tests/utssdk/TUInt8ClampedArray.js b/uni_modules/uts-tests/utssdk/TUInt8ClampedArray.js new file mode 100644 index 0000000000000000000000000000000000000000..6739cdc0bab2cbfebb34c5eaa819fadfae80d944 --- /dev/null +++ b/uni_modules/uts-tests/utssdk/TUInt8ClampedArray.js @@ -0,0 +1,262 @@ +import { + describe, + test, + expect, + Result +} from './tests.uts' + +export class TUint8ClampedArray { + test() { + this.testMAX(); + 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(); + } + + testMAX() { + let float32 = new Uint8ClampedArray(16); + float32[0] = 255; + expect(float32[0]).toEqual(255); + } + + testConstructor() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint8ClampedArray(buffer); + float32[5] = 42; + expect(float32.toString()).toEqual("0,0,0,0,0,42,0,0,0,0,0,0,0,0,0,0"); + } + + testSet() { + let float32 = new Uint8ClampedArray(8); + var array = [1, 2, 3] + float32.set(array, 1); + expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0"); + } + + testCopyWith() { + console.log("testCopyWith 1") + let float32 = new Uint8ClampedArray(8); + float32.set([1, 2, 3], 1); + float32.copyWithin(3, 0, 3); + + expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0"); + } + + testEvery() { + // const isBelowThreshold = (currentValue: number, index: number, array:Uint8ClampedArray): boolean => currentValue < 40; + let result = new Uint8ClampedArray([12, 5, 8, 130, 44]).every((value: number, index: number, array: + Uint8ClampedArray): boolean => value < 40); // + expect(result).toEqual(false); + } + + testFill() { + let float32 = new Uint8ClampedArray([1, 2, 3]).fill(4); + expect(float32.toString()).toEqual("4,4,4"); + + float32 = new Uint8ClampedArray([1, 2, 3]).fill(4, 1); + expect(float32.toString()).toEqual("1,4,4"); + + float32 = new Uint8ClampedArray([1, 2, 3]).fill(4, 1, 2); + expect(float32.toString()).toEqual("1,4,3"); + + float32 = new Uint8ClampedArray([1, 2, 3]).fill(4, 1, 1); + expect(float32.toString()).toEqual("1,2,3"); + + float32 = new Uint8ClampedArray([1, 2, 3]).fill(4, -3, -2); + expect(float32.toString()).toEqual("4,2,3"); + } + + testFilter() { + // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10; + + let float32 = new Uint8ClampedArray([12, 5, 8, 44]).filter((value: number, index: number, array: + Uint8ClampedArray): boolean => value >= 10); + expect(float32.toString()).toEqual("12,44"); + } + + find() { + let float32 = new Uint8ClampedArray([4, 5, 8, 12]); + let res = float32.find((value: number, index: number, obj: Uint8ClampedArray): boolean => value > 5); + expect(res).toEqual(8); + } + + findIndex() { + let float32 = new Uint8ClampedArray([4, 6, 8, 12]); + let res = float32.findIndex((value: number, index: number, obj: Uint8ClampedArray): boolean => value > 100); + expect(res).toEqual(-1); + + let ufloat32 = new Uint8ClampedArray([4, 6, 7, 120]); + res = ufloat32.findIndex((value: number, index: number, obj: Uint8ClampedArray): boolean => value > 100); + expect(res).toEqual(3); + } + + foreach() { + new Uint8ClampedArray([0, 1, 2, 3]).forEach((value: number, index: number, array: Uint8ClampedArray) => { + console.log(`a[${index}] = ${value}`); + }); + } + + iterator() { + let arr = new Uint8ClampedArray([10, 20, 30, 40, 50]); + let entries = arr.entries(); + expect(entries.next().value[1]).toEqual(10); + + } + + includes() { + let float32 = new Uint8ClampedArray([1, 2, 3]); + let res = float32.includes(2.0); + expect(res).toEqual(true); + + res = float32.includes(4.0); + expect(res).toEqual(false); + + res = float32.includes(3.0, 3); + expect(res).toEqual(false); + } + + indexOf() { + let float32 = new Uint8ClampedArray([2, 5, 9]); + let res = float32.indexOf(2); + expect(res).toEqual(0); + + res = float32.indexOf(7); + expect(res).toEqual(-1); + + res = float32.indexOf(9, 2); + expect(res).toEqual(2); + + res = float32.indexOf(2, -1); + expect(res).toEqual(-1); + + res = float32.indexOf(2, -3); + expect(res).toEqual(0); + } + + join() { + let float32 = new Uint8ClampedArray([1, 2, 3]); + let res = float32.join(); + expect(res).toEqual("1,2,3"); + + res = float32.join(" / "); + expect(res).toEqual("1 / 2 / 3"); + + res = float32.join(""); + expect(res).toEqual("123"); + } + + keys() { + let arr = new Uint8ClampedArray([10, 20, 30, 40, 50]); + let keys = arr.keys(); + expect(keys.next().value).toEqual(0); + } + + map() { + let numbers = new Uint8ClampedArray([1, 4, 9]); + let doubles = numbers.map((value: number, index: number, array: Uint8ClampedArray): number => value * 2); + expect(numbers.toString()).toEqual("1,4,9"); + expect(doubles.toString()).toEqual("2,8,18"); + } + + reduce() { + let total = new Uint8ClampedArray([0, 1, 2, 3]); + let res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: + Uint8ClampedArray): number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Uint8ClampedArray([0, 1, 2, 3]); + res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Uint8ClampedArray): + number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reduceRight() { + let total = new Uint8ClampedArray([0, 1, 2, 3]); + let res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: + Uint8ClampedArray): number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Uint8ClampedArray([0, 1, 2, 3]); + res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: + Uint8ClampedArray): number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reverse() { + let float32 = new Uint8ClampedArray([1, 2, 3]); + float32.reverse(); + expect(float32.toString()).toEqual("3,2,1"); + } + + slice() { + let float32 = new Uint8ClampedArray([1, 2, 3]); + let res = float32.slice(1); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(2); + expect(res.toString()).toEqual("3"); + + res = float32.slice(-2); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(0, 1); + expect(res.toString()).toEqual("1"); + } + + sort() { + let numbers = new Uint8ClampedArray([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"); + } + + subarray() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint8ClampedArray(buffer); + float32.set([1, 2, 3]); + expect(float32.toString()).toEqual("1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0"); + + let sub = float32.subarray(0, 4); + expect(sub.toString()).toEqual("1,2,3,0"); + } + + values() { + let arr = new Uint8ClampedArray([1, 2, 3]); + let values = arr.values(); + expect(values.next().value).toEqual(1); + + } + + arrayBufferSlice() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint8ClampedArray(buffer); + float32[4] = 42; + expect(float32.toString()).toEqual("0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"); + + let res = buffer.slice(4, 12); + let sliced = new Uint8ClampedArray(res); + expect(sliced[0]).toEqual(42); + } +} \ No newline at end of file diff --git a/uni_modules/uts-tests/utssdk/TUint16Array.js b/uni_modules/uts-tests/utssdk/TUint16Array.js new file mode 100644 index 0000000000000000000000000000000000000000..c15c63591867c3cec0d40f33210381e3c20bf26e --- /dev/null +++ b/uni_modules/uts-tests/utssdk/TUint16Array.js @@ -0,0 +1,276 @@ +import { + describe, + test, + expect, + Result +} from './tests.uts' + +export class TUint16Array { + test() { + this.testfloat32(); + 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(); + } + + testfloat32() { + let float32 = new Uint16Array(2); + float32[0] = 42; + expect(float32[0]).toEqual(42); + expect(float32.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 float32 = new Uint16Array(buffer); + float32[1] = 42; + expect(float32.toString()).toEqual("0,42,0,0,0,0,0,0"); + } + + testSet() { + let float32 = new Uint16Array(8); + var array = [1, 2, 3] + float32.set(array, 1); + expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0"); + } + + testCopyWith() { + console.log("testCopyWith 1") + let float32 = new Uint16Array(8); + float32.set([1, 2, 3], 1); + console.log("testCopyWith 1") + float32.copyWithin(3, 0, 3); + console.log("testCopyWith 1") + expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0"); + } + + testEvery() { + // const isBelowThreshold = (currentValue: number, index: number, array:Uint16Array): boolean => currentValue < 40; + let result = new Uint16Array([12, 5, 8, 130, 44]).every((value: number, index: number, array: Uint16Array): + boolean => value < 40); // + expect(result).toEqual(false); + } + + testFill() { + let float32 = new Uint16Array([1, 2, 3]).fill(4); + expect(float32.toString()).toEqual("4,4,4"); + + float32 = new Uint16Array([1, 2, 3]).fill(4, 1); + expect(float32.toString()).toEqual("1,4,4"); + + float32 = new Uint16Array([1, 2, 3]).fill(4, 1, 2); + expect(float32.toString()).toEqual("1,4,3"); + + float32 = new Uint16Array([1, 2, 3]).fill(4, 1, 1); + expect(float32.toString()).toEqual("1,2,3"); + + float32 = new Uint16Array([1, 2, 3]).fill(4, -3, -2); + expect(float32.toString()).toEqual("4,2,3"); + } + + testFilter() { + // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10; + + let float32 = new Uint16Array([12, 5, 8, 44]).filter((value: number, index: number, array: Uint16Array): + boolean => value >= 10); + expect(float32.toString()).toEqual("12,44"); + } + + find() { + let float32 = new Uint16Array([4, 5, 8, 12]); + let res = float32.find((value: number, index: number, obj: Uint16Array): boolean => value > 5); + expect(res).toEqual(8); + } + + findIndex() { + let float32 = new Uint16Array([4, 6, 8, 12]); + let res = float32.findIndex((value: number, index: number, obj: Uint16Array): boolean => value > 100); + expect(res).toEqual(-1); + + let ufloat32 = new Uint16Array([4, 6, 7, 120]); + res = ufloat32.findIndex((value: number, index: number, obj: Uint16Array): boolean => value > 100); + expect(res).toEqual(3); + } + + foreach() { + new Uint16Array([0, 1, 2, 3]).forEach((value: number, index: number, array: Uint16Array) => { + console.log(`a[${index}] = ${value}`); + }); + } + + iterator() { + let arr = new Uint16Array([10, 20, 30, 40, 50]); + let entries = arr.entries(); + expect(entries.next().value[1]).toEqual(10); + + } + + includes() { + let float32 = new Uint16Array([1, 2, 3]); + let res = float32.includes(2.0); + expect(res).toEqual(true); + + res = float32.includes(4.0); + expect(res).toEqual(false); + + res = float32.includes(3.0, 3); + expect(res).toEqual(false); + } + + indexOf() { + let float32 = new Uint16Array([2, 5, 9]); + let res = float32.indexOf(2); + expect(res).toEqual(0); + + res = float32.indexOf(7); + expect(res).toEqual(-1); + + res = float32.indexOf(9, 2); + expect(res).toEqual(2); + + res = float32.indexOf(2, -1); + expect(res).toEqual(-1); + + res = float32.indexOf(2, -3); + expect(res).toEqual(0); + } + + join() { + let float32 = new Uint16Array([1, 2, 3]); + let res = float32.join(); + expect(res).toEqual("1,2,3"); + + res = float32.join(" / "); + expect(res).toEqual("1 / 2 / 3"); + + res = float32.join(""); + expect(res).toEqual("123"); + } + + keys() { + let arr = new Uint16Array([10, 20, 30, 40, 50]); + let keys = arr.keys(); + expect(keys.next().value).toEqual(0); + } + + map() { + let numbers = new Uint16Array([1, 4, 9]); + let doubles = numbers.map((value: number, index: number, array: Uint16Array): number => value * 2); + expect(numbers.toString()).toEqual("1,4,9"); + expect(doubles.toString()).toEqual("2,8,18"); + } + + reduce() { + let total = new Uint16Array([0, 1, 2, 3]); + let res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Uint16Array): + number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Uint16Array([0, 1, 2, 3]); + res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Uint16Array): + number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reduceRight() { + let total = new Uint16Array([0, 1, 2, 3]); + let res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: Uint16Array): + number => accumulator + currentValue); + expect(res).toEqual(6.0); + + total = new Uint16Array([0, 1, 2, 3]); + res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: Uint16Array): + number => accumulator + currentValue, 8); + expect(res).toEqual(14.0); + } + + reverse() { + let float32 = new Uint16Array([1, 2, 3]); + float32.reverse(); + expect(float32.toString()).toEqual("3,2,1"); + } + + slice() { + let float32 = new Uint16Array([1, 2, 3]); + let res = float32.slice(1); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(2); + expect(res.toString()).toEqual("3"); + + res = float32.slice(-2); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(0, 1); + expect(res.toString()).toEqual("1"); + } + + 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"); + } + + subarray() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint16Array(buffer); + float32.set([1, 2, 3]); + expect(float32.toString()).toEqual("1,2,3,0,0,0,0,0"); + + let sub = float32.subarray(0, 4); + expect(sub.toString()).toEqual("1,2,3,0"); + } + + values() { + let arr = new Uint16Array([1, 2, 3]); + let values = arr.values(); + expect(values.next().value).toEqual(1); + + } + + arrayBufferSlice() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint16Array(buffer); + float32[3] = 42; + expect(float32.toString()).toEqual("0,0,0,42,0,0,0,0"); + + let res = buffer.slice(8); + let sliced = new Uint16Array(res); + expect(sliced[0]).toEqual(42); + } +} \ No newline at end of file diff --git a/uni_modules/uts-tests/utssdk/TUint32Array.js b/uni_modules/uts-tests/utssdk/TUint32Array.js new file mode 100644 index 0000000000000000000000000000000000000000..93258c4e687564e09f50ab3c9316516f0ca1a759 --- /dev/null +++ b/uni_modules/uts-tests/utssdk/TUint32Array.js @@ -0,0 +1,276 @@ +import { + describe, + test, + expect, + Result +} from './tests.uts' + +export class TUint32Array { + test() { + this.testfloat32(); + 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(); + } + + testfloat32() { + let float32 = new Uint32Array(2); + float32[0] = 42; + expect(float32[0]).toEqual(42); + expect(float32.length).toEqual(2); + expect(Uint32Array.BYTES_PER_ELEMENT).toEqual(4); + + let x = new Uint32Array([21, 31, 3]); + expect(x[1]).toEqual(31); + + let y = new Uint32Array(x); + expect(y[0]).toEqual(21); + + let buffer = new ArrayBuffer(16); + let z = new Uint32Array(buffer, 2, 4); + expect(z.byteOffset).toEqual(2); + expect(z.length).toEqual(4); + } + + testConstructor() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint32Array(buffer); + float32[1] = 42; + expect(float32.toString()).toEqual("0,42,0,0"); + } + + testSet() { + let float32 = new Uint32Array(8); + var array = [1, 2, 3] + float32.set(array, 1); + expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0"); + } + + testCopyWith() { + console.log("testCopyWith 1") + let float32 = new Uint32Array(8); + float32.set([1, 2, 3], 1); + console.log("testCopyWith 1") + float32.copyWithin(3, 0, 3); + console.log("testCopyWith 1") + expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0"); + } + + testEvery() { + // const isBelowThreshold = (currentValue: number, index: number, array:Uint32Array): boolean => currentValue < 40; + let result = new Uint32Array([12, 5, 8, 130, 44]).every((value: number, index: number, array: Uint32Array): + boolean => value < 40); // + expect(result).toEqual(false); + } + + testFill() { + let float32 = new Uint32Array([1, 2, 3]).fill(4); + expect(float32.toString()).toEqual("4,4,4"); + + float32 = new Uint32Array([1, 2, 3]).fill(4, 1); + expect(float32.toString()).toEqual("1,4,4"); + + float32 = new Uint32Array([1, 2, 3]).fill(4, 1, 2); + expect(float32.toString()).toEqual("1,4,3"); + + float32 = new Uint32Array([1, 2, 3]).fill(4, 1, 1); + expect(float32.toString()).toEqual("1,2,3"); + + float32 = new Uint32Array([1, 2, 3]).fill(4, -3, -2); + expect(float32.toString()).toEqual("4,2,3"); + } + + testFilter() { + // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10; + + let float32 = new Uint32Array([12, 5, 8, 44]).filter((value: number, index: number, array: Uint32Array): + boolean => value >= 10); + expect(float32.toString()).toEqual("12,44"); + } + + find() { + let float32 = new Uint32Array([4, 5, 8, 12]); + let res = float32.find((value: number, index: number, obj: Uint32Array): boolean => value > 5); + expect(res).toEqual(8); + } + + findIndex() { + let float32 = new Uint32Array([4, 6, 8, 12]); + let res = float32.findIndex((value: number, index: number, obj: Uint32Array): boolean => value > 100); + expect(res).toEqual(-1); + + let ufloat32 = new Uint32Array([4, 6, 7, 120]); + res = ufloat32.findIndex((value: number, index: number, obj: Uint32Array): boolean => value > 100); + expect(res).toEqual(3); + } + + foreach() { + new Uint32Array([0, 1, 2, 3]).forEach((value: number, index: number, array: Uint32Array) => { + console.log(`a[${index}] = ${value}`); + }); + } + + iterator() { + let arr = new Uint32Array([10, 20, 30, 40, 50]); + let entries = arr.entries(); + expect(entries.next().value[1]).toEqual(10); + + } + + includes() { + let float32 = new Uint32Array([1, 2, 3]); + let res = float32.includes(2); + expect(res).toEqual(true); + + res = float32.includes(4); + expect(res).toEqual(false); + + res = float32.includes(3, 3); + expect(res).toEqual(false); + } + + indexOf() { + let float32 = new Uint32Array([2, 5, 9]); + let res = float32.indexOf(2); + expect(res).toEqual(0); + + res = float32.indexOf(7); + expect(res).toEqual(-1); + + res = float32.indexOf(9, 2); + expect(res).toEqual(2); + + res = float32.indexOf(2, -1); + expect(res).toEqual(-1); + + res = float32.indexOf(2, -3); + expect(res).toEqual(0); + } + + join() { + let float32 = new Uint32Array([1, 2, 3]); + let res = float32.join(); + expect(res).toEqual("1,2,3"); + + res = float32.join(" / "); + expect(res).toEqual("1 / 2 / 3"); + + res = float32.join(""); + expect(res).toEqual("123"); + } + + keys() { + let arr = new Uint32Array([10, 20, 30, 40, 50]); + let keys = arr.keys(); + expect(keys.next().value).toEqual(0); + } + + map() { + let numbers = new Uint32Array([1, 4, 9]); + let doubles = numbers.map((value: number, index: number, array: Uint32Array): number => value * 2); + expect(numbers.toString()).toEqual("1,4,9"); + expect(doubles.toString()).toEqual("2,8,18"); + } + + reduce() { + let total = new Uint32Array([0, 1, 2, 3]); + let res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Uint32Array): + number => accumulator + currentValue); + expect(res).toEqual(6); + + total = new Uint32Array([0, 1, 2, 3]); + res = total.reduce((accumulator: number, currentValue: number, currentIndex: number, array: Uint32Array): + number => accumulator + currentValue, 8); + expect(res).toEqual(14); + } + + reduceRight() { + let total = new Uint32Array([0, 1, 2, 3]); + let res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: Uint32Array): + number => accumulator + currentValue); + expect(res).toEqual(6); + + total = new Uint32Array([0, 1, 2, 3]); + res = total.reduceRight((accumulator: number, currentValue: number, currentIndex: number, array: Uint32Array): + number => accumulator + currentValue, 8); + expect(res).toEqual(14); + } + + reverse() { + let float32 = new Uint32Array([1, 2, 3]); + float32.reverse(); + expect(float32.toString()).toEqual("3,2,1"); + } + + slice() { + let float32 = new Uint32Array([1, 2, 3]); + let res = float32.slice(1); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(2); + expect(res.toString()).toEqual("3"); + + res = float32.slice(-2); + expect(res.toString()).toEqual("2,3"); + + res = float32.slice(0, 1); + expect(res.toString()).toEqual("1"); + } + + sort() { + let numbers = new Uint32Array([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"); + } + + subarray() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint32Array(buffer); + float32.set([1, 2, 3]); + expect(float32.toString()).toEqual("1,2,3,0"); + + let sub = float32.subarray(0, 4); + expect(sub.toString()).toEqual("1,2,3,0"); + } + + values() { + let arr = new Uint32Array([1, 2, 3]); + let values = arr.values(); + expect(values.next().value).toEqual(1); + + } + + arrayBufferSlice() { + let buffer = new ArrayBuffer(16); + let float32 = new Uint32Array(buffer); + float32[3] = 42; + expect(float32.toString()).toEqual("0,0,0,42"); + + let res = buffer.slice(8); + let sliced = new Uint32Array(res); + expect(sliced[1]).toEqual(42); + } +} \ No newline at end of file diff --git a/uni_modules/uts-tests/utssdk/index.uts b/uni_modules/uts-tests/utssdk/index.uts index 129973f3d06d19ce8613322451eaf559fcc00409..ab5295ac534cc4ef4d30d512d68855f08f209236 100644 --- a/uni_modules/uts-tests/utssdk/index.uts +++ b/uni_modules/uts-tests/utssdk/index.uts @@ -16,7 +16,9 @@ import { testRegExp } from './RegExp.uts' import { testForLoop } from './ForLoop.uts' import { testGlobal } from './Global.uts' import { testType } from './Type.uts' -export { Result } from './tests.uts' +export { Result } from './tests.uts' +import { testArrayBuffer } from './ArrayBuffer.uts' + // Promise、Proxy、Reflect、Weakmap、WeakSet 不支持 export function runTests() : UTSJSONObject { @@ -37,7 +39,8 @@ export function runTests() : UTSJSONObject { const TypeRes = testType(); const JSONLargeRes = testJSONLarge(); const consoleRes = testConsole(); - const UTSJSONObjectRes = testUTSJSONObject(); + const UTSJSONObjectRes = testUTSJSONObject(); + const ArrayBufferRes = testArrayBuffer(); return { Array: ArrayRes, Date: DateRes, @@ -56,6 +59,7 @@ export function runTests() : UTSJSONObject { Global: GlobalRes, Type: TypeRes, console:consoleRes, - UTSJSONObject:UTSJSONObjectRes + UTSJSONObject:UTSJSONObjectRes, + ArrayBuffer:ArrayBufferRes } } \ No newline at end of file