Skip to content

Commit

Permalink
Add tests for neighborhood search
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed Feb 19, 2021
1 parent 74b6187 commit d16f15d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
80 changes: 80 additions & 0 deletions test/neighborsearch.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
@testset "Neighborhood search" begin
@testset "NeighborhoodSearch" begin
π’Ÿ = CartesianGrid((10, 10), T.((-0.5,-0.5)), T.((1.0,1.0)))

S = NeighborhoodSearch(π’Ÿ, NormBall(T(1)))
n = search(P2(0,0), S)
@test Set(n) == Set([1,2,11])
n = search(P2(9,0), S)
@test Set(n) == Set([9,10,20])
n = search(P2(0,9), S)
@test Set(n) == Set([91,81,92])
n = search(P2(9,9), S)
@test Set(n) == Set([100,99,90])

S = NeighborhoodSearch(π’Ÿ, NormBall(T(√2+eps(T))))
n = search(P2(0,0), S)
@test Set(n) == Set([1,2,11,12])
n = search(P2(9,0), S)
@test Set(n) == Set([9,10,19,20])
n = search(P2(0,9), S)
@test Set(n) == Set([81,82,91,92])
n = search(P2(9,9), S)
@test Set(n) == Set([89,90,99,100])

# non MinkowskiMetric example
π’Ÿ = CartesianGrid((360,180), T.((0.0,-90.0)), T.((1.0,1.0)))
S = NeighborhoodSearch(π’Ÿ, NormBall(T(150), Haversine(T(6371))))
n = search(P2(0,0), S)
@test Set(n) == Set([32041, 32400, 32401, 32760])
end

@testset "KNearestSearch" begin
π’Ÿ = CartesianGrid((10,10), T.((-0.5,-0.5)), T.((1.0,1.0)))
S = KNearestSearch(π’Ÿ, 3)
n = search(P2(0,0), S)
@test Set(n) == Set([1,2,11])
n = search(P2(9,0), S)
@test Set(n) == Set([9,10,20])
n = search(P2(0,9), S)
@test Set(n) == Set([91,81,92])
n = search(P2(9,9), S)
@test Set(n) == Set([100,99,90])
end

@testset "KBallSearch" begin
π’Ÿ = CartesianGrid((10,10), T.((-0.5,-0.5)), T.((1.0,1.0)))

s = KBallSearch(π’Ÿ, 10, NormBall(T(100)))
n = search(P2(5,5), s)
@test length(n) == 10

s = KBallSearch(π’Ÿ, 10, NormBall(T(1)))
n = search(P2(5,5), s)
@test length(n) == 5
@test n[1] == 56

mask = trues(nelements(π’Ÿ))
mask[56] = false
n = search(P2(5,5), s, mask=mask)
@test length(n) == 4
n = search(P2(-0.2,-0.2), s)
@test length(n) == 1
n = search(P2(-10,-10), s)
@test length(n) == 0
end

@testset "BoundedSearch" begin
π’Ÿ = CartesianGrid((10,10), T.((-0.5,-0.5)), T.((1.0,1.0)))
S1 = NeighborhoodSearch(π’Ÿ, NormBall(T(5)))
S2 = KNearestSearch(π’Ÿ, 10)
B1 = BoundedSearch(S1, 5)
B2 = BoundedSearch(S2, 5)
p = P2(coordinates(π’Ÿ, rand(1:100)))
n = search(p, B1)
@test length(n) == 5
p = P2(coordinates(π’Ÿ, rand(1:100)))
n = search(p, B2)
@test length(n) == 5
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ testfiles = [
"traits.jl",
"views.jl",
"neighborhoods.jl",
"neighborsearch.jl",
"sampling.jl",
"discretization.jl",
"boundingboxes.jl"
Expand Down

0 comments on commit d16f15d

Please sign in to comment.