forked from tshort/StaticCompiler.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtests.jl
130 lines (105 loc) · 3.91 KB
/
runtests.jl
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
using StaticCompiler
using Test
using Libdl
using LinearAlgebra
@testset "Basics" begin
simple_sum(x) = x + one(typeof(x))
# This probably needs a macro
@test ccall(generate_shlib_fptr(simple_sum, (Int,)), Int, (Int,), 1) == Int(2)
@test ccall(generate_shlib_fptr(simple_sum, (Float64,)), Float64, (Float64 ,), 1) == Float64(2)
@test ccall(generate_shlib_fptr(simple_sum, (Int32,)), Int32, (Int32,), 1) == Int32(2)
@test ccall(generate_shlib_fptr(simple_sum, (Float32,)), Float32, (Float32 ,), 1) == Float16(2)
@test ccall(generate_shlib_fptr(simple_sum, (Int16,)), Int16, (Int16,), 1) == Int16(2)
@test ccall(generate_shlib_fptr(simple_sum, (Float16,)), Float16, (Float16 ,), 1) == Float16(2)
end
fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2) # for some reason, if this is defined in the testset, it segfaults
@testset "Recursion" begin
# This works on the REPL but fails here
fib_ptr = generate_shlib_fptr(fib, (Int,))
@test @ccall( $fib_ptr(10::Int) :: Int ) == 55
end
# Call binaries for testing
# @testset "Generate binary" begin
# fib(n) = n <= 1 ? n : fib(n - 1) + fib(n - 2)
# libname = tempname()
# generate_shlib(fib, (Int,), libname)
# ptr = Libdl.dlopen(libname * "." * Libdl.dlext, Libdl.RTLD_LOCAL)
# fptr = Libdl.dlsym(ptr, "julia_fib")
# @assert fptr != C_NULL
# # This works on REPL
# @test_skip ccall(fptr, Int, (Int,), 10) == 55
# end
@testset "Loops" begin
function sum_first_N_int(N)
s = 0
for a in 1:N
s += a
end
s
end
@test ccall(generate_shlib_fptr(sum_first_N_int, (Int,)), Int, (Int,), 10) == 55
function sum_first_N_float64(N)
s = Float64(0)
for a in 1:N
s += Float64(a)
end
s
end
@test ccall(generate_shlib_fptr(sum_first_N_float64, (Int,)), Float64, (Int,), 10) == 55.
function sum_first_N_int_inbounds(N)
s = 0
@inbounds for a in 1:N
s += a
end
s
end
@test ccall(generate_shlib_fptr(sum_first_N_int_inbounds, (Int,)), Int, (Int,), 10) == 55
function sum_first_N_float64_inbounds(N)
s = Float64(0)
@inbounds for a in 1:N
s += Float64(a)
end
s
end
@test ccall(generate_shlib_fptr(sum_first_N_float64_inbounds, (Int,)), Float64, (Int,), 10) == 55.
end
# Arrays with different input types Int32, Int64, Float32, Float64, Complex?
@testset "Arrays" begin
function array_sum(n, A)
s = zero(eltype(A))
for i in 1:n
s += A[i]
end
s
end
array_sum_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Int}})
@test ( @ccall $array_sum_ptr(10::Int, collect(1:10)::Vector{Int})::Int ) == 55
# this will segfault on my machine if I use 64 bit complex numbers!
array_sum_complex_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Complex{Float32}}})
@test ( @ccall $array_sum_complex_ptr(2::Int, [1f0+im, 1f0-im]::Vector{Complex{Float32}})::Complex{Float32} ) ≈ 2.0
#This will segfault
array_sum_complex64_ptr = generate_shlib_fptr(array_sum, Tuple{Int, Vector{Complex{Float64}}})
@test_skip ( @ccall $array_sum_complex_ptr(2::Int, [1.0+im, 1.0-im]::Vector{Complex{Float64}})::Complex{Float64} ) ≈ 2.0
end
# Just to call external libraries
@testset "BLAS" begin
function mydot(a::Vector{Float64})
N = length(a)
BLAS.dot(N, a, 1, a, 1)
end
a = [1.0, 2.0]
mydot_ptr = generate_shlib_fptr(mydot, Tuple{Vector{Float64}})
@test @ccall( $mydot_ptr(a::Vector{Float64})::Float64 ) == 5.0
end
@testset "Hello World" begin
function hello(N)
println("Hello World $N")
N
end
# How do I test this?
# Also ... this segfaults
@test_skip ccall(generate_shlib_fptr(hello, (Int,)), Int, (Int,), 1) == 1
end
# data structures, dictionaries, tuples, named tuples
# passing pointers?
# @inbounds LoopVectorization