Skip to content

Commit bfdb1e6

Browse files
authored
delete test/old/macros.jl (jump-dev#1769)
* delete test/old/macros.jl Copying over a few tests that still seemed relevant. No failures revealed. Closes jump-dev#1737 * fix operator strings on windows * formatting fix
1 parent 04b446f commit bfdb1e6

File tree

4 files changed

+183
-709
lines changed

4 files changed

+183
-709
lines changed

test/macros.jl

+115-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,50 @@
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
248

349
mutable struct MyVariable
450
test_kw::Int
@@ -166,6 +212,34 @@ function macros_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType::Typ
166212
@test c.set == MOI.LessThan(-38.0)
167213
end
168214

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+
169243
@testset "@build_constraint (scalar inequality)" begin
170244
model = ModelType()
171245
@variable(model, x)
@@ -320,6 +394,46 @@ end
320394
@test con3.func == 9 * x^2
321395
@test con4.func == convert(QuadExpr, 3 * x)
322396
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
323437
end
324438

325439
@testset "Macros for JuMPExtension.MyModel" begin

test/nlp.jl

+24
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,30 @@
387387
@test JuMP.value(p) == 10.0
388388
end
389389

390+
@testset "@NLconstraints" begin
391+
model = Model()
392+
@variable(model, 0 <= x <= 1)
393+
@variable(model, y[1:3])
394+
@objective(model, Max, x)
395+
396+
@NLconstraints(model, begin
397+
ref[i=1:3], y[i] == 0
398+
x + y[1] * y[2] * y[3] <= 0.5
399+
end)
400+
401+
@test JuMP.num_nl_constraints(model) == 4
402+
evaluator = JuMP.NLPEvaluator(model)
403+
MOI.initialize(evaluator, [:ExprGraph])
404+
405+
for i in 1:3
406+
@test MOI.constraint_expr(evaluator, i) ==
407+
:(x[$(y[i].index)] - 0.0 == 0.0)
408+
end
409+
@test MOI.constraint_expr(evaluator, 4) ==
410+
:((x[$(x.index)] + x[$(y[1].index)] * x[$(y[2].index)] *
411+
x[$(y[3].index)]) - 0.5 <= 0.0)
412+
end
413+
390414
# This covers the code that computes Hessians in odd chunks of Hess-vec
391415
# products.
392416
@testset "Dense Hessian" begin

0 commit comments

Comments
 (0)