[Ofer Meshi] Added a script to convert from FastInf fileformat to libDAI fileformat
[libdai.git] / tests / unit / util_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/util.h>
12 #include <strstream>
13 #include <string>
14 #include <vector>
15 #include <map>
16 #include <set>
17
18
19 using namespace dai;
20
21
22 #define BOOST_TEST_MODULE UtilTest
23
24
25 #include <boost/test/unit_test.hpp>
26
27
28 BOOST_AUTO_TEST_CASE( IsNanTest ) {
29 double x = 0.0, y = 0.0;
30 BOOST_CHECK( isnan( x / y ) );
31 BOOST_CHECK( !isnan( x ) );
32 }
33
34
35 BOOST_AUTO_TEST_CASE( RndTest ) {
36 rnd_seed( 123 );
37 Real a1 = rnd_uniform();
38 Real b1 = rnd_stdnormal();
39 int c1 = rnd_int(0,1);
40 int d1 = rnd(2);
41
42 rnd_seed( 123 );
43 Real a2 = rnd_uniform();
44 Real b2 = rnd_stdnormal();
45 int c2 = rnd_int(0,1);
46 int d2 = rnd(2);
47
48 BOOST_CHECK_EQUAL( a1, a2 );
49 BOOST_CHECK_EQUAL( b1, b2 );
50 BOOST_CHECK_EQUAL( c1, c2 );
51 BOOST_CHECK_EQUAL( d1, d2 );
52
53 for( size_t i = 0; i < 10000; i++ ) {
54 Real x = rnd_uniform();
55 BOOST_CHECK( x >= 0.0 );
56 BOOST_CHECK( x < 1 );
57 }
58
59 for( int min = -5; min <= 5; min++ )
60 for( int max = min; max <= min + 10; max++ )
61 for( size_t i = 0; i < 1000; i++ ) {
62 int j = rnd_int( min, max );
63 BOOST_CHECK( j >= min );
64 BOOST_CHECK( j <= max );
65 }
66
67 for( int max = 1; max <= 100; max++ )
68 for( size_t i = 0; i < 100; i++ ) {
69 int j = rnd( max );
70 BOOST_CHECK( j >= 0 );
71 BOOST_CHECK( j < max );
72 }
73 }
74
75
76 BOOST_AUTO_TEST_CASE( StreamTest ) {
77 std::vector<int> v;
78 v.push_back( 1 );
79 v.push_back( 3 );
80 v.push_back( 2 );
81 std::stringstream ss;
82 ss << v;
83 std::string s;
84 std::getline( ss, s );
85 BOOST_CHECK_EQUAL( s, "(1, 3, 2)" );
86
87 std::set<int> x;
88 x.insert( 6 );
89 x.insert( 5 );
90 x.insert( 4 );
91 std::stringstream ss2;
92 ss2 << x;
93 std::getline( ss2, s );
94 BOOST_CHECK_EQUAL( s, "{4, 5, 6}" );
95
96 std::map<int,int> y;
97 y[1] = 6;
98 y[3] = 5;
99 y[2] = 4;
100 std::stringstream ss3;
101 ss3 << y;
102 std::getline( ss3, s );
103 BOOST_CHECK_EQUAL( s, "{1->6, 2->4, 3->5}" );
104
105 std::pair<int,double> z;
106 z.first = 5;
107 z.second = 1.2345;
108 std::stringstream ss4;
109 ss4 << z;
110 std::getline( ss4, s );
111 BOOST_CHECK_EQUAL( s, "(5, 1.2345)" );
112 }
113
114
115 BOOST_AUTO_TEST_CASE( concatTest ) {
116 std::vector<int> a;
117 a.push_back( 0 );
118 a.push_back( 1 );
119 std::vector<int> b;
120 b.push_back( 2 );
121 b.push_back( 3 );
122 b.push_back( 4 );
123 std::vector<int> c;
124 c.push_back( 0 );
125 c.push_back( 1 );
126 c.push_back( 2 );
127 c.push_back( 3 );
128 c.push_back( 4 );
129 BOOST_CHECK( concat( a, b ) == c );
130 }
131
132
133 BOOST_AUTO_TEST_CASE( tokenizeStringTest ) {
134 std::string s("Hello\tworld.\nThis is it.");
135 std::vector<std::string> words;
136 tokenizeString( s, words );
137 BOOST_CHECK_EQUAL( words.size(), 3 );
138 BOOST_CHECK_EQUAL( words[0], "Hello" );
139 BOOST_CHECK_EQUAL( words[1], "world." );
140 BOOST_CHECK_EQUAL( words[2], "This is it." );
141 words.clear();
142 tokenizeString( s, words, " " );
143 BOOST_CHECK_EQUAL( words.size(), 3 );
144 BOOST_CHECK_EQUAL( words[0], "Hello\tworld.\nThis" );
145 BOOST_CHECK_EQUAL( words[1], "is" );
146 BOOST_CHECK_EQUAL( words[2], "it." );
147 }