-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_Configuration.cpp
More file actions
2703 lines (2329 loc) · 100 KB
/
Copy pathtests_Configuration.cpp
File metadata and controls
2703 lines (2329 loc) · 100 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------------*/
/*---------------------- File tests_Configuration.cpp ----------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Unit tests for the Configuration of the core: SimpleConfiguration,
* ComputeConfig, BlockConfig and OCRBlockConfig, BlockSolverConfig.
*
* A Configuration is read from text, written and read back in netCDF and
* cloned, and each of these has to give back what was put in; the text
* format is also taken through the ways it has to redirect to a file ("*"
* followed by a filename, with the executable-wide prefix and the "[idx]"
* position in a netCDF file) and through the cascade override "*file +" of
* a ComputeConfig, with and without the slot of its extra Configuration. The
* Configuration of a Block are got from a Block and applied to another, in
* both modes, and cleared; the BlockSolverConfig register their Solver, and
* the cleared ones remove all and only these.
*
* A second set of tests, whose files go in a directory of their own that
* is removed at the end, compares what is read with what was written field
* by field: the pairs of numbers and the nested SimpleConfiguration, the
* meta-configuration with its "*file" and "*file +" entries, whose extra
* slot may be left out before the next key, the ComputeConfig applied to a
* ThinComputeInterface, the ten slots of a BlockConfig and what its print()
* writes, the :BlockConfig with the handlers of the Objective, of the
* Constraint and of the sub-Block, and the RBlockSolverConfig.
*
* The checks that are made with expect() rather than assert() are those
* whose failure leaves the test able to go on: each failed one is printed,
* and main() returns non-zero if any has failed, so that a run shows all of
* them at once.
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "AbstractBlock.h"
#include "BlockSolverConfig.h"
#include "FakeSolver.h"
#include "RBlockConfig.h"
#include <chrono>
#include <filesystem>
#include <memory>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
// last, so that the headers above are read as the library was compiled
#include "TestAssert.h"
/*--------------------------------------------------------------------------*/
/*-------------------------------- USING -----------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace SMSpp_di_unipi_it;
using PairConf = SimpleConfiguration< std::pair< Configuration * ,
Configuration * > >;
using MapConf = SimpleConfiguration< std::map< std::string ,
Configuration * > >;
using VecConf = SimpleConfiguration< std::vector< Configuration * > >;
using SC_int = SimpleConfiguration< int >;
using SC_dbl = SimpleConfiguration< double >;
using SC_map =
SimpleConfiguration< std::map< std::string , Configuration * > >;
/*--------------------------------------------------------------------------*/
/*-------------------------- STATIC MEMBERS --------------------------------*/
/*--------------------------------------------------------------------------*/
// the core does not register SimpleConfiguration< std::string >, whoever
// uses it does [see SimpleConfiguration]
SMSpp_insert_in_factory_cpp_0_t( SimpleConfiguration< std::string > );
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
static int n_failed = 0; ///< the number of expect() that have failed
/// reports a failed check without stopping the test
static void expect( bool ok , const std::string & what )
{
if( ok )
return;
++n_failed;
std::cout << "FAILED: " << what << std::endl;
}
/*--------------------------------------------------------------------------*/
/// writes the text in the file with the given name
static void write_file( const std::string & name , const std::string & text )
{
std::ofstream f( name );
f << text;
}
/*--------------------------------------------------------------------------*/
/// the netCDF file every round trip goes through
static const std::string nc_file = "tests_Configuration.nc4";
/* Writes the Configuration as the only one of an eConfigFile and reads it
* back with Configuration::deserialize( filename ); the caller owns what
* comes back, which is nullptr if the reading fails. */
/* The serialize() that takes a filename is called through the base class,
* since the :Configuration that define their own serialize() hide it. */
static void write_nc( const Configuration & c , const std::string & fn ,
int type = eConfigFile , bool replace = true )
{
c.serialize( fn , type , replace );
}
/*--------------------------------------------------------------------------*/
static Configuration * nc_round_trip( const Configuration & c )
{
write_nc( c , nc_file );
return( Configuration::deserialize( nc_file ) );
}
/*--------------------------------------------------------------------------*/
/// reads a Configuration out of the text with Configuration::deserialize()
static Configuration * from_text( const std::string & text )
{
std::istringstream in( text );
return( Configuration::deserialize( in ) );
}
/*--------------------------------------------------------------------------*/
/* Loads the Configuration out of the text with the operator>>() of
* Configuration, which is called through the base class: for a
* SimpleConfiguration< T > the operator>>() that SMSTypedefs.h has for any
* container C< T > is a better match, and it does not compile. */
static void load_into( Configuration & c , const std::string & text )
{
std::istringstream in( text );
in >> c;
}
/*--------------------------------------------------------------------------*/
/// what the Configuration prints
static std::string printed( const Configuration & c )
{
std::ostringstream out;
out << c;
return( out.str() );
}
/*--------------------------------------------------------------------------*/
/// the value of a Configuration that has to be a SimpleConfiguration< T >
template< class T >
static const T & value_of( const Configuration * c )
{
auto sc = dynamic_cast< const SimpleConfiguration< T > * >( c );
assert( sc );
return( sc->f_value );
}
/*--------------------------------------------------------------------------*/
/// true if the call throws an exception of type E
template< class E , class F >
static bool throws( F f )
{
try {
f();
}
catch( E & ) {
return( true );
}
catch( ... ) {
return( false );
}
return( false );
}
/*--------------------------------------------------------------------------*/
/// true if the two ComputeConfig hold the same parameters and flags
static bool same( const ComputeConfig & a , const ComputeConfig & b )
{
return( ( a.diff() == b.diff() ) && ( a.relax() == b.relax() ) &&
( a.int_pars == b.int_pars ) && ( a.dbl_pars == b.dbl_pars ) &&
( a.str_pars == b.str_pars ) && ( a.vint_pars == b.vint_pars ) &&
( a.vdbl_pars == b.vdbl_pars ) && ( a.vstr_pars == b.vstr_pars ) );
}
/*--------------------------------------------------------------------------*/
/*------------------- TESTS OF SimpleConfiguration -------------------------*/
/*--------------------------------------------------------------------------*/
/* A SimpleConfiguration< int > is read past comments, prints its value in a
* form that reads back to the same one, goes through netCDF and clone(),
* and a value that is not an int makes load() throw. */
static void test_simple_int( void )
{
SimpleConfiguration< int > c;
load_into( c , "# a comment\n -42 # and another one\n" );
assert( c.f_value == -42 );
SimpleConfiguration< int > r;
load_into( r , printed( c ) );
assert( r.f_value == -42 );
auto n = nc_round_trip( c );
assert( n && ( value_of< int >( n ) == -42 ) );
assert( n->classname() == "SimpleConfiguration<int>" );
delete n;
auto k = c.clone();
assert( ( k != & c ) && ( k->f_value == -42 ) );
delete k;
SimpleConfiguration< int > bad;
assert( throws< std::invalid_argument >( [ & ]() {
load_into( bad , "notanumber" ); } ) );
// the name of the class first, then the value
auto t = from_text( "SimpleConfiguration<int> 7" );
assert( t && ( value_of< int >( t ) == 7 ) );
delete t;
std::cout << "SimpleConfiguration< int >: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A SimpleConfiguration< double > keeps values that are exact in binary
* through text, netCDF and clone(). */
static void test_simple_double( void )
{
for( double v : { 3.25 , -0.5 , 0.0 , 1e+300 } ) {
SimpleConfiguration< double > c( v );
SimpleConfiguration< double > r;
load_into( r , printed( c ) );
assert( r.f_value == v );
auto n = nc_round_trip( c );
assert( n && ( value_of< double >( n ) == v ) );
delete n;
auto k = c.clone();
assert( k->f_value == v );
delete k;
}
std::cout << "SimpleConfiguration< double >: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A SimpleConfiguration< std::string >, registered in this file, reads one
* word, prints it, and goes through netCDF and clone(). */
static void test_simple_string( void )
{
SimpleConfiguration< std::string > c;
load_into( c , " # comment\n hello_world trailing" );
assert( c.f_value == "hello_world" );
assert( printed( c ) == "hello_world" );
auto n = nc_round_trip( c );
assert( n && ( value_of< std::string >( n ) == "hello_world" ) );
delete n;
// the empty string is a value too, in netCDF
SimpleConfiguration< std::string > e( std::string( "" ) );
n = nc_round_trip( e );
assert( n && value_of< std::string >( n ).empty() );
delete n;
auto k = c.clone();
assert( k->f_value == "hello_world" );
delete k;
std::cout << "SimpleConfiguration< std::string >: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A SimpleConfiguration< std::vector< int > > reads the dense, the sparse
* and the empty format, and a nonempty and an empty one go through netCDF
* and clone(). */
static void test_simple_vector_int( void )
{
using V = std::vector< int >;
SimpleConfiguration< V > c;
load_into( c , "3 # three of them\n 1 -2 3" );
assert( ( c.f_value == V{ 1 , -2 , 3 } ) );
// five elements, two not default: 7 in position 1 and 9 in position 3
load_into( c , "-5 2 1 7 3 9" );
assert( ( c.f_value == V{ 0 , 7 , 0 , 9 , 0 } ) );
auto n = nc_round_trip( c );
assert( n && ( value_of< V >( n ) == V{ 0 , 7 , 0 , 9 , 0 } ) );
delete n;
auto k = c.clone();
assert( ( k->f_value == V{ 0 , 7 , 0 , 9 , 0 } ) );
delete k;
load_into( c , "0" );
assert( c.f_value.empty() );
n = nc_round_trip( c );
assert( n && value_of< V >( n ).empty() );
delete n;
std::cout << "SimpleConfiguration< std::vector< int > >: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* The same for a SimpleConfiguration< std::vector< double > >, whose
* sparse format may have all its elements at the default. */
static void test_simple_vector_double( void )
{
using V = std::vector< double >;
SimpleConfiguration< V > c;
load_into( c , "2 0.25 -1.5" );
assert( ( c.f_value == V{ 0.25 , -1.5 } ) );
auto n = nc_round_trip( c );
assert( n && ( value_of< V >( n ) == V{ 0.25 , -1.5 } ) );
delete n;
load_into( c , "-3 0" );
assert( ( c.f_value == V{ 0 , 0 , 0 } ) );
auto k = c.clone();
assert( ( k->f_value == V{ 0 , 0 , 0 } ) );
delete k;
std::cout << "SimpleConfiguration< std::vector< double > >: OK"
<< std::endl;
}
/*--------------------------------------------------------------------------*/
/* A pair of Configuration * is read with the "*" for nullptr, is cloned
* deeply, clear()-s both halves, and goes through netCDF with a nullptr in
* it. */
static void test_simple_pair_of_configurations( void )
{
auto c = from_text( "SimpleConfiguration<std::pair<Configuration*,"
"Configuration*>>\n"
" SimpleConfiguration<int> 3 # the first\n"
" * # no second\n" );
auto p = dynamic_cast< PairConf * >( c );
assert( p );
assert( p->f_value.first && ( ! p->f_value.second ) );
assert( value_of< int >( p->f_value.first ) == 3 );
auto k = p->clone();
assert( k->f_value.first && ( k->f_value.first != p->f_value.first ) );
assert( value_of< int >( k->f_value.first ) == 3 );
assert( ! k->f_value.second );
delete k;
auto n = nc_round_trip( *p );
auto np = dynamic_cast< PairConf * >( n );
assert( np );
assert( np->f_value.first && ( value_of< int >( np->f_value.first ) == 3 ) );
assert( ! np->f_value.second );
delete n;
// a pair with two ComputeConfig: clear() goes to both
PairConf two;
auto cc1 = new ComputeConfig;
cc1->set_par( "a" , 1 );
auto cc2 = new ComputeConfig;
cc2->set_par( "b" , 2.0 );
two.f_value = { cc1 , cc2 };
two.clear();
assert( cc1->int_pars.empty() && cc2->dbl_pars.empty() );
delete c;
std::cout << "SimpleConfiguration< std::pair< Configuration * , "
<< "Configuration * > >: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A map from strings to Configuration * is read with its count, with a
* nullptr among the values, is cloned deeply and goes through netCDF; the
* empty one is read out of a zero count. */
static void test_simple_map( void )
{
auto c = from_text( "SimpleConfiguration<std::map<std::string,"
"Configuration*>>\n"
"3\n"
"alpha SimpleConfiguration<int> 1\n"
"beta *\n"
"gamma SimpleConfiguration<double> 2.5\n" );
auto m = dynamic_cast< MapConf * >( c );
assert( m );
assert( m->f_value.size() == 3 );
assert( value_of< int >( m->f_value.at( "alpha" ) ) == 1 );
assert( ! m->f_value.at( "beta" ) );
assert( value_of< double >( m->f_value.at( "gamma" ) ) == 2.5 );
auto k = m->clone();
assert( k->f_value.size() == 3 );
assert( k->f_value.at( "alpha" ) != m->f_value.at( "alpha" ) );
assert( value_of< int >( k->f_value.at( "alpha" ) ) == 1 );
delete k;
// what is written in netCDF is what is read back, into a fresh one
auto n = nc_round_trip( *m );
auto nm = dynamic_cast< MapConf * >( n );
expect( nm , "netCDF round trip of SimpleConfiguration< std::map< "
"std::string , Configuration * > > gives back a map" );
if( nm ) {
expect( nm->f_value.size() == 3 ,
"netCDF round trip of SimpleConfiguration< std::map< std::string "
", Configuration * > >: 3 keys written, " +
std::to_string( nm->f_value.size() ) + " read back" );
auto it = nm->f_value.find( "alpha" );
expect( ( it != nm->f_value.end() ) && it->second &&
( value_of< int >( it->second ) == 1 ) ,
"netCDF round trip of the map: \"alpha\" -> 1 read back" );
it = nm->f_value.find( "beta" );
expect( ( it != nm->f_value.end() ) && ( ! it->second ) ,
"netCDF round trip of the map: \"beta\" -> nullptr read back" );
it = nm->f_value.find( "gamma" );
expect( ( it != nm->f_value.end() ) && it->second &&
( value_of< double >( it->second ) == 2.5 ) ,
"netCDF round trip of the map: \"gamma\" -> 2.5 read back" );
}
delete n;
auto e = from_text( "SimpleConfiguration<std::map<std::string,"
"Configuration*>> 0" );
assert( e && dynamic_cast< MapConf * >( e )->f_value.empty() );
delete e;
delete c;
std::cout << "SimpleConfiguration< std::map< std::string , "
<< "Configuration * > >: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A vector of Configuration * goes through netCDF, which needs the "type"
* attribute every serialized Configuration has to start with [see
* Configuration::serialize( netCDF::NcGroup )]. */
static void test_simple_vector_of_configurations( void )
{
VecConf v;
v.f_value = { new SimpleConfiguration< int >( 4 ) ,
new SimpleConfiguration< double >( 0.5 ) };
auto k = v.clone();
assert( ( k->f_value.size() == 2 ) && ( k->f_value[ 0 ] != v.f_value[ 0 ] ) );
assert( value_of< int >( k->f_value[ 0 ] ) == 4 );
delete k;
auto n = nc_round_trip( v );
auto nv = dynamic_cast< VecConf * >( n );
expect( nv , "netCDF round trip of SimpleConfiguration< std::vector< "
"Configuration * > > gives back a vector (the serialized group has "
"no \"type\" attribute)" );
if( nv ) {
expect( ( nv->f_value.size() == 2 ) && nv->f_value[ 1 ] &&
( value_of< int >( nv->f_value[ 0 ] ) == 4 ) &&
( value_of< double >( nv->f_value[ 1 ] ) == 0.5 ) ,
"netCDF round trip of the vector of Configuration *: { 4 , 0.5 } "
"read back" );
}
delete n;
// a nullptr in it is read back as nullptr
VecConf w;
w.f_value = { nullptr , new SimpleConfiguration< int >( 1 ) };
n = nc_round_trip( w );
nv = dynamic_cast< VecConf * >( n );
expect( nv && ( nv->f_value.size() == 2 ) && ( ! nv->f_value[ 0 ] ) &&
nv->f_value[ 1 ] && ( value_of< int >( nv->f_value[ 1 ] ) == 1 ) ,
"netCDF round trip of the vector { nullptr , 1 } of Configuration *" );
delete n;
std::cout << "SimpleConfiguration< std::vector< Configuration * > >: done"
<< std::endl;
}
/*--------------------------------------------------------------------------*/
/*------------------- TESTS OF THE TEXT AND FILE FORMATS -------------------*/
/*--------------------------------------------------------------------------*/
/* Configuration::deserialize( std::istream ): the empty stream and a "*"
* alone give nullptr, a name not in the factory throws, "*filename" loads
* a text or a netCDF file, and a missing file gives nullptr. */
static void test_deserialize_stream( void )
{
assert( ! from_text( "" ) );
assert( ! from_text( " # only a comment\n" ) );
assert( ! from_text( "* # nothing" ) );
assert( ! from_text( "* # nothing\n" ) );
// a "*" that ends the stream is followed by the empty string as well
bool alone = false;
try {
alone = ! from_text( "*" );
}
catch( std::exception & e ) {
std::cout << "\"*\" at the end of the stream: " << e.what() << std::endl;
}
expect( alone , "Configuration::deserialize( std::istream ) of a \"*\" "
"that ends the stream gives nullptr" );
assert( throws< std::invalid_argument >( []() {
from_text( "NoSuchConfiguration 1" ); } ) );
write_file( "tests_Configuration_int.txt" , "SimpleConfiguration<int> 11\n" );
auto t = from_text( "*tests_Configuration_int.txt" );
assert( t && ( value_of< int >( t ) == 11 ) );
delete t;
SimpleConfiguration< int > ni( 12 );
write_nc( ni , "tests_Configuration_int.nc4" );
t = from_text( "*tests_Configuration_int.nc4" );
assert( t && ( value_of< int >( t ) == 12 ) );
delete t;
assert( ! from_text( "*tests_Configuration_missing.txt" ) );
// a Configuration is read at the point of the stream it starts at, and the
// stream is left right after it
std::istringstream two( "SimpleConfiguration<int> 1 SimpleConfiguration<int> 2" );
auto c1 = Configuration::deserialize( two );
auto c2 = Configuration::deserialize( two );
assert( c1 && c2 && ( value_of< int >( c1 ) == 1 ) &&
( value_of< int >( c2 ) == 2 ) );
assert( ! Configuration::deserialize( two ) );
delete c1;
delete c2;
std::cout << "Configuration::deserialize( std::istream ): OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* The "[idx]" at the end of a netCDF filename selects the Configuration of
* an eConfigFile, the one appended by serialize( ... , false ) being the
* second; a position that is not in the file gives nullptr. */
static void test_netCDF_positions( void )
{
const std::string fn = "tests_Configuration_multi.nc4";
SimpleConfiguration< int > first( 1 );
SimpleConfiguration< double > second( 2.5 );
write_nc( first , fn );
write_nc( second , fn , eConfigFile , false );
auto c0 = Configuration::deserialize( fn );
auto c1 = Configuration::deserialize( fn + "[1]" );
auto c00 = Configuration::deserialize( fn + "[0]" );
assert( c0 && ( value_of< int >( c0 ) == 1 ) );
assert( c00 && ( value_of< int >( c00 ) == 1 ) );
assert( c1 && ( value_of< double >( c1 ) == 2.5 ) );
delete c0;
delete c1;
delete c00;
assert( ! Configuration::deserialize( fn + "[2]" ) );
// replace == true starts the file anew
write_nc( second , fn );
c0 = Configuration::deserialize( fn );
assert( c0 && ( value_of< double >( c0 ) == 2.5 ) );
delete c0;
assert( ! Configuration::deserialize( fn + "[1]" ) );
// a file type that is not one of a Configuration is refused
assert( throws< std::invalid_argument >( [ & ]() {
write_nc( first , fn , eBlockFile ); } ) );
std::cout << "positions in a netCDF file: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* The filename prefix is put before every relative filename and not before
* an absolute one. */
static void test_filename_prefix( void )
{
write_file( "tests_Configuration_int.txt" , "SimpleConfiguration<int> 11\n" );
const std::string abs = std::filesystem::absolute(
"tests_Configuration_int.txt" ).string();
Configuration::set_filename_prefix( "./" );
auto t = Configuration::deserialize( "tests_Configuration_int.txt" );
assert( t && ( value_of< int >( t ) == 11 ) );
delete t;
Configuration::set_filename_prefix( "/no/such/directory/" );
assert( ! Configuration::deserialize( "tests_Configuration_int.txt" ) );
t = Configuration::deserialize( abs );
assert( t && ( value_of< int >( t ) == 11 ) );
delete t;
Configuration::set_filename_prefix( "" );
assert( Configuration::get_filename_prefix().empty() );
std::cout << "filename prefix: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/*------------------------- TESTS OF ComputeConfig -------------------------*/
/*--------------------------------------------------------------------------*/
/// a ComputeConfig with every kind of parameter and an extra Configuration
static const char * full_ComputeConfig =
"ComputeConfig 3 # diff and relax\n"
"2 intA 5 intB -3 # int\n"
"1 dblA 0.5 # double\n"
"1 strA hello # string\n"
"2 vintA 3 1 2 3 vintB 0 # vector of int, one of them empty\n"
"1 vdblA 2 0.25 0.75 # vector of double\n"
"1 vstrA 2 one two # vector of string\n"
"SimpleConfiguration<int> 7 # extra\n";
/*--------------------------------------------------------------------------*/
/* The full format is read in all its parts, and the ComputeConfig goes
* through netCDF and clone() with all of them, the extra Configuration
* included. */
static void test_ComputeConfig_load( void )
{
auto c = from_text( full_ComputeConfig );
auto cc = dynamic_cast< ComputeConfig * >( c );
assert( cc );
assert( cc->diff() && cc->relax() );
assert( ( cc->int_pars == decltype( cc->int_pars ){ { "intA" , 5 } ,
{ "intB" , -3 } } ) );
assert( ( cc->dbl_pars == decltype( cc->dbl_pars ){ { "dblA" , 0.5 } } ) );
assert( ( cc->str_pars == decltype( cc->str_pars ){ { "strA" , "hello" } } ) );
assert( ( cc->vint_pars == decltype( cc->vint_pars ){
{ "vintA" , { 1 , 2 , 3 } } , { "vintB" , {} } } ) );
assert( ( cc->vdbl_pars == decltype( cc->vdbl_pars ){
{ "vdblA" , { 0.25 , 0.75 } } } ) );
assert( ( cc->vstr_pars == decltype( cc->vstr_pars ){
{ "vstrA" , { "one" , "two" } } } ) );
assert( value_of< int >( cc->f_extra_Configuration ) == 7 );
assert( ! cc->empty() );
auto n = nc_round_trip( *cc );
auto nc = dynamic_cast< ComputeConfig * >( n );
expect( nc , "netCDF round trip of a ComputeConfig with every kind of "
"parameter gives back a ComputeConfig" );
if( nc ) {
expect( same( *nc , *cc ) , "netCDF round trip of a ComputeConfig with "
"every kind of parameter gives back the same parameters" );
assert( nc->f_extra_Configuration &&
( value_of< int >( nc->f_extra_Configuration ) == 7 ) );
}
delete n;
// one kind of parameter at a time, to tell which of them do not make it
for( char kind : { 'i' , 'd' , 's' , 'I' , 'D' , 'S' } ) {
ComputeConfig one( *cc );
for( char other : { 'i' , 'd' , 's' , 'I' , 'D' , 'S' } )
if( other != kind )
switch( other ) {
case( 'i' ): one.int_pars.clear(); break;
case( 'd' ): one.dbl_pars.clear(); break;
case( 's' ): one.str_pars.clear(); break;
case( 'I' ): one.vint_pars.clear(); break;
case( 'D' ): one.vdbl_pars.clear(); break;
default: one.vstr_pars.clear();
}
n = nc_round_trip( one );
nc = dynamic_cast< ComputeConfig * >( n );
std::string what = std::string( "netCDF round trip of a ComputeConfig "
"with only the parameters of kind '" ) +
kind + "'";
expect( nc , what + " gives back a ComputeConfig" );
if( nc ) {
std::string got;
for( auto & p : nc->int_pars ) got += " \"" + p.first + "\"";
for( auto & p : nc->dbl_pars ) got += " \"" + p.first + "\"";
for( auto & p : nc->str_pars ) got += " \"" + p.first + "\" = \"" +
p.second + "\"";
for( auto & p : nc->vint_pars ) got += " \"" + p.first + "\"";
for( auto & p : nc->vdbl_pars ) got += " \"" + p.first + "\"";
expect( same( *nc , one ) , what + " gives back the same parameters "
"(names read back:" + got + " )" );
}
delete n;
}
auto k = cc->clone();
assert( same( *k , *cc ) );
assert( k->f_extra_Configuration &&
( k->f_extra_Configuration != cc->f_extra_Configuration ) );
k->set_par( "intA" , 6 );
assert( cc->int_pars[ 0 ].second == 5 );
delete k;
// an empty ComputeConfig goes through netCDF as well
ComputeConfig e;
e.set_diff( false );
n = nc_round_trip( e );
nc = dynamic_cast< ComputeConfig * >( n );
assert( nc && nc->empty() && ( ! nc->diff() ) && ( ! nc->relax() ) );
delete n;
delete c;
std::cout << "ComputeConfig load, netCDF and clone: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* The flags are bit 0 (diff) and bit 1 (relax); a stream that ends between
* two sections leaves the rest empty, and one that ends before the flags
* leaves f_diff == false [see ComputeConfig::load()]; a count larger than
* the entries that follow throws. */
static void test_ComputeConfig_flags( void )
{
const std::pair< const char * , std::pair< bool , bool > > cases[] = {
{ "ComputeConfig 0" , { false , false } } ,
{ "ComputeConfig 1" , { true , false } } ,
{ "ComputeConfig 2" , { false , true } } ,
{ "ComputeConfig 3" , { true , true } } };
for( auto & [ text , flags ] : cases ) {
auto c = dynamic_cast< ComputeConfig * >( from_text( text ) );
assert( c && ( c->diff() == flags.first ) &&
( c->relax() == flags.second ) && c->empty() );
delete c;
}
// the stream ends after the int parameters
auto c = dynamic_cast< ComputeConfig * >(
from_text( "ComputeConfig 0 1 intA 4\n" ) );
assert( c && ( c->int_pars.size() == 1 ) && c->dbl_pars.empty() &&
c->vstr_pars.empty() && ( ! c->f_extra_Configuration ) );
delete c;
// the stream ends before the flags
c = dynamic_cast< ComputeConfig * >( from_text( "ComputeConfig" ) );
assert( c && c->empty() );
expect( ! c->diff() , "ComputeConfig::load() of a stream that ends before "
"the flags leaves f_diff == false, as the comment of load() says "
"(\"if the attribute is not there, f_diff == false is assumed\")" );
delete c;
// two int parameters are announced, one is there
assert( throws< std::logic_error >( []() {
from_text( "ComputeConfig 0 2 intA 1 0" ); } ) );
// a count that is not a number
assert( throws< std::logic_error >( []() {
from_text( "ComputeConfig 0 1 intA 1 x" ); } ) );
std::cout << "ComputeConfig flags and partial streams: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* set_par() adds a parameter that is not there, tells whether the value
* has changed, reset_par() removes one and ignores a name that is not
* there, clear() empties everything and clears the extra Configuration. */
static void test_ComputeConfig_set_par( void )
{
ComputeConfig cc;
assert( cc.diff() && ( ! cc.relax() ) && cc.empty() );
assert( cc.set_par( "a" , 1 ) );
assert( ! cc.set_par( "a" , 1 ) );
assert( cc.set_par( "a" , 2 ) );
assert( ( cc.int_pars.size() == 1 ) && ( cc.int_pars[ 0 ].second == 2 ) );
assert( cc.set_par( "d" , 0.5 ) );
assert( ! cc.set_par( "d" , 0.5 ) );
assert( cc.set_par( "s" , std::string( "x" ) ) );
assert( ! cc.set_par( "s" , std::string( "x" ) ) );
assert( cc.set_par( "vi" , std::vector< int >{ 1 , 2 } ) );
assert( ! cc.set_par( "vi" , std::vector< int >{ 1 , 2 } ) );
assert( cc.set_par( "vd" , std::vector< double >{} ) );
assert( cc.set_par( "vs" , std::vector< std::string >{ "p" } ) );
// one entry of a vector that is there, past its end: it is extended
cc.set_par( "vi" , 4u , 9 );
assert( ( cc.vint_pars[ 0 ].second == std::vector< int >{ 1 , 2 , 0 , 0 ,
9 } ) );
cc.set_par( "vd" , 0u , 1.5 );
assert( ( cc.vdbl_pars[ 0 ].second == std::vector< double >{ 1.5 } ) );
cc.reset_par( "a" , 'i' );
assert( cc.int_pars.empty() );
cc.reset_par( "nosuchname" , 'd' );
assert( cc.dbl_pars.size() == 1 );
cc.reset_par( "vs" , 'S' );
assert( cc.vstr_pars.empty() );
assert( throws< std::invalid_argument >( [ & ]() {
cc.reset_par( "d" , 'x' ); } ) );
// clear() empties the lists, clears the extra Configuration and leaves
// f_diff == false
auto extra = new ComputeConfig;
extra->set_par( "inner" , 1 );
cc.f_extra_Configuration = extra;
cc.set_relax( true );
cc.clear();
assert( ( ! cc.diff() ) && ( ! cc.relax() ) );
assert( cc.dbl_pars.empty() && cc.str_pars.empty() &&
cc.vint_pars.empty() && cc.vdbl_pars.empty() );
assert( ( cc.f_extra_Configuration == extra ) && extra->empty() );
assert( ! cc.empty() ); // the extra Configuration is still there
std::cout << "ComputeConfig set_par, reset_par, clear: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* set_par( name , pos , value ) on a name that is not there adds the vector
* parameter, extended up to pos [see ComputeConfig::set_par()]. */
static void test_ComputeConfig_set_par_entry_of_new_name( void )
{
std::cout << "ComputeConfig set_par( name , pos , value ) on a name not "
<< "yet there: checking (a crash right after this line is this "
<< "check failing)" << std::endl;
ComputeConfig cc;
cc.set_par( "vi" , 2u , 5 );
expect( ( cc.vint_pars.size() == 1 ) &&
( cc.vint_pars[ 0 ].first == "vi" ) &&
( cc.vint_pars[ 0 ].second == std::vector< int >{ 0 , 0 , 5 } ) ,
"set_par( \"vi\" , 2 , 5 ) on an empty ComputeConfig gives vi = "
"{ 0 , 0 , 5 }" );
std::cout << "ComputeConfig set_par( name , pos , value ) on a name not "
<< "yet there: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/// the base of the cascade overrides
static const char * base_ComputeConfig =
"ComputeConfig 1\n"
"2 intA 1 intB 2\n"
"1 dblA 0.5\n"
"0\n"
"1 vintA 2 1 1\n"
"0\n"
"0\n"
"SimpleConfiguration<int> 7\n";
/// the body of an override of the base, without the extra slot
static const char * override_body =
" 0 # the flags are those of the override\n"
" 1 intB 20 # replaced\n"
" 1 dblB 1.5 # added\n"
" 0\n"
" 1 vintA 1 9 # replaced\n"
" 0\n"
" 0\n";
/*--------------------------------------------------------------------------*/
/* "*file +" loads the file and merges the body that follows onto it: the
* parameters in the body replace or are added to those of the file, the
* others are kept. At the end of the stream the extra slot can be left
* out, and the extra Configuration of the file is kept; the slot, if it is
* there, replaces it [see ComputeConfig::merge_overrides()]. */
static void test_ComputeConfig_override( void )
{
write_file( "tests_Configuration_base.txt" , base_ComputeConfig );
auto c = from_text( std::string( "*tests_Configuration_base.txt +\n" ) +
override_body );
auto cc = dynamic_cast< ComputeConfig * >( c );
assert( cc );
assert( ! cc->diff() );
assert( ( cc->int_pars == decltype( cc->int_pars ){ { "intA" , 1 } ,
{ "intB" , 20 } } ) );
assert( ( cc->dbl_pars == decltype( cc->dbl_pars ){ { "dblA" , 0.5 } ,
{ "dblB" , 1.5 } } ) );
assert( ( cc->vint_pars == decltype( cc->vint_pars ){ { "vintA" , { 9 } } } ) );
// the stream has ended where the slot would be: the extra is kept
assert( cc->f_extra_Configuration &&
( value_of< int >( cc->f_extra_Configuration ) == 7 ) );
delete c;
// the slot is there and says nullptr
c = from_text( std::string( "*tests_Configuration_base.txt +\n" ) +
override_body + " * # [none]\n" );
cc = dynamic_cast< ComputeConfig * >( c );
assert( cc && ( cc->int_pars.size() == 2 ) );
assert( ! cc->f_extra_Configuration );
delete c;
// the slot is there and says something else
c = from_text( std::string( "*tests_Configuration_base.txt +\n" ) +
override_body + " SimpleConfiguration<double> 0.25\n" );
cc = dynamic_cast< ComputeConfig * >( c );
assert( cc && cc->f_extra_Configuration &&
( value_of< double >( cc->f_extra_Configuration ) == 0.25 ) );
delete c;
// a Configuration that has no named parameters cannot be overridden
write_file( "tests_Configuration_int.txt" , "SimpleConfiguration<int> 11\n" );
assert( throws< std::invalid_argument >( []() {
from_text( "*tests_Configuration_int.txt + 4" ); } ) );
// and nothing can be overridden if the file gives nothing
assert( throws< std::invalid_argument >( []() {
from_text( "*tests_Configuration_missing.txt + 0" ); } ) );
// whitespace and comments may come between the filename and the "+"
c = from_text( std::string( "*tests_Configuration_base.txt # base\n"
" + # and now the override\n" ) + override_body );
cc = dynamic_cast< ComputeConfig * >( c );
assert( cc && ( cc->int_pars.size() == 2 ) &&
( cc->int_pars[ 1 ].second == 20 ) );
delete c;
std::cout << "ComputeConfig cascade override: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* An override body reads exactly as many items as a full ComputeConfig
* [see Configuration::merge_overrides()], the extra slot included: in a
* container, the slot "*" has to be written for the next Configuration to
* be read as the next one; without it, the next Configuration is read as
* the extra Configuration of the override. */
static void test_ComputeConfig_override_in_a_container( void )
{
write_file( "tests_Configuration_base.txt" , base_ComputeConfig );
const std::string head = "SimpleConfiguration<std::pair<Configuration*,"
"Configuration*>>\n"
"*tests_Configuration_base.txt +\n";
// with the slot
auto c = from_text( head + override_body + " * # [none]\n"
"ComputeConfig 1 1 intC 3\n" );
auto p = dynamic_cast< PairConf * >( c );
assert( p );
auto first = dynamic_cast< ComputeConfig * >( p->f_value.first );
auto second = dynamic_cast< ComputeConfig * >( p->f_value.second );
assert( first && ( first->int_pars.size() == 2 ) &&
( ! first->f_extra_Configuration ) );
assert( second && second->diff() && ( second->int_pars.size() == 1 ) &&
( second->int_pars[ 0 ].first == "intC" ) );
delete c;
// without the slot, the second ComputeConfig becomes the extra of the first
c = from_text( head + override_body + "ComputeConfig 1 1 intC 3\n" );
p = dynamic_cast< PairConf * >( c );
assert( p );
first = dynamic_cast< ComputeConfig * >( p->f_value.first );
assert( first );
auto swallowed = dynamic_cast< ComputeConfig * >(
first->f_extra_Configuration );
assert( swallowed && ( swallowed->int_pars.size() == 1 ) &&
( swallowed->int_pars[ 0 ].first == "intC" ) );
assert( ! p->f_value.second );
delete c;
std::cout << "ComputeConfig cascade override in a container: OK"
<< std::endl;
}
/*--------------------------------------------------------------------------*/
/*------------------------ TESTS OF BlockConfig ----------------------------*/
/*--------------------------------------------------------------------------*/
/* The text format: the flag, the version, then the ten slots in order; the
* old format without the version and a version that is not the current one
* throw, a stream that ends early leaves the rest nullptr. */
static void test_BlockConfig_load( void )
{
auto c = from_text( "BlockConfig 1 2\n"
"* # structure\n"