diff --git a/README.md b/README.md index 44ab5c9..cacb824 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ Note: You need to make sure that the compilation environment of the dynamic libr ## Usage -View [test.ts](./test.ts) for the latest usage +View [tests/index.ts](./tests/index.ts) for the latest usage Here is an example of how to use ffi-rs: diff --git a/package.json b/package.json index f2805ef..4438f89 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "build:dev": "env=development node scripts/build.js", "build": "node scripts/build.js", "publish:npm": "node scripts/publish.js", - "test": "esno ./test.ts", + "test": "esno ./tests/index.ts", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add . && git commit -m \"docs: update changelog.md\" && git push origin master", "pub": "npm version patch && git push origin master --tags && npm run changelog", "pub:alpha": "npm version prerelease --preid=alpha && git push origin master --tags" diff --git a/test.ts b/tests/index.ts similarity index 72% rename from test.ts rename to tests/index.ts index f5612fe..9f5acc1 100644 --- a/test.ts +++ b/tests/index.ts @@ -14,15 +14,13 @@ import { define, PointerType, isNullPointer -} from "./index" - +} from "../index" +import { logGreen } from "./utils" +import { testObject } from "./struct" +import { person, personType } from "./types" const platform = process.platform; const dynamicLib = platform === "win32" ? "./sum.dll" : "./libsum.so"; -const logGreen = (text) => { - if (process.env.SILENT) return - console.log('\x1b[32m%s\x1b[0m', text); -} open({ library: "libsum", @@ -278,160 +276,7 @@ const testPointer = () => { equal(isNullPointer(unwrapPointer(rsNullPointer)[0]), true) logGreen('test null pointer success') } -const parent = { - age: 43, - doubleArray: [1.1, 2.2, 3.3], - parent: {}, - doubleProps: 3.3, - name: "tom father", - stringArray: ["tom", "father"], - i32Array: [5, 6, 7], - staticBytes: Buffer.from(new Array(16).fill(88)), - boolTrue: true, - boolFalse: false, - longVal: 5294967296, - stackStruct: { - age: 22, - }, - byte: 66, - byteArray: Buffer.from([103, 104]), -}; -const person = { - age: 23, - doubleArray: [1.1, 2.2, 3.3], - parent, - doubleProps: 1.1, - name: "tom", - stringArray: ["tom"], - i32Array: [1, 2, 3, 4], - staticBytes: Buffer.from(new Array(16).fill(99)), - boolTrue: true, - boolFalse: false, - longVal: 4294967296, - stackStruct: { - age: 16 - }, - byte: 65, - byteArray: Buffer.from([101, 102]), -}; -const parentType = { - age: DataType.I32, - doubleArray: arrayConstructor({ - type: DataType.DoubleArray, - length: parent.doubleArray.length, - }), - parent: {}, - doubleProps: DataType.Double, - name: DataType.String, - stringArray: arrayConstructor({ - type: DataType.StringArray, - length: parent.stringArray.length, - }), - i32Array: arrayConstructor({ - type: DataType.I32Array, - length: parent.i32Array.length, - }), - staticBytes: arrayConstructor({ - type: DataType.U8Array, - length: parent.staticBytes.length, - ffiTypeTag: DataType.StackArray - }), - boolTrue: DataType.Boolean, - boolFalse: DataType.Boolean, - longVal: DataType.I64, - stackStruct: { - age: DataType.I32, - ffiTypeTag: DataType.StackStruct, - }, - byte: DataType.U8, - byteArray: arrayConstructor({ - type: DataType.U8Array, - length: parent.byteArray.length, - }), -}; -const personType = { - age: DataType.I32, - doubleArray: arrayConstructor({ - type: DataType.DoubleArray, - length: person.doubleArray.length, - }), - parent: parentType, - doubleProps: DataType.Double, - name: DataType.String, - stringArray: arrayConstructor({ - type: DataType.StringArray, - length: person.stringArray.length, - }), - i32Array: arrayConstructor({ - type: DataType.I32Array, - length: person.i32Array.length, - }), - staticBytes: arrayConstructor({ - type: DataType.U8Array, - length: person.staticBytes.length, - ffiTypeTag: DataType.StackArray - }), - boolTrue: DataType.Boolean, - boolFalse: DataType.Boolean, - longVal: DataType.I64, - stackStruct: { - ffiTypeTag: DataType.StackStruct, - age: DataType.I32, - }, - byte: DataType.U8, - byteArray: arrayConstructor({ - type: DataType.U8Array, - length: person.byteArray.length, - }), -}; -const testObject = () => { - const personObj = load({ - library: "libsum", - funcName: "getStruct", - retType: personType, - paramsType: [ - personType - ], - paramsValue: [person], - freeResultMemory: false - }); - deepStrictEqual(person, personObj); - logGreen('test getStruct succeed') - const createdPerson = load({ - library: "libsum", - funcName: "createPerson", - retType: personType, - paramsType: [], - paramsValue: [], - }); - deepStrictEqual(createdPerson, person); - logGreen('test createdPerson succeed') - let personPointer = createPointer({ - paramsType: [personType], - paramsValue: [person] - }) - const personObjByPointer = load({ - library: "libsum", - funcName: "getStruct", - retType: personType, - paramsType: [ - DataType.External - ], - freeResultMemory: false, - paramsValue: unwrapPointer(personPointer), - }); - deepStrictEqual(person, personObjByPointer); - logGreen('test getStructByPointer succeed') - personPointer = createPointer({ - paramsType: [personType], - paramsValue: [person] - }) - const restorePersonObjByPointer = restorePointer({ - paramsValue: personPointer, - retType: [personType] - }) - deepStrictEqual(person, restorePersonObjByPointer[0]) -} + const testRunInNewThread = () => { load({ diff --git a/tests/struct.ts b/tests/struct.ts new file mode 100644 index 0000000..dc57312 --- /dev/null +++ b/tests/struct.ts @@ -0,0 +1,60 @@ +import { deepStrictEqual } from "assert" +import { + load, + DataType, + arrayConstructor, + createPointer, + restorePointer, + unwrapPointer, +} from "../index" +import { logGreen } from "./utils" +import { person, personType } from "./types" + +export const testObject = () => { + const personObj = load({ + library: "libsum", + funcName: "getStruct", + retType: personType, + paramsType: [ + personType + ], + paramsValue: [person], + freeResultMemory: false + }); + deepStrictEqual(person, personObj); + logGreen('test getStruct succeed') + const createdPerson = load({ + library: "libsum", + funcName: "createPerson", + retType: personType, + paramsType: [], + paramsValue: [], + }); + deepStrictEqual(createdPerson, person); + logGreen('test createdPerson succeed') + let personPointer = createPointer({ + paramsType: [personType], + paramsValue: [person] + }) + const personObjByPointer = load({ + library: "libsum", + funcName: "getStruct", + retType: personType, + paramsType: [ + DataType.External + ], + freeResultMemory: false, + paramsValue: unwrapPointer(personPointer), + }); + deepStrictEqual(person, personObjByPointer); + logGreen('test getStructByPointer succeed') + personPointer = createPointer({ + paramsType: [personType], + paramsValue: [person] + }) + const restorePersonObjByPointer = restorePointer({ + paramsValue: personPointer, + retType: [personType] + }) + deepStrictEqual(person, restorePersonObjByPointer[0]) +} \ No newline at end of file diff --git a/tests/types.ts b/tests/types.ts new file mode 100644 index 0000000..8218578 --- /dev/null +++ b/tests/types.ts @@ -0,0 +1,108 @@ +import { DataType, arrayConstructor } from "../index" + +export const parent = { + age: 43, + doubleArray: [1.1, 2.2, 3.3], + parent: {}, + doubleProps: 3.3, + name: "tom father", + stringArray: ["tom", "father"], + i32Array: [5, 6, 7], + staticBytes: Buffer.from(new Array(16).fill(88)), + boolTrue: true, + boolFalse: false, + longVal: 5294967296, + stackStruct: { + age: 22, + }, + byte: 66, + byteArray: Buffer.from([103, 104]), +}; +export const person = { + age: 23, + doubleArray: [1.1, 2.2, 3.3], + parent, + doubleProps: 1.1, + name: "tom", + stringArray: ["tom"], + i32Array: [1, 2, 3, 4], + staticBytes: Buffer.from(new Array(16).fill(99)), + boolTrue: true, + boolFalse: false, + longVal: 4294967296, + stackStruct: { + age: 16 + }, + byte: 65, + byteArray: Buffer.from([101, 102]), +}; +export const parentType = { + age: DataType.I32, + doubleArray: arrayConstructor({ + type: DataType.DoubleArray, + length: parent.doubleArray.length, + }), + parent: {}, + doubleProps: DataType.Double, + name: DataType.String, + stringArray: arrayConstructor({ + type: DataType.StringArray, + length: parent.stringArray.length, + }), + i32Array: arrayConstructor({ + type: DataType.I32Array, + length: parent.i32Array.length, + }), + staticBytes: arrayConstructor({ + type: DataType.U8Array, + length: parent.staticBytes.length, + ffiTypeTag: DataType.StackArray + }), + boolTrue: DataType.Boolean, + boolFalse: DataType.Boolean, + longVal: DataType.I64, + stackStruct: { + age: DataType.I32, + ffiTypeTag: DataType.StackStruct, + }, + byte: DataType.U8, + byteArray: arrayConstructor({ + type: DataType.U8Array, + length: parent.byteArray.length, + }), +}; +export const personType = { + age: DataType.I32, + doubleArray: arrayConstructor({ + type: DataType.DoubleArray, + length: person.doubleArray.length, + }), + parent: parentType, + doubleProps: DataType.Double, + name: DataType.String, + stringArray: arrayConstructor({ + type: DataType.StringArray, + length: person.stringArray.length, + }), + i32Array: arrayConstructor({ + type: DataType.I32Array, + length: person.i32Array.length, + }), + staticBytes: arrayConstructor({ + type: DataType.U8Array, + length: person.staticBytes.length, + ffiTypeTag: DataType.StackArray + }), + boolTrue: DataType.Boolean, + boolFalse: DataType.Boolean, + longVal: DataType.I64, + stackStruct: { + ffiTypeTag: DataType.StackStruct, + age: DataType.I32, + }, + byte: DataType.U8, + byteArray: arrayConstructor({ + type: DataType.U8Array, + length: person.byteArray.length, + }), +}; \ No newline at end of file diff --git a/tests/utils.ts b/tests/utils.ts new file mode 100644 index 0000000..46abef4 --- /dev/null +++ b/tests/utils.ts @@ -0,0 +1,4 @@ +export const logGreen = (text) => { + if (process.env.SILENT) return + console.log('\x1b[32m%s\x1b[0m', text); +}