-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_BlockModification.cpp
More file actions
1601 lines (1394 loc) · 57.5 KB
/
Copy pathtests_BlockModification.cpp
File metadata and controls
1601 lines (1394 loc) · 57.5 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_BlockModification.cpp ------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Unit tests for the Modification a Block issues and for the way they reach
* the Solver registered to it or to one of its ancestors.
*
* A FakeSolver is registered to an AbstractBlock and the list of the
* Modification it has received is inspected after each change: the tests go
* over the addition and the removal of dynamic Variable and Constraint (with
* the empty Range and the empty Subset, which the documentation gives
* opposite meanings), over the arithmetic of the parameter that says if, how
* and where a Modification is issued, over the channels that pack them into
* a GroupModification (nested, forced, empty, emptied, discarded, and
* hijacking the default channel), and over the path of a Modification from a sub-Block to the
* Solver of its father.
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Universita' di Pisa \n
*
* \copyright © by Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "AbstractBlock.h"
#include "ColVariable.h"
#include "FakeSolver.h"
#include "FRowConstraint.h"
#include "LinearFunction.h"
#include <iostream>
#include <list>
#include <stdexcept>
#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 Index = Block::Index;
using Range = Block::Range;
using Subset = Block::Subset;
using ChnlName = Observer::ChnlName;
/*--------------------------------------------------------------------------*/
/*------------------------------ FUNCTIONS ---------------------------------*/
/*--------------------------------------------------------------------------*/
/// number of failed checks of the documented contract
static int n_failed = 0;
/// records and prints a failed check of the documented contract
/** Used where the documentation of the library promises something that the
* test found to be false: the failure is printed and counted, and the other
* cases still run. */
static void expect( bool ok , const char * what )
{
if( ok )
return;
std::cout << "FAILED: " << what << std::endl;
++n_failed;
}
/*--------------------------------------------------------------------------*/
/// true if calling f() throws std::invalid_argument
template< class F >
static bool throws( F f )
{
try {
f();
}
catch( std::invalid_argument & ) {
return( true );
}
return( false );
}
/*--------------------------------------------------------------------------*/
/// the Modification downcast to M, nullptr if it is not one
template< class M >
static std::shared_ptr< M > as( const sp_Mod & mod )
{
return( std::dynamic_pointer_cast< M >( mod ) );
}
/*--------------------------------------------------------------------------*/
/// an AbstractBlock that records every Modification it is given
/** It records what reaches its add_Modification() before handing it to the
* base class, i.e., what the Block itself "sees", as opposed to what its
* Solver receive. */
class PeepingBlock : public AbstractBlock {
public:
explicit PeepingBlock( Block * father = nullptr )
: AbstractBlock( father ) {}
void add_Modification( sp_Mod mod , ChnlName chnl = 0 ) override {
seen.push_back( mod );
AbstractBlock::add_Modification( mod , chnl );
}
std::vector< sp_Mod > seen; ///< what the Block has been given
};
/*--------------------------------------------------------------------------*/
/// true if the stuff is among the active stuff of the Variable, by scanning
static bool listed( const Variable & v , const ThinVarDepInterface * stuff )
{
for( Index i = 0 ; i < v.get_num_active() ; ++i )
if( v.get_active( i ) == stuff )
return( true );
return( false );
}
/*--------------------------------------------------------------------------*/
/// the values of the ColVariable of a list, in list order
static std::vector< double > values_of( const std::list< ColVariable > & l )
{
std::vector< double > rv;
for( const auto & v : l )
rv.push_back( v.get_value() );
return( rv );
}
/*--------------------------------------------------------------------------*/
/// a list of n ColVariable whose values are start, start + 1, ...
static void number( std::list< ColVariable > & l , double start )
{
for( auto & v : l )
v.set_value( start++ );
}
/*--------------------------------------------------------------------------*/
/// gives each row of the list the Function 1 * var and the given RHS
/** The RHS are start, start + 1, ... in list order, which is what tells the
* rows apart once they have been moved into a Modification. */
static void fill( std::list< FRowConstraint > & rows , ColVariable & var ,
double start )
{
for( auto & row : rows ) {
row.set_function( new LinearFunction( { { & var , 1.0 } } ) , eNoMod );
row.set_lhs( - Inf< double >() , eNoMod );
row.set_rhs( start++ , eNoMod );
}
}
/*--------------------------------------------------------------------------*/
/// the RHS of the rows of a list, in list order
static std::vector< double > rhs_of( const std::list< FRowConstraint > & l )
{
std::vector< double > rv;
for( const auto & r : l )
rv.push_back( r.get_rhs() );
return( rv );
}
/*--------------------------------------------------------------------------*/
/*--------------------------------- TESTS ----------------------------------*/
/*--------------------------------------------------------------------------*/
/* The arithmetic of the composite parameter: make_par( iM , 0 ) == iM, the
* mode and the channel come back out of it untouched for every mode and for
* the extreme channel names, un_ModBlock() / not_ModBlock() downgrade only
* eModBlck and keep the channel, and par2concern() / not_dry_run() read the
* mode alone. */
static void test_ModParam_arithmetic( void )
{
const ModParam modes[] = { eDryRun , eNoMod , eNoBlck , eModBlck };
const ChnlName chnls[] = { 0 , 1 , 2 , 1000 , Observer::max_channel_name };
for( auto iM : modes ) {
assert( Observer::make_par( iM , 0 ) == iM );
for( auto ch : chnls ) {
const auto par = Observer::make_par( iM , ch );
assert( Observer::par2mod( par ) == iM );
assert( Observer::par2chnl( par ) == ch );
assert( Observer::par2concern( par ) == ( iM == eModBlck ) );
assert( Observer::not_dry_run( par ) == ( iM != eDryRun ) );
const auto down = Observer::un_ModBlock( par );
assert( Observer::par2chnl( down ) == ch );
assert( Observer::par2mod( down ) == ( iM == eModBlck ? eNoBlck : iM ) );
ModParam inplace = par;
Observer::not_ModBlock( inplace );
assert( inplace == down );
}
}
// the greatest composite value fits a ModParam without wrapping
const auto top = Observer::make_par( eModBlck , Observer::max_channel_name );
assert( Observer::par2chnl( top ) == Observer::max_channel_name );
assert( Observer::par2mod( top ) == eModBlck );
std::cout << "ModParam arithmetic: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* issue_mod() and issue_pmod() against anyone_there(): eModBlck always
* issues an "abstract" Modification and eNoBlck only if someone listens,
* while a "physical" one treats eModBlck as eNoBlck; eDryRun and eNoMod
* never issue anything. The channel part of the parameter is irrelevant. */
static void test_issue_mod( void )
{
auto block = new AbstractBlock;
for( int there = 0 ; there < 2 ; ++there ) {
if( there )
block->register_Solver( new FakeSolver() );
assert( block->anyone_there() == bool( there ) );
for( ChnlName ch : { ChnlName( 0 ) , ChnlName( 5 ) } ) {
assert( ! block->issue_mod( Observer::make_par( eDryRun , ch ) ) );
assert( ! block->issue_mod( Observer::make_par( eNoMod , ch ) ) );
assert( block->issue_mod( Observer::make_par( eNoBlck , ch ) ) ==
bool( there ) );
assert( block->issue_mod( Observer::make_par( eModBlck , ch ) ) );
assert( ! block->issue_pmod( Observer::make_par( eDryRun , ch ) ) );
if( block->issue_pmod( Observer::make_par( eNoMod , ch ) ) ) {
std::cout << " issue_pmod( make_par( eNoMod , " << ch
<< " ) ) is true with " << ( there ? "a" : "no" )
<< " Solver" << std::endl;
expect( false , "issue_pmod() is false for eNoMod (Observer.h: "
"\"eNoMod the Modification is *not* issued\")" );
}
assert( block->issue_pmod( Observer::make_par( eNoBlck , ch ) ) ==
bool( there ) );
assert( block->issue_pmod( Observer::make_par( eModBlck , ch ) ) ==
bool( there ) );
}
}
block->unregister_Solvers( true );
assert( ! block->anyone_there() );
delete block;
std::cout << "issue_mod / issue_pmod: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* open_if_needed() opens a channel only if at least two Modification are
* going to be issued and the mode issues them at all, nesting it into the
* channel of the parameter if there is one; close_if_needed() closes only
* what the former has opened. */
static void test_open_if_needed( void )
{
auto block = new AbstractBlock;
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
// nothing is opened for less than two, nor for eNoMod
assert( block->open_if_needed( eModBlck , 1 ) == eModBlck );
assert( block->open_if_needed( eModBlck , 0 ) == eModBlck );
assert( block->open_if_needed( eNoMod , 5 ) == eNoMod );
block->close_if_needed( eModBlck , 1 );
block->close_if_needed( eNoMod , 5 );
assert( mods.empty() );
// a new channel, keeping the mode
auto par = block->open_if_needed( eNoBlck , 2 );
const auto ch = Observer::par2chnl( par );
assert( ch != 0 );
assert( Observer::par2mod( par ) == eNoBlck );
// nested into the one of the parameter: the name does not change
auto inner = block->open_if_needed( Observer::make_par( eModBlck , ch ) , 3 );
assert( Observer::par2chnl( inner ) == ch );
assert( Observer::par2mod( inner ) == eModBlck );
block->close_if_needed( inner , 3 );
assert( mods.empty() );
block->close_if_needed( par , 2 );
// the two levels were both closed: one GroupModification holding the
// empty inner one
assert( mods.size() == 1 );
auto gm = as< GroupModification >( mods.front() );
assert( gm && ( gm->father() == nullptr ) );
assert( gm->sub_Modifications().size() == 1 );
assert( as< GroupModification >( gm->sub_Modifications().front() ) );
assert( throws( [ & ]() { block->close_channel( ch ); } ) );
block->unregister_Solvers( true );
delete block;
std::cout << "open_if_needed / close_if_needed: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* Adding dynamic Variable: one BlockModAdd< ColVariable > whose first() is
* the size of the list before the addition and whose added() are, in order,
* the addresses the new Variable have in the list; eNoBlck gives
* concerns_Block() == false, eNoMod and an empty list to add give nothing,
* and in all cases the new Variable belong to the Block. */
static void test_add_dynamic_variables( void )
{
auto block = new AbstractBlock;
auto x = new std::list< ColVariable >( 2 );
block->add_dynamic_variable( *x , "x" );
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
mods.clear();
std::list< ColVariable > more( 3 );
std::vector< ColVariable * > addr;
for( auto & v : more )
addr.push_back( & v );
block->add_dynamic_variables( *x , more );
assert( more.empty() && ( x->size() == 5 ) );
assert( mods.size() == 1 );
auto add = as< BlockModAdd< ColVariable > >( mods.front() );
assert( add );
assert( add->is_variable() && add->is_added() && add->concerns_Block() );
assert( ( & add->whc() == x ) && ( add->first() == 2 ) );
assert( add->added() == addr );
assert( add->get_Block() == block );
{
// the addresses did not change in the splice: they are x[ 2 ], ... x[ 4 ]
auto it = std::next( x->begin() , 2 );
for( auto p : addr )
assert( & *( it++ ) == p );
for( auto p : addr )
assert( p->get_Block() == block );
std::vector< Variable * > vv;
add->get_elements( vv );
assert( vv.size() == 3 );
std::vector< Constraint * > cv( 7 );
add->get_elements( cv );
assert( cv.empty() );
}
// eNoBlck: issued, but not a concern of the Block
mods.clear();
std::list< ColVariable > one( 1 );
block->add_dynamic_variables( *x , one , eNoBlck );
assert( mods.size() == 1 );
add = as< BlockModAdd< ColVariable > >( mods.front() );
assert( add && ( ! add->concerns_Block() ) && ( add->first() == 5 ) );
// eNoMod: done, not issued
mods.clear();
std::list< ColVariable > two( 2 );
block->add_dynamic_variables( *x , two , eNoMod );
assert( mods.empty() && ( x->size() == 8 ) );
assert( x->back().get_Block() == block );
// nothing to add: nothing done, nothing issued
std::list< ColVariable > none;
block->add_dynamic_variables( *x , none );
assert( mods.empty() && ( x->size() == 8 ) );
block->unregister_Solvers( true );
delete block;
std::cout << "add dynamic Variable: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* Adding dynamic Constraint: the same as for Variable, with the new
* Constraint belonging to the Block and a BlockModAdd< FRowConstraint > that
* gives no Variable in get_elements(). */
static void test_add_dynamic_constraints( void )
{
auto block = new AbstractBlock;
auto var = new ColVariable;
block->add_static_variable( *var , "v" );
auto rows = new std::list< FRowConstraint >( 1 );
fill( *rows , *var , 0 );
block->add_dynamic_constraint( *rows , "rows" );
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
mods.clear();
std::list< FRowConstraint > more( 2 );
fill( more , *var , 1 );
std::vector< FRowConstraint * > addr;
for( auto & r : more )
addr.push_back( & r );
block->add_dynamic_constraints( *rows , more );
assert( more.empty() && ( rows->size() == 3 ) );
assert( ( rhs_of( *rows ) == std::vector< double >( { 0 , 1 , 2 } ) ) );
assert( mods.size() == 1 );
auto add = as< BlockModAdd< FRowConstraint > >( mods.front() );
assert( add && ( ! add->is_variable() ) && add->is_added() );
assert( ( & add->whc() == rows ) && ( add->first() == 1 ) );
assert( add->added() == addr );
assert( add->get_Block() == block );
for( auto p : addr )
assert( p->get_Block() == block );
{
std::vector< Constraint * > cv;
add->get_elements( cv );
assert( ( cv.size() == 2 ) && ( cv[ 0 ] == addr[ 0 ] ) );
std::vector< Variable * > vv( 4 );
add->get_elements( vv );
assert( vv.empty() );
}
// the base BlockModAD is what a Solver not knowing the type catches
assert( as< BlockModAD >( mods.front() ) );
mods.clear();
std::list< FRowConstraint > none;
block->add_dynamic_constraints( *rows , none );
assert( mods.empty() );
block->unregister_Solvers( true );
delete block;
std::cout << "add dynamic Constraint: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* Removing a Range of dynamic Variable: a BlockModRmvRngd with that range
* and the removed Variable, in their order, still alive inside it; an empty
* or reversed Range removes and issues nothing; a Range covering the whole
* list gives a BlockModRmvSbst with an empty subset(); a Range past the end
* throws. */
static void test_remove_range( void )
{
auto block = new AbstractBlock;
auto x = new std::list< ColVariable >( 6 );
number( *x , 0 );
block->add_dynamic_variable( *x , "x" );
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
mods.clear();
block->remove_dynamic_variables( *x , Range( 1 , 3 ) );
assert( ( values_of( *x ) == std::vector< double >( { 0 , 3 , 4 , 5 } ) ) );
assert( mods.size() == 1 );
{
auto rmv = as< BlockModRmvRngd< ColVariable > >( mods.front() );
assert( rmv && rmv->is_variable() && ( ! rmv->is_added() ) );
assert( rmv->concerns_Block() && ( & rmv->whc() == x ) );
assert( rmv->range() == Range( 1 , 3 ) );
assert( ( values_of( rmv->removed() ) ==
std::vector< double >( { 1 , 2 } ) ) );
assert( rmv->get_Block() == block );
std::vector< Variable * > vv;
rmv->get_elements( vv );
assert( ( vv.size() == 2 ) && ( vv[ 0 ] == & rmv->removed().front() ) );
}
// empty and reversed Range: nothing at all
mods.clear();
block->remove_dynamic_variables( *x , Range( 2 , 2 ) );
block->remove_dynamic_variables( *x , Range( 3 , 1 ) );
assert( mods.empty() && ( x->size() == 4 ) );
// past the end: an error, and the list is left alone
assert( throws( [ & ]() {
block->remove_dynamic_variables( *x , Range( 2 , 9 ) ); } ) );
assert( mods.empty() && ( x->size() == 4 ) );
// eNoMod: done, not issued
block->remove_dynamic_variables( *x , Range( 0 , 1 ) , eNoMod );
assert( mods.empty() );
assert( ( values_of( *x ) == std::vector< double >( { 3 , 4 , 5 } ) ) );
// the whole list: a BlockModRmvSbst with empty subset()
block->remove_dynamic_variables( *x , Range( 0 , 3 ) , eNoBlck );
assert( x->empty() && ( mods.size() == 1 ) );
{
auto rmv = as< BlockModRmvSbst< ColVariable > >( mods.front() );
assert( rmv && rmv->subset().empty() && ( ! rmv->concerns_Block() ) );
assert( ( values_of( rmv->removed() ) ==
std::vector< double >( { 3 , 4 , 5 } ) ) );
}
// an empty list with an empty Range is fine, a non-empty Range throws
mods.clear();
block->remove_dynamic_variables( *x , Range( 0 , 0 ) );
assert( throws( [ & ]() {
block->remove_dynamic_variables( *x , Range( 0 , 1 ) ); } ) );
assert( mods.empty() );
block->unregister_Solvers( true );
delete block;
std::cout << "remove a Range: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* Removing a Subset of dynamic Variable: an unordered Subset is ordered
* inside and the BlockModRmvSbst gives it ordered, with removed() in the
* same order; a contiguous Subset is a Range, and either Modification must
* say the same positions; a Subset that is the whole list gives an empty
* subset(); the empty Subset removes everything, and on an empty list it
* removes and issues nothing; a position past the end throws. */
static void test_remove_subset( void )
{
auto block = new AbstractBlock;
auto x = new std::list< ColVariable >( 8 );
number( *x , 0 );
block->add_dynamic_variable( *x , "x" );
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
mods.clear();
block->remove_dynamic_variables( *x , Subset( { 6 , 1 , 4 } ) );
assert( ( values_of( *x ) ==
std::vector< double >( { 0 , 2 , 3 , 5 , 7 } ) ) );
assert( mods.size() == 1 );
{
auto rmv = as< BlockModRmvSbst< ColVariable > >( mods.front() );
assert( rmv && rmv->concerns_Block() && ( & rmv->whc() == x ) );
assert( ( rmv->subset() == Subset( { 1 , 4 , 6 } ) ) );
assert( ( values_of( rmv->removed() ) ==
std::vector< double >( { 1 , 4 , 6 } ) ) );
}
// a contiguous Subset: whatever the Modification, the same positions
mods.clear();
block->remove_dynamic_variables( *x , Subset( { 2 , 1 } ) );
assert( ( values_of( *x ) == std::vector< double >( { 0 , 5 , 7 } ) ) );
assert( mods.size() == 1 );
if( auto rng = as< BlockModRmvRngd< ColVariable > >( mods.front() ) )
assert( rng->range() == Range( 1 , 3 ) );
else {
auto sbst = as< BlockModRmvSbst< ColVariable > >( mods.front() );
assert( sbst && ( sbst->subset() == Subset( { 1 , 2 } ) ) );
}
// past the end: an error, and the list is left alone
mods.clear();
assert( throws( [ & ]() {
block->remove_dynamic_variables( *x , Subset( { 0 , 3 } ) ); } ) );
assert( mods.empty() && ( x->size() == 3 ) );
// the whole list in disorder: nothing left, empty subset()
block->remove_dynamic_variables( *x , Subset( { 2 , 0 , 1 } ) );
assert( x->empty() && ( mods.size() == 1 ) );
{
auto rmv = as< BlockModRmvSbst< ColVariable > >( mods.front() );
assert( rmv && rmv->subset().empty() );
assert( ( values_of( rmv->removed() ) ==
std::vector< double >( { 0 , 5 , 7 } ) ) );
}
// the empty Subset: "remove all"
std::list< ColVariable > more( 4 );
number( more , 10 );
block->add_dynamic_variables( *x , more , eNoMod );
mods.clear();
block->remove_dynamic_variables( *x , Subset() );
assert( x->empty() && ( mods.size() == 1 ) );
{
auto rmv = as< BlockModRmvSbst< ColVariable > >( mods.front() );
assert( rmv && rmv->subset().empty() );
assert( ( values_of( rmv->removed() ) ==
std::vector< double >( { 10 , 11 , 12 , 13 } ) ) );
assert( rmv->get_Block() == block );
}
// the empty Subset of an empty list: nothing to remove, nothing issued
mods.clear();
block->remove_dynamic_variables( *x , Subset() );
assert( mods.empty() );
// a non-empty Subset of an empty list is an error
assert( throws( [ & ]() {
block->remove_dynamic_variables( *x , Subset( { 0 } ) ); } ) );
// the empty Subset with eNoMod: everything goes, nothing is issued
std::list< ColVariable > again( 2 );
block->add_dynamic_variables( *x , again , eNoMod );
block->remove_dynamic_variables( *x , Subset() , false , eNoMod );
assert( x->empty() && mods.empty() );
block->unregister_Solvers( true );
delete block;
std::cout << "remove a Subset: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* Removing single elements and vectors of iterators: one element in the
* middle is the Range [ i , i + 1 ), the last one left is the empty
* subset(); ordered iterators give the positions they had, a contiguous run
* of them a Range, and unordered ones are an error when the Modification is
* issued. */
static void test_remove_iterators( void )
{
auto block = new AbstractBlock;
auto x = new std::list< ColVariable >( 6 );
number( *x , 0 );
block->add_dynamic_variable( *x , "x" );
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
mods.clear();
block->remove_dynamic_variable( *x , std::next( x->begin() , 2 ) );
assert( ( values_of( *x ) ==
std::vector< double >( { 0 , 1 , 3 , 4 , 5 } ) ) );
assert( mods.size() == 1 );
{
auto rmv = as< BlockModRmvRngd< ColVariable > >( mods.front() );
assert( rmv && ( rmv->range() == Range( 2 , 3 ) ) );
assert( rmv->removed().front().get_value() == 2 );
}
// ordered, not contiguous: the positions they had
mods.clear();
{
std::vector< std::list< ColVariable >::iterator > its =
{ std::next( x->begin() , 1 ) , std::next( x->begin() , 3 ) };
block->remove_dynamic_variables( *x , its );
}
assert( ( values_of( *x ) == std::vector< double >( { 0 , 3 , 5 } ) ) );
assert( mods.size() == 1 );
{
auto rmv = as< BlockModRmvSbst< ColVariable > >( mods.front() );
assert( rmv && ( rmv->subset() == Subset( { 1 , 3 } ) ) );
assert( ( values_of( rmv->removed() ) ==
std::vector< double >( { 1 , 4 } ) ) );
}
// ordered and contiguous: a Range
mods.clear();
{
std::vector< std::list< ColVariable >::iterator > its =
{ std::next( x->begin() , 1 ) , std::next( x->begin() , 2 ) };
block->remove_dynamic_variables( *x , its );
}
assert( ( values_of( *x ) == std::vector< double >( { 0 } ) ) );
assert( mods.size() == 1 );
{
auto rmv = as< BlockModRmvRngd< ColVariable > >( mods.front() );
assert( rmv && ( rmv->range() == Range( 1 , 3 ) ) );
}
// the last one: the empty subset()
mods.clear();
block->remove_dynamic_variable( *x , x->begin() );
assert( x->empty() && ( mods.size() == 1 ) );
{
auto rmv = as< BlockModRmvSbst< ColVariable > >( mods.front() );
assert( rmv && rmv->subset().empty() && ( rmv->removed().size() == 1 ) );
}
// an empty vector of iterators: nothing
mods.clear();
{
std::vector< std::list< ColVariable >::iterator > its;
block->remove_dynamic_variables( *x , its );
}
assert( mods.empty() );
// unordered iterators are an error when the Modification is issued; the
// list they are taken from is a throwaway one, since the documentation
// says nothing about what is left of it
auto y = new std::list< ColVariable >( 4 );
block->add_dynamic_variable( *y , "y" );
{
std::vector< std::list< ColVariable >::iterator > its =
{ std::next( y->begin() , 3 ) , std::next( y->begin() , 1 ) };
assert( throws( [ & ]() { block->remove_dynamic_variables( *y , its ); } ) );
}
block->unregister_Solvers( true );
delete block;
std::cout << "remove by iterators: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* Removing dynamic Constraint: the Variable they were active in no longer
* list them, whether or not the Modification is issued, and the rows in
* the Modification are the removed ones in their order; the empty Subset
* removes all of them. */
static void test_remove_constraints( void )
{
auto block = new AbstractBlock;
auto var = new ColVariable;
block->add_static_variable( *var , "v" );
auto rows = new std::list< FRowConstraint >( 6 );
fill( *rows , *var , 0 );
block->add_dynamic_constraint( *rows , "rows" );
assert( var->get_num_active() == 6 );
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
mods.clear();
std::vector< const FRowConstraint * > gone;
for( auto it = std::next( rows->begin() , 1 ) ;
it != std::next( rows->begin() , 3 ) ; ++it )
gone.push_back( & *it );
block->remove_dynamic_constraints( *rows , Range( 1 , 3 ) );
assert( ( rhs_of( *rows ) == std::vector< double >( { 0 , 3 , 4 , 5 } ) ) );
assert( var->get_num_active() == 4 );
for( auto c : gone ) {
assert( ! listed( *var , c ) );
// is_active() of a stuff that is not there is >= get_num_active()
if( var->is_active( c ) < var->get_num_active() )
std::cout << " ColVariable::is_active() of a removed row gives "
<< var->is_active( c ) << " < get_num_active() = "
<< var->get_num_active() << std::endl;
expect( var->is_active( c ) >= var->get_num_active() ,
"ColVariable::is_active() of a stuff it is not active in is >= "
"get_num_active() (Variable.h)" );
}
assert( mods.size() == 1 );
{
auto rmv = as< BlockModRmvRngd< FRowConstraint > >( mods.front() );
assert( rmv && ( ! rmv->is_variable() ) && ( rmv->range() == Range( 1 , 3 ) ) );
assert( ( rhs_of( rmv->removed() ) == std::vector< double >( { 1 , 2 } ) ) );
assert( & rmv->removed().front() == gone[ 0 ] );
}
// a Subset
mods.clear();
block->remove_dynamic_constraints( *rows , Subset( { 3 , 0 } ) );
assert( ( rhs_of( *rows ) == std::vector< double >( { 3 , 4 } ) ) );
assert( var->get_num_active() == 2 );
assert( mods.size() == 1 );
{
auto rmv = as< BlockModRmvSbst< FRowConstraint > >( mods.front() );
assert( rmv && ( rmv->subset() == Subset( { 0 , 3 } ) ) );
assert( ( rhs_of( rmv->removed() ) == std::vector< double >( { 0 , 5 } ) ) );
}
// eNoMod: the Variable is told all the same
mods.clear();
block->remove_dynamic_constraints( *rows , Range( 0 , 1 ) , eNoMod );
assert( mods.empty() && ( rows->size() == 1 ) );
assert( var->get_num_active() == 1 );
// the empty Subset: all of them
block->remove_dynamic_constraints( *rows , Subset() );
assert( rows->empty() && ( var->get_num_active() == 0 ) );
assert( mods.size() == 1 );
{
auto rmv = as< BlockModRmvSbst< FRowConstraint > >( mods.front() );
assert( rmv && rmv->subset().empty() && ( rmv->removed().size() == 1 ) );
}
// the single-element version
std::list< FRowConstraint > more( 3 );
fill( more , *var , 10 );
block->add_dynamic_constraints( *rows , more , eNoMod );
mods.clear();
block->remove_dynamic_constraint( *rows , std::next( rows->begin() ) );
assert( ( rhs_of( *rows ) == std::vector< double >( { 10 , 12 } ) ) );
assert( mods.size() == 1 );
{
auto rmv = as< BlockModRmvRngd< FRowConstraint > >( mods.front() );
assert( rmv && ( rmv->range() == Range( 1 , 2 ) ) );
}
block->unregister_Solvers( true );
delete block;
std::cout << "remove dynamic Constraint: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* What the removal of a Constraint relies on in the Variable: is_active()
* of a stuff the Variable is not active in is >= get_num_active() (also
* when that stuff would sort before one it is active in), and
* remove_active() of such a stuff does not remove another one. The two
* rows are elements of the same vector, hence the first has the smaller
* address. */
static void test_active_of_absent_stuff( void )
{
std::vector< FRowConstraint > r( 2 );
ColVariable v;
v.add_active( & r[ 1 ] );
assert( ( v.get_num_active() == 1 ) && listed( v , & r[ 1 ] ) );
assert( v.is_active( & r[ 1 ] ) == 0 );
const auto pos = v.is_active( & r[ 0 ] );
if( pos < v.get_num_active() )
std::cout << " ColVariable::is_active() of a stuff it is not "
<< "active in gives " << pos << std::endl;
expect( pos >= v.get_num_active() ,
"ColVariable::is_active() of a stuff it is not active in, sorting "
"before one it is active in, is >= get_num_active() (Variable.h)" );
try {
v.remove_active( & r[ 0 ] );
}
catch( std::invalid_argument & ) {}
if( ! listed( v , & r[ 1 ] ) )
std::cout << " ColVariable::remove_active( &r[ 0 ] ) removed "
<< "&r[ 1 ] instead" << std::endl;
expect( listed( v , & r[ 1 ] ) ,
"ColVariable::remove_active() of a stuff it is not active in leaves "
"the other active stuff alone" );
std::cout << "active stuff of a Variable: done" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* Removing a dynamic Variable active in a Constraint: the Constraint loses
* it in any case; with issueindMod == eNoMod only the BlockModRmv is issued,
* otherwise the Modification of the Constraint come first. */
static void test_remove_active_variable( void )
{
auto block = new AbstractBlock;
auto x = new std::list< ColVariable >( 2 );
block->add_dynamic_variable( *x , "x" );
auto rows = new std::vector< FRowConstraint >( 1 );
block->add_static_constraint( *rows , "r" );
auto & row = ( *rows )[ 0 ];
row.set_function( new LinearFunction( { { & x->front() , 1.0 } ,
{ & x->back() , 2.0 } } ) , eNoMod );
assert( row.get_num_active_var() == 2 );
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
mods.clear();
// the individual Modification switched off
block->remove_dynamic_variables( *x , Range( 1 , 2 ) , eModBlck , eNoMod );
assert( row.get_num_active_var() == 1 );
assert( row.get_active_var( 0 ) == & x->front() );
assert( mods.size() == 1 );
assert( as< BlockModRmvRngd< ColVariable > >( mods.front() ) );
// the individual Modification issued: they precede the BlockModRmv
mods.clear();
block->remove_dynamic_variables( *x , Subset() );
assert( row.get_num_active_var() == 0 );
assert( mods.size() >= 2 );
assert( as< BlockModRmvSbst< ColVariable > >( mods.back() ) );
for( auto it = mods.begin() ; it != std::prev( mods.end() ) ; ++it )
assert( ! as< BlockModAD >( *it ) );
block->unregister_Solvers( true );
delete block;
std::cout << "remove an active Variable: OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* Who sees what according to the mode, with and without a Solver: eModBlck
* is issued even if nobody listens, and the Block sees it, but it goes no
* further; eNoBlck is not issued at all when nobody listens; eNoMod is never
* issued. VariableMod carries the state before and after the change, a
* change that changes nothing issues nothing, and set_value() is not a
* Modification at all. */
static void test_modes_and_listening( void )
{
auto block = new PeepingBlock;
auto s = new std::vector< ColVariable >( 2 );
block->add_static_variable( *s , "s" );
auto & v = ( *s )[ 0 ];
// nobody listening
v.is_fixed( true , eNoBlck );
assert( v.is_fixed() && block->seen.empty() );
v.is_fixed( false );
assert( ( ! v.is_fixed() ) && ( block->seen.size() == 1 ) );
v.is_fixed( true , eNoMod );
assert( v.is_fixed() && ( block->seen.size() == 1 ) );
// a Solver listening
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
mods.clear();
block->seen.clear();
v.is_fixed( false , eNoBlck );
assert( ( block->seen.size() == 1 ) && ( mods.size() == 1 ) );
assert( mods.front() == block->seen.front() );
{
auto vm = as< VariableMod >( mods.front() );
assert( vm && ( vm->variable() == & v ) && ( ! vm->concerns_Block() ) );
assert( ( vm->old_state() & 1 ) && ! ( vm->new_state() & 1 ) );
assert( vm->get_Block() == block );
}
mods.clear();
v.is_fixed( true );
assert( mods.size() == 1 );
{
auto vm = as< VariableMod >( mods.front() );
assert( vm && vm->concerns_Block() && ( vm->new_state() & 1 ) );
}
// nothing changes, nothing issued
mods.clear();
v.is_fixed( true );
v.is_integer( false );
assert( mods.empty() );
// is_integer() reports a change of the "type" in the same way
v.is_integer( true );
assert( mods.size() == 1 );
{
auto vm = as< VariableMod >( mods.front() );
assert( vm && ( vm->old_state() != vm->new_state() ) );
assert( ( vm->old_state() & 1 ) && ( vm->new_state() & 1 ) );
}
// eNoMod: nothing
mods.clear();
block->seen.clear();
v.is_fixed( false , eNoMod );
assert( mods.empty() && block->seen.empty() );
// the value is not a Modification
v.set_value( 42 );
( *s )[ 1 ].set_value( -1 );
assert( mods.empty() && block->seen.empty() );
block->unregister_Solvers( true );
delete block;
std::cout << "modes and anyone_there(): OK" << std::endl;
}
/*--------------------------------------------------------------------------*/
/* A channel: what is sent to it reaches the Solver as exactly one
* GroupModification when it is closed, in the order it was sent, while the
* Block itself sees each Modification naked when it is issued; what is sent
* to channel 0 meanwhile goes through at once; the GroupModification
* concerns the Block if any of its elements does; a closed channel cannot be
* closed or sent to again; two open channels have distinct names. */
static void test_channel( void )
{
auto block = new PeepingBlock;
auto s = new std::vector< ColVariable >( 3 );
block->add_static_variable( *s , "s" );
auto solver = new FakeSolver();
block->register_Solver( solver );
auto & mods = solver->get_Modification_list();
const auto ch = block->open_channel();
assert( ( ch != 0 ) && ( ch <= Observer::max_channel_name ) );
const auto other = block->open_channel();
assert( ( other != 0 ) && ( other != ch ) );
block->close_channel( other );
mods.clear();
( *s )[ 0 ].is_fixed( true , Observer::make_par( eNoBlck , ch ) );
( *s )[ 1 ].is_fixed( true ); // the default channel: at once
( *s )[ 2 ].is_fixed( true , Observer::make_par( eNoBlck , ch ) );
assert( mods.size() == 1 );
assert( as< VariableMod >( mods.front() )->variable() == & ( *s )[ 1 ] );
assert( block->seen.size() == 3 );
assert( as< VariableMod >( block->seen[ 0 ] ) );
mods.clear();
block->seen.clear();
block->close_channel( ch );
assert( mods.size() == 1 );
auto gm = as< GroupModification >( mods.front() );
assert( gm && ( gm->father() == nullptr ) );
assert( gm->sub_Modifications().size() == 2 );
assert( as< VariableMod >( gm->sub_Modifications().front() )->variable() ==
& ( *s )[ 0 ] );
assert( as< VariableMod >( gm->sub_Modifications().back() )->variable() ==
& ( *s )[ 2 ] );
assert( ! gm->concerns_Block() ); // all of them were eNoBlck