Skip to content

Commit bc3fc37

Browse files
test: Fix libc-related tests in wintest.lua
- Do not call libc-functions from export list of lua.exe interpreter, which makes problems with interpreter binaries compiled with /MT option - Instead explicitly load required DLL from Windows OS - Also added a PASSED statement in basetype.lua to indicate a result to the user
1 parent 3e88768 commit bc3fc37

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

tests/basetype.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,5 @@ assert(tostring(ffi.typeof('time_t')) == 'ctype<time_t>')
109109

110110
assert(tostring(ffi.typeof('struct ComplexStruct')) == 'ctype<struct ComplexStruct>')
111111
assert(tostring(ffi.typeof('Point')) == 'ctype<struct Point>')
112+
113+
print("All tests PASSED.")

tests/wintest.lua

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44
local ffi = require "ffi"
55
local lfs = require "lfs"
66

7+
-- Load C standard library
8+
local ok, libc = pcall(ffi.load, "ucrtbase")
9+
if not ok then
10+
ok, libc = pcall(ffi.load, "msvcrt")
11+
end
12+
if not ok then
13+
error("Could not load C standard library (ucrtbase.dll or msvcrt.dll not found).")
14+
end
15+
716
-- Declaration of utilized C-functions
817
-- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/crt-alphabetical-function-reference
918
-- std lib functions
1019
ffi.cdef([[
1120
void srand(unsigned int seed);
1221
int rand(void);
1322
size_t strlen(const char *str);
23+
int64_t _time64(int64_t *destTime);
1424
char *_strtime(char *timestr);
15-
time_t time(time_t *destTime);
1625
]])
1726
-- National Instruments DAQmx fubnctions (ffi.cdef may be called not just one time)
1827
ffi.cdef([[
@@ -28,9 +37,9 @@ print(" C-declaration: int rand(void);")
2837
for i=1, 10 do
2938
if i == 1 then
3039
print(" Setting random seed to 12345.")
31-
ffi.C.srand(12345)
40+
libc.srand(12345)
3241
end
33-
io.write(string.format(" %d",ffi.C.rand()))
42+
io.write(string.format(" %d",libc.rand()))
3443
end
3544
print("\nDone.\n")
3645

@@ -40,7 +49,7 @@ print(" C-declaration: size_t strlen(const char *str);")
4049
local teststr = "The quick brown fox jumps over the lazy dog."
4150
print(string.format(" The test string is '%s'",teststr))
4251
print(string.format(" Lua: #teststr = %d",#teststr))
43-
print(string.format(" C: strlen(..) = %d",ffi.C.strlen(teststr)))
52+
print(string.format(" C: strlen(..) = %d",libc.strlen(teststr)))
4453
print("Done.\n")
4554

4655
-- https://github.com/q66/cffi-lua/blob/master/tests/cast.lua
@@ -82,15 +91,15 @@ print("Done.\n")
8291
print("Testing char[] passing to _strtime() ...")
8392
print(" C-declaration: char *_strtime(char *timestr);")
8493
local buf=ffi.new("char[]",9)
85-
print(string.format(" Current time is %s",ffi.string(ffi.C._strtime(buf))))
94+
print(string.format(" Current time is %s",ffi.string(libc._strtime(buf))))
8695
print("Done.\n")
8796

8897
-- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/time-time32-time64
8998
print("Testing seconds elapsed since midnight (00:00:00), January 1, 1970 ...")
90-
print(" C-declaration: time_t time(time_t *destTime);")
91-
print(string.format(" Using function return value : %d",ffi.C.time(ffi.nullptr)))
92-
local t = ffi.new("time_t")
93-
ffi.C.time(ffi.addressof(t))
99+
print(" C-declaration: int64_t _time64(time_t *destTime);")
100+
print(string.format(" Using function return value : %d",libc._time64(ffi.nullptr)))
101+
local t = ffi.new("int64_t")
102+
libc._time64(ffi.addressof(t))
94103
print(string.format(" Using destTime argument : %d",ffi.tonumber(t)))
95104
print("Done.\n")
96105

@@ -138,3 +147,5 @@ else
138147
print(string.format(" DAQmxGetSysNIDAQUpdateVersion = %d",ffi.tonumber(arg)))
139148
end
140149
print("Done.\n")
150+
151+
print("All tests PASSED.")

0 commit comments

Comments
 (0)