Skip to content

Commit 4786b65

Browse files
committed
Expand measurements testing
1 parent cd339b6 commit 4786b65

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/utils.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ const BASE_NUMERIC_TYPES = Union{
8989
Rational{BigInt},
9090
}
9191

92-
for (type, _, _) in ABSTRACT_QUANTITY_TYPES
93-
@eval begin
92+
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
93+
!(base_type <: Number) && @eval begin
9494
function Base.convert(::Type{Q}, x::BASE_NUMERIC_TYPES) where {T,D,Q<:$type{T,D}}
9595
return new_quantity(Q, convert(T, x), D())
9696
end
97+
end
98+
@eval begin
9799
function Base.promote_rule(::Type{Q}, ::Type{T2}) where {T,D,Q<:$type{T,D},T2<:BASE_NUMERIC_TYPES}
98100
return with_type_parameters(promote_quantity_on_value(Q, T2), promote_type(T, T2), D)
99101
end
@@ -268,7 +270,7 @@ end
268270
Base.one(::Type{D}) where {D<:AbstractDimensions} = D()
269271
Base.one(::D) where {D<:AbstractDimensions} = one(D)
270272

271-
# Additive identities (zero)
273+
# Additive identities (zero). We have to invalidate these due to different behavior with conversion
272274
Base.zero(q::Q) where {Q<:UnionAbstractQuantity} = new_quantity(Q, zero(ustrip(q)), dimension(q))
273275
Base.zero(::AbstractDimensions) = error("There is no such thing as an additive identity for a `AbstractDimensions` object, as + is only defined for `UnionAbstractQuantity`.")
274276
Base.zero(::Type{<:UnionAbstractQuantity}) = error("Cannot create an additive identity for a `UnionAbstractQuantity` type, as the dimensions are unknown. Please use `zero(::UnionAbstractQuantity)` instead.")
@@ -316,6 +318,10 @@ for (type, _, _) in ABSTRACT_QUANTITY_TYPES, (type2, _, _) in ABSTRACT_QUANTITY_
316318
Base.convert(::Type{Q}, q::$type) where {T,Q<:$type2{T}} = new_quantity(Q, convert(T, ustrip(q)), dimension(q))
317319
Base.convert(::Type{Q}, q::$type) where {T,D,Q<:$type2{T,D}} = new_quantity(Q, convert(T, ustrip(q)), convert(D, dimension(q)))
318320
end
321+
# TODO: This invalidates some methods. But we have to, because
322+
# the conversion in `number.jl` has a type assertion step, whereas
323+
# we want to allow things like `convert(Quantity{Float64}, 1.0u"m")`,
324+
# with the type for the dimensions being inferred.
319325
end
320326

321327
Base.convert(::Type{D}, d::AbstractDimensions) where {D<:AbstractDimensions} = d

test/test_measurements.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using DynamicQuantities
22
using Measurements
33
using Measurements: value, uncertainty
44

5-
for Q in (Quantity, GenericQuantity)
5+
for Q in (RealQuantity, Quantity, GenericQuantity)
66
x = Q(1.0u"m/s") ± Q(0.1u"m/s")
77

88
@test ustrip(x^2) == ustrip(x)^2
@@ -11,7 +11,9 @@ for Q in (Quantity, GenericQuantity)
1111
@test dimension(x)^2 == dimension(x^2)
1212
@test_throws DimensionError 0.5u"m" ± 0.1u"s"
1313

14-
# Mixed types:
15-
y = Q{Float16}(0.1u"m/s") ± Q{Float32}(0.1u"m/s")
16-
@test typeof(y) <: Q{Measurement{Float32}}
14+
if Q in (Quantity, GenericQuantity)
15+
# Mixed types:
16+
y = Q{Float16}(0.1u"m/s") ± Q{Float32}(0.1u"m/s")
17+
@test typeof(y) <: Q{Measurement{Float32}}
18+
end
1719
end

0 commit comments

Comments
 (0)