|
| 1 | +module TestCoreFunctionUtils |
| 2 | + |
| 3 | +import Test |
| 4 | +import CTBase.Core |
| 5 | + |
| 6 | +const VERBOSE = isdefined(Main, :TestOptions) ? Main.TestOptions.VERBOSE : true |
| 7 | +const SHOWTIMING = isdefined(Main, :TestOptions) ? Main.TestOptions.SHOWTIMING : true |
| 8 | + |
| 9 | +function test_function_utils() |
| 10 | + Test.@testset verbose = VERBOSE showtiming = SHOWTIMING "to_out_of_place" begin |
| 11 | + |
| 12 | + Test.@testset "to_out_of_place - basic conversion" begin |
| 13 | + f!(r, x) = (r[1] = sin(x); r[2] = cos(x)) |
| 14 | + f = Core.to_out_of_place(f!, 2) |
| 15 | + result = f(π / 4) |
| 16 | + Test.@test result isa Vector |
| 17 | + Test.@test length(result) == 2 |
| 18 | + Test.@test result[1] ≈ sin(π / 4) |
| 19 | + Test.@test result[2] ≈ cos(π / 4) |
| 20 | + end |
| 21 | + |
| 22 | + Test.@testset "to_out_of_place - scalar output (n=1)" begin |
| 23 | + g!(r, x) = (r[1] = x^2) |
| 24 | + g = Core.to_out_of_place(g!, 1) |
| 25 | + result = g(3.0) |
| 26 | + Test.@test result isa Float64 |
| 27 | + Test.@test result ≈ 9.0 |
| 28 | + end |
| 29 | + |
| 30 | + Test.@testset "to_out_of_place - with kwargs" begin |
| 31 | + h!(r, x; scale=1.0) = (r[1] = x * scale; r[2] = x^2 * scale) |
| 32 | + h = Core.to_out_of_place(h!, 2) |
| 33 | + Test.@test h(2.0)[1] ≈ 2.0 |
| 34 | + Test.@test h(2.0)[2] ≈ 4.0 |
| 35 | + Test.@test h(2.0; scale=3.0)[1] ≈ 6.0 |
| 36 | + Test.@test h(2.0; scale=3.0)[2] ≈ 12.0 |
| 37 | + end |
| 38 | + |
| 39 | + Test.@testset "to_out_of_place - multiple arguments" begin |
| 40 | + k!(r, x, y) = (r[1] = x + y; r[2] = x * y) |
| 41 | + k = Core.to_out_of_place(k!, 2) |
| 42 | + result = k(3.0, 4.0) |
| 43 | + Test.@test result[1] ≈ 7.0 |
| 44 | + Test.@test result[2] ≈ 12.0 |
| 45 | + end |
| 46 | + |
| 47 | + Test.@testset "to_out_of_place - custom type" begin |
| 48 | + m!(r, x) = (r[1] = x + 1; r[2] = x + 2) |
| 49 | + m = Core.to_out_of_place(m!, 2; T=Int) |
| 50 | + result = m(5) |
| 51 | + Test.@test result isa Vector{Int} |
| 52 | + Test.@test result[1] == 6 |
| 53 | + Test.@test result[2] == 7 |
| 54 | + end |
| 55 | + |
| 56 | + Test.@testset "to_out_of_place - nothing input" begin |
| 57 | + Test.@test Core.to_out_of_place(nothing, 2) === nothing |
| 58 | + end |
| 59 | + |
| 60 | + Test.@testset "to_out_of_place - larger output" begin |
| 61 | + big!(r, x) = (for i in 1:5; r[i] = x * i; end) |
| 62 | + big = Core.to_out_of_place(big!, 5) |
| 63 | + result = big(2.0) |
| 64 | + Test.@test length(result) == 5 |
| 65 | + Test.@test result == [2.0, 4.0, 6.0, 8.0, 10.0] |
| 66 | + end |
| 67 | + |
| 68 | + end |
| 69 | + return nothing |
| 70 | +end |
| 71 | + |
| 72 | +end # module TestCoreFunctionUtils |
| 73 | + |
| 74 | +test_function_utils() = TestCoreFunctionUtils.test_function_utils() |
0 commit comments