root/library/tests/testsuite/rv_test.cpp @ 1078

Revision 1078, 4.7 kB (checked in by smidl, 14 years ago)

fixed rv_test

  • Property svn:eol-style set to native
RevLine 
[422]1#include <string>
2#include <sstream>
[386]3#include "../bdm/base/bdmbase.h"
[422]4#include "UnitTest++.h"
[147]5
[254]6using namespace bdm;
[147]7
[689]8TEST ( rv_test ) {
[1064]9    RV::clear_all();
[722]10
[1064]11    RV a = RV ( "{a_in_test_rv }", "3" );
12    CHECK ( a.equal ( a ) );
13    CHECK_EQUAL ( 1, a.length() );
14    CHECK_EQUAL ( 3, a.size ( 0 ) );
15    CHECK_EQUAL ( std::string ( "a_in_test_rv" ), a.name ( 0 ) );
[147]16
[1064]17    RV b = RV ( "{b_in_test_rv }", "2" );
18    CHECK ( !b.equal ( a ) );
19    CHECK_EQUAL ( 0, b.mint() );
[422]20
[1064]21    RV c = RV ( "{c_in_test_rv }" );
22    CHECK_EQUAL ( 1, c.length() );
23    CHECK_EQUAL ( 1, c.size ( 0 ) );
[422]24
[1064]25    RV trv = RV ( "{e_in_test_rv f_in_test_rv }", "1 2", "3 4" );
26    CHECK_EQUAL ( 2, trv.length() );
27    CHECK_EQUAL ( 3, trv.mint() );
[422]28
[1064]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 ) );
35    CHECK_EQUAL ( std::string ( "a_in_test_rv" ), ab.name ( 0 ) );
36    CHECK_EQUAL ( 2, ab.size ( 1 ) );
37    CHECK_EQUAL ( std::string ( "b_in_test_rv" ), ab.name ( 1 ) );
[422]38
[1064]39    std::stringstream abss;
40    abss << ab;
41    CHECK_EQUAL ( std::string ( "[a_in_test_rv(3)_{0}; b_in_test_rv(2)_{0}; ]" ), abss.str() );
[422]42
[1064]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;
48    CHECK_EQUAL ( std::string ( "[a_in_test_rv(3)_{0}; b_in_test_rv(2)_{0}; c_in_test_rv(1)_{0}; ]" ), abcss.str() );
[422]49
[1064]50    // structure of a, b, c
51    str s = abc.tostr();
[1078]52    int exp_ids[] = { a.id(0), a.id(0), a.id(0), b.id(0), b.id(0), c.id(0) };
[1064]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
[1064]61    RV slice = abc ( 1, 2 );
62    CHECK_EQUAL ( 1, slice.length() );
63    CHECK_EQUAL ( 3, slice.size ( 0 ) );
64    CHECK_EQUAL ( std::string ( "a_in_test_rv" ), slice.name ( 0 ) );
[422]65
[1064]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
[1064]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
[1064]81    // subtract b from abc
82    RV ac = abc.subt ( b );
83    std::stringstream acss;
84    acss << ac;
85    CHECK_EQUAL ( std::string ( "[a_in_test_rv(3)_{0}; c_in_test_rv(1)_{0}; ]" ), acss.str() );
[422]86
[1064]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
[1064]96    // subselect
97    RV bc = abc ( ivec ( "1 2" ) );
98    std::stringstream bcss;
99    bcss << bc;
100    CHECK_EQUAL ( std::string ( "[b_in_test_rv(2)_{0}; c_in_test_rv(1)_{0}; ]" ), bcss.str() );
[531]101
102#if 0
[1064]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() );
[531]108#endif
109
[1064]110    // Copy indices between ba and ab
111    RV ba = b;
112    ba.add ( a );
[147]113
[1064]114    ivec ai;
115    ivec bi;
116    ba.dataind ( ac, ai, bi );
[422]117
[1064]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    }
[477]126
[1064]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    }
[722]135
[1064]136    // check uniqueness
137    RV join = a;
138    join.add ( b );
139    RV tmp = a;
140    tmp.t_plus ( 1 );
141    join.add ( tmp );
142    tmp = b;
143    tmp.t_plus ( -1 );
144    join.add ( tmp );
[604]145
[1064]146    CHECK_EQUAL ( unique ( join._ids() ), vec_2 ( a.id ( 0 ), b.id ( 0 ) ) ); // find only ids of a and b
147    CHECK_EQUAL ( unique_complement ( join._ids(), vec_1 ( a.id ( 0 ) ) ), vec_1 ( b.id ( 0 ) ) ); // complemnet of a in previous is b
[722]148
[1064]149    //test if unique names work
150    RV uniq1 ( "y", 1 );
151    RV uniq2 ( "y", 1 );
[722]152
[1064]153    CHECK_EQUAL ( uniq1.id ( 0 ), uniq2.id ( 0 ) );
[722]154
[1064]155    // check scalarname
156    CHECK_EQUAL ( "a_in_test_rv_2", abc.scalarname ( 2 ) );
157    CHECK_EQUAL ( "b_in_test_rv_0", abc.scalarname ( 3 ) );
[147]158}
Note: See TracBrowser for help on using the browser.