-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_ColVariable.cpp
More file actions
230 lines (193 loc) · 7.31 KB
/
Copy pathtests_ColVariable.cpp
File metadata and controls
230 lines (193 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/** @file
* Unit tests for Variable and ColVariable.
*
* \author Niccolo' Iardella \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Niccolo' Iardella, Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "ColVariable.h"
#include "SMSTypedefs.h"
#include <cmath>
// last, so that the headers above are read as the library was compiled
#include "TestAssert.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
void runAllTests()
{
ColVariable var;
// test FixUnfix
assert( ! var.is_fixed() );
var.is_fixed( true );
assert( var.is_fixed() );
var.is_fixed( false );
assert( ! var.is_fixed() );
// test IsContinuous
assert( var.get_value() == 0 );
assert( var.get_type() == ColVariable::kContinuous );
assert( ! var.is_integer() );
assert( ! var.is_positive() );
assert( ! var.is_negative() );
assert( ! var.is_unitary() );
assert( var.get_lb() == -Inf< ColVariable::VarValue >() );
assert( var.get_ub() == Inf< ColVariable::VarValue >() );
// test SetsValue
ColVariable::VarValue val = 42.0;
var.set_value( val );
assert( val == var.get_value() );
// test IsInteger
var.set_type( ColVariable::kInteger );
assert( var.get_type() == ColVariable::kInteger );
assert( var.is_integer() );
assert( ! var.is_positive() );
assert( ! var.is_negative() );
assert( ! var.is_unitary() );
assert( var.get_lb() == -Inf< ColVariable::VarValue >() );
assert( var.get_ub() == Inf< ColVariable::VarValue >() );
// test IsPositive
var.set_type( ColVariable::kNonNegative );
assert( var.get_type() == ColVariable::kNonNegative );
assert( ! var.is_integer() );
assert( var.is_positive() );
assert( ! var.is_negative() );
assert( ! var.is_unitary() );
assert( var.get_lb() == 0 );
assert( var.get_ub() == Inf< ColVariable::VarValue >() );
// test IsNegative
var.set_type( ColVariable::kNonPositive );
assert( var.get_type() == ColVariable::kNonPositive );
assert( ! var.is_integer() );
assert( ! var.is_positive() );
assert( var.is_negative() );
assert( ! var.is_unitary() );
assert( var.get_lb() == -Inf< ColVariable::VarValue >() );
assert( var.get_ub() == 0 );
// test IsUnitary
var.set_type( ColVariable::kUnitary );
assert( var.get_type() == ColVariable::kUnitary );
assert( ! var.is_integer() );
assert( ! var.is_positive() );
assert( ! var.is_negative() );
assert( var.is_unitary() );
assert( var.get_lb() == -1 );
assert( var.get_ub() == 1 );
// test IsFeasible
var.set_value( 42.42 );
var.set_type( ColVariable::kInteger );
assert( ! var.is_feasible() );
var.set_value( 42.42 );
var.set_type( ColVariable::kNonNegative );
assert( var.is_feasible() );
var.set_value( -42.42 );
var.set_type( ColVariable::kUnitary );
assert( ! var.is_feasible() );
var.set_value( 0.42 );
var.set_type( ColVariable::kUnitary );
assert( var.is_feasible() );
var.set_value( 42 );
var.set_type( ColVariable::kNatural );
assert( var.is_feasible() );
}
/*--------------------------------------------------------------------------*/
/* Every one of the sixteen types, and the values at the edge of each. The
* tests above try six of them with a value well inside or well outside the
* set, which says nothing about what happens at the bounds themselves, and
* the ten that are left are the "weird" combinations the class warns about,
* which are the ones a caller gets wrong. Rather than writing down the two
* bounds of each type, which would only repeat the code, what is checked
* here is what has to hold of any of them. */
static void test_every_type( void )
{
using VV = ColVariable::VarValue;
const VV inf = Inf< VV >();
/* Nine values that straddle every bound any of the types has: each of -1,
* 0 and 1 is a bound of one of them, so each is tried on the bound, half a
* unit inside it and half a unit outside, and one unit past either end. */
static const VV probe[] = { -2 , -1.5 , -1 , -0.5 , 0 , 0.5 , 1 , 1.5 , 2 };
for( int t = 0 ; t < ColVariable::ColVarLastType ; ++t ) {
ColVariable var;
var.set_type( ColVariable::col_var_type( t ) );
const auto lb = var.get_lb();
const auto ub = var.get_ub();
// a set that is empty would be a type nobody can satisfy
assert( lb <= ub );
// an integer type has integer bounds, or the bound itself would be a
// value the type forbids
if( var.is_integer() ) {
if( lb > - inf )
assert( lb == std::floor( lb ) );
if( ub < inf )
assert( ub == std::floor( ub ) );
}
// is_positive() and is_negative() say where the set sits, and both hold
// of the types that are the single point 0
assert( var.is_positive() == ( lb >= 0 ) );
assert( var.is_negative() == ( ub <= 0 ) );
/* And the set is exactly what the two bounds and the integrality say:
* this is the one thing every caller assumes of a type, it is what the
* tests above check of six of the sixteen with one value each, and it is
* what has to hold at the bounds themselves. */
for( auto x : probe ) {
var.set_value( x );
const bool in = ( x >= lb ) && ( x <= ub ) &&
( ( ! var.is_integer() ) || ( x == std::floor( x ) ) );
assert( var.is_feasible() == in );
}
}
}
/*--------------------------------------------------------------------------*/
/* The types whose set is a single point, and the two that are a point but
* carry a range: these are the ones where an off-by-one in the bounds does
* not show, both bounds being the same number. */
static void test_single_point_types( void )
{
for( auto t : { ColVariable::kZeroReal , ColVariable::kZeroInteger ,
ColVariable::kZeroRealU , ColVariable::kZeroIntU } ) {
ColVariable var;
var.set_type( t );
assert( var.get_lb() == 0 );
assert( var.get_ub() == 0 );
var.set_value( 0 );
assert( var.is_feasible() );
var.set_value( 1e-12 );
assert( ! var.is_feasible() );
}
// the binary one, which is the type most models are made of
ColVariable bin;
bin.set_type( ColVariable::kBinary );
assert( bin.get_lb() == 0 );
assert( bin.get_ub() == 1 );
assert( bin.is_integer() );
for( auto v : { 0.0 , 1.0 } ) {
bin.set_value( v );
assert( bin.is_feasible() );
}
for( auto v : { -1.0 , 0.5 , 2.0 } ) {
bin.set_value( v );
assert( ! bin.is_feasible() );
}
}
/*--------------------------------------------------------------------------*/
int main( int argc , char ** argv )
{
runAllTests();
test_every_type();
test_single_point_types();
return( 0 );
}
/*--------------------------------------------------------------------------*/
/*--------------------- End File tests_ColVariable.cpp ---------------------*/
/*--------------------------------------------------------------------------*/