[422] | 1 | #include <string> |
---|
| 2 | #include <sstream> |
---|
[386] | 3 | #include "../bdm/base/bdmbase.h" |
---|
[422] | 4 | #include "UnitTest++.h" |
---|
[147] | 5 | |
---|
[254] | 6 | using namespace bdm; |
---|
[147] | 7 | |
---|
[477] | 8 | TEST ( test_rv ) { |
---|
[530] | 9 | RV::clear_all(); |
---|
| 10 | |
---|
[528] | 11 | RV a = RV ( "{a_in_test_rv }", "3" ); |
---|
[531] | 12 | CHECK ( a.equal ( a ) ); |
---|
[477] | 13 | CHECK_EQUAL ( 1, a.length() ); |
---|
| 14 | CHECK_EQUAL ( 3, a.size ( 0 ) ); |
---|
[528] | 15 | CHECK_EQUAL ( std::string ( "a_in_test_rv" ), a.name ( 0 ) ); |
---|
[147] | 16 | |
---|
[528] | 17 | RV b = RV ( "{b_in_test_rv }", "2" ); |
---|
[531] | 18 | CHECK ( !b.equal ( a ) ); |
---|
[477] | 19 | CHECK_EQUAL ( 0, b.mint() ); |
---|
[422] | 20 | |
---|
[528] | 21 | RV c = RV ( "{c_in_test_rv }" ); |
---|
[477] | 22 | CHECK_EQUAL ( 1, c.length() ); |
---|
| 23 | CHECK_EQUAL ( 1, c.size ( 0 ) ); |
---|
[422] | 24 | |
---|
[528] | 25 | RV trv = RV ( "{e_in_test_rv f_in_test_rv }", "1 2", "3 4" ); |
---|
[477] | 26 | CHECK_EQUAL ( 2, trv.length() ); |
---|
| 27 | CHECK_EQUAL ( 3, trv.mint() ); |
---|
[422] | 28 | |
---|
[477] | 29 | // add a and b |
---|
| 30 | RV ab = a; |
---|
| 31 | bool added = ab.add ( b ); |
---|
| 32 | CHECK ( added ); |
---|
| 33 | CHECK_EQUAL ( 2, ab.length() ); |
---|
| 34 | CHECK_EQUAL ( 3, ab.size ( 0 ) ); |
---|
[528] | 35 | CHECK_EQUAL ( std::string ( "a_in_test_rv" ), ab.name ( 0 ) ); |
---|
[477] | 36 | CHECK_EQUAL ( 2, ab.size ( 1 ) ); |
---|
[528] | 37 | CHECK_EQUAL ( std::string ( "b_in_test_rv" ), ab.name ( 1 ) ); |
---|
[422] | 38 | |
---|
[477] | 39 | std::stringstream abss; |
---|
| 40 | abss << ab; |
---|
[528] | 41 | CHECK_EQUAL ( std::string ( "1(3)=a_in_test_rv_{0}; 2(2)=b_in_test_rv_{0}; " ), abss.str() ); |
---|
[422] | 42 | |
---|
[477] | 43 | // concat a, b and c |
---|
| 44 | RV abc = concat ( ab, c ); |
---|
| 45 | CHECK_EQUAL ( 3, abc.length() ); |
---|
| 46 | std::stringstream abcss; |
---|
| 47 | abcss << abc; |
---|
[528] | 48 | CHECK_EQUAL ( std::string ( "1(3)=a_in_test_rv_{0}; 2(2)=b_in_test_rv_{0}; 3(1)=c_in_test_rv_{0}; " ), abcss.str() ); |
---|
[422] | 49 | |
---|
[477] | 50 | // structure of a, b, c |
---|
| 51 | str s = abc.tostr(); |
---|
| 52 | int exp_ids[] = { 1, 1, 1, 2, 2, 3 }; |
---|
| 53 | int exp_sz = sizeof ( exp_ids ) / sizeof ( exp_ids[0] ); |
---|
| 54 | CHECK_EQUAL ( exp_sz, s.ids.size() ); |
---|
| 55 | CHECK_EQUAL ( exp_sz, s.times.size() ); |
---|
| 56 | for ( int i = 0; i < exp_sz; ++i ) { |
---|
| 57 | CHECK_EQUAL ( exp_ids[i], s.ids ( i ) ); |
---|
| 58 | CHECK_EQUAL ( 0, s.times ( i ) ); |
---|
| 59 | } |
---|
[422] | 60 | |
---|
[477] | 61 | RV slice = abc ( 1, 2 ); |
---|
| 62 | CHECK_EQUAL ( 1, slice.length() ); |
---|
| 63 | CHECK_EQUAL ( 3, slice.size ( 0 ) ); |
---|
[528] | 64 | CHECK_EQUAL ( std::string ( "a_in_test_rv" ), slice.name ( 0 ) ); |
---|
[422] | 65 | |
---|
[477] | 66 | // find a in abc |
---|
| 67 | ivec f = a.findself ( abc ); |
---|
| 68 | CHECK_EQUAL ( 1, f.length() ); |
---|
| 69 | CHECK_EQUAL ( 0, f ( 0 ) ); |
---|
[422] | 70 | |
---|
[477] | 71 | // find abc in a |
---|
| 72 | f = abc.findself ( a ); |
---|
| 73 | int exp_indices[] = { 0, -1, -1 }; |
---|
| 74 | CHECK_EQUAL ( 3, f.length() ); |
---|
| 75 | for ( unsigned i = 0; |
---|
| 76 | i < sizeof ( exp_indices ) / sizeof ( exp_indices[0] ); |
---|
| 77 | ++i ) { |
---|
| 78 | CHECK_EQUAL ( exp_indices[i], f ( i ) ); |
---|
| 79 | } |
---|
[422] | 80 | |
---|
[477] | 81 | // subtract b from abc |
---|
| 82 | RV ac = abc.subt ( b ); |
---|
| 83 | std::stringstream acss; |
---|
| 84 | acss << ac; |
---|
[528] | 85 | CHECK_EQUAL ( std::string ( "1(3)=a_in_test_rv_{0}; 3(1)=c_in_test_rv_{0}; " ), acss.str() ); |
---|
[422] | 86 | |
---|
[477] | 87 | // data index of ac in abc |
---|
| 88 | ivec di = ac.dataind ( abc ); |
---|
| 89 | int exp_di[] = { 0, 1, 2, 5 }; |
---|
| 90 | exp_sz = sizeof ( exp_di ) / sizeof ( exp_di[0] ); |
---|
| 91 | CHECK_EQUAL ( exp_sz, di.size() ); |
---|
| 92 | for ( int i = 0; i < exp_sz; ++i ) { |
---|
| 93 | CHECK_EQUAL ( exp_di[i], di ( i ) ); |
---|
| 94 | } |
---|
[422] | 95 | |
---|
[531] | 96 | // subselect |
---|
| 97 | RV bc = abc ( ivec ( "1 2" ) ); |
---|
| 98 | std::stringstream bcss; |
---|
| 99 | bcss << bc; |
---|
| 100 | CHECK_EQUAL ( std::string ( "2(2)=b_in_test_rv_{0}; 3(1)=c_in_test_rv_{0}; " ), bcss.str() ); |
---|
| 101 | |
---|
| 102 | #if 0 |
---|
| 103 | // actually doesn't select, just reorders the variables - |
---|
| 104 | // wonder if that's correct... |
---|
| 105 | bc = abc ( 1, 2 ); |
---|
| 106 | bcss << bc; |
---|
| 107 | CHECK_EQUAL ( std::string ( "2(2)=b_in_test_rv_{0}; 3(1)=c_in_test_rv_{0}; " ), bcss.str() ); |
---|
| 108 | #endif |
---|
| 109 | |
---|
[477] | 110 | // Copy indices between ba and ab |
---|
| 111 | RV ba = b; |
---|
| 112 | ba.add ( a ); |
---|
[147] | 113 | |
---|
[477] | 114 | ivec ai; |
---|
| 115 | ivec bi; |
---|
| 116 | ba.dataind ( ac, ai, bi ); |
---|
[422] | 117 | |
---|
[477] | 118 | int exp_ai[] = { 2, 3, 4 }; |
---|
| 119 | exp_sz = sizeof ( exp_ai ) / sizeof ( exp_ai[0] ); |
---|
| 120 | CHECK_EQUAL ( exp_sz, ai.size() ); |
---|
| 121 | for ( unsigned i = 0; |
---|
| 122 | i < sizeof ( exp_ai ) / sizeof ( exp_ai[0] ); |
---|
| 123 | ++i ) { |
---|
| 124 | CHECK_EQUAL ( exp_ai[i], ai ( i ) ); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | int exp_bi[] = { 0, 1, 2 }; |
---|
| 128 | exp_sz = sizeof ( exp_bi ) / sizeof ( exp_bi[0] ); |
---|
| 129 | CHECK_EQUAL ( exp_sz, bi.size() ); |
---|
| 130 | for ( unsigned i = 0; |
---|
| 131 | i < sizeof ( exp_bi ) / sizeof ( exp_bi[0] ); |
---|
| 132 | ++i ) { |
---|
| 133 | CHECK_EQUAL ( exp_bi[i], bi ( i ) ); |
---|
| 134 | } |
---|
[584] | 135 | |
---|
| 136 | // check uniqueness |
---|
| 137 | RV join=a; |
---|
| 138 | join.add(b); |
---|
| 139 | RV tmp=a; tmp.t(1); |
---|
| 140 | join.add(tmp); |
---|
| 141 | tmp=b; tmp.t(-1); |
---|
| 142 | join.add(tmp); |
---|
| 143 | |
---|
| 144 | CHECK_EQUAL(unique(join._ids()), vec_2(a.id(0), b.id(0))); |
---|
[147] | 145 | } |
---|