Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug 407 #408

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 14 additions & 35 deletions src/utils/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,53 +161,32 @@
field_name in exclude && continue
val1 = getproperty(x, field_name)
val2 = getproperty(y, field_name)
if !isempty(fieldnames(typeof(val1)))
if !compare_values(
match_fn,
val1,
val2;
compare_uuids = compare_uuids,
exclude = exclude,
)
@error "values do not match" T field_name val1 val2
match = false
end
elseif val1 isa AbstractArray
if !compare_values(
match_fn,
val1,
val2;
compare_uuids = compare_uuids,
exclude = exclude,
)
@error "values do not match" T field_name val1 val2
match = false
end
else
if !_fetch_match_fn(match_fn)(val1, val2)
@error "values do not match" T field_name val1 val2
match = false
end
sub_result = compare_values(match_fn, val1, val2;
compare_uuids = compare_uuids, exclude = exclude)
if !sub_result
@error "values do not match" T field_name val1 val2
match = false
end
end

return match
end

# compare_values of an AbstractArray: ignore the fields, iterate over all dimensions of the array
function compare_values(
match_fn::Union{Function, Nothing},
x::Vector{T},
y::Vector{T};
x::AbstractArray,
y::AbstractArray;
compare_uuids = false,
exclude = Set{Symbol}(),
) where {T}
if length(x) != length(y)
@error "lengths do not match" T length(x) length(y)
)
if size(x) != size(y)
@error "sizes do not match" size(x) size(y)

Check warning on line 184 in src/utils/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/utils.jl#L184

Added line #L184 was not covered by tests
return false
end

match = true
for i in range(1; length = length(x))
for i in keys(x)
if !compare_values(
match_fn,
x[i],
Expand All @@ -225,8 +204,8 @@

function compare_values(
match_fn::Union{Function, Nothing},
x::Dict,
y::Dict;
x::AbstractDict,
y::AbstractDict;
compare_uuids = false,
exclude = Set{Symbol}(),
)
Expand Down
11 changes: 11 additions & 0 deletions test/test_system_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ end
special1,
))
@test IS.compare_values(==, special1, special1)

# https://github.com/NREL-Sienna/InfrastructureSystems.jl/issues/407
@test InfrastructureSystems.compare_values([0 0], [0 0])

# Test that for arrays and dicts we are actually comparing the values
my_match_fn_3(::Int64, ::Int64) = true
my_match_fn_3(::Any, ::Any) = false
@test IS.compare_values(my_match_fn_3, [0, 1], [0, 1])
@test IS.compare_values(my_match_fn_3, [0 1], [0 1])
@test IS.compare_values(my_match_fn_3,
Dict("a" => 0, "b" => 1), Dict("a" => 0, "b" => 1))
end

@testset "Test compression settings" begin
Expand Down
Loading