Skip to content

Commit

Permalink
Merge pull request #3812 from JuliaReach/schillic/test
Browse files Browse the repository at this point in the history
Use helper methods from `EmptySetModule` and revise tests
  • Loading branch information
schillic authored Feb 11, 2025
2 parents c1a31bc + 9c383d8 commit ce745b2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 65 deletions.
4 changes: 4 additions & 0 deletions src/ConcreteOperations/cartesian_product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ function cartesian_product(U::Universe, H::HalfSpace)
return HalfSpace(prepend_zeros(H.a, dim(U)), H.b)
end

@commutative function cartesian_product(∅::EmptySet, X::LazySet)
return _cartesian_product_emptyset(∅, X)
end

"""
# Extended help
Expand Down
12 changes: 7 additions & 5 deletions src/ConcreteOperations/intersection.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
export intersection!

for T in [:LazySet, :AbstractSingleton, :Interval, :Universe, :LinearMap,
:UnionSet, :UnionSetArray]
@commutative function intersection(∅::EmptySet, X::LazySet)
return _intersection_emptyset(∅, X)
end

# disambiguation
for T in [:AbstractSingleton, :Interval, :Universe, :LinearMap, :UnionSet, :UnionSetArray]
@eval begin
@commutative function intersection(∅::EmptySet, X::$T)
@assert dim(∅) == dim(X) "cannot take the intersection between a " *
"$(dim(∅))-dimensional set and a $(dim(X))-dimensional set"
return
return _intersection_emptyset(∅, X)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions src/LazySets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ include("Sets/Ellipsoid/EllipsoidModule.jl")

include("Sets/EmptySet/EmptySetModule.jl")
@reexport using ..EmptySetModule: EmptySet, ∅
using ..EmptySetModule: _convex_hull_emptyset, _difference_emptyset, _difference_emptyset2,
_distance_emptyset, _isdisjoint_emptyset, _issubset_emptyset,
_issubset_emptyset2, _linear_combination_emptyset,
_minkowski_difference_emptyset, _minkowski_difference_emptyset2,
_minkowski_sum_emptyset
using ..EmptySetModule: _cartesian_product_emptyset, _convex_hull_emptyset, _difference_emptyset,
_difference_emptyset2, _distance_emptyset, _intersection_emptyset,
_isdisjoint_emptyset, _issubset_emptyset, _issubset_emptyset2,
_linear_combination_emptyset, _minkowski_difference_emptyset,
_minkowski_difference_emptyset2, _minkowski_sum_emptyset

include("Sets/HParallelotope/HParallelotopeModule.jl")
@reexport using ..HParallelotopeModule: HParallelotope,
Expand Down
66 changes: 40 additions & 26 deletions test/Sets/EmptySet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,23 @@ for N in [Float64, Float32, Rational{Int}]
@test v isa N && v == N(Inf)
end

# exponential_map / linear_map
for f in (exponential_map, linear_map)
@test_throws AssertionError f(ones(N, 2, 3), E)
E2 = f(ones(N, 2, 2), E)
@test isidentical(E, E2)
end
# exponential_map
@test_throws AssertionError exponential_map(ones(N, 2, 3), E)
@test_throws AssertionError exponential_map(ones(N, 3, 2), E)
E2 = linear_map(ones(N, 3, 2), E)
@test isidentical(E3, E2)
E2 = exponential_map(ones(N, 2, 2), E)
@test isidentical(E, E2)

# in
@test_throws AssertionError N[0] E
@test N[0, 0] E

# linear_map
@test_throws AssertionError exponential_map(ones(N, 2, 3), E)
E2 = exponential_map(ones(N, 2, 2), E)
@test isidentical(E, E2)
E2 = linear_map(ones(N, 3, 2), E)
@test isidentical(E3, E2)

# linear_map_inverse
@test_broken LazySets.linear_map_inverse(ones(N, 2, 3), E) # TODO this should maybe change
# E2 = LazySets.linear_map_inverse(ones(N, 2, 3), E)
Expand Down Expand Up @@ -279,6 +282,9 @@ for N in [Float64, Float32, Rational{Int}]
for E2 in (cartesian_product(E, E3), cartesian_product(E3, E))
@test E2 isa EmptySet{N} && dim(E2) == 5
end
for E2 in (cartesian_product(E, B), cartesian_product(B, E))
@test E2 isa EmptySet{N} && dim(E2) == 4
end

# convex_hull (binary)
@test_throws AssertionError convex_hull(E, E3)
Expand All @@ -291,41 +297,37 @@ for N in [Float64, Float32, Rational{Int}]
# difference
@test_throws AssertionError difference(B, E3)
@test_throws AssertionError difference(E3, B)
for E2 in (difference(E, E), difference(E, B))
for E2 in (difference(E, E), difference(E, B), difference(E, U))
@test isidentical(E, E2)
end
X = difference(B, E)
@test X isa BallInf{N} && X == B
U2 = difference(U, E)
@test U2 isa Universe{N} && U2 == U

# distance (between sets)
@test_throws AssertionError distance(E, E3)
@test_throws AssertionError distance(E3, E)
res = distance(E, E)
@test res isa N && res == N(Inf)

# exact_sum / minkowski_sum
for f in (exact_sum, minkowski_sum)
@test_throws AssertionError f(E, E3)
@test_throws AssertionError f(E3, E)
for E2 in (f(E, E), f(E, B), f(B, E))
@test isidentical(E, E2)
end
end
for E2 in (minkowski_sum(E, U), minkowski_sum(U, E), minkowski_sum(E, Z), minkowski_sum(Z, E),
minkowski_sum(E, B), minkowski_sum(B, E))
@test E2 isa EmptySet{N} && E2 == E
# exact_sum
@test_throws AssertionError exact_sum(E, E3)
@test_throws AssertionError exact_sum(E3, E)
for E2 in (exact_sum(E, E), exact_sum(E, B), exact_sum(B, E))
@test isidentical(E, E2)
end

# intersection
@test_throws AssertionError intersection(E, E3)
for E2 in (intersection(E, E), intersection(E, B), intersection(B, E))
for E2 in (intersection(E, E), intersection(E, B), intersection(B, E),
intersection(E, U), intersection(U, E), intersection(E, Z), intersection(Z, E),)
@test isidentical(E, E2)
end

# isapprox
@test E E
@test !(E E3)
@test !(E3 E)
@test E EmptySet{N}(2)
@test !(E E3) && !(E3 E) && !(E Pe) && !(Pe E)

# isdisjoint
@test_throws AssertionError isdisjoint(E, E3)
Expand All @@ -337,8 +339,8 @@ for N in [Float64, Float32, Rational{Int}]
end

# isequal
@test E == E
@test !(E == E3) && !(E3 == E)
@test E == EmptySet{N}(2)
@test E != E3 && E3 != E && E != B && B != E

# isequivalent
@test_throws AssertionError isequivalent(E, E3)
Expand Down Expand Up @@ -378,6 +380,7 @@ for N in [Float64, Float32, Rational{Int}]
# linear_combination
@test_throws AssertionError linear_combination(E, E3)
for E2 in (linear_combination(E, Pnc), linear_combination(Pnc, E),
linear_combination(E, B), linear_combination(B, E),
linear_combination(E, U), linear_combination(U, E))
@test isidentical(E, E2)
end
Expand All @@ -389,8 +392,19 @@ for N in [Float64, Float32, Rational{Int}]
minkowski_difference(E, U), minkowski_difference(E, Z))
@test isidentical(E, E2)
end
U2 = minkowski_difference(U, E)
@test U2 isa Universe{N} && dim(U2) == 2
X = minkowski_difference(B, E)
@test X isa BallInf{N} && X == B

# minkowski_sum
@test_throws AssertionError minkowski_sum(E, E3)
@test_throws AssertionError minkowski_sum(E3, E)
for E2 in (minkowski_sum(E, E), minkowski_sum(E, B), minkowski_sum(B, E), minkowski_sum(U, E),
minkowski_sum(E, U), minkowski_sum(E, Z), minkowski_sum(Z, E), minkowski_sum(E, B),
minkowski_sum(B, E))
@test isidentical(E, E2)
end
end

for N in [Float64, Float32]
Expand Down
55 changes: 26 additions & 29 deletions test/Sets/Universe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,6 @@ for N in [Float64, Float32, Rational{Int}]
end
X = difference(U, B)
@test X isa UnionSetArray{N,<:HalfSpace} && length(array(X)) == 4 && X == complement(B)
U2 = difference(U, E)
@test isidentical(U, U2)
E2 = difference(E, U)
@test E2 isa EmptySet{N} && E2 == E

# distance (between sets)
@test_throws AssertionError distance(U, U3)
Expand All @@ -304,23 +300,11 @@ for N in [Float64, Float32, Rational{Int}]
@test v isa N && v == N(Inf)
end

# exact_sum / minkowski_sum
for f in (exact_sum, minkowski_sum)
@test_throws AssertionError f(U, U3)
@test_throws AssertionError f(U3, U)
for U2 in (f(U, U), f(U, B), f(B, U))
@test isidentical(U, U2)
end
for E2 in (f(U, E), f(E, U))
@test E2 isa EmptySet{N} && E2 == E
end
for X in (f(U, Pe), f(Pe, U))
@test X isa HPolygon{N} && X == Pe
end
end
for U2 in (minkowski_sum(U, Z), minkowski_sum(Z, U), minkowski_sum(U, B), minkowski_sum(B, U),
minkowski_sum(U, Pnc), minkowski_sum(Pnc, U))
@test U2 isa Universe{N} && U2 == U
# exact_sum
@test_throws AssertionError exact_sum(U, U3)
@test_throws AssertionError exact_sum(U3, U)
for U2 in (exact_sum(U, U), exact_sum(U, B), exact_sum(B, U))
@test isidentical(U, U2)
end

# intersection
Expand All @@ -335,8 +319,8 @@ for N in [Float64, Float32, Rational{Int}]
end

# isapprox
@test U U
@test !(U U3) && !(U3 U)
@test U Universe{N}(2)
@test !(U U3) && !(U3 U) && !(U B) && !(B U)

# isdisjoint
@test_throws AssertionError isdisjoint(U, U3)
Expand All @@ -355,8 +339,8 @@ for N in [Float64, Float32, Rational{Int}]
end

# isequal
@test U == U
@test !(U == U3) && !(U3 == U)
@test U == Universe{N}(2)
@test U != U3 && U3 != U && U != B && B != U

# isequivalent
@test_throws AssertionError isequivalent(U, U3)
Expand Down Expand Up @@ -392,7 +376,7 @@ for N in [Float64, Float32, Rational{Int}]
@test !res && w isa Vector{N} && w X && w U
end
end
# TODO test with non-Universe `X` for which `isuniversal(X) == true` (currently n/a)
# TODO test `U ⊆ X` with non-Universe `X` for which `isuniversal(X) == true` (currently n/a)

# linear_combination
@test_throws AssertionError linear_combination(U, U3)
Expand All @@ -407,13 +391,26 @@ for N in [Float64, Float32, Rational{Int}]
# minkowski_difference
@test_throws AssertionError minkowski_difference(B, U3)
@test_throws AssertionError minkowski_difference(U3, B)
for U2 in (minkowski_difference(U, U), minkowski_difference(U, B),
minkowski_difference(U, E), minkowski_difference(U, Z))
for U2 in (minkowski_difference(U, U), minkowski_difference(U, B), minkowski_difference(U, Z))
@test isidentical(U, U2)
end
E2 = minkowski_difference(B, U)
@test E2 isa EmptySet{N} && dim(E2) == 2
# TODO test with non-Universe `X` for which `isuniversal(X) == true` (currently n/a)
# TODO test `minkowski_difference(X, U)` with non-Universe `X` for which `isuniversal(X) == true` (currently n/a)

# minkowski_sum
@test_throws AssertionError minkowski_sum(U, U3)
@test_throws AssertionError minkowski_sum(U3, U)
for U2 in (minkowski_sum(U, U), minkowski_sum(U, B), minkowski_sum(B, U))
@test isidentical(U, U2)
end
for X in (minkowski_sum(U, Pe), minkowski_sum(Pe, U))
@test X isa HPolygon{N} && X == Pe
end
for U2 in (minkowski_sum(U, Z), minkowski_sum(Z, U), minkowski_sum(U, B), minkowski_sum(B, U),
minkowski_sum(U, Pnc), minkowski_sum(Pnc, U))
@test U2 isa Universe{N} && U2 == U
end
end

for N in [Float64, Float32]
Expand Down

0 comments on commit ce745b2

Please sign in to comment.