Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 12, 2024
1 parent da986bc commit 453534e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
20 changes: 9 additions & 11 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ function Base.show(io::IO, A::AbstractStarAlgebra)
return print(ioc, "*-algebra of ", object(A))
end

__prints_with_minus(::Any) = false
__prints_with_minus(x::Real) = x < 0
__needs_parens(::Any) = false
__needs_parens(a::AlgebraElement) = true

Expand All @@ -13,9 +11,9 @@ __needs_parens(a::AlgebraElement) = true
# `Int`, `Float64` don't support MIME"text/latex".
# We could add a check with `showable` if a `Real` subtype supports it and
# the feature is requested.
print_coefficient(io::IO, mime, coeff::Real) = print(io, coeff)
print_coefficient(io::IO, ::MIME, coeff::Real) = print(io, coeff)
# Scientific notation does not display well in LaTeX so we rewrite it
function print_coefficient(io::IO, mime::MIME"text/latex", coeff::AbstractFloat)
function print_coefficient(io::IO, ::MIME"text/latex", coeff::AbstractFloat)
s = string(coeff)
if occursin('e', s)
s = replace(s, 'e' => " \\cdot 10^{") * '}'

Check warning on line 19 in src/show.jl

View check run for this annotation

Codecov / codecov/patch

src/show.jl#L16-L19

Added lines #L16 - L19 were not covered by tests
Expand Down Expand Up @@ -62,8 +60,11 @@ function print_coefficient(io::IO, mime, coeff)
return print(io, ")")
end

isnegative(x::Real) = x < 0
isnegative(x) = false

_print_dot(io, ::MIME"text/latex") = print(io, " \\cdot ")

Check warning on line 66 in src/show.jl

View check run for this annotation

Codecov / codecov/patch

src/show.jl#L66

Added line #L66 was not covered by tests
_print_dot(io, ::MIME) = print(io, '')
_print_dot(io, ::MIME) = print(io, '·')

function _coeff_elt_print(io, mime, c, elt)
print_coefficient(io, mime, c)
Expand Down Expand Up @@ -106,12 +107,9 @@ function _show(io::IO, mime, a::AlgebraElement)
_coeff_elt_print(io, mime, c, elt)
_first = false
else
if __prints_with_minus(c)
print(io, ' ')
else
print(io, ' ', '+', ' ')
end
_coeff_elt_print(io, mime, c, elt)
neg = isnegative(c)
print(io, ' ', neg ? '-' : '+', ' ')
_coeff_elt_print(io, mime, neg ? abs(c) : c, elt)
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@
# @test SA.supp_ind(aa) == [b[s]] == SA.supp_ind(dense_aa)
@test SA.supp(aa) == [s]

@test sprint(show, a) == "2·(id) +1·b·c"
@test sprint(show, -a) == "-2·(id) -1·b·c"
@test sprint(show, a) == "2·(id) + 1·b·c"
@test sprint(show, -a) == "-2·(id) - 1·b·c"
Z = AlgebraElement{Float64}(a)
@test Z == a
@test sprint(show, Z) == "2.0·(id) +1.0·b·c"
@test sprint(show, 2one(RG) - RG(p)) == "2·(id) -1·b·c"
@test sprint(show, Z) == "2.0·(id) + 1.0·b·c"
@test sprint(show, 2one(RG) - RG(p)) == "2·(id) - 1·b·c"
@test sprint(show, (2 + im) * one(RG) - (3im) * RG(p)) == "(2 + 1im)·(id) + (0 - 3im)·b·c"

@test LinearAlgebra.norm(a, 1) == 3

Expand Down

0 comments on commit 453534e

Please sign in to comment.