Skip to content

Commit

Permalink
chore: create tests folder
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Dec 14, 2024
1 parent a7c4fc8 commit ef2c51d
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 162 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
165 changes: 5 additions & 160 deletions test.ts → tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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({
Expand Down
60 changes: 60 additions & 0 deletions tests/struct.ts
Original file line number Diff line number Diff line change
@@ -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])
}
108 changes: 108 additions & 0 deletions tests/types.ts
Original file line number Diff line number Diff line change
@@ -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,
}),
};
4 changes: 4 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const logGreen = (text) => {
if (process.env.SILENT) return
console.log('\x1b[32m%s\x1b[0m', text);
}

0 comments on commit ef2c51d

Please sign in to comment.