|
| 1 | +@testset "Unit" begin |
| 2 | + @test isrevertible(Unit(u"m")) |
| 3 | + |
| 4 | + a = [2.7, 2.9, 2.2, 1.4, 1.8, 3.3] * u"m" |
| 5 | + b = [300, 500, missing, 800, missing, 400] * u"cm" |
| 6 | + c = [8, 2, 5, 7, 9, 4] * u"km" |
| 7 | + d = [0.3, 0.1, 0.9, 0.2, 0.7, 0.4] |
| 8 | + e = ["no", "no", "yes", "yes", "no", "yes"] |
| 9 | + t = Table(; a, b, c, d, e) |
| 10 | + |
| 11 | + T = Unit(u"m") |
| 12 | + n, c = apply(T, t) |
| 13 | + @test unit(eltype(n.a)) == u"m" |
| 14 | + @test unit(eltype(n.b)) == u"m" |
| 15 | + @test unit(eltype(n.c)) == u"m" |
| 16 | + @test eltype(n.d) <: Float64 |
| 17 | + @test eltype(n.e) <: String |
| 18 | + tₒ = revert(T, n, c) |
| 19 | + @test unit(eltype(tₒ.a)) == u"m" |
| 20 | + @test unit(eltype(tₒ.b)) == u"cm" |
| 21 | + @test unit(eltype(tₒ.c)) == u"km" |
| 22 | + @test all(isapprox.(tₒ.a, t.a)) |
| 23 | + @test all(isapprox.(skipmissing(tₒ.b), skipmissing(t.b))) |
| 24 | + @test all(isapprox.(tₒ.c, t.c)) |
| 25 | + @test tₒ.d == t.d |
| 26 | + @test tₒ.e == t.e |
| 27 | + |
| 28 | + a = [2.7, 2.9, 2.2, 1.4, 1.8, 3.3] * u"m" |
| 29 | + b = [300, 500, missing, 800, missing, 400] * u"cm" |
| 30 | + c = [8, 2, 5, 7, 9, 4] * u"km" |
| 31 | + d = [29.1, missing, 29.2, missing, 28.4, 26.4] * u"°C" |
| 32 | + e = [0.9, 0.4, 0.5, 0.1, 0.3, 0.6] * u"kg" |
| 33 | + f = 0.5u"ppm" * e |
| 34 | + t = Table(; a, b, c, d, e, f) |
| 35 | + |
| 36 | + T = Unit(4 => u"K") |
| 37 | + n, c = apply(T, t) |
| 38 | + @test unit(eltype(n.a)) == u"m" |
| 39 | + @test unit(eltype(n.b)) == u"cm" |
| 40 | + @test unit(eltype(n.c)) == u"km" |
| 41 | + @test unit(eltype(n.d)) == u"K" |
| 42 | + @test unit(eltype(n.e)) == u"kg" |
| 43 | + @test unit(eltype(n.f)) == u"kg * ppm" |
| 44 | + tₒ = revert(T, n, c) |
| 45 | + @test unit(eltype(tₒ.d)) == u"°C" |
| 46 | + @test tₒ.a == t.a |
| 47 | + @test isequal(tₒ.b, t.b) |
| 48 | + @test tₒ.c == t.c |
| 49 | + @test all(isapprox.(skipmissing(tₒ.d), skipmissing(t.d))) |
| 50 | + @test tₒ.e == t.e |
| 51 | + @test tₒ.f == t.f |
| 52 | + |
| 53 | + T = Unit(:e => u"g") |
| 54 | + n, c = apply(T, t) |
| 55 | + @test unit(eltype(n.a)) == u"m" |
| 56 | + @test unit(eltype(n.b)) == u"cm" |
| 57 | + @test unit(eltype(n.c)) == u"km" |
| 58 | + @test unit(eltype(n.d)) == u"°C" |
| 59 | + @test unit(eltype(n.e)) == u"g" |
| 60 | + @test unit(eltype(n.f)) == u"kg * ppm" |
| 61 | + tₒ = revert(T, n, c) |
| 62 | + @test unit(eltype(tₒ.e)) == u"kg" |
| 63 | + @test tₒ.a == t.a |
| 64 | + @test isequal(tₒ.b, t.b) |
| 65 | + @test tₒ.c == t.c |
| 66 | + @test isequal(tₒ.d, t.d) |
| 67 | + @test all(isapprox.(tₒ.e, t.e)) |
| 68 | + @test tₒ.f == t.f |
| 69 | + |
| 70 | + T = Unit("f" => u"kg") |
| 71 | + n, c = apply(T, t) |
| 72 | + @test unit(eltype(n.a)) == u"m" |
| 73 | + @test unit(eltype(n.b)) == u"cm" |
| 74 | + @test unit(eltype(n.c)) == u"km" |
| 75 | + @test unit(eltype(n.d)) == u"°C" |
| 76 | + @test unit(eltype(n.e)) == u"kg" |
| 77 | + @test unit(eltype(n.f)) == u"kg" |
| 78 | + tₒ = revert(T, n, c) |
| 79 | + @test unit(eltype(tₒ.f)) == u"kg * ppm" |
| 80 | + @test tₒ.a == t.a |
| 81 | + @test isequal(tₒ.b, t.b) |
| 82 | + @test tₒ.c == t.c |
| 83 | + @test isequal(tₒ.d, t.d) |
| 84 | + @test tₒ.e == t.e |
| 85 | + @test all(isapprox.(tₒ.f, t.f)) |
| 86 | + |
| 87 | + T = Unit([1, 2, 3] => u"m") |
| 88 | + n, c = apply(T, t) |
| 89 | + @test unit(eltype(n.a)) == u"m" |
| 90 | + @test unit(eltype(n.b)) == u"m" |
| 91 | + @test unit(eltype(n.c)) == u"m" |
| 92 | + @test unit(eltype(n.d)) == u"°C" |
| 93 | + @test unit(eltype(n.e)) == u"kg" |
| 94 | + @test unit(eltype(n.f)) == u"kg * ppm" |
| 95 | + tₒ = revert(T, n, c) |
| 96 | + @test unit(eltype(tₒ.a)) == u"m" |
| 97 | + @test unit(eltype(tₒ.b)) == u"cm" |
| 98 | + @test unit(eltype(tₒ.c)) == u"km" |
| 99 | + @test all(isapprox.(tₒ.a, t.a)) |
| 100 | + @test all(isapprox.(skipmissing(tₒ.b), skipmissing(t.b))) |
| 101 | + @test all(isapprox.(tₒ.c, t.c)) |
| 102 | + @test isequal(tₒ.d, t.d) |
| 103 | + @test tₒ.e == t.e |
| 104 | + @test tₒ.f == t.f |
| 105 | + |
| 106 | + T = Unit([:a, :b, :c] => u"cm") |
| 107 | + n, c = apply(T, t) |
| 108 | + @test unit(eltype(n.a)) == u"cm" |
| 109 | + @test unit(eltype(n.b)) == u"cm" |
| 110 | + @test unit(eltype(n.c)) == u"cm" |
| 111 | + @test unit(eltype(n.d)) == u"°C" |
| 112 | + @test unit(eltype(n.e)) == u"kg" |
| 113 | + @test unit(eltype(n.f)) == u"kg * ppm" |
| 114 | + tₒ = revert(T, n, c) |
| 115 | + @test unit(eltype(tₒ.a)) == u"m" |
| 116 | + @test unit(eltype(tₒ.b)) == u"cm" |
| 117 | + @test unit(eltype(tₒ.c)) == u"km" |
| 118 | + @test all(isapprox.(tₒ.a, t.a)) |
| 119 | + @test all(isapprox.(skipmissing(tₒ.b), skipmissing(t.b))) |
| 120 | + @test all(isapprox.(tₒ.c, t.c)) |
| 121 | + @test isequal(tₒ.d, t.d) |
| 122 | + @test tₒ.e == t.e |
| 123 | + @test tₒ.f == t.f |
| 124 | + |
| 125 | + T = Unit(["a", "b", "c"] => u"km") |
| 126 | + n, c = apply(T, t) |
| 127 | + @test unit(eltype(n.a)) == u"km" |
| 128 | + @test unit(eltype(n.b)) == u"km" |
| 129 | + @test unit(eltype(n.c)) == u"km" |
| 130 | + @test unit(eltype(n.d)) == u"°C" |
| 131 | + @test unit(eltype(n.e)) == u"kg" |
| 132 | + @test unit(eltype(n.f)) == u"kg * ppm" |
| 133 | + tₒ = revert(T, n, c) |
| 134 | + @test unit(eltype(tₒ.a)) == u"m" |
| 135 | + @test unit(eltype(tₒ.b)) == u"cm" |
| 136 | + @test unit(eltype(tₒ.c)) == u"km" |
| 137 | + @test all(isapprox.(tₒ.a, t.a)) |
| 138 | + @test all(isapprox.(skipmissing(tₒ.b), skipmissing(t.b))) |
| 139 | + @test all(isapprox.(tₒ.c, t.c)) |
| 140 | + @test isequal(tₒ.d, t.d) |
| 141 | + @test tₒ.e == t.e |
| 142 | + @test tₒ.f == t.f |
| 143 | + |
| 144 | + T = Unit(r"[abc]" => u"m") |
| 145 | + n, c = apply(T, t) |
| 146 | + @test unit(eltype(n.a)) == u"m" |
| 147 | + @test unit(eltype(n.b)) == u"m" |
| 148 | + @test unit(eltype(n.c)) == u"m" |
| 149 | + @test unit(eltype(n.d)) == u"°C" |
| 150 | + @test unit(eltype(n.e)) == u"kg" |
| 151 | + @test unit(eltype(n.f)) == u"kg * ppm" |
| 152 | + tₒ = revert(T, n, c) |
| 153 | + @test unit(eltype(tₒ.a)) == u"m" |
| 154 | + @test unit(eltype(tₒ.b)) == u"cm" |
| 155 | + @test unit(eltype(tₒ.c)) == u"km" |
| 156 | + @test all(isapprox.(tₒ.a, t.a)) |
| 157 | + @test all(isapprox.(skipmissing(tₒ.b), skipmissing(t.b))) |
| 158 | + @test all(isapprox.(tₒ.c, t.c)) |
| 159 | + @test isequal(tₒ.d, t.d) |
| 160 | + @test tₒ.e == t.e |
| 161 | + @test tₒ.f == t.f |
| 162 | + |
| 163 | + # error: cannot create Unit transform without arguments |
| 164 | + @test_throws ArgumentError Unit() |
| 165 | +end |
0 commit comments