Skip to content

Commit

Permalink
Restrict Levels to real quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
sostock committed Nov 20, 2020
1 parent 47d131b commit 8790dec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,24 +225,25 @@ used in promotion to put levels and gains onto a common log scale.
"""
abstract type LogScaled{L<:LogInfo} <: Number end

const RealQuantity = Union{Real, AbstractQuantity{<:Real}}

"""
struct Level{L, S, T<:Number} <: LogScaled{L}
A logarithmic scale-based level. Details about the logarithmic scale are encoded in
`L <: LogInfo`. `S` is a reference quantity for the level, not a type. This type has one
field, `val::T`, and the log of the ratio `val/S` is taken. This type differs from
[`Unitful.Gain`](@ref) in that `val` is a linear quantity.
"""
struct Level{L, S, T<:Number} <: LogScaled{L}
struct Level{L, S, T<:RealQuantity} <: LogScaled{L}
val::T
function Level{L,S,T}(x) where {L,S,T}
S isa ReferenceQuantity || throw(DomainError(S, "Reference quantity must be real."))
dimension(S) != dimension(x) && throw(DimensionError(S,x))
return new{L,S,T}(x)
end
end
function Level{L,S}(val::Number) where {L,S}
dimension(S) != dimension(val) && throw(DimensionError(S, val))
return Level{L,S,typeof(val)}(val)
end
Level{L,S}(val::Number) where {L,S} = Level{L,S,real(typeof(val))}(val)
Level{L,S}(val::RealQuantity) where {L,S} = Level{L,S,typeof(val)}(val)

"""
struct Gain{L, S, T<:Real} <: LogScaled{L}
Expand Down Expand Up @@ -279,6 +280,8 @@ const RootPowerRatio{T} = IsRootPowerRatio{true,T}
@inline unwrap(x::IsRootPowerRatio) = x.val
@inline unwrap(x) = x

const ReferenceQuantity = Union{RealQuantity, IsRootPowerRatio{S,<:RealQuantity} where S}

"""
reflevel(x::Level{L,S})
reflevel(::Type{Level{L,S}})
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1329,9 +1329,22 @@ end
# Outer constructor
@test Level{Decibel,1}(2) isa Level{Decibel,1,Int}
@test_throws DimensionError Level{Decibel,1}(2V)
@test_throws DimensionError Level{Decibel,1V}(2)
@test_throws InexactError Level{Decibel,1}(1+1im)
@test_throws DomainError Level{Decibel,1+0im}(2)
@test_throws DomainError Level{Decibel,(1+0im)V}(2V)
@test_throws DomainError Level{Decibel,(1+1im)V}(2V)

# Inner constructor
@test Level{Decibel,1,Int}(2) === Level{Decibel,1}(2)
@test Level{Decibel,1,Int}(2) === Level{Decibel,1,Int}(2.0)
@test Level{Decibel,1,Int}(2) === Level{Decibel,1,Int}(2+0im)
@test_throws DimensionError Level{Decibel,1,typeof(2V)}(2V)
@test_throws DimensionError Level{Decibel,1V,Int}(2)
@test_throws TypeError Level{Decibel,1,Complex{Int}}(1+1im)
@test_throws TypeError Level{Decibel,1V,typeof((1+1im)V)}((1+1im)V)
@test_throws DomainError Level{Decibel,1+0im,Int}(2)
@test_throws DomainError Level{Decibel,(1+0im)V,typeof(2V)}(2V)
end

@testset ">> Gain" begin
Expand Down

0 comments on commit 8790dec

Please sign in to comment.