From 9f7ae5839b6fe826bb368896af591491dc6cd6ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 22 Aug 2019 14:20:08 +0200 Subject: [PATCH 1/3] Implement how for IndexMap --- src/Utilities/copy.jl | 3 +++ test/Utilities/copy.jl | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Utilities/copy.jl b/src/Utilities/copy.jl index 35d62d1738..824084fdba 100644 --- a/src/Utilities/copy.jl +++ b/src/Utilities/copy.jl @@ -88,6 +88,9 @@ Base.haskey(idxmap::IndexMap, vi::MOI.VariableIndex) = haskey(idxmap.varmap, vi) Base.keys(idxmap::IndexMap) = Iterators.flatten((keys(idxmap.varmap), keys(idxmap.conmap))) +Base.length(idxmap::IndexMap) = length(idxmap.varmap) + length(idxmap.conmap) +Base.iterate(idxmap::MOIU.IndexMap, args...) = iterate(Base.Iterators.flatten((idxmap.varmap, idxmap.conmap)), args...) + """ pass_attributes(dest::MOI.ModelLike, src::MOI.ModelLike, copy_names::Bool, idxmap::IndexMap, pass_attr::Function=MOI.set) diff --git a/test/Utilities/copy.jl b/test/Utilities/copy.jl index 93fb22ece9..a102078711 100644 --- a/test/Utilities/copy.jl +++ b/test/Utilities/copy.jl @@ -6,6 +6,24 @@ const MOIU = MOI.Utilities include("../dummy.jl") +remove_moi(x::String) = replace(x, "MathOptInterface." => "") +function compare_without_moi(x::String, y::String) + @test remove_moi(x) == remove_moi(y) +end + +@testset "IndexMap" begin + map = MOIU.IndexMap() + @test length(map) == 0 + compare_without_moi(sprint(show, map), "Utilities.IndexMap()") + x = MOI.VariableIndex(1) + y = MOI.VariableIndex(2) + cx = MOI.ConstraintIndex{MOI.SingleVariable, MOI.Integer}(1) + cy = MOI.ConstraintIndex{MOI.SingleVariable, MOI.Integer}(2) + map = MOIU.IndexMap(Dict(x => y), Dict(cx => cy)) + @test length(map) == 2 + compare_without_moi(sprint(show, map), "Utilities.IndexMap($x=>$y,Pair{ConstraintIndex,ConstraintIndex}($cx, $cy))") +end + @testset "AUTOMATIC" begin src = DummyModel() dest = DummyModel() From 114382d3d7ae4999f471a1f4a687d65aa735ea99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 22 Aug 2019 18:13:33 +0200 Subject: [PATCH 2/3] Fix for Julia v1.2 --- test/Utilities/copy.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Utilities/copy.jl b/test/Utilities/copy.jl index a102078711..6bcacff037 100644 --- a/test/Utilities/copy.jl +++ b/test/Utilities/copy.jl @@ -21,7 +21,7 @@ end cy = MOI.ConstraintIndex{MOI.SingleVariable, MOI.Integer}(2) map = MOIU.IndexMap(Dict(x => y), Dict(cx => cy)) @test length(map) == 2 - compare_without_moi(sprint(show, map), "Utilities.IndexMap($x=>$y,Pair{ConstraintIndex,ConstraintIndex}($cx, $cy))") + compare_without_moi(sprint(show, map), "Utilities.IndexMap($(x => y),Pair{ConstraintIndex,ConstraintIndex}($cx, $cy))") end @testset "AUTOMATIC" begin From a57b57ef4a391c9f71f524c0d7a636f25fb9e438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 22 Aug 2019 20:45:18 +0200 Subject: [PATCH 3/3] Better fix --- test/Utilities/copy.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/Utilities/copy.jl b/test/Utilities/copy.jl index 6bcacff037..ba68b9ca77 100644 --- a/test/Utilities/copy.jl +++ b/test/Utilities/copy.jl @@ -21,7 +21,9 @@ end cy = MOI.ConstraintIndex{MOI.SingleVariable, MOI.Integer}(2) map = MOIU.IndexMap(Dict(x => y), Dict(cx => cy)) @test length(map) == 2 - compare_without_moi(sprint(show, map), "Utilities.IndexMap($(x => y),Pair{ConstraintIndex,ConstraintIndex}($cx, $cy))") + # `x=>y` in Julia <= 1.1 and `x => y` in Julia >= 1.2 + x_y = string(Dict(x => y))[6:end-1] + compare_without_moi(sprint(show, map), "Utilities.IndexMap($x_y,Pair{ConstraintIndex,ConstraintIndex}($cx, $cy))") end @testset "AUTOMATIC" begin