Skip to content

Commit

Permalink
More tests of AutoFloat
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Oct 16, 2023
1 parent 8ec0891 commit 3bc4463
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/lazy_float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ Base.promote_rule(::Type{AutoFloat}, ::Type{T}) where {T} = promote_type(Float64

Base.show(io::IO, x::AutoFloat) = print(io, float(x))

Base.:(==)(a::AutoFloat, b::AutoFloat) = float(a) == float(b)
Base.:+(a::AutoFloat, b::AutoFloat) = AutoFloat(float(a) + float(b))
Base.:-(a::AutoFloat) = AutoFloat(-float(a))
Base.:-(a::AutoFloat, b::AutoFloat) = AutoFloat(float(a) - float(b))
Base.:-(a::AutoFloat, b::AutoFloat) = AutoFloat(float(a) + float(-b))
Base.:*(a::AutoFloat, b::AutoFloat) = AutoFloat(float(a) * float(b))
Base.inv(a::AutoFloat) = AutoFloat(inv(float(a)))
Base.abs(a::AutoFloat) = AutoFloat(abs(float(a)))
Expand Down
37 changes: 32 additions & 5 deletions test/unittests.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
using DynamicQuantities
using DynamicQuantities: FixedRational
using DynamicQuantities: DEFAULT_DIM_BASE_TYPE, DEFAULT_DIM_TYPE, DEFAULT_VALUE_TYPE, DEFAULT_UNIT_TYPE
using DynamicQuantities: AutoFloat
using DynamicQuantities: array_type, value_type, dim_type, quantity_type
using Ratios: SimpleRatio
using SaferIntegers: SafeInt16
using StaticArrays: SArray, MArray
using LinearAlgebra: norm
using Test

function show_string(i)
io = IOBuffer()
show(io, i)
return String(take!(io))
end

@testset "Basic utilities" begin

for T in [DEFAULT_VALUE_TYPE, Float16, Float32, Float64], R in [DEFAULT_DIM_BASE_TYPE, Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
Expand Down Expand Up @@ -400,6 +407,31 @@ end
@test_throws LoadError eval(:(u":x"))
end

@testset "AutoFloat" begin
@test promote_type(AutoFloat, Float16) == Float16
@test promote_type(AutoFloat, Float32) == Float32
@test promote_type(AutoFloat, Float64) == Float64
@test promote_type(AutoFloat, BigFloat) == BigFloat
@test promote_type(AutoFloat, Int64) == Float64
@test promote_type(AutoFloat, ComplexF16) == promote_type(Float64, ComplexF16)

x = AutoFloat(1.5)
@test show_string(x) == "1.5"

@test -x == AutoFloat(-1.5)
@test abs(-x) == x
@test sqrt(x) == AutoFloat(sqrt(1.5))
@test cbrt(x) == AutoFloat(cbrt(1.5))
@test inv(x) == AutoFloat(inv(1.5))

y = AutoFloat(2.1)
@test x + y == AutoFloat(1.5 + 2.1)
@test x - y == AutoFloat(1.5 - 2.1)

# Should promote to array:
@test typeof([u"km/s", 1.5u"km/s"]) <: Vector{<:Quantity{Float64}}
end

@testset "Constants" begin
@test Constants.h * Constants.c / (1000.0u"nm") 1.9864458571489284e-19u"J"

Expand Down Expand Up @@ -431,11 +463,6 @@ end
@test convert(Rational, FixedRational{UInt8,6}(2)) === Rational{UInt8}(2)

# Showing rationals
function show_string(i)
io = IOBuffer()
show(io, i)
return String(take!(io))
end
@test show_string(FixedRational{Int,10}(2)) == "2"
@test show_string(FixedRational{Int,10}(11//10)) == "11//10"

Expand Down

0 comments on commit 3bc4463

Please sign in to comment.