root/library/tests/rv_test.cpp @ 530

Revision 530, 3.2 kB (checked in by vbarta, 15 years ago)

clearing up RVs before test for results independent of link order

  • 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 ( test_rv ) {
9        RV::clear_all();
10
11        RV a = RV ( "{a_in_test_rv }", "3" );
12        CHECK_EQUAL ( 1, a.length() );
13        CHECK_EQUAL ( 3, a.size ( 0 ) );
14        CHECK_EQUAL ( std::string ( "a_in_test_rv" ), a.name ( 0 ) );
15
16        RV b = RV ( "{b_in_test_rv }", "2" );
17        CHECK_EQUAL ( 0, b.mint() );
18
19        RV c = RV ( "{c_in_test_rv }" );
20        CHECK_EQUAL ( 1, c.length() );
21        CHECK_EQUAL ( 1, c.size ( 0 ) );
22
23        RV trv = RV ( "{e_in_test_rv f_in_test_rv }", "1 2", "3 4" );
24        CHECK_EQUAL ( 2, trv.length() );
25        CHECK_EQUAL ( 3, trv.mint() );
26
27        // add a and b
28        RV ab = a;
29        bool added = ab.add ( b );
30        CHECK ( added );
31        CHECK_EQUAL ( 2, ab.length() );
32        CHECK_EQUAL ( 3, ab.size ( 0 ) );
33        CHECK_EQUAL ( std::string ( "a_in_test_rv" ), ab.name ( 0 ) );
34        CHECK_EQUAL ( 2, ab.size ( 1 ) );
35        CHECK_EQUAL ( std::string ( "b_in_test_rv" ), ab.name ( 1 ) );
36
37        std::stringstream abss;
38        abss << ab;
39        CHECK_EQUAL ( std::string ( "1(3)=a_in_test_rv_{0}; 2(2)=b_in_test_rv_{0}; " ), abss.str() );
40
41        // concat a, b and c
42        RV abc = concat ( ab, c );
43        CHECK_EQUAL ( 3, abc.length() );
44        std::stringstream abcss;
45        abcss << abc;
46        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() );
47
48        // structure of a, b, c
49        str s = abc.tostr();
50        int exp_ids[] = { 1, 1, 1, 2, 2, 3 };
51        int exp_sz = sizeof ( exp_ids ) / sizeof ( exp_ids[0] );
52        CHECK_EQUAL ( exp_sz, s.ids.size() );
53        CHECK_EQUAL ( exp_sz, s.times.size() );
54        for ( int i = 0; i < exp_sz; ++i ) {
55                CHECK_EQUAL ( exp_ids[i], s.ids ( i ) );
56                CHECK_EQUAL ( 0, s.times ( i ) );
57        }
58
59        RV slice = abc ( 1, 2 );
60        CHECK_EQUAL ( 1, slice.length() );
61        CHECK_EQUAL ( 3, slice.size ( 0 ) );
62        CHECK_EQUAL ( std::string ( "a_in_test_rv" ), slice.name ( 0 ) );
63
64        // find a in abc
65        ivec f = a.findself ( abc );
66        CHECK_EQUAL ( 1, f.length() );
67        CHECK_EQUAL ( 0, f ( 0 ) );
68
69        // find abc in a
70        f = abc.findself ( a );
71        int exp_indices[] = { 0, -1, -1 };
72        CHECK_EQUAL ( 3, f.length() );
73        for ( unsigned i = 0;
74                i < sizeof ( exp_indices ) / sizeof ( exp_indices[0] );
75                ++i ) {
76                CHECK_EQUAL ( exp_indices[i], f ( i ) );
77        }
78
79        // subtract b from abc
80        RV ac = abc.subt ( b );
81        std::stringstream acss;
82        acss << ac;
83        CHECK_EQUAL ( std::string ( "1(3)=a_in_test_rv_{0}; 3(1)=c_in_test_rv_{0}; " ), acss.str() );
84
85        // data index of ac in abc
86        ivec di = ac.dataind ( abc );
87        int exp_di[] = { 0, 1, 2, 5 };
88        exp_sz = sizeof ( exp_di ) / sizeof ( exp_di[0] );
89        CHECK_EQUAL ( exp_sz, di.size() );
90        for ( int i = 0; i < exp_sz; ++i ) {
91                CHECK_EQUAL ( exp_di[i], di ( i ) );
92        }
93
94        // Copy indices between ba and ab
95        RV ba = b;
96        ba.add ( a );
97
98        ivec ai;
99        ivec bi;
100        ba.dataind ( ac, ai, bi );
101
102        int exp_ai[] = { 2, 3, 4 };
103        exp_sz = sizeof ( exp_ai ) / sizeof ( exp_ai[0] );
104        CHECK_EQUAL ( exp_sz, ai.size() );
105        for ( unsigned i = 0;
106                i < sizeof ( exp_ai ) / sizeof ( exp_ai[0] );
107                ++i ) {
108                CHECK_EQUAL ( exp_ai[i], ai ( i ) );
109        }
110
111        int exp_bi[] = { 0, 1, 2 };
112        exp_sz = sizeof ( exp_bi ) / sizeof ( exp_bi[0] );
113        CHECK_EQUAL ( exp_sz, bi.size() );
114        for ( unsigned i = 0;
115                i < sizeof ( exp_bi ) / sizeof ( exp_bi[0] );
116                ++i ) {
117                CHECK_EQUAL ( exp_bi[i], bi ( i ) );
118        }
119}
Note: See TracBrowser for help on using the browser.