|
1 |
| -# TODO: Copy over tests that are still relevant from old/macros.jl. |
| 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 http://mozilla.org/MPL/2.0/. |
| 5 | +############################################################################# |
| 6 | +# JuMP |
| 7 | +# An algebraic modeling language for Julia |
| 8 | +# See http://github.com/JuliaOpt/JuMP.jl |
| 9 | +############################################################################# |
| 10 | +# test/macros.jl |
| 11 | +# Testing for macros |
| 12 | +############################################################################# |
| 13 | + |
| 14 | +@testset "Check Julia generator expression parsing" begin |
| 15 | + sumexpr = :(sum(x[i,j] * y[i,j] for i = 1:N, j in 1:M if i != j)) |
| 16 | + @test sumexpr.head == :call |
| 17 | + @test sumexpr.args[1] == :sum |
| 18 | + @test sumexpr.args[2].head == :generator |
| 19 | + @test sumexpr.args[2].args[1] == :(x[i,j] * y[i,j]) |
| 20 | + @test sumexpr.args[2].args[2].head == :filter |
| 21 | + @test sumexpr.args[2].args[2].args[1] == :(i != j) |
| 22 | + @test sumexpr.args[2].args[2].args[2] == :(i = 1:N) |
| 23 | + @test sumexpr.args[2].args[2].args[3] == :(j = 1:M) |
| 24 | + |
| 25 | + sumexpr = :(sum(x[i,j] * y[i,j] for i = 1:N, j in 1:M)) |
| 26 | + @test sumexpr.head == :call |
| 27 | + @test sumexpr.args[1] == :sum |
| 28 | + @test sumexpr.args[2].head == :generator |
| 29 | + @test sumexpr.args[2].args[1] == :(x[i,j] * y[i,j]) |
| 30 | + @test sumexpr.args[2].args[2] == :(i = 1:N) |
| 31 | + @test sumexpr.args[2].args[3] == :(j = 1:M) |
| 32 | +end |
| 33 | + |
| 34 | +@testset "Check Julia condition expression parsing" begin |
| 35 | + ex = :(x[12;3]) |
| 36 | + @test ex.head == :typed_vcat |
| 37 | + @test ex.args == [:x, 12, 3] |
| 38 | + |
| 39 | + if VERSION >= v"1.0-" |
| 40 | + ex = :(x[i=1:3, j=S; isodd(i) && i + j >= 2]) |
| 41 | + @test ex.head == :ref |
| 42 | + @test ex.args == [:x, |
| 43 | + Expr(:parameters, Expr(:&&, :(isodd(i)), :(i + j >= 2))), |
| 44 | + Expr(:kw, :i, :(1:3)), |
| 45 | + Expr(:kw, :j, :S)] |
| 46 | + end |
| 47 | +end |
2 | 48 |
|
3 | 49 | mutable struct MyVariable
|
4 | 50 | test_kw::Int
|
@@ -166,6 +212,34 @@ function macros_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType::Typ
|
166 | 212 | @test c.set == MOI.LessThan(-38.0)
|
167 | 213 | end
|
168 | 214 |
|
| 215 | + @testset "Unicode comparison operators" begin |
| 216 | + model = ModelType() |
| 217 | + @variable(model, x) |
| 218 | + @variable(model, y) |
| 219 | + t = 10.0 |
| 220 | + |
| 221 | + cref = @constraint(model, (x + y) / 2 ≥ 1) |
| 222 | + c = JuMP.constraint_object(cref) |
| 223 | + @test JuMP.isequal_canonical(c.func, 0.5 * x + 0.5 * y) |
| 224 | + @test c.set == MOI.GreaterThan(1.0) |
| 225 | + |
| 226 | + cref = @constraint(model, (x + y) / 2 ≤ 1) |
| 227 | + c = JuMP.constraint_object(cref) |
| 228 | + @test JuMP.isequal_canonical(c.func, 0.5 * x + 0.5 * y) |
| 229 | + @test c.set == MOI.LessThan(1.0) |
| 230 | + |
| 231 | + cref = @constraint(model, -1 ≤ x - y ≤ t) |
| 232 | + c = JuMP.constraint_object(cref) |
| 233 | + @test JuMP.isequal_canonical(c.func, x - y) |
| 234 | + @test c.set == MOI.Interval(-1.0, t) |
| 235 | + |
| 236 | + cref = @constraint(model, 1 ≥ x ≥ 0) |
| 237 | + c = JuMP.constraint_object(cref) |
| 238 | + @test c.func isa JuMP.GenericAffExpr |
| 239 | + @test JuMP.isequal_canonical(c.func, 1 * x) |
| 240 | + @test c.set == MOI.Interval(0.0, 1.0) |
| 241 | + end |
| 242 | + |
169 | 243 | @testset "@build_constraint (scalar inequality)" begin
|
170 | 244 | model = ModelType()
|
171 | 245 | @variable(model, x)
|
|
320 | 394 | @test con3.func == 9 * x^2
|
321 | 395 | @test con4.func == convert(QuadExpr, 3 * x)
|
322 | 396 | end
|
| 397 | + |
| 398 | + @testset "AffExpr in macros" begin |
| 399 | + eq = JuMP.math_symbol(REPLMode, :eq) |
| 400 | + ge = JuMP.math_symbol(REPLMode, :geq) |
| 401 | + |
| 402 | + model = Model() |
| 403 | + @variable(model, x) |
| 404 | + @variable(model, y) |
| 405 | + temp = x + 2 * y + 1 |
| 406 | + a = 1.0 * x |
| 407 | + con1 = @constraint(model, 3 * temp - x - 2 >= 0) |
| 408 | + con2 = @constraint(model, (2 + 2) * ((3 + 4) * (1 + a)) == 0) |
| 409 | + con3 = @constraint(model, 1 + 0 * temp == 0) |
| 410 | + @test string(con1) == "2 x + 6 y $ge -1.0" |
| 411 | + @test string(con2) == "28 x $eq -28.0" |
| 412 | + @test string(con3) == "0 $eq -1.0" |
| 413 | + end |
| 414 | + |
| 415 | + @testset "@constraints" begin |
| 416 | + eq = JuMP.math_symbol(REPLMode, :eq) |
| 417 | + ge = JuMP.math_symbol(REPLMode, :geq) |
| 418 | + |
| 419 | + model = Model() |
| 420 | + @variable(model, x) |
| 421 | + @variable(model, y[1:3]) |
| 422 | + |
| 423 | + @constraints(model, begin |
| 424 | + x + y[1] == 1 |
| 425 | + ref[i=1:3], y[1] + y[i] >= i |
| 426 | + end) |
| 427 | + |
| 428 | + @test string(model) == """ |
| 429 | + Feasibility |
| 430 | + Subject to |
| 431 | + x + y[1] $eq 1.0 |
| 432 | + 2 y[1] $ge 1.0 |
| 433 | + y[1] + y[2] $ge 2.0 |
| 434 | + y[1] + y[3] $ge 3.0 |
| 435 | + """ |
| 436 | + end |
323 | 437 | end
|
324 | 438 |
|
325 | 439 | @testset "Macros for JuMPExtension.MyModel" begin
|
|
0 commit comments