Fixed two problems related to g++ 4.0.0 on Darwin 9.8.0
[libdai.git] / tests / unit / index_test.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2010 Joris Mooij [joris dot mooij at libdai dot org]
8 */
9
10
11 #include <dai/index.h>
12 #include <strstream>
13 #include <map>
14
15
16 using namespace dai;
17
18
19 #define BOOST_TEST_MODULE IndexTest
20
21
22 #include <boost/test/unit_test.hpp>
23
24
25 BOOST_AUTO_TEST_CASE( IndexForTest ) {
26 IndexFor x;
27 BOOST_CHECK( !x.valid() );
28 x.reset();
29 BOOST_CHECK( x.valid() );
30
31 size_t nrVars = 5;
32 std::vector<Var> vars;
33 for( size_t i = 0; i < nrVars; i++ )
34 vars.push_back( Var( i, i+2 ) );
35
36 for( size_t repeat = 0; repeat < 10000; repeat++ ) {
37 VarSet indexVars;
38 VarSet forVars;
39 for( size_t i = 0; i < 5; i++ ) {
40 if( rnd(2) == 0 )
41 indexVars |= vars[i];
42 if( rnd(2) == 0 )
43 forVars |= vars[i];
44 }
45 IndexFor ind( indexVars, forVars );
46 size_t iter = 0;
47 for( ; ind.valid(); ind++, iter++ )
48 BOOST_CHECK_EQUAL( calcLinearState( indexVars, calcState( forVars, iter ) ), (size_t)ind );
49 BOOST_CHECK_EQUAL( iter, forVars.nrStates() );
50 iter = 0;
51 ind.reset();
52 for( ; ind.valid(); ++ind, iter++ )
53 BOOST_CHECK_EQUAL( calcLinearState( indexVars, calcState( forVars, iter ) ), (size_t)ind );
54 BOOST_CHECK_EQUAL( iter, forVars.nrStates() );
55 }
56 }
57
58
59 BOOST_AUTO_TEST_CASE( PermuteTest ) {
60 Permute x;
61
62 Var x0(0, 2);
63 Var x1(1, 3);
64 Var x2(2, 2);
65 std::vector<Var> V;
66 V.push_back( x1 );
67 V.push_back( x2 );
68 V.push_back( x0 );
69 VarSet X( V.begin(), V.end() );
70 Permute sigma(V);
71 BOOST_CHECK_EQUAL( sigma.sigma()[0], 2 );
72 BOOST_CHECK_EQUAL( sigma.sigma()[1], 0 );
73 BOOST_CHECK_EQUAL( sigma.sigma()[2], 1 );
74 BOOST_CHECK_EQUAL( sigma[0], 2 );
75 BOOST_CHECK_EQUAL( sigma[1], 0 );
76 BOOST_CHECK_EQUAL( sigma[2], 1 );
77 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 0 ), 0 );
78 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 1 ), 2 );
79 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 2 ), 4 );
80 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 3 ), 6 );
81 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 4 ), 8 );
82 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 5 ), 10 );
83 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 6 ), 1 );
84 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 7 ), 3 );
85 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 8 ), 5 );
86 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 9 ), 7 );
87 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 10 ), 9 );
88 BOOST_CHECK_EQUAL( sigma.convertLinearIndex( 11 ), 11 );
89
90 std::vector<size_t> rs, sig;
91 rs.push_back(2);
92 rs.push_back(3);
93 rs.push_back(2);
94 sig.push_back(2);
95 sig.push_back(0);
96 sig.push_back(1);
97 Permute tau( rs, sig );
98 BOOST_CHECK( tau.sigma() == sig );
99 BOOST_CHECK_EQUAL( tau[0], 2 );
100 BOOST_CHECK_EQUAL( tau[1], 0 );
101 BOOST_CHECK_EQUAL( tau[2], 1 );
102 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 0 ), 0 );
103 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 1 ), 2 );
104 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 2 ), 4 );
105 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 3 ), 6 );
106 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 4 ), 8 );
107 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 5 ), 10 );
108 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 6 ), 1 );
109 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 7 ), 3 );
110 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 8 ), 5 );
111 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 9 ), 7 );
112 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 10 ), 9 );
113 BOOST_CHECK_EQUAL( tau.convertLinearIndex( 11 ), 11 );
114 }
115
116
117 BOOST_AUTO_TEST_CASE( multiforTest ) {
118 multifor x;
119 BOOST_CHECK( x.valid() );
120
121 std::vector<size_t> ranges;
122 ranges.push_back( 3 );
123 ranges.push_back( 4 );
124 ranges.push_back( 5 );
125 multifor S(ranges);
126 size_t s = 0;
127 for( size_t s2 = 0; s2 < 5; s2++ )
128 for( size_t s1 = 0; s1 < 4; s1++ )
129 for( size_t s0 = 0; s0 < 3; s0++, s++, S++ ) {
130 BOOST_CHECK( S.valid() );
131 BOOST_CHECK_EQUAL( s, (size_t)S );
132 BOOST_CHECK_EQUAL( S[0], s0 );
133 BOOST_CHECK_EQUAL( S[1], s1 );
134 BOOST_CHECK_EQUAL( S[2], s2 );
135 }
136 BOOST_CHECK( !S.valid() );
137
138 for( size_t repeat = 0; repeat < 10000; repeat++ ) {
139 std::vector<size_t> dims;
140 size_t total = 1;
141 for( size_t i = 0; i < 4; i++ ) {
142 dims.push_back( rnd(3) + 1 );
143 total *= dims.back();
144 }
145 multifor ind( dims );
146 size_t iter = 0;
147 for( ; ind.valid(); ind++, iter++ ) {
148 BOOST_CHECK_EQUAL( (size_t)ind, iter );
149 BOOST_CHECK_EQUAL( ind[0], iter % dims[0] );
150 BOOST_CHECK_EQUAL( ind[1], (iter / dims[0]) % dims[1] );
151 BOOST_CHECK_EQUAL( ind[2], (iter / (dims[0] * dims[1])) % dims[2] );
152 BOOST_CHECK_EQUAL( ind[3], (iter / (dims[0] * dims[1] * dims[2])) % dims[3] );
153 }
154 BOOST_CHECK_EQUAL( iter, total );
155 iter = 0;
156 ind.reset();
157 for( ; ind.valid(); ++ind, iter++ ) {
158 BOOST_CHECK_EQUAL( (size_t)ind, iter );
159 BOOST_CHECK_EQUAL( ind[0], iter % dims[0] );
160 BOOST_CHECK_EQUAL( ind[1], (iter / dims[0]) % dims[1] );
161 BOOST_CHECK_EQUAL( ind[2], (iter / (dims[0] * dims[1])) % dims[2] );
162 BOOST_CHECK_EQUAL( ind[3], (iter / (dims[0] * dims[1] * dims[2])) % dims[3] );
163 }
164 BOOST_CHECK_EQUAL( iter, total );
165 }
166 }
167
168
169 BOOST_AUTO_TEST_CASE( StateTest ) {
170 State x;
171 BOOST_CHECK( x.valid() );
172
173 Var v0( 0, 3 );
174 Var v1( 1, 4 );
175 Var v2( 3, 5 );
176 VarSet vars;
177 vars |= v2;
178 vars |= v1;
179 vars |= v0;
180 State S( vars );
181 size_t s = 0;
182 for( size_t s2 = 0; s2 < 5; s2++ )
183 for( size_t s1 = 0; s1 < 4; s1++ )
184 for( size_t s0 = 0; s0 < 3; s0++, s++, S++ ) {
185 BOOST_CHECK( S.valid() );
186 BOOST_CHECK_EQUAL( s, (size_t)S );
187 BOOST_CHECK_EQUAL( S(v0), s0 );
188 BOOST_CHECK_EQUAL( S(v1), s1 );
189 BOOST_CHECK_EQUAL( S(v2), s2 );
190 BOOST_CHECK_EQUAL( S( Var( 2, 2 ) ), 0 );
191 }
192 BOOST_CHECK( !S.valid() );
193 S.reset();
194 std::vector<std::pair<Var, size_t> > ps;
195 ps.push_back( std::make_pair( Var( 2, 2 ), 1 ) );
196 ps.push_back( std::make_pair( Var( 4, 2 ), 1 ) );
197 S.insert( ps.begin(), ps.end() );
198 BOOST_CHECK( S.valid() );
199 BOOST_CHECK_EQUAL( (size_t)S, 132 );
200
201 for( size_t repeat = 0; repeat < 10000; repeat++ ) {
202 std::vector<size_t> dims;
203 size_t total = 1;
204 for( size_t i = 0; i < 4; i++ ) {
205 dims.push_back( rnd(3) + 1 );
206 total *= dims.back();
207 }
208 std::vector<Var> vs;
209 for( size_t i = 0; i < 4; i++ )
210 vs.push_back( Var( i, dims[i] ) );
211 State ind( VarSet( vs.begin(), vs.end() ) );
212 size_t iter = 0;
213 for( ; ind.valid(); ind++, iter++ ) {
214 BOOST_CHECK_EQUAL( (size_t)ind, iter );
215 BOOST_CHECK_EQUAL( ind(vs[0]), iter % dims[0] );
216 BOOST_CHECK_EQUAL( ind(vs[1]), (iter / dims[0]) % dims[1] );
217 BOOST_CHECK_EQUAL( ind(vs[2]), (iter / (dims[0] * dims[1])) % dims[2] );
218 BOOST_CHECK_EQUAL( ind(vs[3]), (iter / (dims[0] * dims[1] * dims[2])) % dims[3] );
219 BOOST_CHECK_EQUAL( ind(VarSet(vs[0], vs[1])), iter % (dims[0] * dims[1]) );
220 BOOST_CHECK_EQUAL( ind(VarSet(vs[1], vs[2])), (iter / dims[0]) % (dims[1] * dims[2]) );
221 BOOST_CHECK_EQUAL( ind(VarSet(vs[2], vs[3])), (iter / (dims[0] * dims[1])) % (dims[2] * dims[3]) );
222 BOOST_CHECK_EQUAL( ind(VarSet(vs.begin(), vs.end())), iter );
223 State indcopy( VarSet(vs.begin(), vs.end()), (size_t)ind );
224 BOOST_CHECK_EQUAL( ind(vs[0]), indcopy(vs[0]) );
225 BOOST_CHECK_EQUAL( ind(vs[1]), indcopy(vs[1]) );
226 BOOST_CHECK_EQUAL( ind(vs[2]), indcopy(vs[2]) );
227 BOOST_CHECK_EQUAL( ind(vs[3]), indcopy(vs[3]) );
228 State indcopy2( indcopy.get() );
229 BOOST_CHECK_EQUAL( ind(vs[0]), indcopy2(vs[0]) );
230 BOOST_CHECK_EQUAL( ind(vs[1]), indcopy2(vs[1]) );
231 BOOST_CHECK_EQUAL( ind(vs[2]), indcopy2(vs[2]) );
232 BOOST_CHECK_EQUAL( ind(vs[3]), indcopy2(vs[3]) );
233 std::map<Var,size_t> indmap( ind );
234 State indcopy3( indmap );
235 BOOST_CHECK_EQUAL( ind(vs[0]), indcopy3(vs[0]) );
236 BOOST_CHECK_EQUAL( ind(vs[1]), indcopy3(vs[1]) );
237 BOOST_CHECK_EQUAL( ind(vs[2]), indcopy3(vs[2]) );
238 BOOST_CHECK_EQUAL( ind(vs[3]), indcopy3(vs[3]) );
239 }
240 BOOST_CHECK_EQUAL( iter, total );
241 iter = 0;
242 ind.reset();
243 for( ; ind.valid(); ++ind, iter++ ) {
244 BOOST_CHECK_EQUAL( (size_t)ind, iter );
245 BOOST_CHECK_EQUAL( ind(vs[0]), iter % dims[0] );
246 BOOST_CHECK_EQUAL( ind(vs[1]), (iter / dims[0]) % dims[1] );
247 BOOST_CHECK_EQUAL( ind(vs[2]), (iter / (dims[0] * dims[1])) % dims[2] );
248 BOOST_CHECK_EQUAL( ind(vs[3]), (iter / (dims[0] * dims[1] * dims[2])) % dims[3] );
249 State::const_iterator ci = ind.begin();
250 BOOST_CHECK_EQUAL( (ci++)->second, iter % dims[0] );
251 BOOST_CHECK_EQUAL( (ci++)->second, (iter / dims[0]) % dims[1] );
252 BOOST_CHECK_EQUAL( (ci++)->second, (iter / (dims[0] * dims[1])) % dims[2] );
253 BOOST_CHECK_EQUAL( (ci++)->second, (iter / (dims[0] * dims[1] * dims[2])) % dims[3] );
254 BOOST_CHECK( ci == ind.end() );
255 }
256 BOOST_CHECK_EQUAL( iter, total );
257 State::const_iterator ci = ind.begin();
258 BOOST_CHECK_EQUAL( (ci++)->first, vs[0] );
259 BOOST_CHECK_EQUAL( (ci++)->first, vs[1] );
260 BOOST_CHECK_EQUAL( (ci++)->first, vs[2] );
261 BOOST_CHECK_EQUAL( (ci++)->first, vs[3] );
262 BOOST_CHECK( ci == ind.end() );
263 }
264 }