|
| 1 | +# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +module TestTableInterface |
| 7 | + |
| 8 | +using JuMP |
| 9 | +using Test |
| 10 | + |
| 11 | +function runtests() |
| 12 | + for name in names(@__MODULE__; all = true) |
| 13 | + if startswith("$(name)", "test_") |
| 14 | + @testset "$(name)" begin |
| 15 | + getfield(@__MODULE__, name)() |
| 16 | + end |
| 17 | + end |
| 18 | + end |
| 19 | + return |
| 20 | +end |
| 21 | + |
| 22 | +function test_denseaxisarray() |
| 23 | + model = Model() |
| 24 | + @variable(model, x[i = 4:10, j = 2002:2022] >= 0, start = 0.0) |
| 25 | + @test typeof(x) <: Containers.DenseAxisArray |
| 26 | + start_table = Containers.rowtable(start_value, x; header = [:i1, :i2, :i3]) |
| 27 | + T = NamedTuple{(:i1, :i2, :i3),Tuple{Int,Int,Float64}} |
| 28 | + @test start_table isa Vector{T} |
| 29 | + @test length(start_table) == length(x) |
| 30 | + row = first(start_table) |
| 31 | + @test row == (i1 = 4, i2 = 2002, i3 = 0.0) |
| 32 | + x_table = Containers.rowtable(x; header = [:i1, :i2, :i3]) |
| 33 | + @test x_table[1] == (i1 = 4, i2 = 2002, i3 = x[4, 2002]) |
| 34 | + return |
| 35 | +end |
| 36 | + |
| 37 | +function test_array() |
| 38 | + model = Model() |
| 39 | + @variable(model, x[1:10, 1:5] >= 0, start = 0.0) |
| 40 | + @test typeof(x) <: Array{VariableRef} |
| 41 | + start_table = Containers.rowtable(start_value, x; header = [:i1, :i2, :i3]) |
| 42 | + T = NamedTuple{(:i1, :i2, :i3),Tuple{Int,Int,Float64}} |
| 43 | + @test start_table isa Vector{T} |
| 44 | + @test length(start_table) == length(x) |
| 45 | + row = first(start_table) |
| 46 | + @test row == (i1 = 1, i2 = 1, i3 = 0.0) |
| 47 | + x_table = Containers.rowtable(x; header = [:i1, :i2, :i3]) |
| 48 | + @test x_table[1] == (i1 = 1, i2 = 1, i3 = x[1, 1]) |
| 49 | + return |
| 50 | +end |
| 51 | + |
| 52 | +function test_sparseaxisarray() |
| 53 | + model = Model() |
| 54 | + @variable(model, x[i = 1:10, j = 1:5; i + j <= 8] >= 0, start = 0) |
| 55 | + @test typeof(x) <: Containers.SparseAxisArray |
| 56 | + start_table = Containers.rowtable(start_value, x; header = [:i1, :i2, :i3]) |
| 57 | + T = NamedTuple{(:i1, :i2, :i3),Tuple{Int,Int,Float64}} |
| 58 | + @test start_table isa Vector{T} |
| 59 | + @test length(start_table) == length(x) |
| 60 | + @test (i1 = 1, i2 = 1, i3 = 0.0) in start_table |
| 61 | + x_table = Containers.rowtable(x; header = [:i1, :i2, :i3]) |
| 62 | + @test (i1 = 1, i2 = 1, i3 = x[1, 1]) in x_table |
| 63 | + return |
| 64 | +end |
| 65 | + |
| 66 | +function test_col_name_error() |
| 67 | + model = Model() |
| 68 | + @variable(model, x[1:2, 1:2]) |
| 69 | + @test_throws ErrorException Containers.rowtable(x; header = [:y, :a]) |
| 70 | + @test_throws( |
| 71 | + ErrorException, |
| 72 | + Containers.rowtable(x; header = [:y, :a, :b, :c]), |
| 73 | + ) |
| 74 | + @test Containers.rowtable(x; header = [:y, :a, :b]) isa Vector{<:NamedTuple} |
| 75 | + return |
| 76 | +end |
| 77 | + |
| 78 | +# Mockup of custom variable type |
| 79 | +struct _MockVariable <: JuMP.AbstractVariable |
| 80 | + var::JuMP.ScalarVariable |
| 81 | +end |
| 82 | + |
| 83 | +struct _MockVariableRef <: JuMP.AbstractVariableRef |
| 84 | + vref::VariableRef |
| 85 | +end |
| 86 | + |
| 87 | +JuMP.name(v::_MockVariableRef) = JuMP.name(v.vref) |
| 88 | + |
| 89 | +JuMP.owner_model(v::_MockVariableRef) = JuMP.owner_model(v.vref) |
| 90 | + |
| 91 | +JuMP.start_value(v::_MockVariableRef) = JuMP.start_value(v.vref) |
| 92 | + |
| 93 | +struct _Mock end |
| 94 | + |
| 95 | +function JuMP.build_variable(::Function, info::JuMP.VariableInfo, _::_Mock) |
| 96 | + return _MockVariable(JuMP.ScalarVariable(info)) |
| 97 | +end |
| 98 | + |
| 99 | +function JuMP.add_variable(model::Model, x::_MockVariable, name::String) |
| 100 | + variable = JuMP.add_variable(model, x.var, name) |
| 101 | + return _MockVariableRef(variable) |
| 102 | +end |
| 103 | + |
| 104 | +function test_custom_variable() |
| 105 | + model = Model() |
| 106 | + @variable( |
| 107 | + model, |
| 108 | + x[i = 1:3, j = 100:102] >= 0, |
| 109 | + _Mock(), |
| 110 | + container = Containers.DenseAxisArray, |
| 111 | + start = 0.0, |
| 112 | + ) |
| 113 | + @test typeof(x) <: Containers.DenseAxisArray |
| 114 | + start_table = Containers.rowtable(start_value, x) |
| 115 | + T = NamedTuple{(:x1, :x2, :y),Tuple{Int,Int,Float64}} |
| 116 | + @test start_table isa Vector{T} |
| 117 | + @test length(start_table) == length(x) |
| 118 | + @test (x1 = 1, x2 = 100, y = 0.0) in start_table |
| 119 | + x_table = Containers.rowtable(x) |
| 120 | + @test (x1 = 1, x2 = 100, y = x[1, 100]) in x_table |
| 121 | + return |
| 122 | +end |
| 123 | + |
| 124 | +end |
| 125 | + |
| 126 | +TestTableInterface.runtests() |
0 commit comments