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