Skip to content

Commit 03242c1

Browse files
committed
Expand test coverage
1 parent a3f839a commit 03242c1

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

src/DynamicQuantities.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module DynamicQuantities
22

33
export Units, Constants, SymbolicUnits, SymbolicConstants
44
export AbstractDimensions, AbstractQuantity, AbstractGenericQuantity, AbstractRealQuantity, UnionAbstractQuantity
5-
export Quantity, GenericQuantity, RealQuantity, Dimensions, SymbolicDimensions, QuantityArray, DimensionError
5+
export Quantity, GenericQuantity, RealQuantity
6+
export Dimensions, SymbolicDimensions, SymbolicDimensionsSingleton, NoDims
7+
export QuantityArray
8+
export DimensionError
69
export ustrip, dimension
710
export ulength, umass, utime, ucurrent, utemperature, uluminosity, uamount
811
export uparse, @u_str, sym_uparse, @us_str, uexpand, uconvert

src/symbolic_dimensions.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,16 @@ dimension_names(::Type{<:AbstractSymbolicDimensions}) = ALL_SYMBOLS
9999
Base.propertynames(::AbstractSymbolicDimensions) = ALL_SYMBOLS
100100
Base.getindex(d::AbstractSymbolicDimensions, k::Symbol) = getproperty(d, k)
101101
constructorof(::Type{<:SymbolicDimensions}) = SymbolicDimensions
102-
constructorof(::Type{<:SymbolicDimensionsSingleton{R}}) where {R} = SymbolicDimensionsSingleton{R}
102+
constructorof(::Type{<:SymbolicDimensionsSingleton}) = SymbolicDimensionsSingleton
103103
with_type_parameters(::Type{<:SymbolicDimensions}, ::Type{R}) where {R} = SymbolicDimensions{R}
104104
with_type_parameters(::Type{<:SymbolicDimensionsSingleton}, ::Type{R}) where {R} = SymbolicDimensionsSingleton{R}
105105
nzdims(d::SymbolicDimensions) = getfield(d, :nzdims)
106106
nzdims(d::SymbolicDimensionsSingleton) = (getfield(d, :dim),)
107107
nzvals(d::SymbolicDimensions) = getfield(d, :nzvals)
108108
nzvals(::SymbolicDimensionsSingleton{R}) where {R} = (one(R),)
109-
Base.eltype(::AbstractSymbolicDimensions{R}) where {R} = R
110-
Base.eltype(::Type{<:AbstractSymbolicDimensions{R}}) where {R} = R
109+
110+
# Need to construct with `R` if available, as can't figure it out otherwise:
111+
constructorof(::Type{<:SymbolicDimensionsSingleton{R}}) where {R} = SymbolicDimensionsSingleton{R}
111112

112113
# Conversion:
113114
function SymbolicDimensions(d::SymbolicDimensionsSingleton{R}) where {R}
@@ -197,7 +198,7 @@ a function equivalent to `q -> uconvert(qout, q)`.
197198
uconvert(qout::UnionAbstractQuantity{<:Any,<:AbstractSymbolicDimensions}) = Base.Fix1(uconvert, qout)
198199

199200
Base.copy(d::SymbolicDimensions) = SymbolicDimensions(copy(nzdims(d)), copy(nzvals(d)))
200-
Base.copy(d::SymbolicDimensionsSingleton) = constructorof(d)(getfield(d, :dim))
201+
Base.copy(d::SymbolicDimensionsSingleton) = constructorof(typeof(d))(getfield(d, :dim))
201202

202203
function Base.:(==)(l::AbstractSymbolicDimensions, r::AbstractSymbolicDimensions)
203204
nzdims_l = nzdims(l)

test/unittests.jl

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ end
156156
@test abs(x) == abs(Quantity(1.2, length=2 // 5))
157157
@test abs2(x) == Quantity(abs2(-1.2), length=4 // 5)
158158

159+
@test copy(x) == x
160+
159161
@test iszero(x) == false
160162
@test iszero(x * 0) == true
161163
@test isfinite(x) == true
@@ -461,6 +463,7 @@ end
461463
z = u"yr"
462464
@test utime(z) == 1
463465
@test ustrip(z) 60 * 60 * 24 * 365.25
466+
@test z == uparse("yr")
464467

465468
# Test type stability of extreme range of units
466469
@test typeof(u"1") == DEFAULT_QUANTITY_TYPE
@@ -694,6 +697,7 @@ end
694697
@inferred f2(5)
695698
@test uexpand(f2(5)) == u"s"^5
696699

700+
@test_throws ErrorException uparse("'c'")
697701
@test_throws ErrorException sym_uparse("'c'")
698702

699703
# For constants which have a namespace collision, the numerical expansion is used:
@@ -734,6 +738,17 @@ end
734738
@test qa isa Vector{Quantity{Float64,SymbolicDimensions{Rational{Int}}}}
735739
DynamicQuantities.with_type_parameters(SymbolicDimensions{Float64}, Rational{Int}) == SymbolicDimensions{Rational{Int}}
736740

741+
# Many symbols in one:
742+
x = us"pm * fm * nm * μm * mm * cm * dm * m * km"
743+
y = us"s"
744+
@test inv(x) != x
745+
@test dimension(inv(x)).pm == -1
746+
@test x != y
747+
@test y != x
748+
@test dimension(uexpand(x * y)) == dimension(u"m^9 * s")
749+
z = uexpand(x)
750+
@test x == z
751+
737752
@testset "Promotion with Dimensions" begin
738753
x = 0.5u"cm"
739754
y = -0.03u"m"
@@ -1639,12 +1654,63 @@ end
16391654
@test !(Quantity(1.0) > 1.0)
16401655
end
16411656

1642-
@testset "Extra tests of `NoDims`" begin
1657+
@testset "Tests of `NoDims`" begin
16431658
@test promote_type(NoDims{Int16}, NoDims{Int32}) === NoDims{Int32}
16441659

16451660
# Prefer other types, always:
16461661
@test promote_type(Dimensions{Int16}, NoDims{Int32}) === Dimensions{Int16}
16471662
@test promote_type(MyDimensions{Int16}, NoDims{Int32}) === MyDimensions{Int16}
1663+
1664+
# Always zero dimensions
1665+
@test iszero(dimension(1.0))
1666+
@test iszero(dimension([1.0, 2.0]))
1667+
@test dimension(1.0) * u"1" == u"1"
1668+
@test typeof(dimension(1.0) * u"1") === typeof(u"1")
1669+
1670+
# Even when accessed:
1671+
@test NoDims().km == 0
1672+
@test NoDims().m != 1
1673+
1674+
# Even weird user-defined dimensions:
1675+
@test NoDims().cookie == 0
1676+
1677+
# Always returns the same type:
1678+
@test NoDims{Int32}().cookie isa Int32
1679+
@test NoDims{Int32}().cookie == 0
1680+
end
1681+
1682+
@testset "Tests of SymbolicDimensionsSingleton" begin
1683+
km = SymbolicUnits.km
1684+
@test km isa Quantity{T,SymbolicDimensionsSingleton{R}} where {T,R}
1685+
@test dimension(km) isa SymbolicDimensionsSingleton
1686+
@test dimension(km) isa AbstractSymbolicDimensions
1687+
1688+
@test dimension(km).km == 1
1689+
@test dimension(km).m == 0
1690+
VERSION >= v"1.9" &&
1691+
@test_throws "is not available as a symbol" dimension(km).γ
1692+
@test !iszero(km)
1693+
@test inv(km) == us"km^-1"
1694+
@test inv(km) == u"km^-1"
1695+
1696+
# Constructors
1697+
@test SymbolicDimensionsSingleton(:cm) isa SymbolicDimensionsSingleton{DEFAULT_DIM_BASE_TYPE}
1698+
@test constructorof(SymbolicDimensionsSingleton) === SymbolicDimensionsSingleton
1699+
1700+
@test with_type_parameters(
1701+
SymbolicDimensionsSingleton{Int64},
1702+
Int32
1703+
) === SymbolicDimensionsSingleton{Int32}
1704+
1705+
@test convert(
1706+
SymbolicDimensions,
1707+
SymbolicDimensionsSingleton{Int32}(:cm)
1708+
) isa SymbolicDimensions{Int32}
1709+
1710+
@test copy(km) == km
1711+
1712+
# Any operation should immediately convert it:
1713+
@test km ^ -1 isa Quantity{T,DynamicQuantities.SymbolicDimensions{R}} where {T,R}
16481714
end
16491715

16501716
@testset "Test div" begin

0 commit comments

Comments
 (0)