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