From cfa2385dfc74451342f061a9de0a563c275d5a02 Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 15 Jun 2019 18:31:52 +0200 Subject: [PATCH] add unit tests for AbstractZonotope --- test/unit_Hyperrectangle.jl | 11 ++++++++++- test/unit_LineSegment.jl | 9 ++++++++- test/unit_Singleton.jl | 5 ++++- test/unit_Zonotope.jl | 5 +++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/test/unit_Hyperrectangle.jl b/test/unit_Hyperrectangle.jl index 388f94bd02..3b4e795fe5 100644 --- a/test/unit_Hyperrectangle.jl +++ b/test/unit_Hyperrectangle.jl @@ -2,7 +2,7 @@ for N in [Float64, Rational{Int}, Float32] # random hyperrectangle rand(Hyperrectangle) - # center/radius/high/low + # center/radius/high/low/generators c = N[0, 0] r = N[1, 1] h = Hyperrectangle(c, r) @@ -15,6 +15,15 @@ for N in [Float64, Rational{Int}, Float32] @test high(h, i) == r[i] @test low(h, i) == -r[i] end + gens = [Vector(g) for g in generators(h)] + @test ispermutation(gens, [N[1, 0], N[0, 1]]) + @test genmat(h) ∈ [N[1 0; 0 1], N[0 1; 1 0]] + @test ngens(h) == 2 + h_flat = Hyperrectangle(N[1, 2, 3, 4, 5], N[1, 0, 2, 0, 3]) + @test collect(generators(h_flat)) == + [UnitVector(1, 5, N(1)), UnitVector(3, 5, N(2)), UnitVector(5, 5, N(3))] + @test genmat(h_flat) == N[1 0 0; 0 0 0; 0 2 0; 0 0 0; 0 0 3] + @test ngens(h_flat) == 3 # 1D Hyperrectangle h = Hyperrectangle(N[0], N[1]) diff --git a/test/unit_LineSegment.jl b/test/unit_LineSegment.jl index 911fa2756c..0074282af5 100644 --- a/test/unit_LineSegment.jl +++ b/test/unit_LineSegment.jl @@ -30,8 +30,15 @@ for N in [Float64, Rational{Int}, Float32] @test !∈(N[7, 4], l) @test !∈(N[1.5, 1.6], l) - # center function + # center/generators @test center(l) == N[1.5, 1.5] + @test collect(generators(l)) ∈ [[N[1/2, 1/2]], [N[-1/2, -1/2]]] + @test genmat(l) ∈ [hcat(N[1/2 1/2]), hcat(N[-1/2, -1/2])] + @test ngens(l) == 1 + l_degenerate = LineSegment(p, p) + @test collect(generators(l_degenerate)) == Vector{N}() + @test genmat(l_degenerate) == Matrix{N}(undef, 2, 0) + @test ngens(l_degenerate) == 0 # isempty @test !isempty(l) diff --git a/test/unit_Singleton.jl b/test/unit_Singleton.jl index 470fa5ca83..46e61750e8 100644 --- a/test/unit_Singleton.jl +++ b/test/unit_Singleton.jl @@ -2,7 +2,7 @@ for N in [Float64, Rational{Int}, Float32] # random singleton rand(Singleton) - # center/radius/high/low + # center/radius/high/low/generators c = N[0, 0] r = N[0, 0] s = Singleton(c) @@ -15,6 +15,9 @@ for N in [Float64, Rational{Int}, Float32] @test high(s, i) == r[i] @test low(s, i) == -r[i] end + @test collect(generators(s)) == Vector{Vector{N}}() + @test genmat(s) == Matrix{N}(undef, 2, 0) + @test ngens(s) == 0 # 1D singleton s = Singleton(N[1]) diff --git a/test/unit_Zonotope.jl b/test/unit_Zonotope.jl index 8b42dfc58e..d547f6db94 100644 --- a/test/unit_Zonotope.jl +++ b/test/unit_Zonotope.jl @@ -103,6 +103,7 @@ for N in [Float64, Rational{Int}, Float32] Z = Zonotope(N[2, 1], N[-0.5 1.5 0.5 1; 0.5 1.5 1 0.5]) @test ngens(Z) == 4 @test genmat(Z) == Z.generators + @test ispermutation(collect(generators(Z)), [genmat(Z)[:, j] for j in 1:ngens(Z)]) # test order reduction Zred1 = reduce_order(Z, 1) @test ngens(Zred1) == 2 @@ -114,6 +115,10 @@ for N in [Float64, Rational{Int}, Float32] Zred3 = reduce_order(Z, 2) @test ngens(Zred3) == 4 @test order(Zred3) == 2 + Znogen = Zonotope(N[1, 2], Matrix{N}(undef, 2, 0)) + @test ngens(Znogen) == 0 + @test genmat(Znogen) == Matrix{N}(undef, 2, 0) + @test collect(generators(Znogen)) == Vector{N}() # test conversion from hyperrectangular sets Z = convert(Zonotope, Hyperrectangle(N[2, 3], N[4, 5]))