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