New git HEAD version
[libdai.git] / tests / unit / util_test.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <dai/util.h>
10 #include <strstream>
11 #include <string>
12 #include <vector>
13 #include <map>
14 #include <set>
15
16
17 using namespace dai;
18
19
20 #define BOOST_TEST_MODULE UtilTest
21
22
23 #include <boost/test/unit_test.hpp>
24
25
26 BOOST_AUTO_TEST_CASE( IsNanTest ) {
27 double x = 0.0, y = 0.0;
28 BOOST_CHECK( dai::isnan( x / y ) );
29 BOOST_CHECK( !dai::isnan( x ) );
30 }
31
32
33 BOOST_AUTO_TEST_CASE( RndTest ) {
34 rnd_seed( 123 );
35 Real a1 = rnd_uniform();
36 Real b1 = rnd_stdnormal();
37 int c1 = rnd_int(0,1);
38 int d1 = rnd(2);
39
40 rnd_seed( 123 );
41 Real a2 = rnd_uniform();
42 Real b2 = rnd_stdnormal();
43 int c2 = rnd_int(0,1);
44 int d2 = rnd(2);
45
46 BOOST_CHECK_EQUAL( a1, a2 );
47 BOOST_CHECK_EQUAL( b1, b2 );
48 BOOST_CHECK_EQUAL( c1, c2 );
49 BOOST_CHECK_EQUAL( d1, d2 );
50
51 for( size_t i = 0; i < 10000; i++ ) {
52 Real x = rnd_uniform();
53 BOOST_CHECK( x >= 0.0 );
54 BOOST_CHECK( x < 1 );
55 }
56
57 for( int min = -5; min <= 5; min++ )
58 for( int max = min; max <= min + 10; max++ )
59 for( size_t i = 0; i < 1000; i++ ) {
60 int j = rnd_int( min, max );
61 BOOST_CHECK( j >= min );
62 BOOST_CHECK( j <= max );
63 }
64
65 for( int max = 1; max <= 100; max++ )
66 for( size_t i = 0; i < 100; i++ ) {
67 int j = rnd( max );
68 BOOST_CHECK( j >= 0 );
69 BOOST_CHECK( j < max );
70 }
71 }
72
73
74 BOOST_AUTO_TEST_CASE( StreamTest ) {
75 std::vector<int> v;
76 v.push_back( 1 );
77 v.push_back( 3 );
78 v.push_back( 2 );
79 std::stringstream ss;
80 ss << v;
81 std::string s;
82 std::getline( ss, s );
83 BOOST_CHECK_EQUAL( s, "(1, 3, 2)" );
84
85 std::set<int> x;
86 x.insert( 6 );
87 x.insert( 5 );
88 x.insert( 4 );
89 std::stringstream ss2;
90 ss2 << x;
91 std::getline( ss2, s );
92 BOOST_CHECK_EQUAL( s, "{4, 5, 6}" );
93
94 std::map<int,int> y;
95 y[1] = 6;
96 y[3] = 5;
97 y[2] = 4;
98 std::stringstream ss3;
99 ss3 << y;
100 std::getline( ss3, s );
101 BOOST_CHECK_EQUAL( s, "{1->6, 2->4, 3->5}" );
102
103 std::pair<int,double> z;
104 z.first = 5;
105 z.second = 1.2345;
106 std::stringstream ss4;
107 ss4 << z;
108 std::getline( ss4, s );
109 BOOST_CHECK_EQUAL( s, "(5, 1.2345)" );
110 }
111
112
113 BOOST_AUTO_TEST_CASE( concatTest ) {
114 std::vector<int> a;
115 a.push_back( 0 );
116 a.push_back( 1 );
117 std::vector<int> b;
118 b.push_back( 2 );
119 b.push_back( 3 );
120 b.push_back( 4 );
121 std::vector<int> c;
122 c.push_back( 0 );
123 c.push_back( 1 );
124 c.push_back( 2 );
125 c.push_back( 3 );
126 c.push_back( 4 );
127 BOOST_CHECK( concat( a, b ) == c );
128 }
129
130
131 BOOST_AUTO_TEST_CASE( stringTest ) {
132 int a = 5;
133 BOOST_CHECK_EQUAL( toString( a ), std::string("5") );
134 BOOST_CHECK_EQUAL( fromString<int>( "5" ), a );
135
136 bool b = true;
137 BOOST_CHECK_EQUAL( toString( b ), std::string("1") );
138 BOOST_CHECK_EQUAL( fromString<bool>( "1" ), b );
139
140 double c = -3.5;
141 BOOST_CHECK_EQUAL( toString( c ), std::string("-3.5") );
142 BOOST_CHECK_EQUAL( fromString<double>( "-3.5" ), c );
143
144 unsigned char d = 'e';
145 BOOST_CHECK_EQUAL( toString( d ), std::string("e") );
146 BOOST_CHECK_EQUAL( fromString<unsigned char>( "e" ), d );
147 }
148
149
150 BOOST_AUTO_TEST_CASE( tokenizeStringTest ) {
151 std::string s;
152 std::vector<std::string> tokens;
153
154 s = "";
155 tokens = tokenizeString( s, true, " \t" );
156 BOOST_CHECK_EQUAL( tokens[0], "" );
157 BOOST_CHECK_EQUAL( tokens.size(), 1 );
158
159 s = " ";
160 tokens = tokenizeString( s, true, " \t" );
161 BOOST_CHECK_EQUAL( tokens.size(), 2 );
162 BOOST_CHECK_EQUAL( tokens[0], "" );
163 BOOST_CHECK_EQUAL( tokens[1], "" );
164
165 s = " \t";
166 tokens = tokenizeString( s, true, " \t" );
167 BOOST_CHECK_EQUAL( tokens.size(), 3 );
168 BOOST_CHECK_EQUAL( tokens[0], "" );
169 BOOST_CHECK_EQUAL( tokens[1], "" );
170 BOOST_CHECK_EQUAL( tokens[2], "" );
171
172 s = " \tHello";
173 tokens = tokenizeString( s, true, " \t" );
174 BOOST_CHECK_EQUAL( tokens.size(), 3 );
175 BOOST_CHECK_EQUAL( tokens[0], "" );
176 BOOST_CHECK_EQUAL( tokens[1], "" );
177 BOOST_CHECK_EQUAL( tokens[2], "Hello" );
178
179 s = " \tHello\r\n there";
180 tokens = tokenizeString( s, true, " \t" );
181 BOOST_CHECK_EQUAL( tokens.size(), 4 );
182 BOOST_CHECK_EQUAL( tokens[0], "" );
183 BOOST_CHECK_EQUAL( tokens[1], "" );
184 BOOST_CHECK_EQUAL( tokens[2], "Hello\r\n" );
185 BOOST_CHECK_EQUAL( tokens[3], "there" );
186
187 s = " \tHello\r\n there !";
188 tokens = tokenizeString( s, true, " \t" );
189 BOOST_CHECK_EQUAL( tokens.size(), 5 );
190 BOOST_CHECK_EQUAL( tokens[0], "" );
191 BOOST_CHECK_EQUAL( tokens[1], "" );
192 BOOST_CHECK_EQUAL( tokens[2], "Hello\r\n" );
193 BOOST_CHECK_EQUAL( tokens[3], "there" );
194 BOOST_CHECK_EQUAL( tokens[4], "!" );
195
196 s = " \tHello\r\n there !\r";
197 tokens = tokenizeString( s, true, " \t" );
198 BOOST_CHECK_EQUAL( tokens.size(), 5 );
199 BOOST_CHECK_EQUAL( tokens[0], "" );
200 BOOST_CHECK_EQUAL( tokens[1], "" );
201 BOOST_CHECK_EQUAL( tokens[2], "Hello\r\n" );
202 BOOST_CHECK_EQUAL( tokens[3], "there" );
203 BOOST_CHECK_EQUAL( tokens[4], "!\r" );
204
205 s = " \tHello\r\n there !\r";
206 tokens = tokenizeString( s, true, " \t\r\n" );
207 BOOST_CHECK_EQUAL( tokens.size(), 8 );
208 BOOST_CHECK_EQUAL( tokens[0], "" );
209 BOOST_CHECK_EQUAL( tokens[1], "" );
210 BOOST_CHECK_EQUAL( tokens[2], "Hello" );
211 BOOST_CHECK_EQUAL( tokens[3], "" );
212 BOOST_CHECK_EQUAL( tokens[4], "" );
213 BOOST_CHECK_EQUAL( tokens[5], "there" );
214 BOOST_CHECK_EQUAL( tokens[6], "!" );
215 BOOST_CHECK_EQUAL( tokens[7], "" );
216
217
218 s = "";
219 tokens = tokenizeString( s, false, " \t" );
220 BOOST_CHECK_EQUAL( tokens.size(), 0 );
221
222 s = " ";
223 tokens = tokenizeString( s, false, " \t" );
224 BOOST_CHECK_EQUAL( tokens.size(), 0 );
225
226 s = " \t";
227 tokens = tokenizeString( s, false, " \t" );
228 BOOST_CHECK_EQUAL( tokens.size(), 0 );
229
230 s = " \tHello";
231 tokens = tokenizeString( s, false, " \t" );
232 BOOST_CHECK_EQUAL( tokens.size(), 1 );
233 BOOST_CHECK_EQUAL( tokens[0], "Hello" );
234
235 s = " \tHello\r\n there";
236 tokens = tokenizeString( s, false, " \t" );
237 BOOST_CHECK_EQUAL( tokens.size(), 2 );
238 BOOST_CHECK_EQUAL( tokens[0], "Hello\r\n" );
239 BOOST_CHECK_EQUAL( tokens[1], "there" );
240
241 s = " \tHello\r\n there !";
242 tokens = tokenizeString( s, false, " \t" );
243 BOOST_CHECK_EQUAL( tokens.size(), 3 );
244 BOOST_CHECK_EQUAL( tokens[0], "Hello\r\n" );
245 BOOST_CHECK_EQUAL( tokens[1], "there" );
246 BOOST_CHECK_EQUAL( tokens[2], "!" );
247
248 s = " \tHello\r\n there !\r";
249 tokens = tokenizeString( s, false, " \t" );
250 BOOST_CHECK_EQUAL( tokens.size(), 3 );
251 BOOST_CHECK_EQUAL( tokens[0], "Hello\r\n" );
252 BOOST_CHECK_EQUAL( tokens[1], "there" );
253 BOOST_CHECK_EQUAL( tokens[2], "!\r" );
254
255 s = " \tHello\r\n there !\r";
256 tokens = tokenizeString( s, false, " \t\r\n" );
257 BOOST_CHECK_EQUAL( tokens.size(), 3 );
258 BOOST_CHECK_EQUAL( tokens[0], "Hello" );
259 BOOST_CHECK_EQUAL( tokens[1], "there" );
260 BOOST_CHECK_EQUAL( tokens[2], "!" );
261 }