Skip to content

Commit db26059

Browse files
committed
🚨 Improve tests of CRP
1 parent 451a0aa commit db26059

1 file changed

Lines changed: 77 additions & 75 deletions

File tree

test/crp.jl

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
# == File: ./src/crp.jl ====================================================================
88

9+
# -- Functions: CRP ------------------------------------------------------------------------
10+
911
@testset "CRP Constructors" begin
1012
c = CRP(1.0, 2.0, 3.0)
1113
@test c.q1 == 1.0
@@ -29,29 +31,48 @@
2931
@test c.q2 == 0
3032
@test c.q3 == 0
3133

32-
# Test indexing and iteration
3334
@test c[1] == 0
3435
@test c[2] == 0
3536
@test c[3] == 0
3637
@test collect(c) == [0, 0, 0]
3738
@test length(c) == 3
3839
end
3940

41+
# -- Functions: show -----------------------------------------------------------------------
42+
4043
@testset "CRP Show" begin
41-
# c = CRP(1.0, 2.0, 3.0)
42-
# io = IOBuffer()
43-
# show(io, c)
44-
# s = String(take!(io))
45-
# @test occursin("CRP{Float64}", s)
46-
# @test occursin("1.0", s)
47-
# @test occursin("2.0", s)
48-
# @test occursin("3.0", s)
44+
buf = IOBuffer()
45+
io = IOContext(buf)
46+
c = CRP(1.0, -2.0, 3.0)
47+
48+
# Extended printing.
49+
show(io, MIME"text/plain"(), c)
50+
expected = """
51+
CRP{Float64}:
52+
X : + 1.0
53+
Y : - 2.0
54+
Z : + 3.0"""
55+
@test String(take!(io.io)) == expected
56+
57+
# Compact printing.
58+
show(io, c)
59+
expected = "CRP{Float64}: [1.0, 2.0, 3.0]"
60+
@test String(take!(io.io)) == expected
61+
62+
# Colors.
63+
io = IOContext(buf, :color => true)
64+
show(io, MIME"text/plain"(), c)
65+
expected = """
66+
CRP{Float64}:
67+
\e[1mX : \e[0m+ 1.0
68+
\e[1mY : \e[0m- 2.0
69+
\e[1mZ : \e[0m+ 3.0"""
70+
@test String(take!(io.io)) == expected
4971
end
5072

73+
# -- Operators: * --------------------------------------------------------------------------
5174

5275
@testset "CRP Composition" begin
53-
# c3 = c2 * c1 (apply c1 then c2) -> R3 = R2 * R1
54-
5576
eul1 = EulerAngles(0.3, 0.2, 0.1, :ZYX)
5677
eul2 = EulerAngles(-0.2, 0.4, -0.3, :ZYX)
5778

@@ -60,121 +81,107 @@ end
6081

6182
c3 = c2 * c1
6283

63-
# Verify with DCM
64-
dcm1 = angle_to_dcm(eul1)
65-
dcm2 = angle_to_dcm(eul2)
66-
dcm3 = dcm2 * dcm1
67-
84+
# Verify with DCM.
85+
dcm1 = angle_to_dcm(eul1)
86+
dcm2 = angle_to_dcm(eul2)
87+
dcm3 = dcm2 * dcm1
6888
dcm_c3 = crp_to_dcm(c3)
6989
@test maximum(abs.(dcm3 - dcm_c3)) < 1e-12
7090

71-
# Verify compose_rotation
72-
c_comp = compose_rotation(c1, c2) # Apply c1 then c2
73-
@test isapprox(c_comp, c3; atol=1e-12)
91+
# Verify compose_rotation.
92+
c_comp = compose_rotation(c1, c2)
93+
@test isapprox(c_comp, c3; atol = 1e-12)
7494
end
7595

96+
# -- Operators: +, -, *, / ----------------------------------------------------------------
97+
7698
@testset "CRP Arithmetic" begin
7799
c1 = CRP(1.0, 2.0, 3.0)
78100
c2 = CRP(0.5, -0.5, 1.0)
79101

80-
# +
81102
c3 = c1 + c2
82-
@test isapprox(c3.q1, 1.5; atol=1e-12)
83-
@test isapprox(c3.q2, 1.5; atol=1e-12)
84-
@test isapprox(c3.q3, 4.0; atol=1e-12)
103+
@test isapprox(c3.q1, 1.5; atol = 1e-12)
104+
@test isapprox(c3.q2, 1.5; atol = 1e-12)
105+
@test isapprox(c3.q3, 4.0; atol = 1e-12)
85106

86-
# - (binary)
87107
c4 = c1 - c2
88108
@test c4.q1 == 0.5
89109
@test c4.q2 == 2.5
90110
@test c4.q3 == 2.0
91111

92-
# - (unary)
93112
c_neg = -c1
94113
@test c_neg.q1 == -1.0
95114
@test c_neg.q2 == -2.0
96115
@test c_neg.q3 == -3.0
97116

98-
# * (scalar)
99117
c_scaled = 2.0 * c1
100118
@test c_scaled.q1 == 2.0
101119
@test c_scaled.q2 == 4.0
102120
@test c_scaled.q3 == 6.0
121+
@test c1 * 2.0 == c_scaled
103122

104-
c_scaled2 = c1 * 2.0
105-
@test c_scaled2 == c_scaled
106-
107-
# / (scalar)
108123
c_div = c1 / 2.0
109124
@test c_div.q1 == 0.5
110125
@test c_div.q2 == 1.0
111126
@test c_div.q3 == 1.5
112127
end
113128

129+
# -- Operators: inv, /, \ -----------------------------------------------------------------
130+
114131
@testset "CRP Inverse and Division" begin
115132
c1 = CRP(1.0, 2.0, 3.0)
116133
c2 = CRP(0.5, -0.5, 1.0)
117134

118-
# inv
119135
c_inv = inv(c1)
120-
# The inverse of a CRP q is -q
121136
@test c_inv == -c1
122137

123-
# Identity
124-
# c * inv(c) should be identity (0, 0, 0)
125138
c_identity = c1 * c_inv
126-
@test isapprox(c_identity.q1, 0.0; atol=1e-12)
127-
@test isapprox(c_identity.q2, 0.0; atol=1e-12)
128-
@test isapprox(c_identity.q3, 0.0; atol=1e-12)
129-
130-
# / (c1 / c2 = c1 * inv(c2))
131-
c_div = c1 / c2
132-
c_mult = c1 * inv(c2)
133-
@test isapprox(c_div, c_mult)
134-
135-
# \ (c1 \ c2 = inv(c1) * c2)
136-
c_backdiv = c1 \ c2
137-
c_mult_back = inv(c1) * c2
138-
@test isapprox(c_backdiv, c_mult_back)
139+
@test isapprox(c_identity.q1, 0.0; atol = 1e-12)
140+
@test isapprox(c_identity.q2, 0.0; atol = 1e-12)
141+
@test isapprox(c_identity.q3, 0.0; atol = 1e-12)
142+
143+
@test isapprox(c1 / c2, c1 * inv(c2))
144+
@test isapprox(c1 \ c2, inv(c1) * c2)
139145
end
140146

147+
# -- Functions: norm, one, zero, copy, vect -----------------------------------------------
148+
141149
@testset "CRP Utils" begin
142150
c = CRP(3.0, 0.0, 4.0)
143151
@test norm(c) == 5.0
144152

145-
@test one(CRP) == CRP(0.0, 0.0, 0.0)
146-
@test one(c) == CRP(0.0, 0.0, 0.0)
147-
153+
@test one(CRP) == CRP(0.0, 0.0, 0.0)
154+
@test one(c) == CRP(0.0, 0.0, 0.0)
148155
@test zero(CRP) == CRP(0.0, 0.0, 0.0)
149-
@test zero(c) == CRP(0.0, 0.0, 0.0)
156+
@test zero(c) == CRP(0.0, 0.0, 0.0)
150157

151-
# copy
152158
c_copy = copy(c)
153159
@test c_copy == c
154160

155-
# vect
156161
v = vect(c)
157162
@test v isa SVector{3, Float64}
158163
@test v[1] == c.q1
159164
@test v[2] == c.q2
160165
@test v[3] == c.q3
161166

162-
# UniformScaling
163-
@test I * c == c
164-
@test c * I == c
165-
@test I / c == inv(c)
166-
@test c / I == c
167-
@test I \ c == c
168-
@test c \ I == inv(c)
167+
@test I * c == c
168+
@test c * I == c
169+
@test I / c == inv(c)
170+
@test c / I == c
171+
@test I \ c == c
172+
@test c \ I == inv(c)
169173
end
170174

175+
# -- Functions: shadow_rotation -----------------------------------------------------------
176+
171177
@testset "CRP Shadow Rotation" begin
172178
c = CRP(1.0, 2.0, 3.0)
173179
c_shadow = shadow_rotation(c)
174-
175180
@test c == c_shadow
176181
end
177182

183+
# -- Functions: rand -----------------------------------------------------------------------
184+
178185
@testset "CRP Random" begin
179186
Random.seed!(123)
180187
c = rand(CRP)
@@ -184,30 +191,25 @@ end
184191
@test c_float32 isa CRP{Float32}
185192
end
186193

194+
# -- Functions: dcrp -----------------------------------------------------------------------
195+
187196
@testset "CRP Kinematics" begin
188-
# Test analytical derivative against numerical derivative
189197
for T in (Float64,)
190-
# We need a seed to ensure reproducibility
191198
Random.seed!(123)
192199

193-
c = rand(CRP{T})
194-
w = @SVector randn(T, 3)
195-
200+
c = rand(CRP{T})
201+
w = @SVector randn(T, 3)
196202
dc = dcrp(c, w)
197203

198204
dt = 1e-8
199205

200-
# Use DCM to propagate
201-
D = crp_to_dcm(c)
202-
dD = ddcm(D, w)
203-
D_next = D + dD * dt
204-
D_next = orthonormalize(D_next)
206+
D = crp_to_dcm(c)
207+
dD = ddcm(D, w)
208+
D_next = orthonormalize(D + dD * dt)
205209

206210
c_next = dcm_to_crp(D_next)
207-
208211
dc_num = (c_next - c) / dt
209212

210-
# Check alignment
211-
@test isapprox(dc, dc_num; rtol = 1e-4, atol=1e-6)
213+
@test isapprox(dc, dc_num; rtol = 1e-4, atol = 1e-6)
212214
end
213-
end
215+
end

0 commit comments

Comments
 (0)