|
1 | 1 | using DynamicQuantities
|
2 | 2 | using DynamicQuantities: FixedRational
|
3 | 3 | using DynamicQuantities: DEFAULT_DIM_BASE_TYPE, DEFAULT_DIM_TYPE, DEFAULT_VALUE_TYPE, DEFAULT_UNIT_TYPE
|
| 4 | +using DynamicQuantities: AutoFloat |
4 | 5 | using DynamicQuantities: array_type, value_type, dim_type, quantity_type
|
5 | 6 | using Ratios: SimpleRatio
|
6 | 7 | using SaferIntegers: SafeInt16
|
7 | 8 | using StaticArrays: SArray, MArray
|
8 | 9 | using LinearAlgebra: norm
|
9 | 10 | using Test
|
10 | 11 |
|
| 12 | +function show_string(i) |
| 13 | + io = IOBuffer() |
| 14 | + show(io, i) |
| 15 | + return String(take!(io)) |
| 16 | +end |
| 17 | + |
11 | 18 | @testset "Basic utilities" begin
|
12 | 19 |
|
13 | 20 | for T in [DEFAULT_VALUE_TYPE, Float16, Float32, Float64], R in [DEFAULT_DIM_BASE_TYPE, Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
|
|
400 | 407 | @test_throws LoadError eval(:(u":x"))
|
401 | 408 | end
|
402 | 409 |
|
| 410 | +@testset "AutoFloat" begin |
| 411 | + @test promote_type(AutoFloat, Float16) == Float16 |
| 412 | + @test promote_type(AutoFloat, Float32) == Float32 |
| 413 | + @test promote_type(AutoFloat, Float64) == Float64 |
| 414 | + @test promote_type(AutoFloat, BigFloat) == BigFloat |
| 415 | + @test promote_type(AutoFloat, Int64) == Float64 |
| 416 | + @test promote_type(AutoFloat, ComplexF16) == promote_type(Float64, ComplexF16) |
| 417 | + |
| 418 | + x = AutoFloat(1.5) |
| 419 | + @test show_string(x) == "1.5" |
| 420 | + |
| 421 | + @test -x == AutoFloat(-1.5) |
| 422 | + @test abs(-x) == x |
| 423 | + @test sqrt(x) == AutoFloat(sqrt(1.5)) |
| 424 | + @test cbrt(x) == AutoFloat(cbrt(1.5)) |
| 425 | + @test inv(x) == AutoFloat(inv(1.5)) |
| 426 | + |
| 427 | + y = AutoFloat(2.1) |
| 428 | + @test x + y == AutoFloat(1.5 + 2.1) |
| 429 | + @test x - y == AutoFloat(1.5 - 2.1) |
| 430 | + |
| 431 | + # Should promote to array: |
| 432 | + @test typeof([u"km/s", 1.5u"km/s"]) <: Quantity{Float64} |
| 433 | +end |
| 434 | + |
403 | 435 | @testset "Constants" begin
|
404 | 436 | @test Constants.h * Constants.c / (1000.0u"nm") ≈ 1.9864458571489284e-19u"J"
|
405 | 437 |
|
|
431 | 463 | @test convert(Rational, FixedRational{UInt8,6}(2)) === Rational{UInt8}(2)
|
432 | 464 |
|
433 | 465 | # Showing rationals
|
434 |
| - function show_string(i) |
435 |
| - io = IOBuffer() |
436 |
| - show(io, i) |
437 |
| - return String(take!(io)) |
438 |
| - end |
439 | 466 | @test show_string(FixedRational{Int,10}(2)) == "2"
|
440 | 467 | @test show_string(FixedRational{Int,10}(11//10)) == "11//10"
|
441 | 468 |
|
|
0 commit comments