Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Jul 17, 2024
1 parent a0279d1 commit 68b6871
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/Sets/HalfSpace/HalfSpaceModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ function load_symengine_halfspace()
julia> all(_is_halfspace.([:(x1 <= 0), :(x1 < 0), :(x1 > 0), :(x1 >= 0)]))
true
julia> _is_halfspace(:(x1 = 0))
false
julia> _is_halfspace(:(2*x1 <= 4))
true
Expand Down
4 changes: 2 additions & 2 deletions src/Sets/HalfSpace/rand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function rand(::Type{HalfSpace};
rng = reseed!(rng, seed)
a = randn(rng, N, dim)
while iszero(a)
a = randn(rng, N, dim)
end
a = randn(rng, N, dim) # COV_EXCL_LINE
end # COV_EXCL_LINE
b = randn(rng, N)
return HalfSpace(a, b)
end
33 changes: 28 additions & 5 deletions test/Sets/HalfSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,31 @@ for N in [Float64, Rational{Int}, Float32]
@test !is_intersection_empty(hs1, hs4) && !empty_intersection && v hs1 && v hs4

# check for tighter constraint
c1 = HalfSpace([1.0, 0.0], 1.0)
c2 = HalfSpace([2.0, 0.0], 2.0)
c3 = HalfSpace([2.0, 0.0], 1.0)
c1 = HalfSpace(N[1, 0], N(1))
c2 = HalfSpace(N[2, 0], N(2))
c3 = HalfSpace(N[2, 0], N(1))
@test LazySets.is_tighter_same_dir_2D(c1, c2) &&
LazySets.is_tighter_same_dir_2D(c3, c2)
LazySets.is_tighter_same_dir_2D(c3, c2) &&
LazySets.is_tighter_same_dir_2D(c1, c1)
@test !LazySets.is_tighter_same_dir_2D(c1, c2; strict=true) &&
LazySets.is_tighter_same_dir_2D(c3, c2; strict=true)
LazySets.is_tighter_same_dir_2D(c3, c2; strict=true) &&
!LazySets.is_tighter_same_dir_2D(c1, c1; strict=true)
c4 = HalfSpace(N[0, 1], N(2))
c5 = HalfSpace(N[0, 1], N(1))
@test !LazySets.is_tighter_same_dir_2D(c4, c5) &&
LazySets.is_tighter_same_dir_2D(c5, c4)
@test !LazySets.is_tighter_same_dir_2D(c4, c5; strict=true) &&
LazySets.is_tighter_same_dir_2D(c5, c4; strict=true)

# test concrete linear map of a half-space
H = HalfSpace(N[1, -1], N(0)) # x <= y
M = N[1 0; 0 0] # non-invertible matrix
@test_throws ArgumentError linear_map(M, H, algorithm="vrep")
M = N[2 2; 0 1] # invertible matrix
@test linear_map(M, H) == HalfSpace(N[0.5, -2.0], N(0.0))
M = zeros(N, 2, 2) # result is a singleton
X = linear_map(M, H)
@test X isa HPolyhedron && isequivalent(X, ZeroSet{N}(2))

if test_suite_polyhedra
if N == Float64 || N == Float32
Expand All @@ -140,6 +151,9 @@ for N in [Float64, Rational{Int}, Float32]
hs_vec = convert(HalfSpace{N,Vector{N}}, hs_sev)
@test hs_vec.a == N[0, 1, 0] && hs_vec.b == N(1)

# complement
@test complement(HalfSpace(N[1, -2], N(3))) == HalfSpace(N[-1, 2], N(-3))

# complement check
for (h1, h2, eq) in [(HalfSpace(N[-1], N(0)), HalfSpace(N[1 // 2], N(0)), true),
(HalfSpace(N[1, 3], N(1)), HalfSpace(N[-2, -6], N(-2)), true),
Expand Down Expand Up @@ -221,15 +235,24 @@ for N in [Float64]
@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_throws ArgumentError HalfSpace(2x == 5y, vars)

# 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 passing a combination of operations
vars = @variables x[1:2] t
@test HalfSpace(x[1] <= t, vars) == HalfSpace([1.0, 0.0, -1.0], 0.0)

# test with sparse variables
@variables x[1:5]
@test HalfSpace(2x[1] + 5x[4] <= 10.0, x) == HalfSpace([2.0, 0.0, 0.0, 5.0, 0.0], 10.0)
@test HalfSpace(2x[1] + 5x[4] >= -10.0 + x[3], x) ==
HalfSpace([-2.0, 0.0, 1.0, -5.0, 0.0], 10.0)
end
end

# isoperationtype
@test !isoperationtype(HalfSpace)

0 comments on commit 68b6871

Please sign in to comment.