|
| 1 | +using FunctionWrappersWrappers |
| 2 | +using Test |
| 3 | + |
| 4 | +@testset "FunctionWrappersWrappers.jl" begin |
| 5 | + fwplus = FunctionWrappersWrapper( |
| 6 | + +, (Tuple{Float64, Float64}, Tuple{Int, Int}), ( |
| 7 | + Float64, Int, |
| 8 | + ) |
| 9 | + ) |
| 10 | + @test fwplus(4.0, 8.0) === 12.0 |
| 11 | + @test fwplus(4, 8) === 12 |
| 12 | + |
| 13 | + fwexp2 = FunctionWrappersWrapper( |
| 14 | + exp2, (Tuple{Float64}, Tuple{Float32}, Tuple{Int}), (Float64, Float32, Float64) |
| 15 | + ) |
| 16 | + @test fwexp2(4.0) === 16.0 |
| 17 | + @test fwexp2(4.0f0) === 16.0f0 |
| 18 | + @test fwexp2(4) === 16.0 |
| 19 | +end |
| 20 | + |
| 21 | +@testset "Type inference" begin |
| 22 | + # Test return type inference |
| 23 | + fwplus = FunctionWrappersWrapper( |
| 24 | + +, (Tuple{Float64, Float64}, Tuple{Int, Int}), ( |
| 25 | + Float64, Int, |
| 26 | + ) |
| 27 | + ) |
| 28 | + @test @inferred(fwplus(4.0, 8.0)) === 12.0 |
| 29 | + @test @inferred(fwplus(4, 8)) === 12 |
| 30 | + |
| 31 | + fwexp2 = FunctionWrappersWrapper( |
| 32 | + exp2, (Tuple{Float64}, Tuple{Float32}, Tuple{Int}), (Float64, Float32, Float64) |
| 33 | + ) |
| 34 | + @test @inferred(fwexp2(4.0)) === 16.0 |
| 35 | + @test @inferred(fwexp2(4.0f0)) === 16.0f0 |
| 36 | + @test @inferred(fwexp2(4)) === 16.0 |
| 37 | +end |
| 38 | + |
| 39 | +@testset "Introspection functions" begin |
| 40 | + # Test with a simple function |
| 41 | + fwsin = FunctionWrappersWrapper(sin, (Tuple{Float64},), (Float64,)) |
| 42 | + |
| 43 | + @testset "unwrap" begin |
| 44 | + f = unwrap(fwsin) |
| 45 | + @test f === sin |
| 46 | + @test f(0.5) == sin(0.5) |
| 47 | + end |
| 48 | + |
| 49 | + @testset "wrapped_signatures" begin |
| 50 | + sigs = wrapped_signatures(fwsin) |
| 51 | + @test sigs == (Tuple{Float64},) |
| 52 | + end |
| 53 | + |
| 54 | + @testset "wrapped_return_types" begin |
| 55 | + rets = wrapped_return_types(fwsin) |
| 56 | + @test rets == (Float64,) |
| 57 | + end |
| 58 | + |
| 59 | + # Test with multiple signatures |
| 60 | + fwplus = FunctionWrappersWrapper( |
| 61 | + +, (Tuple{Float64, Float64}, Tuple{Int, Int}), ( |
| 62 | + Float64, Int, |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + @testset "unwrap with multiple signatures" begin |
| 67 | + f = unwrap(fwplus) |
| 68 | + @test f === + |
| 69 | + @test f(1, 2) == 3 |
| 70 | + end |
| 71 | + |
| 72 | + @testset "wrapped_signatures with multiple signatures" begin |
| 73 | + sigs = wrapped_signatures(fwplus) |
| 74 | + @test sigs == (Tuple{Float64, Float64}, Tuple{Int, Int}) |
| 75 | + end |
| 76 | + |
| 77 | + @testset "wrapped_return_types with multiple signatures" begin |
| 78 | + rets = wrapped_return_types(fwplus) |
| 79 | + @test rets == (Float64, Int) |
| 80 | + end |
| 81 | + |
| 82 | + # Test with a custom function |
| 83 | + my_func(x) = x^2 |
| 84 | + fwcustom = FunctionWrappersWrapper( |
| 85 | + my_func, (Tuple{Float64}, Tuple{Int}), ( |
| 86 | + Float64, Int, |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + @testset "unwrap with custom function" begin |
| 91 | + f = unwrap(fwcustom) |
| 92 | + @test f === my_func |
| 93 | + @test f(3) == 9 |
| 94 | + @test f(2.5) == 6.25 |
| 95 | + end |
| 96 | +end |
0 commit comments