Skip to content

Commit f5c6195

Browse files
authored
Merge pull request #29 from tkoolen/tk/fix-28
Fix #28
2 parents b9f6752 + 1bdae26 commit f5c6195

File tree

5 files changed

+82
-8
lines changed

5 files changed

+82
-8
lines changed

src/lcmtype.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,24 @@ end
175175
end
176176

177177
@inline resizevec!(vec::SVector, x::LCMType, dim::LCMDimension{Int}) = nothing
178-
@inline resizevec!(vec::Vector, x::LCMType, dim::LCMDimension{Symbol}) = (resize!(vec, getfield(x, dim.size)); nothing)
178+
@inline resizevec!(vec::Vector, x::LCMType, dim::LCMDimension{Symbol}) = (_resizevec!(vec, getfield(x, dim.size)); nothing)
179179
@inline function resizevec!(vec::AbstractVector, x::LCMType, dimhead::LCMDimension, dimtail::LCMDimension...)
180180
resizevec!(vec, x, dimhead)
181181
for vi in vec
182182
resizevec!(vi, x, dimtail...)
183183
end
184184
end
185185

186+
@inline function _resizevec!(vec::Vector{T}, newsize::Integer) where T
187+
# Note: separated from resizevec! to introduce a function barrier and
188+
# achieve zero allocation despite the type-unstable `getfield` call.
189+
oldsize = length(vec)
190+
resize!(vec, newsize)
191+
for i in oldsize + 1 : newsize
192+
@inbounds vec[i] = defaultval(T)
193+
end
194+
end
195+
186196
# checkvalid
187197
"""
188198
checkvalid(x::LCMType)

test/lcmtypes/lcmtesttypes.jl

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module lcmtesttypes
22

3-
export lcm_test_type_1, lcm_test_type_2, lcm_test_type_3
3+
export lcm_test_type_1, lcm_test_type_2, lcm_test_type_3, polynomial_t, polynomial_matrix_t
44
export hard_coded_example
55

66
using LCMCore, StaticArrays
@@ -133,4 +133,66 @@ function hard_coded_example(::Type{lcm_test_type_3})
133133
ret
134134
end
135135

136+
mutable struct polynomial_t <: LCMType
137+
timestamp::Int64
138+
num_coefficients::Int32
139+
coefficients::Vector{Float64}
140+
end
141+
142+
@lcmtypesetup(polynomial_t,
143+
(coefficients, 1) => num_coefficients,
144+
)
145+
146+
function Base.:(==)(x::polynomial_t, y::polynomial_t)
147+
x.timestamp == y.timestamp || return false
148+
x.num_coefficients == y.num_coefficients || return false
149+
x.coefficients == y.coefficients || return false
150+
true
151+
end
152+
153+
function Base.rand(::Type{polynomial_t})
154+
timestamp = rand(Int64)
155+
num_coefficients = rand(Int32(0) : Int32(10))
156+
coefficients = rand(num_coefficients)
157+
polynomial_t(timestamp, num_coefficients, coefficients)
158+
end
159+
160+
function hard_coded_example(::Type{polynomial_t})
161+
polynomial_t(1234, 4, [5.0, 0.0, 1.0, 3.0])
162+
end
163+
164+
165+
mutable struct polynomial_matrix_t <: LCMType
166+
timestamp::Int64
167+
rows::Int32
168+
cols::Int32
169+
polynomials::Vector{Vector{polynomial_t}}
170+
end
171+
172+
@lcmtypesetup(polynomial_matrix_t,
173+
(polynomials, 1) => rows,
174+
(polynomials, 2) => cols
175+
)
176+
177+
function Base.:(==)(x::polynomial_matrix_t, y::polynomial_matrix_t)
178+
x.timestamp == y.timestamp || return false
179+
x.rows == y.rows || return false
180+
x.cols == y.cols || return false
181+
x.polynomials == y.polynomials || return false
182+
true
183+
end
184+
185+
function Base.rand(::Type{polynomial_matrix_t})
186+
timestamp = rand(Int64)
187+
rows = rand(Int32(0) : Int32(10))
188+
cols = rand(Int32(0) : Int32(10))
189+
polynomials = [[rand(polynomial_t) for col = 1 : cols] for row = 1 : rows]
190+
polynomial_matrix_t(timestamp, rows, cols, polynomials)
191+
end
192+
193+
function hard_coded_example(::Type{polynomial_matrix_t})
194+
polynomials = [[polynomial_t(1234, 4, [5.0, 0.0, 1.0, 3.0])], [polynomial_t(1234, 4, [10.0, 0.0, 2.0, 6.0])]]
195+
polynomial_matrix_t(1234, 2, 1, polynomials)
196+
end
197+
136198
end # module
112 Bytes
Binary file not shown.
52 Bytes
Binary file not shown.

test/test_lcmtype.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ end
3838
test_encode_decode(lcm_test_type_1)
3939
test_encode_decode(lcm_test_type_2)
4040
test_encode_decode(lcm_test_type_3)
41+
test_encode_decode(polynomial_t)
42+
test_encode_decode(polynomial_matrix_t)
4143

4244
# Check that decoding types without `String`s doesn't allocate
4345
test_in_place_decode_noalloc(lcm_test_type_1)
4446
test_in_place_decode_noalloc(lcm_test_type_2)
47+
test_in_place_decode_noalloc(polynomial_t)
48+
test_in_place_decode_noalloc(polynomial_matrix_t)
4549

4650
# Mismatch between length field and length of corresponding vector
4751
bad = rand(lcm_test_type_1)
@@ -55,10 +59,8 @@ end
5559
@test_throws LCMCore.FingerprintException decode(badbytes, lcm_test_type_1)
5660

5761
# Test against byte blobs that were encoded using pylcm
58-
bytes = read(Pkg.dir("LCMCore", "test", "lcmtypes", "lcm_test_type_1_example_bytes"))
59-
@test hard_coded_example(lcm_test_type_1) == decode(bytes, lcm_test_type_1)
60-
bytes = read(Pkg.dir("LCMCore", "test", "lcmtypes", "lcm_test_type_2_example_bytes"))
61-
@test hard_coded_example(lcm_test_type_2) == decode(bytes, lcm_test_type_2)
62-
bytes = read(Pkg.dir("LCMCore", "test", "lcmtypes", "lcm_test_type_3_example_bytes"))
63-
@test hard_coded_example(lcm_test_type_3) == decode(bytes, lcm_test_type_3)
62+
for lcmt in [lcm_test_type_1, lcm_test_type_2, lcm_test_type_3, polynomial_t, polynomial_matrix_t]
63+
bytes = read(Pkg.dir("LCMCore", "test", "lcmtypes", string(lcmt.name.name) * "_example_bytes"))
64+
@test hard_coded_example(lcmt) == decode(bytes, lcmt)
65+
end
6466
end

0 commit comments

Comments
 (0)