@@ -948,3 +948,39 @@ func Test_reg_executing_and_recording()
948948 delfunc s: save_reg_stat
949949 unlet s: reg_stat
950950endfunc
951+
952+ func Test_libcall_libcallnr ()
953+ if ! has (' libcall' )
954+ return
955+ endif
956+
957+ if has (' win32' )
958+ let libc = ' msvcrt.dll'
959+ elseif has (' mac' )
960+ let libc = ' libSystem.B.dylib'
961+ else
962+ " On Unix, libc.so can be in various places.
963+ " Interestingly, using an empty string for the 1st argument of libcall
964+ " allows to call functions from libc which is not documented.
965+ let libc = ' '
966+ endif
967+
968+ if has (' win32' )
969+ call assert_equal ($USERPROFILE , libcall (libc, ' getenv' , ' USERPROFILE' ))
970+ else
971+ call assert_equal ($HOME , libcall (libc, ' getenv' , ' HOME' ))
972+ endif
973+
974+ " If function returns NULL, libcall() should return an empty string.
975+ call assert_equal (' ' , libcall (libc, ' getenv' , ' X_ENV_DOES_NOT_EXIT' ))
976+
977+ " Test libcallnr() with string and integer argument.
978+ call assert_equal (4 , libcallnr (libc, ' strlen' , ' abcd' ))
979+ call assert_equal (char2nr (' A' ), libcallnr (libc, ' toupper' , char2nr (' a' )))
980+
981+ call assert_fails (" call libcall(libc, 'Xdoesnotexist_', '')" , ' E364:' )
982+ call assert_fails (" call libcallnr(libc, 'Xdoesnotexist_', '')" , ' E364:' )
983+
984+ call assert_fails (" call libcall('Xdoesnotexist_', 'getenv', 'HOME')" , ' E364:' )
985+ call assert_fails (" call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')" , ' E364:' )
986+ endfunc
0 commit comments