Skip to content

Commit

Permalink
Merge pull request #408 from NREL-Sienna/gks/fix_407
Browse files Browse the repository at this point in the history
Fix bug 407
  • Loading branch information
jd-lara authored Oct 29, 2024
2 parents 627c8dd + 83da575 commit 8c4d57d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
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 @@ function compare_values(match_fn::Union{Function, Nothing}, x::T, y::U;
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)
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 @@ end

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

0 comments on commit 8c4d57d

Please sign in to comment.