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
Line 
1#include <string>
2#include <sstream>
3#include "../bdm/base/bdmbase.h"
4#include "UnitTest++.h"
5
6using namespace bdm;
7
8TEST ( rv_test ) {
9    RV::clear_all();
10
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 ) );
16
17    RV b = RV ( "{b_in_test_rv }", "2" );
18    CHECK ( !b.equal ( a ) );
19    CHECK_EQUAL ( 0, b.mint() );
20
21    RV c = RV ( "{c_in_test_rv }" );
22    CHECK_EQUAL ( 1, c.length() );
23    CHECK_EQUAL ( 1, c.size ( 0 ) );
24
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() );
28
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 ) );
38
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() );
42
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() );
49
50    // structure of a, b, c
51    str s = abc.tostr();
52    int exp_ids[] = { a.id(0), a.id(0), a.id(0), b.id(0), b.id(0), c.id(0) };
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    }
60
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 ) );
65
66    // find a in abc
67    ivec f = a.findself ( abc );
68    CHECK_EQUAL ( 1, f.length() );
69    CHECK_EQUAL ( 0, f ( 0 ) );
70
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    }
80
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() );
86
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    }
95
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() );
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
110    // Copy indices between ba and ab
111    RV ba = b;
112    ba.add ( a );
113
114    ivec ai;
115    ivec bi;
116    ba.dataind ( ac, ai, bi );
117
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    }
135
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 );
145
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
148
149    //test if unique names work
150    RV uniq1 ( "y", 1 );
151    RV uniq2 ( "y", 1 );
152
153    CHECK_EQUAL ( uniq1.id ( 0 ), uniq2.id ( 0 ) );
154
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 ) );
158}
Note: See TracBrowser for help on using the browser.