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