Skip to content

Commit 9ac4df0

Browse files
committed
More generic quantity tests
1 parent 1d85f85 commit 9ac4df0

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

test/unittests.jl

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ using Test
1111

1212
@testset "Basic utilities" begin
1313

14-
for T in [DEFAULT_VALUE_TYPE, Float16, Float32, Float64], R in [DEFAULT_DIM_BASE_TYPE, Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
14+
for Q in [Quantity, GenericQuantity], T in [DEFAULT_VALUE_TYPE, Float16, Float32, Float64], R in [DEFAULT_DIM_BASE_TYPE, Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
1515
D = Dimensions{R}
16-
x = Quantity(T(0.2), D, length=1, mass=2.5)
16+
x = Q(T(0.2), D, length=1, mass=2.5)
1717

1818
@test typeof(x).parameters[1] == T
1919
@test typeof(x).parameters[2] == D
@@ -22,7 +22,7 @@ using Test
2222
@test ustrip(x) T(0.2)
2323
@test dimension(x) == Dimensions(R, length=1, mass=5 // 2)
2424
if R == DEFAULT_DIM_TYPE
25-
@test x == Quantity(T(0.2), length=1, mass=2.5)
25+
@test x == Q(T(0.2), length=1, mass=2.5)
2626
@test dimension(x) == Dimensions(length=1, mass=5 // 2)
2727
end
2828

@@ -77,11 +77,11 @@ using Test
7777
@test iszero(x.dimensions) == false
7878
@test iszero(y.dimensions) == true
7979

80-
y = Quantity(T(2 // 10), D, length=1, mass=5 // 2)
80+
y = Q(T(2 // 10), D, length=1, mass=5 // 2)
8181

8282
@test y x
8383

84-
y = Quantity(T(2 // 10), D, length=1, mass=6 // 2)
84+
y = Q(T(2 // 10), D, length=1, mass=6 // 2)
8585

8686
@test !(y x)
8787

@@ -105,26 +105,26 @@ using Test
105105
@test uamount(y) == R(0)
106106
@test ustrip(y) T(0.2^2.1)
107107

108-
dimensionless = Quantity(one(T), D)
108+
dimensionless = Q(one(T), D)
109109
y = T(2) + dimensionless
110110
@test ustrip(y) == T(3)
111111
@test dimension(y) == Dimensions(R)
112-
@test typeof(y) == Quantity{T,D}
112+
@test typeof(y) == Q{T,D}
113113

114114
y = T(2) - dimensionless
115115
@test ustrip(y) == T(1)
116116
@test dimension(y) == Dimensions(R)
117-
@test typeof(y) == Quantity{T,D}
117+
@test typeof(y) == Q{T,D}
118118

119119
y = dimensionless + T(2)
120120
@test ustrip(y) == T(3)
121121
y = dimensionless - T(2)
122122
@test ustrip(y) == T(-1)
123123

124-
@test_throws DimensionError Quantity(one(T), D, length=1) + 1.0
125-
@test_throws DimensionError Quantity(one(T), D, length=1) - 1.0
126-
@test_throws DimensionError 1.0 + Quantity(one(T), D, length=1)
127-
@test_throws DimensionError 1.0 - Quantity(one(T), D, length=1)
124+
@test_throws DimensionError Q(one(T), D, length=1) + 1.0
125+
@test_throws DimensionError Q(one(T), D, length=1) - 1.0
126+
@test_throws DimensionError 1.0 + Q(one(T), D, length=1)
127+
@test_throws DimensionError 1.0 - Q(one(T), D, length=1)
128128
end
129129

130130
x = Quantity(-1.2, length=2 // 5)
@@ -593,6 +593,12 @@ end
593593
# Test deprecated method
594594
q = 1.5us"km/s"
595595
@test expand_units(q) == uexpand(q)
596+
597+
# Test promotions:
598+
x = Quantity{Float32,SymbolicDimensions{Rational{Int}}}(0.2us"km/s")
599+
y = 0.5us"km/s"
600+
qa = [x, y]
601+
@test qa isa Vector{Quantity{Float64,SymbolicDimensions{Rational{Int}}}}
596602
end
597603

598604
@testset "uconvert" begin
@@ -671,6 +677,11 @@ end
671677
qarr1 = QuantityArray(randn(3), u"km/s")
672678
qarr2 = qarr1
673679
@test convert(typeof(qarr2), qarr2) === qarr1
680+
681+
x = 1.0u"m"
682+
y = x ^ (3//2)
683+
@test y == Quantity(1.0, length=3//2)
684+
@test typeof(y) == Quantity{Float64,DEFAULT_DIM_TYPE}
674685
end
675686

676687
@testset "Arrays" begin
@@ -989,6 +1000,9 @@ end
9891000
@test x isa Vector{<:GenericQuantity{Float64,<:Dimensions}}
9901001
@test ustrip(x[1]) == 500.0
9911002
@test ustrip(x[2]) == 1.0
1003+
1004+
x = [GenericQuantity([1.0, 2.0]), GenericQuantity([3f0, 4f0], Dimensions{Rational{Int}}, length=1)]
1005+
@test x isa Vector{GenericQuantity{Vector{Float64},Dimensions{Rational{Int}}}}
9921006
end
9931007

9941008
@testset "GenericQuantity broadcasting" begin

0 commit comments

Comments
 (0)