Added TProb<T>::operator==( const TProb<T> & ) and added some unit tests for prob...
[libdai.git] / tests / unit / prob.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/prob.h>
15 #include <strstream>
16
17
18 using namespace dai;
19
20
21 #define BOOST_TEST_MODULE ProbTest
22
23
24 #include <boost/test/unit_test.hpp>
25
26
27 BOOST_AUTO_TEST_CASE( ConstructorsTest ) {
28 // check constructors
29 Prob x1;
30 BOOST_CHECK_EQUAL( x1.size(), 0 );
31 BOOST_CHECK( x1.p() == std::vector<Real>() );
32
33 Prob x2( 3 );
34 BOOST_CHECK_EQUAL( x2.size(), 3 );
35 BOOST_CHECK( x2.p() == std::vector<Real>( 3, 1.0 / 3.0 ) );
36 BOOST_CHECK_EQUAL( x2[0], 1.0 / 3.0 );
37 BOOST_CHECK_EQUAL( x2[1], 1.0 / 3.0 );
38 BOOST_CHECK_EQUAL( x2[2], 1.0 / 3.0 );
39
40 Prob x3( 4, 1.0 );
41 BOOST_CHECK_EQUAL( x3.size(), 4 );
42 BOOST_CHECK( x3.p() == std::vector<Real>( 4, 1.0 ) );
43 BOOST_CHECK_EQUAL( x3[0], 1.0 );
44 BOOST_CHECK_EQUAL( x3[1], 1.0 );
45 BOOST_CHECK_EQUAL( x3[2], 1.0 );
46 BOOST_CHECK_EQUAL( x3[3], 1.0 );
47 x3[0] = 0.5;
48 x3[1] = 1.0;
49 x3[2] = 2.0;
50 x3[3] = 4.0;
51
52 Prob x4( x3.begin(), x3.end() );
53 BOOST_CHECK_EQUAL( x4.size(), 4 );
54 BOOST_CHECK( x4.p() == x3.p() );
55 BOOST_CHECK_EQUAL( x4[0], 0.5 );
56 BOOST_CHECK_EQUAL( x4[1], 1.0 );
57 BOOST_CHECK_EQUAL( x4[2], 2.0 );
58 BOOST_CHECK_EQUAL( x4[3], 4.0 );
59
60 x3.p() = std::vector<Real>( 4, 2.5 );
61 Prob x5( x3.begin(), x3.end(), x3.size() );
62 BOOST_CHECK_EQUAL( x5.size(), 4 );
63 BOOST_CHECK( x5.p() == x3.p() );
64 BOOST_CHECK_EQUAL( x5[0], 2.5 );
65 BOOST_CHECK_EQUAL( x5[1], 2.5 );
66 BOOST_CHECK_EQUAL( x5[2], 2.5 );
67 BOOST_CHECK_EQUAL( x5[3], 2.5 );
68
69 std::vector<int> y( 3, 2 );
70 Prob x6( y );
71 BOOST_CHECK_EQUAL( x6.size(), 3 );
72 BOOST_CHECK( x6.p() == std::vector<Real>( 3, 2.0 ) );
73 BOOST_CHECK_EQUAL( x6[0], 2.0 );
74 BOOST_CHECK_EQUAL( x6[1], 2.0 );
75 BOOST_CHECK_EQUAL( x6[2], 2.0 );
76
77 Prob x7( x6 );
78 BOOST_CHECK( x7 == x6 );
79
80 Prob x8 = x6;
81 BOOST_CHECK( x8 == x6 );
82 }
83
84
85 BOOST_AUTO_TEST_CASE( IteratorTest ) {
86 Prob x( 5, 0.0 );
87 size_t i;
88 for( i = 0; i < x.size(); i++ )
89 x[i] = i;
90
91 i = 0;
92 for( Prob::const_iterator cit = x.begin(); cit != x.end(); cit++, i++ )
93 BOOST_CHECK_EQUAL( *cit, i );
94
95 i = 0;
96 for( Prob::iterator it = x.begin(); it != x.end(); it++, i++ )
97 *it = 4 - i;
98
99 i = 0;
100 for( Prob::const_iterator it = x.begin(); it != x.end(); it++, i++ )
101 BOOST_CHECK_EQUAL( *it, 4 - i );
102
103 i = 0;
104 for( Prob::const_reverse_iterator crit = x.rbegin(); crit != x.rend(); crit++, i++ )
105 BOOST_CHECK_EQUAL( *crit, i );
106
107 i = 0;
108 for( Prob::reverse_iterator rit = x.rbegin(); rit != x.rend(); rit++, i++ )
109 *rit = 2 * i;
110
111 i = 0;
112 for( Prob::const_reverse_iterator crit = x.rbegin(); crit != x.rend(); crit++, i++ )
113 BOOST_CHECK_EQUAL( *crit, 2 * i );
114 }
115
116
117 BOOST_AUTO_TEST_CASE( QueriesTest ) {
118 }
119
120
121 BOOST_AUTO_TEST_CASE( UnaryTransformationsTest ) {
122 }
123
124
125 BOOST_AUTO_TEST_CASE( UnaryOperationsTest ) {
126 }
127
128
129 BOOST_AUTO_TEST_CASE( ScalarOperationsTest ) {
130 }
131
132
133 BOOST_AUTO_TEST_CASE( VectorTransformationsTest ) {
134 }
135
136
137 BOOST_AUTO_TEST_CASE( VectorOperationsTest ) {
138 }
139
140
141 BOOST_AUTO_TEST_CASE( RelatedFunctionsTest ) {
142 }