Skip to content

Commit

Permalink
Fix bug 407 better
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielKS committed Oct 29, 2024
1 parent 9fa405c commit 83da575
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/utils/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ _fetch_match_fn(::Nothing) = isequivalent
# Whether to stop recursing and apply the match_fn
_is_compare_directly(::DataType, ::DataType) = true
_is_compare_directly(::T, ::U) where {T, U} = true
# As of 1.11, Arrays have fields we don't want to touch
_is_compare_directly(::T, ::T) where {T <: AbstractArray} = true
_is_compare_directly(::T, ::T) where {T} = isempty(fieldnames(T))

"""
Expand Down Expand Up @@ -163,12 +161,8 @@ 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)
sub_result = if _is_compare_directly(val1, val2)
_fetch_match_fn(match_fn)(val1, val2)
else
compare_values(match_fn, val1, val2;
compare_uuids = compare_uuids, exclude = exclude)
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
Expand All @@ -178,20 +172,21 @@ function compare_values(match_fn::Union{Function, Nothing}, x::T, y::U;
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 @@ -209,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
8 changes: 8 additions & 0 deletions test/test_system_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ end

# 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 83da575

Please sign in to comment.