|
| 1 | +# Tests for CommonSolve.solve integration (PR #7 by @jverzani) |
| 2 | +# Verifies that Giac.Commands.solve and CommonSolve.solve refer to the same |
| 3 | +# generic function, so Giac plays well with the broader Julia "solve" verb |
| 4 | +# ecosystem (DifferentialEquations.jl, NLsolve.jl, Symbolics.jl, …). |
| 5 | + |
| 6 | +using CommonSolve |
| 7 | + |
| 8 | +@testset "CommonSolve integration" begin |
| 9 | + |
| 10 | + @testset "solve identity: Giac.Commands.solve === CommonSolve.solve" begin |
| 11 | + @test Giac.Commands.solve === CommonSolve.solve |
| 12 | + end |
| 13 | + |
| 14 | + @testset "solve dispatches Giac methods through CommonSolve" begin |
| 15 | + @giac_var x |
| 16 | + |
| 17 | + # Calling CommonSolve.solve on a GiacExpr equation routes to Giac's |
| 18 | + # auto-generated solve method (added to CommonSolve.solve via Julia's |
| 19 | + # extend-imported-function mechanism). |
| 20 | + result = CommonSolve.solve(x^2 - 1, x) |
| 21 | + @test result isa GiacExpr |
| 22 | + |
| 23 | + result_str = string(result) |
| 24 | + @test occursin("-1", result_str) |
| 25 | + @test occursin("1", result_str) |
| 26 | + end |
| 27 | + |
| 28 | + @testset "solve via Giac.Commands and via CommonSolve produce equal results" begin |
| 29 | + using Giac.Commands: solve as gsolve |
| 30 | + @giac_var x y |
| 31 | + |
| 32 | + # Single equation |
| 33 | + @test string(gsolve(x^2 - 4, x)) == string(CommonSolve.solve(x^2 - 4, x)) |
| 34 | + |
| 35 | + # System of equations |
| 36 | + @test string(gsolve([x + y ~ 3, x - y ~ 1], [x, y])) == |
| 37 | + string(CommonSolve.solve([x + y ~ 3, x - y ~ 1], [x, y])) |
| 38 | + end |
| 39 | + |
| 40 | + @testset "GiacMatrix path also extends CommonSolve.solve" begin |
| 41 | + # The auto-generator emits a GiacMatrix overload for every command, so |
| 42 | + # CommonSolve.solve(::GiacMatrix, …) should dispatch the same way. |
| 43 | + @giac_var x |
| 44 | + # `solve` on a matrix isn't a typical use case, but the method must |
| 45 | + # exist for dispatch consistency. |
| 46 | + @test hasmethod(CommonSolve.solve, Tuple{GiacMatrix, Vararg{Any}}) |
| 47 | + end |
| 48 | + |
| 49 | + @testset "init / solve! are NOT extended by Giac" begin |
| 50 | + # Giac is a symbolic CAS (non-iterative), so only CommonSolve.solve is |
| 51 | + # integrated. CommonSolve.init and CommonSolve.solve! must remain |
| 52 | + # un-shadowed by anything in Giac so packages implementing the |
| 53 | + # iterative-solver protocol can extend them without conflict. |
| 54 | + @giac_var x |
| 55 | + |
| 56 | + # No Giac method on init or solve! for symbolic inputs. |
| 57 | + @test !hasmethod(CommonSolve.init, Tuple{GiacExpr, Vararg{Any}}) |
| 58 | + @test !hasmethod(CommonSolve.solve!, Tuple{GiacExpr, Vararg{Any}}) |
| 59 | + |
| 60 | + # And the bindings themselves are not aliased by Giac (they live in |
| 61 | + # CommonSolve only). |
| 62 | + @test parentmodule(CommonSolve.init) === CommonSolve |
| 63 | + @test parentmodule(CommonSolve.solve!) === CommonSolve |
| 64 | + end |
| 65 | +end |
0 commit comments