Skip to content

Commit 2140dde

Browse files
committed
more tests for basic_types.jl
1 parent 47d943b commit 2140dde

File tree

5 files changed

+148
-30
lines changed

5 files changed

+148
-30
lines changed

src/basic_types.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ end
337337
Base.getindex(mp::MultiPolygon, i) = mp.polygons[i]
338338
Base.size(mp::MultiPolygon) = size(mp.polygons)
339339
Base.length(mp::MultiPolygon) = length(mp.polygons)
340+
Base.:(==)(a::MultiPolygon, b::MultiPolygon) = a.polygons == b.polygons
340341

341342
"""
342343
LineString(points::AbstractVector{<:Point})
@@ -361,6 +362,7 @@ end
361362
Base.getindex(ms::MultiLineString, i) = ms.linestrings[i]
362363
Base.size(ms::MultiLineString) = size(ms.linestrings)
363364
Base.length(mpt::MultiLineString) = length(mpt.linestrings)
365+
Base.:(==)(a::MultiLineString, b::MultiLineString) = a.linestrings == b.linestrings
364366

365367
"""
366368
MultiPoint(points::AbstractVector{AbstractPoint})

test/geometrytypes.jl

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,19 @@ end
204204
@test values(ns) Vec3f[[0.9805807, 0.0, 0.19611615], [0.0, 0.9805807, 0.19611615], [-0.9805807, 0.0, 0.19611615], [0.0, -0.9805807, 0.19611615], [0.0, 0.0, -1.0]]
205205
end
206206

207-
NFace = NgonFace
207+
@testset "Type Utils" begin
208+
@test GeometryBasics.Face(TriangleFace, Int32) == TriangleFace{Int32}
209+
@test GeometryBasics.Face(GLTriangleFace, Int32) == TriangleFace{GLIndex}
210+
end
208211

209212
@testset "Faces" begin
210213
@test convert_simplex(GLTriangleFace, QuadFace{Int}(1, 2, 3, 4)) ==
211214
(GLTriangleFace(1, 2, 3), GLTriangleFace(1, 3, 4))
212-
@test convert_simplex(NFace{3,ZeroIndex{Int}}, QuadFace{ZeroIndex{Int}}(1, 2, 3, 4)) ==
213-
(NFace{3,ZeroIndex{Int}}(1, 2, 3), NFace{3,ZeroIndex{Int}}(1, 3, 4))
214-
@test convert_simplex(NFace{3,OffsetInteger{3,Int}},
215-
NFace{4,OffsetInteger{2,Int}}(1, 2, 3, 4)) ==
216-
(NFace{3,OffsetInteger{3,Int}}(1, 2, 3), NFace{3,OffsetInteger{3,Int}}(1, 3, 4))
215+
@test convert_simplex(NgonFace{3,ZeroIndex{Int}}, QuadFace{ZeroIndex{Int}}(1, 2, 3, 4)) ==
216+
(NgonFace{3,ZeroIndex{Int}}(1, 2, 3), NgonFace{3,ZeroIndex{Int}}(1, 3, 4))
217+
@test convert_simplex(NgonFace{3,OffsetInteger{3,Int}},
218+
NgonFace{4,OffsetInteger{2,Int}}(1, 2, 3, 4)) ==
219+
(NgonFace{3,OffsetInteger{3,Int}}(1, 2, 3), NgonFace{3,OffsetInteger{3,Int}}(1, 3, 4))
217220
@test convert_simplex(LineFace{Int}, QuadFace{Int}(1, 2, 3, 4)) ==
218221
(LineFace{Int}(1, 2), LineFace{Int}(2, 3), LineFace{Int}(3, 4),
219222
LineFace{Int}(4, 1))
@@ -226,6 +229,33 @@ NFace = NgonFace
226229
@test convert_simplex(NgonFace{1,UInt32}, face) === (NgonFace{1,UInt32}((1,)),)
227230
@test convert_simplex(typeof(face), face) === (face,)
228231
end
232+
233+
ps = rand(Point2f, 10)
234+
f = GLTriangleFace(1, 2, 3)
235+
@test ps[f] == Triangle(ps[[1,2,3]]...)
236+
data = [string(i) for i in 1:10]
237+
f = QuadFace(3, 4, 7, 8)
238+
@test data[f] == ("3", "4", "7", "8")
239+
240+
@test hash(f) != hash(QuadFace(1,2,3,4))
241+
@test hash(f) == hash(QuadFace(3,4,7,8))
242+
# cyclic permutation does not change the face
243+
@test hash(f) == hash(QuadFace(7,8,3,4))
244+
@test hash(GLTriangleFace(1,2,3)) == hash(GLTriangleFace(1,2,3))
245+
@test hash(GLTriangleFace(1,2,3)) == hash(GLTriangleFace(2,3,1))
246+
@test hash(GLTriangleFace(1,2,3)) == hash(GLTriangleFace(3,1,2))
247+
end
248+
249+
@testset "FaceView" begin
250+
ps = rand(Point2f, 5)
251+
fs = GLTriangleFace[(1,2,3), (2,3,4), (5,5,5)]
252+
fv = FaceView(ps, fs)
253+
@test faces(fv) == fs
254+
@test values(fv) == ps
255+
@test fv[fs[1]] == ps[fs[1]]
256+
@test !isempty(fv)
257+
@test fv == FaceView(ps, fs)
258+
@test length(fv) == 5
229259
end
230260

231261
@testset "Normals" begin
@@ -253,7 +283,6 @@ end
253283
v_ns = normals(coordinates(c), filter!(f -> f isa QuadFace, faces(c)))[1:end-2]
254284
@test values(ns)[1:15] v_ns[1:15]
255285
@test values(ns)[1:15] v_ns[16:30] # repeated via FaceView in ns
256-
257286
end
258287

259288
@testset "HyperSphere" begin
@@ -455,4 +484,32 @@ end
455484
r = Rect2i(2, 4, 2, 4)
456485
@test M[r] == [53 63 73 83; 54 64 74 84]
457486

487+
end
488+
489+
@testset "LineStrings" begin
490+
ps1 = rand(Point2f, 10)
491+
ls1 = LineString(ps1)
492+
_ls1 = LineString(ps1)
493+
@test coordinates(ls1) == ps1
494+
@test length(ls1) == 10
495+
@test ls1 == _ls1
496+
497+
ls2 = LineString(rand(Point2f, 6))
498+
ls3 = LineString(rand(Point2f, 4))
499+
mls = MultiLineString([ls1, ls2, ls3])
500+
@test mls.linestrings == [ls1, ls2, ls3]
501+
@test mls[1] == ls1
502+
@test mls[2] == ls2
503+
@test mls[3] == ls3
504+
@test size(mls) == (3, ) # TODO: Does this make sense?
505+
@test length(mls) == 3
506+
@test MultiLineString(OffsetArray([ls1, ls2, ls3], 0)) == mls
507+
end
508+
509+
@testset "MultiPoint" begin
510+
ps1 = rand(Point2f, 10)
511+
mp = MultiPoint(ps1)
512+
@test all(getindex.(Ref(mp), 1:10) .== ps1)
513+
@test size(mp) == (10, ) # TODO: Does this make sense?
514+
@test length(mp) == 10
458515
end

test/meshes.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,50 @@ end
173173
@test hasproperty(m, :uv)
174174
@test hasproperty(m, :faces)
175175

176+
@test GeometryBasics.meta(m) == NamedTuple()
177+
@test Mesh(m) === m
178+
176179
mm = MetaMesh(m, name = "test")
177180

178181
@test Mesh(mm) == m
179182
@test haskey(mm, :name)
180183
@test get(mm, :name, nothing) == "test"
184+
@test get(() -> nothing, mm, :name) == "test"
185+
@test get(mm, :foo, nothing) === nothing
186+
@test get(() -> nothing, mm, :foo) === nothing
181187
@test mm[:name] == "test"
182188
@test !haskey(mm, :foo)
183-
@test get!(mm, :foo, "bar") == "bar"
189+
@test get!(mm, :foo, "foo") == "foo"
184190
@test haskey(mm, :foo)
191+
@test get!(() -> "bar", mm, :bar) == "bar"
192+
@test haskey(mm, :bar)
193+
mm[:foo] = "bar"
194+
@test mm[:foo] == "bar"
195+
delete!(mm, :bar)
196+
@test !haskey(mm, :bar)
185197
@test keys(mm) == keys(getfield(mm, :meta))
186198

187199
@test vertex_attributes(mm) == getfield(m, :vertex_attributes)
188200
@test coordinates(mm) == vertex_attributes(m)[:position]
189201
@test normals(mm) == vertex_attributes(m)[:normal]
190202
@test texturecoordinates(mm) == vertex_attributes(m)[:uv]
191203
@test faces(mm) == getfield(m, :faces)
204+
205+
@test hasproperty(mm, :vertex_attributes)
206+
@test hasproperty(mm, :position)
207+
@test hasproperty(mm, :normal)
208+
@test hasproperty(mm, :uv)
209+
@test hasproperty(mm, :faces)
210+
@test propertynames(mm) == (:mesh, :meta, propertynames(m)...)
211+
212+
@test mm.vertex_attributes == getfield(m, :vertex_attributes)
213+
@test mm.position == vertex_attributes(m)[:position]
214+
@test mm.normal == vertex_attributes(m)[:normal]
215+
@test mm.uv == vertex_attributes(m)[:uv]
216+
@test mm.faces == getfield(m, :faces)
217+
218+
@test GeometryBasics.meta(mm) == mm.meta
219+
@test Mesh(mm) === mm.mesh
192220
end
193221

194222
@testset "mesh() constructors" begin

test/polygons.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
@testset "Polygon" begin
2-
@testset "from points" begin
2+
@testset "Constructors" begin
33
points = connect([1, 2, 3, 4, 5, 6], Point2f)
44
polygon = Polygon(points)
55
@test polygon == Polygon(points)
6+
@test polygon == copy(polygon)
7+
@test coordinates(polygon) == points
8+
@test Polygon(OffsetArray(points)) == polygon
9+
10+
interiors = [rand(Point2f, 4), rand(Point2f, 4)]
11+
exterior = rand(Point2f, 5)
12+
p1 = Polygon(exterior, interiors)
13+
@test p1.interiors == interiors
14+
@test p1.exterior == exterior
15+
p2 = Polygon(OffsetArray(exterior, 0), interiors)
16+
@test p2 == p1
17+
18+
# TODO: promote polygon type automatically
19+
polygon = Polygon(Point2f.(points))
20+
mp = MultiPolygon([polygon, p1, p2])
21+
@test mp.polygons == [polygon, p1, p2]
22+
@test mp[1] == polygon
23+
@test mp[2] == p1
24+
@test size(mp) == (3,) # TODO: What does size even mean here?
25+
@test length(mp) == 3
26+
@test MultiPolygon(OffsetArray([polygon, p1, p2], 0)) == mp
627
end
728

829
rect = Rect2f(0, 0, 1, 1)

test/runtests.jl

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ end
9292
tetrahedra = connect(x, NSimplex{4})
9393
@test tetrahedra == [Tetrahedron(x[1], x[2], x[3], x[4])]
9494

95+
@test length(tetrahedra[1]) == 4
96+
@test all(coordinates(tetrahedra[1]) .== x)
97+
9598
@testset "matrix non-copy point views" begin
9699
# point in row
97100
points = [1 2; 1 4; 66 77]
@@ -237,36 +240,43 @@ end
237240
@test GeometryBasics.faces(m) isa Vector{GLTriangleFace}
238241
end
239242

240-
@testset "lines intersects" begin
243+
@testset "Lines" begin
241244
a = Line(Point(0.0, 0.0), Point(4.0, 1.0))
242245
b = Line(Point(0.0, 0.25), Point(3.0, 0.25))
243246
c = Line(Point(0.0, 0.25), Point(0.5, 0.25))
244247
d = Line(Point(0.0, 0.0), Point(0.0, 4.0))
245248
e = Line(Point(1.0, 0.0), Point(0.0, 4.0))
246249
f = Line(Point(5.0, 0.0), Point(6.0, 0.0))
247250

248-
@test intersects(a, b) === (true, Point(1.0, 0.25))
249-
@test intersects(a, c) === (false, Point(0.0, 0.0))
250-
@test intersects(d, d) === (false, Point(0.0, 0.0))
251-
found, point = intersects(d, e)
252-
@test found && point Point(0.0, 4.0)
253-
@test intersects(a, f) === (false, Point(0.0, 0.0))
254-
255-
# issue #168
256-
# If these tests fail then you can increase the tolerance on the checks so
257-
# long as you know what you're doing :)
258-
line_helper(a, b, c, d) = Line(Point(a, b), Point(c, d))
259-
b, loc = intersects(line_helper(-3.1, 15.588457268119894, 3.1, 15.588457268119894),
260-
line_helper(2.0866025403784354, 17.37050807568877, -4.0866025403784505, 13.806406460551015))
261-
@test b
262-
@test loc Point(-1.0000000000000058, 15.588457268119894)
263-
264-
b, loc = intersects(line_helper(5743.933982822018, 150.0, 5885.355339059327, -50.0),
265-
line_helper(5760.0, 100.0, 5760.0, 140.0))
266-
@test b
267-
@test loc Point(5760.0, 127.27922061357884)
251+
multi_line = [a,b,c,d,e,f]
252+
@test coordinates(multi_line) == vcat([[x.points...] for x in multi_line]...)
253+
254+
@testset "intersects" begin
255+
@test intersects(a, b) === (true, Point(1.0, 0.25))
256+
@test intersects(a, c) === (false, Point(0.0, 0.0))
257+
@test intersects(d, d) === (false, Point(0.0, 0.0))
258+
found, point = intersects(d, e)
259+
@test found && point Point(0.0, 4.0)
260+
@test intersects(a, f) === (false, Point(0.0, 0.0))
261+
262+
# issue #168
263+
# If these tests fail then you can increase the tolerance on the checks so
264+
# long as you know what you're doing :)
265+
line_helper(a, b, c, d) = Line(Point(a, b), Point(c, d))
266+
b, loc = intersects(line_helper(-3.1, 15.588457268119894, 3.1, 15.588457268119894),
267+
line_helper(2.0866025403784354, 17.37050807568877, -4.0866025403784505, 13.806406460551015))
268+
@test b
269+
@test loc Point(-1.0000000000000058, 15.588457268119894)
270+
271+
b, loc = intersects(line_helper(5743.933982822018, 150.0, 5885.355339059327, -50.0),
272+
line_helper(5760.0, 100.0, 5760.0, 140.0))
273+
@test b
274+
@test loc Point(5760.0, 127.27922061357884)
275+
end
268276
end
269277

278+
279+
270280
@testset "Offsetintegers" begin
271281
x = 1
272282
@test GeometryBasics.raw(x) isa Int

0 commit comments

Comments
 (0)