|
1 | | -using Test: @test, @inferred |
| 1 | +using Test: @test, @testset, @inferred |
2 | 2 | using BaseType: base_numeric_type |
3 | | -using DualNumbers: DualNumbers |
| 3 | +using DualNumbers: DualNumbers, Dual |
4 | 4 | using DynamicQuantities: DynamicQuantities |
5 | 5 | using Measurements: ± |
6 | 6 | using Unitful: Unitful |
7 | 7 |
|
8 | | -expected_type_pairs = [ |
9 | | - Float32 => Float32, |
10 | | - ComplexF64 => Float64, |
11 | | - DualNumbers.Dual{Int64} => Int64, |
12 | | - DynamicQuantities.Quantity{Float32} => Float32, |
13 | | - typeof(1.5DynamicQuantities.u"km/s") => Float64, |
14 | | - typeof(1.5f0Unitful.u"km/s") => Float32, |
15 | | - BigFloat => BigFloat, |
16 | | - typeof(1.5 ± 0.2) => Float64, |
17 | | - typeof(1.5f0 ± 0.2f0) => Float32, |
18 | | -] |
| 8 | +@testset "Basic usage" begin |
| 9 | + expected_type_pairs = [ |
| 10 | + Float32 => Float32, |
| 11 | + ComplexF64 => Float64, |
| 12 | + DualNumbers.Dual{Int64} => Int64, |
| 13 | + DynamicQuantities.Quantity{Float32} => Float32, |
| 14 | + typeof(1.5DynamicQuantities.u"km/s") => Float64, |
| 15 | + typeof(1.5f0Unitful.u"km/s") => Float32, |
| 16 | + BigFloat => BigFloat, |
| 17 | + typeof(1.5 ± 0.2) => Float64, |
| 18 | + typeof(1.5f0 ± 0.2f0) => Float32, |
| 19 | + ] |
19 | 20 |
|
20 | | -for (x, y) in expected_type_pairs |
21 | | - @eval @test base_numeric_type($x) == $y |
22 | | - # Make sure compiler can inline it: |
23 | | - @eval @inferred $y base_numeric_type($x) |
| 21 | + for (x, y) in expected_type_pairs |
| 22 | + @eval @test base_numeric_type($x) == $y |
| 23 | + # Make sure compiler can inline it: |
| 24 | + @eval @inferred $y base_numeric_type($x) |
| 25 | + end |
| 26 | + |
| 27 | + @test base_numeric_type(1.5DynamicQuantities.u"km/s") == base_numeric_type(typeof(1.5DynamicQuantities.u"km/s")) |
| 28 | + @inferred base_numeric_type(1.5DynamicQuantities.u"km/s") |
| 29 | +end |
| 30 | + |
| 31 | +@testset "Nested types" begin |
| 32 | + # Quantity ∘ Measurement: |
| 33 | + x = 5Unitful.u"m/s" ± 0.1Unitful.u"m/s" |
| 34 | + @test base_numeric_type(x) == Float64 |
| 35 | + |
| 36 | + # Quantity ∘ Dual: |
| 37 | + y = Dual(1.0)Unitful.u"m/s" |
| 38 | + @test base_numeric_type(y) == Float64 |
24 | 39 | end |
25 | 40 |
|
26 | | -@test base_numeric_type(1.5DynamicQuantities.u"km/s") == base_numeric_type(typeof(1.5DynamicQuantities.u"km/s")) |
27 | | -@inferred base_numeric_type(1.5DynamicQuantities.u"km/s") |
| 41 | +struct Node{T} |
| 42 | + child::Union{Node{T},Nothing} |
| 43 | + value::T |
| 44 | +end |
| 45 | + |
| 46 | +@testset "Safe default behavior for recursive types" begin |
| 47 | + c = Node{Int}(Node{Int}(nothing, 1), 2) |
| 48 | + @test base_numeric_type(c) == Int |
| 49 | +end |
0 commit comments