Skip to content

Commit

Permalink
Implement == (and hash) for Element
Browse files Browse the repository at this point in the history
So that e.g. `resistor(1000) == resistor(1000)`, which previously gave
`false`.
  • Loading branch information
martinholters committed Jan 17, 2023
1 parent 5dfdbc8 commit 8f992d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/ACME.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015, 2016, 2017, 2018, 2019, 2020, 2021 Martin Holters
# Copyright 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2023 Martin Holters
# See accompanying license file.

module ACME
Expand Down Expand Up @@ -97,6 +97,11 @@ struct Element
end
end

Base.:(==)(e1::Element, e2::Element) =
all(f -> (getfield(e1, f) == getfield(e2, f))::Bool, fieldnames(Element))
Base.hash(e::Element, h::UInt) =
foldl((h, f) -> hash(getfield(e, f), h)::UInt, fieldnames(Element); init=h)

for (n,m) in Dict(:nb => :mv, :nx => :mx, :nq => :mq, :nu => :mu)
@eval ($n)(e::Element) = size(e.$m, 2)
end
Expand Down
12 changes: 11 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Martin Holters
# Copyright 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Martin Holters
# See accompanying license file.

include("checklic.jl")
Expand Down Expand Up @@ -40,6 +40,16 @@ end
@test !ACME.setlhs!(solver, zeros(3,3))
end

@testset "Element equality" begin
@test resistor(1e3) == resistor(1e3)
@test hash(resistor(1e3)) == hash(resistor(1e3))
@test resistor(1e3) != resistor(2.2e3)
@test resistor(1) != voltagesource(1)
@test bjt(:npn) == bjt(:npn)
@test hash(bjt(:npn)) == hash(bjt(:npn))
@test bjt(:npn) != bjt(:pnp)
end

@testset "simple circuits" begin
@testset "empty circuit" begin
circ = @circuit begin end
Expand Down

0 comments on commit 8f992d1

Please sign in to comment.