Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
100 lines (85 loc) · 3.7 KB
/
Copy pathtest.cpp
File metadata and controls
100 lines (85 loc) · 3.7 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
/*--------------------------------------------------------------------------*/
/*-------------------------- File test.cpp ---------------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Main for testing MultiStageStochasticBlock.
*
* The common part is that of every stochastic Block [see stochastic_test.h];
* this one only adds, for each copy and after the Solver have run, the
* round trip of its Solution [see CheckSolutionRoundTrip()].
*
* \author Antonio Frangioni \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Antonio Frangioni, Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include <cstdio>
#include <iostream>
#include <memory>
#include <random>
#include "stochastic_test.h"
#include "MultiStageStochasticBlock.h"
#include <Solution.h>
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
/*--------------------------------------------------------------------------*/
// Permanent regression check of the MultiStageStochasticBlock Solution
// machinery. MSSB has no dedicated Solution class: it reuses the inherited
// TwoStageStochasticBlockSolution, recursively over the Block tree (here built
// from a shared scenario tree via views). After the Block has been solved, the
// Solution must survive a full round-trip without error:
// s = get_Solution(); s->read( block ); s->serialize( file );
// s2 = Solution::deserialize( file ); s2->write( block );
// The serialize( const std::string & ) overload is used on purpose: it sets
// the file-level SMS++_file_type attribute that Solution::deserialize requires
// (the bare serialize( NcFile & ) does not). Returns true iff the round-trip
// completes and deserialize yields a non-null Solution.
static bool CheckSolutionRoundTrip( Block * block )
{
// a name of its own, since the batteries run in parallel in the same
// directory and would otherwise write and read each other's file
const std::string fn = "mssb_solution_roundtrip_" +
std::to_string( std::random_device()() ) + ".nc4";
bool ok = false;
try {
std::unique_ptr< Solution > s1( block->get_Solution() );
s1->read( block );
s1->serialize( fn );
std::unique_ptr< Solution > s2( Solution::deserialize( fn ) );
if( ! s2 )
std::cerr << "Solution round-trip: deserialize returned null" << std::endl;
else {
s2->write( block ); // restore the round-tripped Solution into the Block
ok = true;
}
}
catch( std::exception & e ) {
std::cerr << "Solution round-trip error: " << e.what() << std::endl;
}
std::remove( fn.c_str() );
return( ok );
}
/*--------------------------------------------------------------------------*/
int main( int argc , char ** argv )
{
StochasticTester t;
t.classname = "MultiStageStochasticBlock";
t.is_one = []( Block * b ) {
return( dynamic_cast< MultiStageStochasticBlock * >( b ) != nullptr );
};
// permanent round-trip check of the MSSB Solution machinery (runs on every
// instance, baked and shared-tree alike)
t.check = CheckSolutionRoundTrip;
return( stochastic_test( argc , argv , t ) );
}
/*--------------------------------------------------------------------------*/
/*--------------------------- End File test.cpp ----------------------------*/
/*--------------------------------------------------------------------------*/