Skip to content

Commit

Permalink
Make sure the object is still alive during unsafe_load in ccall tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Jul 16, 2015
1 parent 97f62a9 commit 300413b
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions test/ccall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,41 @@ ccall_test_func(x) = ccall((:testUcharX, "./libccalltest"), Int32, (UInt8,), x %

# Test for proper round-trip of Ref{T} type
ccall_echo_func{T,U}(x, ::Type{T}, ::Type{U}) = ccall((:test_echo_p, "./libccalltest"), T, (U,), x)
# Make sure object x is still valid (rooted as argument)
# when loading the pointer. This works as long as we still keep the argument
# rooted but might fail if we are smarter about eliminating dead root.
ccall_echo_load{T,U}(x, ::Type{T}, ::Type{U}) =
unsafe_load(ccall_echo_func(x, T, U))
ccall_echo_objref{T,U}(x, ::Type{T}, ::Type{U}) =
unsafe_pointer_to_objref(ccall_echo_func(x, Ptr{T}, U))
type IntLike
x::Int
end
@test unsafe_load(ccall_echo_func(132, Ptr{Int}, Ref{Int})) === 132
@test unsafe_load(ccall_echo_func(Ref(921), Ptr{Int}, Ref{Int})) === 921
@test unsafe_load(ccall_echo_func(IntLike(993), Ptr{Int}, Ref{IntLike})) === 993
@test unsafe_load(ccall_echo_func(IntLike(881), Ptr{IntLike}, Ref{IntLike})).x === 881
@test ccall_echo_load(132, Ptr{Int}, Ref{Int}) === 132
@test ccall_echo_load(Ref(921), Ptr{Int}, Ref{Int}) === 921
@test ccall_echo_load(IntLike(993), Ptr{Int}, Ref{IntLike}) === 993
@test ccall_echo_load(IntLike(881), Ptr{IntLike}, Ref{IntLike}).x === 881
@test ccall_echo_func(532, Int, Int) === 532
if WORD_SIZE == 64
# this test is valid only for x86_64 and win64
@test ccall_echo_func(164, IntLike, Int).x === 164
end
@test ccall_echo_func(IntLike(828), Int, IntLike) === 828
@test ccall_echo_func(913, Any, Any) === 913
@test unsafe_pointer_to_objref(ccall_echo_func(553, Ptr{Any}, Any)) === 553
@test ccall_echo_objref(553, Ptr{Any}, Any) === 553
@test ccall_echo_func(124, Ref{Int}, Any) === 124
@test unsafe_load(ccall_echo_func(422, Ptr{Any}, Ref{Any})) === 422
@test unsafe_load(ccall_echo_func([383], Ptr{Int}, Ref{Int})) === 383
@test unsafe_load(ccall_echo_func(Ref([144,172],2), Ptr{Int}, Ref{Int})) === 172
#@test unsafe_load(ccall_echo_func(Ref([8],1,1), Ptr{Int}, Ref{Int})) === 8
@test ccall_echo_load(422, Ptr{Any}, Ref{Any}) === 422
@test ccall_echo_load([383], Ptr{Int}, Ref{Int}) === 383
@test ccall_echo_load(Ref([144,172],2), Ptr{Int}, Ref{Int}) === 172
# @test ccall_echo_load(Ref([8],1,1), Ptr{Int}, Ref{Int}) === 8

# Tests for passing and returning structs
ci = 20+51im
b = ccall((:ctest, "./libccalltest"), Complex{Int}, (Complex{Int},), ci)
@test b == ci + 1 - 2im
b = unsafe_load(ccall((:cptest, "./libccalltest"), Ptr{Complex{Int}}, (Ptr{Complex{Int}},), [ci]))
ci_ary = [ci] # Make sure the array is alive during unsafe_load
b = unsafe_load(ccall((:cptest, "./libccalltest"), Ptr{Complex{Int}},
(Ptr{Complex{Int}},), ci_ary))
@test b == ci + 1 - 2im
@test ci == 20+51im

Expand All @@ -47,14 +56,16 @@ Libc.free(convert(Ptr{Void},b))
cf64 = 2.84+5.2im
b = ccall((:cgtest, "./libccalltest"), Complex128, (Complex128,), cf64)
@test b == cf64 + 1 - 2im
b = unsafe_load(ccall((:cgptest, "./libccalltest"), Ptr{Complex128}, (Ptr{Complex128},), [cf64]))
cf64_ary = [cf64] # Make sure the array is alive during unsafe_load
b = unsafe_load(ccall((:cgptest, "./libccalltest"), Ptr{Complex128}, (Ptr{Complex128},), cf64_ary))
@test b == cf64 + 1 - 2im
@test cf64 == 2.84+5.2im

cf32 = 3.34f0+53.2f0im
b = ccall((:cftest, "./libccalltest"), Complex64, (Complex64,), cf32)
@test b == cf32 + 1 - 2im
b = unsafe_load(ccall((:cfptest, "./libccalltest"), Ptr{Complex64}, (Ptr{Complex64},), [cf32]))
cf32_ary = [cf32] # Make sure the array is alive during unsafe_load
b = unsafe_load(ccall((:cfptest, "./libccalltest"), Ptr{Complex64}, (Ptr{Complex64},), cf32_ary))
@test b == cf32 + 1 - 2im
@test cf32 == 3.34f0+53.2f0im

Expand Down

0 comments on commit 300413b

Please sign in to comment.