From 68b687191e2ef48bb3775e651a6e4e35b711fc8a Mon Sep 17 00:00:00 2001 From: schillic Date: Wed, 17 Jul 2024 18:17:13 +0200 Subject: [PATCH] add tests --- src/Sets/HalfSpace/HalfSpaceModule.jl | 3 +++ src/Sets/HalfSpace/rand.jl | 4 ++-- test/Sets/HalfSpace.jl | 33 +++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/Sets/HalfSpace/HalfSpaceModule.jl b/src/Sets/HalfSpace/HalfSpaceModule.jl index a07013a2bb..04d8e4e705 100644 --- a/src/Sets/HalfSpace/HalfSpaceModule.jl +++ b/src/Sets/HalfSpace/HalfSpaceModule.jl @@ -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 diff --git a/src/Sets/HalfSpace/rand.jl b/src/Sets/HalfSpace/rand.jl index d4a701d0aa..479ba22844 100644 --- a/src/Sets/HalfSpace/rand.jl +++ b/src/Sets/HalfSpace/rand.jl @@ -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 diff --git a/test/Sets/HalfSpace.jl b/test/Sets/HalfSpace.jl index 7d29013a9d..e55205909c 100644 --- a/test/Sets/HalfSpace.jl +++ b/test/Sets/HalfSpace.jl @@ -109,13 +109,21 @@ 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 @@ -123,6 +131,9 @@ for N in [Float64, Rational{Int}, Float32] @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 @@ -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), @@ -221,11 +235,17 @@ 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) @@ -233,3 +253,6 @@ for N in [Float64] HalfSpace([-2.0, 0.0, 1.0, -5.0, 0.0], 10.0) end end + +# isoperationtype +@test !isoperationtype(HalfSpace)