Repository navigation
Expand file tree
/
Copy pathtests_Module.cpp
More file actions
94 lines (77 loc) · 3.54 KB
/
Copy pathtests_Module.cpp
File metadata and controls
94 lines (77 loc) · 3.54 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
/** @file
* Unit test of the C++20 module smspp, built when SMSpp_BUILD_MODULE is ON.
*
* The program includes no header of SMS++: it imports the module, and with
* what that exports it builds an AbstractBlock (Variable, a row with a
* LinearFunction, a box, an objective), checks the feasibility of a point,
* writes the Block to the image of a netCDF file in memory and reads it
* back, and gets a Block from the factory by its classname. Each of these
* steps names a different part of the core, so a name that the module
* fails to export, or a class whose factory registration does not survive
* the import, stops the build or the run.
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <iostream>
#include <memory>
#include <span>
#include <vector>
#undef NDEBUG
#include <cassert>
import smspp;
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
/*-------------------------------- MAIN ------------------------------------*/
/*--------------------------------------------------------------------------*/
int main( void )
{
// min x_0 + x_1 s.t. x_0 + 2 x_1 <= 4 , -1 <= x_0 <= 5
auto block = std::make_unique< AbstractBlock >();
auto x = new std::vector< ColVariable >( 2 );
block->add_static_variable( *x , "x" );
auto rows = new std::vector< FRowConstraint >( 1 );
( *rows )[ 0 ].set_function( new LinearFunction(
{ { & ( *x )[ 0 ] , 1 } , { & ( *x )[ 1 ] , 2 } } ) );
( *rows )[ 0 ].set_lhs( - Inf< double >() );
( *rows )[ 0 ].set_rhs( 4 );
block->add_static_constraint( *rows , "r" );
auto box = new BoxConstraint( block.get() , & ( *x )[ 0 ] , -1 , 5 );
block->add_static_constraint( *box , "box" );
block->set_objective( new FRealObjective( block.get() , new LinearFunction(
{ { & ( *x )[ 0 ] , 1 } , { & ( *x )[ 1 ] , 1 } } ) )
, eNoMod );
// a feasible point and an infeasible one
( *x )[ 0 ].set_value( 2 );
( *x )[ 1 ].set_value( 1 );
assert( RowConstraint::is_feasible( ( *rows )[ 0 ] ) );
( *x )[ 1 ].set_value( 2 );
assert( ! RowConstraint::is_feasible( ( *rows )[ 0 ] ) );
// through the image of a netCDF file in memory and back
std::vector< unsigned char > image;
block->Block::serialize( image , eBlockFile );
assert( ! image.empty() );
std::unique_ptr< Block > back( Block::deserialize(
std::span< const unsigned char >( image ) , 0 ) );
auto ab = dynamic_cast< AbstractBlock * >( back.get() );
assert( ab );
assert( ab->get_number_static_variables() == 1 );
assert( ab->get_number_static_constraints() == 2 );
// the factory, by classname
std::unique_ptr< Block > fresh( Block::new_Block( "AbstractBlock" ) );
assert( dynamic_cast< AbstractBlock * >( fresh.get() ) );
std::cout << "module smspp: OK" << std::endl;
return( 0 );
}
/*--------------------------------------------------------------------------*/
/*------------------------ End File tests_Module.cpp -----------------------*/
/*--------------------------------------------------------------------------*/