Skip to content

Commit

Permalink
Write some tests for unitful interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
RomeoV committed Nov 20, 2023
1 parent 55a5d13 commit 74a73d1
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,7 @@ end
Base.:+(a::Num, b::Num) = Num(a.x + b.x)
Base.:-(a::Num, b::Num) = Num(a.x - b.x)
Base.:*(a::Num, b::Num) = Num(a.x * b.x)
Base.:<(a::Num, b::Num) = a.x < b.x
Base.promote_rule(::Type{Num}, ::Type{<:Real}) = Num
Base.ArithmeticStyle(::Type{Num}) = Base.ArithmeticRounds()
Base.OrderStyle(::Type{Num}) = Base.Unordered()
Expand All @@ -2079,6 +2080,18 @@ Base.OrderStyle(::Type{Num}) = Base.Unordered()
# Test that @generated functions work with Quantities + custom types (#231)
@test uconvert(u"°C", Num(373.15)u"K") == Num(100)u"°C"
end
area_of_circle(radius::similar_dims(u"m")) = pi*radius^2
area_of_square(side::similar_units(u"m")) = side^2

@testset "Unitful interfaces" begin
@test area_of_circle(Num(1.0)u"m") pi*m^2
@test area_of_circle(Num(1.0)u"km") pi*km^2
@test_throws MethodError area_of_circle(Num(1.0)u"s")

@test area_of_square(Num(0.5)u"m") 0.25*m^2
@test_throws MethodError area_of_square(Num(0.5)u"km")
@test_throws MethodError area_of_square(Num(0.5)u"s")
end

@testset "Traits" begin
@testset "> ArithmeticStyle" begin
Expand Down

0 comments on commit 74a73d1

Please sign in to comment.