Skip to content

Commit 7aaa7fc

Browse files
committed
add more tests
1 parent 5366a2d commit 7aaa7fc

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/named_systems2.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ function check_all_unique(s1, s2; throw=true)
2525
end
2626

2727
function generate_unique_x_names(systems...)
28-
x_names = [s.x for s in systems]
28+
x_names = reduce(vcat, s.x for s in systems)
2929
uniq = check_unique(x_names, "x", throw = false)
3030
if uniq
3131
return x_names
3232
else
3333
# For each system, if it has a name, append the system name to the x names. If neither system has a name, append gensym names to both systems' x names.
3434
systemnames = [s.name for s in systems]
35-
x_names = if any(isempty(s.name) for s in systems) || length(unique(systemnames)) < length(systemnames)
36-
# Any name is empty or more than one system has the same name
35+
x_names = if count(isempty(s.name) for s in systems) > 1 || length(unique(systemnames)) < length(systemnames)
36+
# More than one system has empty name or more than one system has the same name
37+
# We can handle one of the names being empty, which is a common case when the plant has a name but a controller/filter is auto promoted to a named system.
3738
[gensym(string(x)) for x in x_names]
3839
else
39-
reduce(vcat, [[Symbol(string(s.name)*string(x)) for x in s.x] for s in systems])
40+
# reduce(vcat, [[Symbol(string(s.name)*string(x)) for x in s.x] for s in systems])
41+
[Symbol(string(s.name)*string(x)) for s in systems for x in s.x]
4042
end
4143
@assert allunique(x_names)
4244
x_names

test/test_named_systems2.jl

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using RobustAndOptimalControl, ControlSystemsBase, LinearAlgebra, Test, Plots
2-
using RobustAndOptimalControl: @check_unique, @check_all_unique
2+
using RobustAndOptimalControl: check_unique, check_all_unique
33

44
@test :x^3 == expand_symbol(:x, 3) == [:x1, :x2, :x3]
55

@@ -150,7 +150,7 @@ G1 = ss(1,1,1,0)
150150
G2 = ss(1,1,1,0)
151151
s1 = named_ss(G1, x = :x, u = :u1, y=:y1)
152152
s2 = named_ss(G2, x = :z, u = :u2, y=:y2)
153-
@test_nowarn @check_all_unique s1 s2
153+
@test_nowarn check_all_unique(s1, s2)
154154

155155
s1e = ExtendedStateSpace(s1, y=s1.y, u=s1.u, z=[], w=[])
156156
@test s1.sys == system_mapping(s1e)
@@ -171,7 +171,46 @@ fb = feedback(s1, s2)
171171

172172
s1 = named_ss(G1, x = [:x], u = [:u1], y=[:y1])
173173
s2 = named_ss(G2, x = [:z], u = [:u1], y=[:y2])
174-
@test_throws ArgumentError @check_all_unique s1 s2
174+
@test_throws ArgumentError check_all_unique(s1, s2)
175+
176+
# Same x names, test automatic generation of new names
177+
s1 = named_ss(ssrand(1,1,2), x = :x, u = :u1, y=:y1)
178+
s2 = named_ss(ssrand(1,1,2), x = :x, u = :u2, y=:y2)
179+
s12 = [s1; s2]
180+
@test occursin("x1", string(s12.x[1]))
181+
@test occursin("x2", string(s12.x[2]))
182+
@test occursin("x1", string(s12.x[3]))
183+
@test occursin("x2", string(s12.x[4]))
184+
185+
# When systems have names, use these to create the new x names
186+
s1 = named_ss(ssrand(1,1,2), "P", x = :x, u = :u1, y=:y1)
187+
s2 = named_ss(ssrand(1,1,2), "C", x = :x, u = :u2, y=:y2)
188+
s12 = [s1; s2]
189+
@test s12.x[1] == :Px1
190+
@test s12.x[2] == :Px2
191+
@test s12.x[3] == :Cx1
192+
@test s12.x[4] == :Cx2
193+
194+
# When one system is missing a name, we use the existing name only
195+
s1 = named_ss(ssrand(1,1,2), "P", x = :x, u = :u1, y=:y1)
196+
s2 = named_ss(ssrand(1,1,2), x = :x, u = :u2, y=:y2)
197+
s12 = [s1; s2]
198+
@test s12.x[1] == :Px1
199+
@test s12.x[2] == :Px2
200+
@test s12.x[3] == :x1
201+
@test s12.x[4] == :x2
202+
203+
# If more than one system is missing a name, we do not use the system names
204+
s1 = named_ss(ssrand(1,1,2), "P", x = :x, u = :u1, y=:y1)
205+
s2 = named_ss(ssrand(1,1,2), x = :x, u = :u2, y=:y2)
206+
s3 = named_ss(ssrand(1,1,2), x = :x, u = :u3, y=:y3)
207+
s12 = [s1; s2; s3]
208+
@test occursin("x1", string(s12.x[1]))
209+
@test occursin("x2", string(s12.x[2]))
210+
@test occursin("x1", string(s12.x[3]))
211+
@test occursin("x2", string(s12.x[4]))
212+
@test occursin("x1", string(s12.x[5]))
213+
@test occursin("x2", string(s12.x[6]))
175214

176215
## Promotion and conversion
177216
@testset "Promotion and conversion" begin

0 commit comments

Comments
 (0)