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