4
4
# Use of this source code is governed by an MIT-style license that can be found
5
5
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
6
6
7
- using Test
7
+ module TestTest
8
8
9
+ using Test
9
10
import MathOptInterface as MOI
10
11
11
- # Some tests are excluded because UniversalFallback accepts absolutely
12
- # everything.
13
-
14
- MOI. Test. runtests (
15
- MOI. Utilities. MockOptimizer (
16
- MOI. Utilities. UniversalFallback (MOI. Utilities. Model {Float64} ()),
17
- ),
18
- MOI. Test. Config (),
19
- exclude = [
20
- r" ^test_model_ScalarFunctionConstantNotZero$" ,
21
- " test_model_copy_to_UnsupportedAttribute" ,
22
- " test_model_copy_to_UnsupportedConstraint" ,
23
- " test_model_supports_constraint_ScalarAffineFunction_EqualTo" ,
24
- " test_model_supports_constraint_VariableIndex_EqualTo" ,
25
- " test_model_supports_constraint_VectorOfVariables_Nonnegatives" ,
26
- ],
27
- warn_unsupported = true ,
28
- )
29
-
30
- # Run the previously excluded tests, this time without UniversalFallback.
31
-
32
- MOI. Test. runtests (
33
- MOI. Utilities. MockOptimizer (
34
- MOI. Utilities. Model {Float64} (),
35
- scalar_function_constant_non_zero = true ,
36
- ),
37
- MOI. Test. Config ();
38
- include = [
39
- r" ^test_model_ScalarFunctionConstantNotZero$" ,
40
- " test_model_copy_to_UnsupportedAttribute" ,
41
- " test_model_copy_to_UnsupportedConstraint" ,
42
- " test_model_supports_constraint_ScalarAffineFunction_EqualTo" ,
43
- " test_model_supports_constraint_VariableIndex_EqualTo" ,
44
- " test_model_supports_constraint_VectorOfVariables_Nonnegatives" ,
45
- ],
46
- verbose = true ,
47
- )
48
-
49
- # Test for Issue #1757
12
+ function runtests ()
13
+ for name in names (@__MODULE__ ; all = true )
14
+ if startswith (" $name " , " test_" )
15
+ @testset " $(name) " begin
16
+ getfield (@__MODULE__ , name)()
17
+ end
18
+ end
19
+ end
20
+ return
21
+ end
50
22
51
- MOI. Test. test_model_ScalarFunctionConstantNotZero (
52
- MOI. Utilities. MockOptimizer (
53
- MOI. Utilities. Model {Float64} (),
54
- scalar_function_constant_non_zero = false ,
55
- ),
56
- MOI. Test. Config (exclude = Any[MOI. ConstraintFunction]),
57
- )
23
+ function test_runtests ()
24
+ # Some tests are excluded because UniversalFallback accepts absolutely
25
+ # everything.
26
+ MOI. Test. runtests (
27
+ MOI. Utilities. MockOptimizer (
28
+ MOI. Utilities. UniversalFallback (MOI. Utilities. Model {Float64} ()),
29
+ ),
30
+ MOI. Test. Config (),
31
+ exclude = [
32
+ r" ^test_model_ScalarFunctionConstantNotZero$" ,
33
+ " test_model_copy_to_UnsupportedAttribute" ,
34
+ " test_model_copy_to_UnsupportedConstraint" ,
35
+ " test_model_supports_constraint_ScalarAffineFunction_EqualTo" ,
36
+ " test_model_supports_constraint_VariableIndex_EqualTo" ,
37
+ " test_model_supports_constraint_VectorOfVariables_Nonnegatives" ,
38
+ ],
39
+ warn_unsupported = true ,
40
+ verbose = true ,
41
+ )
42
+ # Run the previously excluded tests, this time without UniversalFallback.
43
+ MOI. Test. runtests (
44
+ MOI. Utilities. MockOptimizer (
45
+ MOI. Utilities. Model {Float64} (),
46
+ scalar_function_constant_non_zero = true ,
47
+ ),
48
+ MOI. Test. Config ();
49
+ include = [
50
+ r" ^test_model_ScalarFunctionConstantNotZero$" ,
51
+ " test_model_copy_to_UnsupportedAttribute" ,
52
+ " test_model_copy_to_UnsupportedConstraint" ,
53
+ " test_model_supports_constraint_ScalarAffineFunction_EqualTo" ,
54
+ " test_model_supports_constraint_VariableIndex_EqualTo" ,
55
+ " test_model_supports_constraint_VectorOfVariables_Nonnegatives" ,
56
+ ],
57
+ verbose = true ,
58
+ )
59
+ return
60
+ end
58
61
59
- # Test exclude_tests_after. This should work despite no methods being added for IncompleteOptimizer
60
- # because every test should get skipped.
62
+ function test_issue_1757 ()
63
+ MOI. Test. test_model_ScalarFunctionConstantNotZero (
64
+ MOI. Utilities. MockOptimizer (
65
+ MOI. Utilities. Model {Float64} (),
66
+ scalar_function_constant_non_zero = false ,
67
+ ),
68
+ MOI. Test. Config (exclude = Any[MOI. ConstraintFunction]),
69
+ )
70
+ return
71
+ end
61
72
62
73
struct IncompleteOptimizer <: MOI.AbstractOptimizer end
63
74
64
- MOI. Test. runtests (
65
- IncompleteOptimizer (),
66
- MOI. Test. Config ();
67
- exclude_tests_after = v " 0.0.1" ,
68
- )
75
+ # Test exclude_tests_after. This should work despite no methods being added for
76
+ # IncompleteOptimizer because every test should get skipped.
77
+ function test_exclude_tests_after ()
78
+ MOI. Test. runtests (
79
+ IncompleteOptimizer (),
80
+ MOI. Test. Config ();
81
+ exclude_tests_after = v " 0.0.1" ,
82
+ verbose = true ,
83
+ )
84
+ return
85
+ end
69
86
70
87
# Non-Float64 tests
71
88
72
- MOI. Test. runtests (
73
- MOI. Utilities. MockOptimizer (
74
- MOI. Utilities. UniversalFallback (MOI. Utilities. Model {BigFloat} ()),
75
- BigFloat,
76
- ),
77
- MOI. Test. Config (BigFloat),
78
- exclude = [
79
- # ========================= Expected failures ==========================
80
- # UniversalFallback supports these tests.
81
- " test_model_copy_to_UnsupportedAttribute" ,
82
- " test_model_copy_to_UnsupportedConstraint" ,
83
- " test_model_supports_constraint_ScalarAffineFunction_EqualTo" ,
84
- " test_model_supports_constraint_VariableIndex_EqualTo" ,
85
- " test_model_supports_constraint_VectorOfVariables_Nonnegatives" ,
86
- ],
87
- )
89
+ function test_bigfloat_tests ()
90
+ MOI. Test. runtests (
91
+ MOI. Utilities. MockOptimizer (
92
+ MOI. Utilities. UniversalFallback (MOI. Utilities. Model {BigFloat} ()),
93
+ BigFloat,
94
+ ),
95
+ MOI. Test. Config (BigFloat),
96
+ exclude = [
97
+ # ========================= Expected failures ======================
98
+ # UniversalFallback supports these tests.
99
+ " test_model_copy_to_UnsupportedAttribute" ,
100
+ " test_model_copy_to_UnsupportedConstraint" ,
101
+ " test_model_supports_constraint_ScalarAffineFunction_EqualTo" ,
102
+ " test_model_supports_constraint_VariableIndex_EqualTo" ,
103
+ " test_model_supports_constraint_VectorOfVariables_Nonnegatives" ,
104
+ ],
105
+ )
106
+ return
107
+ end
88
108
89
109
# Special test for issue #2010
90
110
@@ -98,21 +118,25 @@ function MOI.supports_constraint(
98
118
return true
99
119
end
100
120
101
- MOI. Test. test_attribute_unsupported_constraint (
102
- _UnsupportedModel (),
103
- MOI. Test. Config (),
104
- )
121
+ function test_issue_2010 ()
122
+ MOI. Test. test_attribute_unsupported_constraint (
123
+ _UnsupportedModel (),
124
+ MOI. Test. Config (),
125
+ )
126
+ return
127
+ end
105
128
106
- @testset " test_attribute_unsupported_constraint_fallback" begin
129
+ function test_attribute_unsupported_constraint_fallback ()
107
130
model = _UnsupportedModel ()
108
131
F, S = MOI. VariableIndex, MOI. ZeroOne
109
132
attr = MOI. ListOfConstraintIndices {F,S} ()
110
133
@test_throws MOI. GetAttributeNotAllowed (attr) MOI. get (model, attr)
111
134
attr = MOI. NumberOfConstraints {F,S} ()
112
135
@test_throws MOI. GetAttributeNotAllowed (attr) MOI. get (model, attr)
136
+ return
113
137
end
114
138
115
- @testset " test_warning_on_ambiguous" begin
139
+ function test_warning_on_ambiguous ()
116
140
model = MOI. Utilities. MockOptimizer (
117
141
MOI. Utilities. UniversalFallback (MOI. Utilities. Model {Float64} ()),
118
142
)
@@ -123,18 +147,20 @@ end
123
147
@test_logs (:warn ,) MOI. Test. runtests (model, config; exclude = exclude)
124
148
exclude = [r" ^test_model_Name$" , r" test_.+" ]
125
149
@test_logs MOI. Test. runtests (model, config; exclude = exclude)
150
+ return
126
151
end
127
152
128
- @testset " test_FeasibilitySenseEvaluator" begin
153
+ function test_FeasibilitySenseEvaluator ()
129
154
evaluator = MOI. Test. FeasibilitySenseEvaluator (true )
130
155
@test MOI. features_available (evaluator) == [:Grad , :Jac , :Hess , :ExprGraph ]
131
156
@test MOI. hessian_lagrangian_structure (evaluator) == [(1 , 1 )]
132
157
evaluator = MOI. Test. FeasibilitySenseEvaluator (false )
133
158
@test MOI. features_available (evaluator) == [:Grad , :Jac , :ExprGraph ]
134
159
@test_throws AssertionError MOI. hessian_lagrangian_structure (evaluator)
160
+ return
135
161
end
136
162
137
- @testset " test_HS071_evaluator" begin
163
+ function test_HS071_evaluator ()
138
164
evaluator = MOI. Test. HS071 (true , true )
139
165
features = [:Grad , :Jac , :JacVec , :ExprGraph , :Hess , :HessVec ]
140
166
@test MOI. features_available (evaluator) == features
204
230
:($ (x[1 ]) * $ (x[2 ]) * $ (x[3 ]) * $ (x[4 ]) >= 25.0 )
205
231
@test MOI. constraint_expr (evaluator, 2 ) ==
206
232
:($ (x[1 ])^ 2 + $ (x[2 ])^ 2 + $ (x[3 ])^ 2 + $ (x[4 ])^ 2 == 40.0 )
233
+ return
207
234
end
235
+
236
+ function test_error_handler ()
237
+ err = ErrorException (" error" )
238
+ @test_throws err try
239
+ @assert false
240
+ catch
241
+ MOI. Test. _error_handler (err, " test_foo" , true )
242
+ end
243
+ err = MOI. AddVariableNotAllowed ()
244
+ @test_logs (:warn ,) MOI. Test. _error_handler (err, " test_foo" , true )
245
+ @test_nowarn MOI. Test. _error_handler (err, " test_foo" , false )
246
+ return
247
+ end
248
+
249
+ end # module
250
+
251
+ TestTest. runtests ()
0 commit comments