Skip to content

Commit

Permalink
add unit tests for AbstractZonotope
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Jun 18, 2019
1 parent 5cb9a9d commit cfa2385
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
11 changes: 10 additions & 1 deletion test/unit_Hyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
Expand Down
9 changes: 8 additions & 1 deletion test/unit_LineSegment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion test/unit_Singleton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
Expand Down
5 changes: 5 additions & 0 deletions test/unit_Zonotope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]))
Expand Down

0 comments on commit cfa2385

Please sign in to comment.