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