Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix global-variable issues in tests #2318

Merged
merged 1 commit into from
Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions test/unit_HalfSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,35 +138,34 @@ for N in [Float64]
o = N[0.07768723948819561, -0.5762273280928935, 0.28897399484750297, 1.9299362784322858]
H = HalfSpace(N[-0.09291863543681655, -0.2176689899601838, -0.07453829739226348, 0.048948632014371496], N(0.1911363393469332))
@test o ∈ H
end

# tests that require ModelingToolkit
@static if VERSION >= v"1.3" && isdefined(@__MODULE__, :ModelingToolkit)

vars = @variables x y
@test HalfSpace(2x + 3y < 5) == HalfSpace([2.0, 3.0], 5.0)
@test HalfSpace(2x + 3y < 5, vars) == HalfSpace([2.0, 3.0], 5.0)
@test HalfSpace(2x + 3y < 5, N=Int) == HalfSpace([2, 3], 5)
# tests that require ModelingToolkit
@static if VERSION >= v"1.3" && isdefined(@__MODULE__, :ModelingToolkit)
vars = @variables x y
@test HalfSpace(2x + 3y < 5) == HalfSpace([2.0, 3.0], 5.0)
@test HalfSpace(2x + 3y < 5, vars) == HalfSpace([2.0, 3.0], 5.0)
@test HalfSpace(2x + 3y < 5, N=Int) == HalfSpace([2, 3], 5)

@test HalfSpace(2x + 3y > 5) == HalfSpace([-2.0, -3.0], -5.0)
@test HalfSpace(2x + 3y > 5, vars) == HalfSpace([-2.0, -3.0], -5.0)
@test HalfSpace(2x + 3y > 5) == HalfSpace([-2.0, -3.0], -5.0)
@test HalfSpace(2x + 3y > 5, vars) == HalfSpace([-2.0, -3.0], -5.0)

@test HalfSpace(2x + 3y ≤ 5) == HalfSpace([2.0, 3.0], 5.0)
@test HalfSpace(2x + 3y ≤ 5, vars) == HalfSpace([2.0, 3.0], 5.0)
@test HalfSpace(2x + 3y ≤ 5) == HalfSpace([2.0, 3.0], 5.0)
@test HalfSpace(2x + 3y ≤ 5, vars) == HalfSpace([2.0, 3.0], 5.0)

@test HalfSpace(2x <= 5y - 1) == HalfSpace([2.0, -5.0], -1.0)
@test HalfSpace(2x ≤ 5y - 1) == HalfSpace([2.0, -5.0], -1.0)
@test HalfSpace(2x <= 5y - 1) == HalfSpace([2.0, -5.0], -1.0)
@test HalfSpace(2x ≤ 5y - 1) == HalfSpace([2.0, -5.0], -1.0)

@test HalfSpace(2x + 3y ≥ 5) == HalfSpace([-2.0, -3.0], -5.0)
@test HalfSpace(2x + 3y ≥ 5, vars) == HalfSpace([-2.0, -3.0], -5.0)
@test HalfSpace(2x + 3y ≥ 5) == HalfSpace([-2.0, -3.0], -5.0)
@test HalfSpace(2x + 3y ≥ 5, vars) == HalfSpace([-2.0, -3.0], -5.0)

# doesn't work because get_vars returns variables [y, x]
# => both tests below require vars to pass
@test HalfSpace(2x ≥ 5y - 1, vars) == HalfSpace([-2.0, 5.0], 1.0)
@test HalfSpace(2x >= 5y - 1, vars) == HalfSpace([-2.0, 5.0], 1.0)
# doesn't work because get_vars returns variables [y, x]
# => both tests below require vars to pass
@test HalfSpace(2x ≥ 5y - 1, vars) == HalfSpace([-2.0, 5.0], 1.0)
@test HalfSpace(2x >= 5y - 1, vars) == HalfSpace([-2.0, 5.0], 1.0)

# test with sparse variables
@variables x[1:5]
@test HalfSpace(2x[1] + 5x[4] <= 10., x) == HalfSpace([2.0, 0.0, 0.0, 5.0, 0.0], 10.0)
@test HalfSpace(2x[1] + 5x[4] >= -10. + x[3], x) == HalfSpace([-2.0, 0.0, 1.0, -5.0, 0.0], 10.0)
# test with sparse variables
@variables x[1:5]
@test HalfSpace(2x[1] + 5x[4] <= 10., x) == HalfSpace([2.0, 0.0, 0.0, 5.0, 0.0], 10.0)
@test HalfSpace(2x[1] + 5x[4] >= -10. + x[3], x) == HalfSpace([-2.0, 0.0, 1.0, -5.0, 0.0], 10.0)
end
end
34 changes: 17 additions & 17 deletions test/unit_Hyperplane.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,22 @@ for N in [Float64]
b = BallInf(N[2, 2, 2], N(1))
empty_intersection, v = is_intersection_empty(b, hp, true)
@test !empty_intersection && !is_intersection_empty(b, hp) && v ∈ hp && v ∈ b
end

# tests that require ModelingToolkit
@static if VERSION >= v"1.3" && isdefined(@__MODULE__, :ModelingToolkit)
vars = @variables x y
@test Hyperplane(2x + 3y == 5) == Hyperplane([2.0, 3.0], 5.0)
@test Hyperplane(2x + 3y == 5, N=Int) == Hyperplane([2, 3], 5)
@test Hyperplane(2x + 3y == 5, vars) == Hyperplane([2.0, 3.0,], 5.0)
@test Hyperplane(2x == 5y) == Hyperplane([2.0, -5.0,], 0.0)
@test Hyperplane(2x == 5y, vars) == Hyperplane([2.0, -5.0,], 0.0)

# test with sparse variables
@variables x[1:5]
@test Hyperplane(2x[1] + 5x[4] == 10., x) == Hyperplane([2.0, 0.0, 0.0, 5.0, 0.0], 10.0)

# test passing a combination of operations
vars = @variables x[1:2] t
@test Hyperplane(x[1] == t, vars) == Hyperplane([1.0, 0.0, -1.0], 0.0)
# tests that require ModelingToolkit
@static if VERSION >= v"1.3" && isdefined(@__MODULE__, :ModelingToolkit)
vars = @variables x y
@test Hyperplane(2x + 3y == 5) == Hyperplane([2.0, 3.0], 5.0)
@test Hyperplane(2x + 3y == 5, N=Int) == Hyperplane([2, 3], 5)
@test Hyperplane(2x + 3y == 5, vars) == Hyperplane([2.0, 3.0,], 5.0)
@test Hyperplane(2x == 5y) == Hyperplane([2.0, -5.0,], 0.0)
@test Hyperplane(2x == 5y, vars) == Hyperplane([2.0, -5.0,], 0.0)

# test with sparse variables
@variables x[1:5]
@test Hyperplane(2x[1] + 5x[4] == 10., x) == Hyperplane([2.0, 0.0, 0.0, 5.0, 0.0], 10.0)

# test passing a combination of operations
vars = @variables x[1:2] t
@test Hyperplane(x[1] == t, vars) == Hyperplane([1.0, 0.0, -1.0], 0.0)
end
end
20 changes: 10 additions & 10 deletions test/unit_Intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@ for N in [Float64, Rational{Int}, Float32]
@test LazySets.is_array_constructor(IntersectionArray)

# intersection of an array of sets
IA = IntersectionArray([B, H])
IArr = IntersectionArray([B, H])

# dim
@test dim(IA) == 2
@test dim(IArr) == 2

# support vector (currently throws an error)
@test_throws ErrorException σ(ones(N, 2), IA)
@test_throws ErrorException σ(ones(N, 2), IArr)

# boundedness
@test isbounded(IA)
@test isbounded(IArr)
@test isbounded(IntersectionArray([Singleton(N[1]), HalfSpace(N[1], N(1))]))
# the following tests crash because ρ(::IntersectionArray) is not implemented yet
@test_throws ErrorException isbounded(IntersectionArray([HalfSpace(N[1], N(1)), HalfSpace(N[1], N(-1))]))
@test_throws ErrorException !isbounded(IntersectionArray([HalfSpace(ones(N, 2), N(1)), HalfSpace(ones(N, 2), N(-1))]))

# isempty
@test_throws MethodError isempty(IA)
@test_throws MethodError isempty(IArr)

# membership
@test ones(N, 2) ∈ IA && N[5, 5] ∉ IA
@test ones(N, 2) ∈ IArr && N[5, 5] ∉ IArr

# array getter
v = Vector{LazySet{N}}()
Expand All @@ -70,15 +70,15 @@ for N in [Float64, Rational{Int}, Float32]
IntersectionArray(10, N)

# concretize
@test concretize(IA) == intersection(B, H)
@test concretize(IArr) == intersection(B, H)

# ================
# common functions
# ================

# absorbing element
@test absorbing(Intersection) == absorbing(IntersectionArray) == EmptySet
@test I ∩ E == E ∩ I == IA ∩ E == E ∩ IA == E ∩ E == E
@test I ∩ E == E ∩ I == IArr ∩ E == E ∩ IArr == E ∩ E == E
end

# ======================
Expand All @@ -89,9 +89,9 @@ for N in [Float64]
B = BallInf(ones(N, 2), N(3))
H = Hyperrectangle(ones(N, 2), ones(N, 2))
I = B ∩ H
IA = IntersectionArray([B, H])
IArr = IntersectionArray([B, H])
clist1 = constraints_list(I)
clist2 = constraints_list(IA)
clist2 = constraints_list(IArr)
@test ispermutation(clist1, clist2) &&
ispermutation(clist1, [HalfSpace(N[1, 0], N(2)),
HalfSpace(N[0, 1], N(2)),
Expand Down
17 changes: 8 additions & 9 deletions test/unit_Polyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ for N in [Float64, Rational{Int}, Float32]
end

# default Float64 constructors
unconstrained_HPolyhedron = HPolyhedron()
@test unconstrained_HPolyhedron isa HPolyhedron{Float64}
@test HPolyhedron() isa HPolyhedron{Float64}

# tests that only work with Float64 and Float32
for N in [Float64, Float32]
Expand Down Expand Up @@ -356,12 +355,12 @@ for N in [Float64]
@test πP isa HPolyhedron{N}
@test ispermutation(constraints_list(πP), [HalfSpace(N[-1, 0], N(0)), HalfSpace(N[0, -1], N(0))])
end
end

# tests that require ModelingToolkit
@static if VERSION >= v"1.3" && isdefined(@__MODULE__, :ModelingToolkit)
vars = @variables x y
p1 = HPolyhedron([x + y <= 1, x + y >= -1, x - y <= 1, x - y >= -1], vars)
b1 = Ball1(zeros(2), 1.0)
@test isequivalent(p1, b1)
# tests that require ModelingToolkit
@static if VERSION >= v"1.3" && isdefined(@__MODULE__, :ModelingToolkit)
vars = @variables x y
p1 = HPolyhedron([x + y <= 1, x + y >= -1, x - y <= 1, x - y >= -1], vars)
b1 = Ball1(zeros(2), 1.0)
@test isequivalent(p1, b1)
end
end
2 changes: 1 addition & 1 deletion test/unit_comparisons.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using LazySets: _leq, _geq, isapproxzero, _isapprox, _ztol

# approximate <= and
# approximate <=
@test _leq(2e-15, 1e-15) && _leq(1e-15, 2e-15)
@test _leq(1//100, 1//99) && _leq(1//100, 0.099)

Expand Down
32 changes: 17 additions & 15 deletions test/unit_samples.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using Distributions: Uniform

P1 = BallInf([0.6, 0.1, -1.3, -0.4, 0.2], 0.6)
A = [1.0 0.0;
0.0 1.0;
-1.0 0.0;
0.0 -1.0]
b = [1.0, 2.0, 3.0, 4.0]
P2 = HPolyhedron(A, b)
P3 = HPolyhedron([0.0 0.1], [3.0])
P4 = Ball2([0.2, -0.3, -1.1, 0.6, -0.7], 0.4)
for N in [Float64]
P1 = BallInf([0.6, 0.1, -1.3, -0.4, 0.2], 0.6)
A = [1.0 0.0;
0.0 1.0;
-1.0 0.0;
0.0 -1.0]
b = [1.0, 2.0, 3.0, 4.0]
P2 = HPolyhedron(A, b)
P3 = HPolyhedron([0.0 0.1], [3.0])
P4 = Ball2([0.2, -0.3, -1.1, 0.6, -0.7], 0.4)

## Test rand samples are contained in the set
p1 = LazySets.sample(P1)
@test p1 ∈ P1
p1_samples = LazySets.sample(P1, 100)
@test sum(p1_samples .∈ Ref(P1)) == length(p1_samples)
## Test rand samples are contained in the set
p1 = LazySets.sample(P1)
@test p1 ∈ P1
p1_samples = LazySets.sample(P1, 100)
@test sum(p1_samples .∈ Ref(P1)) == length(p1_samples)

@test LazySets.RejectionSampler(P2).box_approx == [Uniform(-3.0,1.0), Uniform(-4.0,2.0)]
@test LazySets.RejectionSampler(P2).box_approx == [Uniform(-3.0,1.0), Uniform(-4.0,2.0)]
end
2 changes: 1 addition & 1 deletion test/unit_util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ for _dummy_ in 1:1 # avoid global variable warnings
# removal of zero columns
A = N[1 2; 3 4]
@test remove_zero_columns(A) === A
@test remove_zero_columns(sparse(A)) == A
@test remove_zero_columns(sparse(A)) == A
B = N[1 0 2; 3 0 4]
@test remove_zero_columns(B) == remove_zero_columns(sparse(B)) == A

Expand Down