|
| 1 | +# Interface compatibility tests for MATLABDiffEq.jl |
| 2 | +# These tests verify type checking and interface compliance |
| 3 | + |
| 4 | +using Test |
| 5 | + |
| 6 | +@testset "Interface Compatibility" begin |
| 7 | + @testset "MATLAB type compatibility checks" begin |
| 8 | + # Test _is_matlab_compatible_eltype function |
| 9 | + @test MATLABDiffEq._is_matlab_compatible_eltype(Float64) == true |
| 10 | + @test MATLABDiffEq._is_matlab_compatible_eltype(Float32) == false |
| 11 | + @test MATLABDiffEq._is_matlab_compatible_eltype(Int64) == true |
| 12 | + @test MATLABDiffEq._is_matlab_compatible_eltype(Int32) == true |
| 13 | + @test MATLABDiffEq._is_matlab_compatible_eltype(Complex{Float64}) == true |
| 14 | + @test MATLABDiffEq._is_matlab_compatible_eltype(Complex{Float32}) == false |
| 15 | + @test MATLABDiffEq._is_matlab_compatible_eltype(BigFloat) == false |
| 16 | + @test MATLABDiffEq._is_matlab_compatible_eltype(BigInt) == false |
| 17 | + @test MATLABDiffEq._is_matlab_compatible_eltype(Rational{Int}) == false |
| 18 | + end |
| 19 | + |
| 20 | + @testset "_check_matlab_compatible validation" begin |
| 21 | + # Valid Float64 inputs should pass |
| 22 | + @test MATLABDiffEq._check_matlab_compatible([1.0, 2.0], (0.0, 1.0)) === nothing |
| 23 | + @test MATLABDiffEq._check_matlab_compatible(1.0, (0.0, 1.0)) === nothing |
| 24 | + @test MATLABDiffEq._check_matlab_compatible([1, 2, 3], (0, 10)) === nothing # Integers OK |
| 25 | + |
| 26 | + # Complex Float64 should pass |
| 27 | + @test MATLABDiffEq._check_matlab_compatible([1.0 + 2.0im], (0.0, 1.0)) === nothing |
| 28 | + |
| 29 | + # BigFloat u0 should throw ArgumentError |
| 30 | + @test_throws ArgumentError MATLABDiffEq._check_matlab_compatible( |
| 31 | + BigFloat[1.0, 2.0], (0.0, 1.0) |
| 32 | + ) |
| 33 | + |
| 34 | + # BigFloat tspan should throw ArgumentError |
| 35 | + @test_throws ArgumentError MATLABDiffEq._check_matlab_compatible( |
| 36 | + [1.0, 2.0], (BigFloat(0.0), BigFloat(1.0)) |
| 37 | + ) |
| 38 | + |
| 39 | + # Float32 should throw ArgumentError (MATLAB expects Float64) |
| 40 | + @test_throws ArgumentError MATLABDiffEq._check_matlab_compatible( |
| 41 | + Float32[1.0, 2.0], (0.0, 1.0) |
| 42 | + ) |
| 43 | + end |
| 44 | + |
| 45 | + @testset "Error messages are helpful" begin |
| 46 | + # Test that error messages contain useful information |
| 47 | + try |
| 48 | + MATLABDiffEq._check_matlab_compatible(BigFloat[1.0], (0.0, 1.0)) |
| 49 | + @test false # Should not reach here |
| 50 | + catch e |
| 51 | + @test e isa ArgumentError |
| 52 | + @test occursin("BigFloat", e.msg) |
| 53 | + @test occursin("Float64", e.msg) |
| 54 | + @test occursin("MATLABDiffEq", e.msg) |
| 55 | + end |
| 56 | + |
| 57 | + try |
| 58 | + MATLABDiffEq._check_matlab_compatible([1.0], (BigFloat(0.0), BigFloat(1.0))) |
| 59 | + @test false # Should not reach here |
| 60 | + catch e |
| 61 | + @test e isa ArgumentError |
| 62 | + @test occursin("tspan", lowercase(e.msg)) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + @testset "buildDEStats is type-generic" begin |
| 67 | + # Test that buildDEStats works with different Dict types |
| 68 | + stats1 = Dict{String, Any}("nfevals" => 100, "nsteps" => 50) |
| 69 | + result1 = MATLABDiffEq.buildDEStats(stats1) |
| 70 | + @test result1.nf == 100 |
| 71 | + @test result1.naccept == 50 |
| 72 | + |
| 73 | + # Test with empty dict |
| 74 | + stats2 = Dict{String, Any}() |
| 75 | + result2 = MATLABDiffEq.buildDEStats(stats2) |
| 76 | + @test result2.nf == 0 |
| 77 | + @test result2.naccept == 0 |
| 78 | + |
| 79 | + # Test with all fields |
| 80 | + stats3 = Dict{String, Any}( |
| 81 | + "nfevals" => 200, |
| 82 | + "nfailed" => 10, |
| 83 | + "nsteps" => 190, |
| 84 | + "nsolves" => 100, |
| 85 | + "npds" => 20, |
| 86 | + "ndecomps" => 15 |
| 87 | + ) |
| 88 | + result3 = MATLABDiffEq.buildDEStats(stats3) |
| 89 | + @test result3.nf == 200 |
| 90 | + @test result3.nreject == 10 |
| 91 | + @test result3.naccept == 190 |
| 92 | + @test result3.nsolve == 100 |
| 93 | + @test result3.njacs == 20 |
| 94 | + @test result3.nw == 15 |
| 95 | + end |
| 96 | + |
| 97 | + @testset "Algorithm structs instantiation" begin |
| 98 | + # Test that all algorithm structs can be instantiated |
| 99 | + @test MATLABDiffEq.ode23() isa MATLABDiffEq.MATLABAlgorithm |
| 100 | + @test MATLABDiffEq.ode45() isa MATLABDiffEq.MATLABAlgorithm |
| 101 | + @test MATLABDiffEq.ode113() isa MATLABDiffEq.MATLABAlgorithm |
| 102 | + @test MATLABDiffEq.ode23s() isa MATLABDiffEq.MATLABAlgorithm |
| 103 | + @test MATLABDiffEq.ode23t() isa MATLABDiffEq.MATLABAlgorithm |
| 104 | + @test MATLABDiffEq.ode23tb() isa MATLABDiffEq.MATLABAlgorithm |
| 105 | + @test MATLABDiffEq.ode15s() isa MATLABDiffEq.MATLABAlgorithm |
| 106 | + @test MATLABDiffEq.ode15i() isa MATLABDiffEq.MATLABAlgorithm |
| 107 | + end |
| 108 | +end |
0 commit comments