Skip to content

Commit

Permalink
more tests of Unitful integration
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed May 28, 2020
1 parent 85232ef commit 0d6594b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
9 changes: 1 addition & 8 deletions src/unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import .Unitful: Quantity, FreeUnits

function to_num_str(p::AbstractParticles{T}, d=3) where T <: Quantity
s = std(p)
if T <: AbstractFloat && s < eps(p)
if s.val < eps(p)
string(mean(p))
else
string(mean(p), " ± ", s)
Expand All @@ -18,9 +18,7 @@ for PT in ParticleSymbols
end

function Base.convert(::Type{$PT{Quantity{S,D,U},N}}, y::Quantity) where {S, D, U, T, N}

$PT{Quantity{S,D,U},N}(fill(y, N))

end
end

Expand All @@ -38,11 +36,6 @@ for PT in ParticleSymbols
end

# Below is just the reverse signature of above
function Base.$f(y::Quantity{S,D,U}, p::$PT{T,N}) where {S, D, U, T, N}
NT = promote_type(T, S)
$PT{Quantity{NT,D,U},N}($(op).(y, p.particles))
end

function Base.$f(y::Quantity{S,D,U}, p::$PT{T,N}) where {S, D, U, T <: Quantity, N}
QT = Base.promote_op($op, typeof(y), T)
$PT{QT,N}($(op).(y, p.particles))
Expand Down
6 changes: 5 additions & 1 deletion test/test_unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ register_primitive(unitful_testfunction) # must be outside testset
@test_nowarn println(p1)
@test_nowarn display(p1)

@test_nowarn println(0p1)
@test_nowarn display(0p1)

@test typeof(p1) == typeof(p2)

p3 = unitful_testfunction(p1)
Expand All @@ -35,10 +38,11 @@ register_primitive(unitful_testfunction) # must be outside testset
@test (1 ± 0.5)u"m" + (1 ± 0)u"m" (2 ± 0.5)u"m"
@test (1 ± 0.5)u"m" + 1u"m" (2 ± 0.5)u"m"


@test 1u"m" * (1 ± 0.5)u"kg" (1 ± 0.5)u"kg*m"
@test 1u"m" / (1 ± 0.5)u"kg" (1 ± 0.5)u"m/kg"
@test 1u"m" + (1 ± 0.5)u"m" (2 ± 0.5)u"m"

typeof(promote(1u"V", (1.0 ± 0.1)u"V")) <: Tuple{Particles{<:Quantity}, Particles{<:Quantity}}
end

end

0 comments on commit 0d6594b

Please sign in to comment.