ed772b6995ecc3e268cd9f9d6900a0e8ed01730b
[mussa.git] / alg / test / test_conserved_path.cpp
1 #define BOOST_AUTO_TEST_MAIN
2 #include <boost/test/auto_unit_test.hpp>
3 #include <boost/assign/std/vector.hpp>
4 using namespace boost::assign;
5
6 #include <vector>
7 using namespace std;
8
9 #include "alg/conserved_path.hpp"
10
11 //! write tests for our conserved path types (just to make sure they
12 //! don't change on us.
13 BOOST_AUTO_TEST_CASE ( conserved_path_simple ) 
14 {
15   vector<int> path;
16   path += 3,4,5,1; // magic from boost assign
17
18   ConservedPath cp1;
19   BOOST_CHECK_EQUAL( cp1.size(), 0 ); //empty
20   BOOST_CHECK( cp1.begin() == cp1.end() );
21   
22   cp1.score = 18;
23   cp1.track_indexes = path;
24   BOOST_CHECK_EQUAL( cp1.size(), path.size() );
25
26   ConservedPath cp2(20, 18, path);
27   BOOST_CHECK_EQUAL (cp1, cp2);
28   BOOST_CHECK_EQUAL( cp2.size(), path.size() );
29
30   ConservedPath::iterator cp2_itor;
31   vector<int>::iterator path_itor;
32   for (cp2_itor = cp2.begin(), path_itor = path.begin();
33        cp2_itor != cp2.end() && path_itor != path.end();
34        ++cp2_itor, ++path_itor)
35   {
36     BOOST_CHECK_EQUAL( *cp2_itor, *path_itor );
37   }
38
39   ConservedPath cp3( cp2 );
40   BOOST_CHECK_EQUAL( cp3.size(), cp2.size() );
41   ConservedPath::iterator cp3_itor;
42   for (cp2_itor = cp2.begin(), cp3_itor = cp3.begin();
43        cp2_itor != cp2.end() && cp3_itor != cp3.end();
44        ++cp2_itor, ++cp3_itor)
45   {
46     BOOST_CHECK_EQUAL( *cp2_itor, *cp3_itor );
47   }
48 }
49
50 //! do vector like operations work?
51 BOOST_AUTO_TEST_CASE ( conserved_path_vector )
52 {
53   ConservedPath cp;
54   cp.score = 21.0;
55   
56   BOOST_CHECK_EQUAL( cp.size(), 0 );
57   cp.push_back( 3 );
58   BOOST_CHECK_EQUAL( cp.size(), 1);
59   BOOST_CHECK_EQUAL( cp[0], 3 );
60   cp.push_back( 4 );
61   BOOST_CHECK_EQUAL( cp.size(), 2);
62   BOOST_CHECK_EQUAL( cp[1], 4 );
63   cp.pop_back();
64   BOOST_CHECK_EQUAL( cp.size(), 1 );
65 }
66
67 // does the reverse compliment test work?
68 BOOST_AUTO_TEST_CASE ( conserved_path_reverse_compliment ) 
69 {
70   vector<int> path;
71   path += 3,4,-5,1; // magic from boost assign
72
73   ConservedPath cp1(20, 10, path);
74   vector<bool> reversed = cp1.reverseComplimented();
75
76   BOOST_CHECK_EQUAL( path.size(), reversed.size());
77   vector<int>::iterator path_i = path.begin();
78   vector<bool>::iterator reversed_i = reversed.begin();
79   for(; path_i != path.end() and reversed_i != reversed.end(); 
80       ++path_i, ++reversed_i)
81   {
82     // if we're less than zero our reversed flag should be true
83     BOOST_CHECK_EQUAL( *path_i < 0, *reversed_i);
84   }
85 }
86
87 BOOST_AUTO_TEST_CASE ( extended_conserved_path_simple ) 
88 {
89   vector<int> path;
90   path += 3,4,5,1; // magic from boost assign
91
92   ConservedPath ecp1;
93   ecp1.window_size = 30;
94   ecp1.score = 18;
95   ecp1.track_indexes = path;
96   BOOST_CHECK_EQUAL ( ecp1.size(), path.size() );
97
98   ConservedPath ecp2(30, 18.0, path);
99   BOOST_CHECK_EQUAL ( ecp1, ecp2 );
100   BOOST_CHECK_EQUAL ( ecp2.size(), path.size() );
101
102   ConservedPath ecp_ne(35, 20.0, path);
103   BOOST_CHECK(ecp2 != ecp_ne);
104
105   ConservedPath cp1;
106   cp1.score = 18;
107   cp1.track_indexes = path;
108
109   ConservedPath ecp3(cp1);
110   BOOST_CHECK_EQUAL( ecp2, ecp3 );
111   BOOST_CHECK_EQUAL( ecp3.size(), path.size() );
112 }
113
114 //! Can we grow succesfully?
115 BOOST_AUTO_TEST_CASE ( extended_conserved_path_growth )
116 {
117   vector<int> path_base;
118   path_base += 3,4,5,1; // magic from boost assign
119   vector<int> path_next;
120   path_next += 4,5,6,2; 
121   vector<int> path_next_reversed;
122   path_next_reversed += 4,5,6,-2; 
123   vector<int> path_not_next;
124   path_not_next += 4,5,6,5; 
125   const int window_size = 4;
126
127   ConservedPath ecp_base(window_size, 20, path_base);
128   ConservedPath cp_next(window_size, 25, path_next);
129   ConservedPath cp_next_reversed(window_size, 25, path_next_reversed);
130   ConservedPath cp_not_next(window_size, 22, path_not_next);
131
132   BOOST_CHECK(ecp_base.nextTo(cp_next));
133   BOOST_CHECK(!ecp_base.nextTo(cp_not_next));
134   // FIXME: should something like this actually extend?
135   BOOST_CHECK(!ecp_base.nextTo(cp_next_reversed));
136
137   const int growth = 2;
138   ecp_base.extend(growth);
139   BOOST_CHECK_EQUAL( ecp_base.window_size, window_size + growth );
140 }
141
142 BOOST_AUTO_TEST_CASE ( extended_conserved_normalized ) 
143 {
144   vector<int> path;
145   path += 3,-14,5,-19; // magic from boost assign
146   vector<int> normalized_path;
147   normalized_path += 3,4, 5,9;
148
149   ConservedPath ecp1;
150   ecp1.window_size = 10;
151   ecp1.score = 18;
152   ecp1.track_indexes = path;
153   BOOST_CHECK_EQUAL ( ecp1.size(), path.size() );
154
155   vector<ConservedPath::path_element> normalized_index(ecp1.normalizedIndexes());
156  
157   for(int i=0; i != ecp1.size(); ++i)
158   {
159     BOOST_CHECK_EQUAL(normalized_index[i], normalized_path[i]);
160   }
161 }
162
163 BOOST_AUTO_TEST_CASE( conserved_path_less_than )
164 {
165   vector<int> p111;
166   p111 += 1,1,1;
167   vector<int> p112;
168   p112 += 1,1,2;
169   vector<int> p115;
170   p115 += 1,1,5; 
171   vector<int> p112a;
172   p112a += 1,1,1;
173   vector<int> p1111;
174   p1111 += 1,1,1,1; 
175   
176   ConservedPath cp111(10, 1.0, p111);
177   ConservedPath cp112(10, 1.0, p112);
178   ConservedPath cp112a(10, 1.0, p112a);
179   ConservedPath cp115(10, 1.0, p115);
180   ConservedPath cp1111(10, 1.0, p1111);
181   
182   BOOST_CHECK_EQUAL( cp111 < cp112, true );
183   BOOST_CHECK_EQUAL( cp111 < cp1111, true );
184   BOOST_CHECK_EQUAL( cp1111 < cp111, false );
185   BOOST_CHECK_EQUAL( cp112 < cp112, false );
186   BOOST_CHECK_EQUAL( cp112 == cp112, true );
187   BOOST_CHECK_EQUAL( cp112 < cp115, true );
188 }
189
190 BOOST_AUTO_TEST_CASE( conserved_path_sort )
191 {
192   vector<int> p111;
193   p111 += 1,1,1;
194   vector<int> p112;
195   p112 += 1,1,2;
196   vector<int> p115;
197   p115 += 1,1,5;
198   vector<int> p121;
199   p121 += 1,2,1; 
200
201   ConservedPath cp111(10, 1.0, p111);
202   ConservedPath cp112(10, 1.0, p112);
203   ConservedPath cp115(10, 1.0, p115);
204   ConservedPath cp121(10, 1.0, p121);
205   
206   list<ConservedPath> paths;
207   paths.push_back(cp115);
208   paths.push_back(cp121);
209   paths.push_back(cp112);
210   paths.push_back(cp111);
211   
212   paths.sort();
213   BOOST_REQUIRE_EQUAL( paths.size(), 4 );
214   list<ConservedPath>::const_iterator path_i = paths.begin();
215   
216   BOOST_CHECK_EQUAL( *path_i, cp111 ); ++path_i;
217   BOOST_CHECK_EQUAL( *path_i, cp112 ); ++path_i;
218   BOOST_CHECK_EQUAL( *path_i, cp115 ); ++path_i;
219   BOOST_CHECK_EQUAL( *path_i, cp121 );
220 }