forked from zhaojh329/lua-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwintest.lua
More file actions
151 lines (137 loc) · 5.19 KB
/
wintest.lua
File metadata and controls
151 lines (137 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
--
-- Minimalist lua-ffi examples and testing for MSVC builds
--
local ffi = require "ffi"
local lfs = require "lfs"
-- Load C standard library
local ok, libc = pcall(ffi.load, "ucrtbase")
if not ok then
ok, libc = pcall(ffi.load, "msvcrt")
end
if not ok then
error("Could not load C standard library (ucrtbase.dll or msvcrt.dll not found).")
end
-- Declaration of utilized C-functions
-- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/crt-alphabetical-function-reference
-- std lib functions
ffi.cdef([[
void srand(unsigned int seed);
int rand(void);
size_t strlen(const char *str);
int64_t _time64(int64_t *destTime);
char *_strtime(char *timestr);
]])
-- National Instruments DAQmx fubnctions (ffi.cdef may be called not just one time)
ffi.cdef([[
int32_t DAQmxGetSysNIDAQMajorVersion(uint32_t *data);
int32_t DAQmxGetSysNIDAQMinorVersion(uint32_t *data);
int32_t DAQmxGetSysNIDAQUpdateVersion(uint32_t *data);
]])
-- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/rand
print("Print 10 random numbers by calling rand() from C StdLib ...")
print(" C-declaration: void srand(unsigned int seed);")
print(" C-declaration: int rand(void);")
for i=1, 10 do
if i == 1 then
print(" Setting random seed to 12345.")
libc.srand(12345)
end
io.write(string.format(" %d",libc.rand()))
end
print("\nDone.\n")
-- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strlen-wcslen-mbslen-mbslen-l-mbstrlen-mbstrlen-l
print("Testing a call to strlen() ...")
print(" C-declaration: size_t strlen(const char *str);")
local teststr = "The quick brown fox jumps over the lazy dog."
print(string.format(" The test string is '%s'",teststr))
print(string.format(" Lua: #teststr = %d",#teststr))
print(string.format(" C: strlen(..) = %d",libc.strlen(teststr)))
print("Done.\n")
-- https://github.com/q66/cffi-lua/blob/master/tests/cast.lua
print("Testing strings are convertible to char pointers ...")
local foo = "hello world"
local foop = ffi.cast("const char *", foo)
if ffi.string(foop) == "hello world" then
print(" PASSED")
else
print(" FAILED")
end
print("Done.\n")
print("Copy Lua string to C-string using ffi.copy() ...")
-- create random printable ASCII Lua string of length strlen
local strlen <const> = 4096
local strtab = {}
for _=1, strlen do
strtab[#strtab+1] = string.char(math.random(32,126))
end
teststr = table.concat(strtab)
-- print(string.format(" teststr = %s",teststr))
-- create cstring
local cstr = ffi.new("char[]",strlen+1) -- Extra space for '\0'
-- copy
local cnt = ffi.copy(cstr,teststr)
-- print(string.format(" C-str = %s",ffi.string(cstr)))
print(string.format(" Chars cp'd = %d",cnt))
if ffi.string(cstr) == teststr then
print(" PASSED string comparison")
else
print(" FAILED string comparison")
end
print("Done.\n")
-- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strtime-wstrtime
print("Testing char[] passing to _strtime() ...")
print(" C-declaration: char *_strtime(char *timestr);")
local buf=ffi.new("char[]",9)
print(string.format(" Current time is %s",ffi.string(libc._strtime(buf))))
print("Done.\n")
-- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/time-time32-time64
print("Testing seconds elapsed since midnight (00:00:00), January 1, 1970 ...")
print(" C-declaration: int64_t _time64(time_t *destTime);")
print(string.format(" Using function return value : %d",libc._time64(ffi.nullptr)))
local t = ffi.new("int64_t")
libc._time64(ffi.addressof(t))
print(string.format(" Using destTime argument : %d",ffi.tonumber(t)))
print("Done.\n")
-- 1D-Array-Test
print("Testing uint8_t 1D-array ...")
local arrSize = 1024*1024*4
local arr=ffi.new("uint8_t[]",arrSize)
print(string.format(" Created array size is %f MB.",ffi.sizeof(arr)/1024/1024))
local compare = {}
for i=0, arrSize-1 do
local rnd=math.random(0,255) -- full uint8_t range
compare[i] = rnd
arr[i] = rnd
end
local result = true
for i=0, arrSize-1 do
-- compare
result = result and (compare[i] == arr[i])
end
if result then
print(" PASSED array content comparison")
else
print(" FAILED array content comparison")
end
print("Done.\n")
-- DLL load and usage
local dllfile <const> = "C:/Windows/System32/nicaiu.dll"
print(string.format("Testing NIDAQmx DLL access using %s ...",dllfile))
if lfs.attributes(dllfile) == nil then
-- DLL not installed
print(" DLL not installed, test skipped.")
else
print(" C-declaration: int32_t DAQmxGetSysNIDAQMajorVersion(uint32_t *data);")
print(" C-declaration: int32_t DAQmxGetSysNIDAQMinorVersion(uint32_t *data);")
print(" C-declaration: int32_t DAQmxGetSysNIDAQUpdateVersion(uint32_t *data);")
local dll = ffi.load(dllfile)
local arg = ffi.new("uint32_t")
dll.DAQmxGetSysNIDAQMajorVersion(ffi.addressof(arg))
print(string.format(" DAQmxGetSysNIDAQMajorVersion = %d",ffi.tonumber(arg)))
dll.DAQmxGetSysNIDAQMinorVersion(ffi.addressof(arg))
print(string.format(" DAQmxGetSysNIDAQMinorVersion = %d",ffi.tonumber(arg)))
dll.DAQmxGetSysNIDAQUpdateVersion(ffi.addressof(arg))
print(string.format(" DAQmxGetSysNIDAQUpdateVersion = %d",ffi.tonumber(arg)))
end
print("Done.\n")
print("All tests PASSED.")