diff --git a/src/FileFormats/utils.jl b/src/FileFormats/utils.jl index f33904189b..658fe6f20c 100644 --- a/src/FileFormats/utils.jl +++ b/src/FileFormats/utils.jl @@ -12,19 +12,19 @@ create_unique_names( model::MOI.ModelLike; warn::Bool = false, - replacements::Vector{Function} = Function[], + replacements = (), ) Rename variables in `model` to ensure that all variables and constraints have a unique name. In addition, loop through `replacements` and replace names with -`f(name)`. +`f(name)`. The names are inferred to be `String`s if `replacements` is a tuple. If `warn`, print a warning if a variable or constraint is renamed. """ function create_unique_names( model::MOI.ModelLike; warn::Bool = false, - replacements::Vector{Function} = Function[], + replacements = (), ) create_unique_variable_names(model, warn, replacements) create_unique_constraint_names(model, warn, replacements) @@ -74,7 +74,7 @@ function create_generic_constraint_names( return i end -function _replace(s::String, replacements::Vector{Function}) +function _replace(s::String, replacements) for f in replacements s = f(s) end @@ -170,7 +170,7 @@ end function create_unique_constraint_names( model::MOI.ModelLike, warn::Bool, - replacements::Vector{Function}, + replacements, ) original_names = Set{String}() for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent()) @@ -194,7 +194,7 @@ end function create_unique_variable_names( model::MOI.ModelLike, warn::Bool, - replacements::Vector{Function}, + replacements, ) variables = MOI.get(model, MOI.ListOfVariableIndices()) # This is a list of all of the names currently in the model. We're going to diff --git a/test/FileFormats/test_FileFormats.jl b/test/FileFormats/test_FileFormats.jl index 19fa144af9..f11dc94c01 100644 --- a/test/FileFormats/test_FileFormats.jl +++ b/test/FileFormats/test_FileFormats.jl @@ -178,6 +178,21 @@ function test_unique_names() return end +function test_unique_names_replacements() + replace_spaces = s -> replace(s, " " => "_") + @test (@inferred MOI.FileFormats._replace("a b", ())) == "a b" + @test (@inferred MOI.FileFormats._replace("a b", (replace_spaces,))) == + "a_b" + # A vector of functions is still supported, without inference. + @test MOI.FileFormats._replace("a b", Function[replace_spaces]) == "a_b" + model = MOI.Utilities.Model{Float64}() + x = MOI.add_variables(model, 2) + MOI.set.(model, MOI.VariableName(), x, ["x a", "x_a"]) + MOI.FileFormats.create_unique_names(model; replacements = (replace_spaces,)) + @test MOI.get.(model, MOI.VariableName(), x) == ["x_a", "x_a_1"] + return +end + end TestFileFormats.runtests() diff --git a/test/JuliaC/MyApp/src/MyApp.jl b/test/JuliaC/MyApp/src/MyApp.jl index ac787d5e17..177a6b9a90 100644 --- a/test/JuliaC/MyApp/src/MyApp.jl +++ b/test/JuliaC/MyApp/src/MyApp.jl @@ -21,6 +21,9 @@ function @main(args::Vector{String})::Cint end # The following commands are not traced by `test_juliac.jl`: the compiled # binary must run them with the methods resolved at compile time. + if head == "--names" + return run_names(args) + end return 1 # Incorrect head end @@ -54,4 +57,12 @@ function run_mathoptformat(args::Vector{String})::Cint return _write(model, args) end +# A second variable named `x`, which is renamed. +function run_names(args::Vector{String})::Cint + model, _ = _model() + y = MOI.add_variable(model) + MOI.set(model, MOI.VariableName(), y, "x") + return _write(model, args) +end + end # module MyApp diff --git a/test/JuliaC/test_juliac.jl b/test/JuliaC/test_juliac.jl index 13bc9d5cbc..c6fb90f029 100644 --- a/test/JuliaC/test_juliac.jl +++ b/test/JuliaC/test_juliac.jl @@ -119,6 +119,14 @@ function test_mathoptformat(output_dir) return end +function test_names(output_dir) + compiled, reference = _run(output_dir, "--names") + @test compiled == reference + object = JSON.parse(compiled) + @test object["variables"] == [Dict("name" => "x"), Dict("name" => "x_1")] + return +end + end # module TestJuliaC TestJuliaC.runtests()