Finished a new release: libDAI v0.2.6
[libdai.git] / tests / unit / properties_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/properties.h>
12 #include <strstream>
13
14
15 using namespace dai;
16
17
18 #define BOOST_TEST_MODULE PropertiesTest
19
20
21 #include <boost/test/unit_test.hpp>
22
23
24 BOOST_AUTO_TEST_CASE( PropertyTest ) {
25 std::stringstream str1, str2, str3, str4, str5, str6, str7, str8;
26 std::string s;
27
28 Property p;
29 p.first = "key";
30
31 p.second = (int)-5;
32 str1 << p;
33 str1 >> s;
34 BOOST_CHECK_EQUAL( s, "key=-5" );
35
36 p.second = (size_t)5;
37 str2 << p;
38 str2 >> s;
39 BOOST_CHECK_EQUAL( s, "key=5" );
40
41 p.second = std::string("value");
42 str3 << p;
43 str3 >> s;
44 BOOST_CHECK_EQUAL( s, "key=value" );
45
46 p.second = 3.141;
47 str4 << p;
48 str4 >> s;
49 BOOST_CHECK_EQUAL( s, "key=3.141" );
50
51 p.second = (long double)3.141;
52 str5 << p;
53 str5 >> s;
54 BOOST_CHECK_EQUAL( s, "key=3.141" );
55
56 p.second = false;
57 str6 << p;
58 str6 >> s;
59 BOOST_CHECK_EQUAL( s, "key=0" );
60
61 p.second = PropertySet()("prop2",(size_t)5)("prop1",std::string("hi"));
62 str7 << p;
63 str7 >> s;
64 BOOST_CHECK_EQUAL( s, "key=[prop1=hi,prop2=5]" );
65
66 p.second = std::vector<int>();
67 BOOST_CHECK_THROW( str7 << p, Exception );
68 }
69
70
71 BOOST_AUTO_TEST_CASE( ConstructorsTest ) {
72 PropertySet x;
73 PropertySet y("[key1=val1,key2=5,key3=[key3a=val,key3b=7.0],key4=1.0]");
74 PropertySet z = PropertySet()("key1",std::string("val1"))("key2",5)("key3",PropertySet("[key3a=val,key3b=7.0]"))("key4",1.0);
75
76 BOOST_CHECK( !x.hasKey("key") );
77 BOOST_CHECK( !y.hasKey("key") );
78 BOOST_CHECK( !z.hasKey("key") );
79 BOOST_CHECK( y.hasKey("key1") );
80 BOOST_CHECK( y.hasKey("key2") );
81 BOOST_CHECK( y.hasKey("key3") );
82 BOOST_CHECK( !y.hasKey("key3a") );
83 BOOST_CHECK( !y.hasKey("key3b") );
84 BOOST_CHECK( y.hasKey("key4") );
85 BOOST_CHECK( z.hasKey("key1") );
86 BOOST_CHECK( z.hasKey("key2") );
87 BOOST_CHECK( z.hasKey("key3") );
88 BOOST_CHECK( !z.hasKey("key3a") );
89 BOOST_CHECK( !z.hasKey("key3b") );
90 BOOST_CHECK( z.hasKey("key4") );
91
92 BOOST_CHECK_EQUAL( x.size(), 0 );
93 BOOST_CHECK_EQUAL( y.size(), 4 );
94 BOOST_CHECK_EQUAL( z.size(), 4 );
95 std::set<std::string> keys;
96 keys.insert( "key1" );
97 keys.insert( "key2" );
98 keys.insert( "key3" );
99 keys.insert( "key4" );
100 BOOST_CHECK( y.keys() == keys );
101 BOOST_CHECK( z.keys() == keys );
102
103 BOOST_CHECK_THROW( x.get( "key" ), Exception );
104 BOOST_CHECK_THROW( y.get( "key" ), Exception );
105 BOOST_CHECK_THROW( z.get( "key" ), Exception );
106 BOOST_CHECK_THROW( x.getAs<std::string>( "key" ), Exception );
107 BOOST_CHECK_THROW( y.getAs<int>( "key" ), Exception );
108 BOOST_CHECK_THROW( z.getAs<double>( "key" ), Exception );
109 BOOST_CHECK_THROW( x.getStringAs<int>( "key" ), Exception );
110 BOOST_CHECK_THROW( y.getStringAs<int>( "key" ), Exception );
111 BOOST_CHECK_THROW( z.getStringAs<int>( "key" ), Exception );
112
113 BOOST_CHECK_EQUAL( boost::any_cast<std::string>(y.get( "key1" )), std::string("val1") );
114 BOOST_CHECK_EQUAL( boost::any_cast<std::string>(y.get( "key2" )), std::string("5") );
115 BOOST_CHECK_EQUAL( boost::any_cast<std::string>(y.get( "key3" )), std::string("[key3a=val,key3b=7.0]") );
116 BOOST_CHECK_EQUAL( boost::any_cast<std::string>(y.get( "key4" )), std::string("1.0") );
117 BOOST_CHECK_EQUAL( y.getAs<std::string>( "key1" ), std::string("val1") );
118 BOOST_CHECK_EQUAL( y.getAs<std::string>( "key2" ), std::string("5") );
119 BOOST_CHECK_EQUAL( y.getAs<std::string>( "key3" ), std::string("[key3a=val,key3b=7.0]") );
120 BOOST_CHECK_EQUAL( y.getAs<std::string>( "key4" ), std::string("1.0") );
121 BOOST_CHECK_EQUAL( y.getStringAs<std::string>( "key1" ), std::string("val1") );
122 BOOST_CHECK_EQUAL( y.getStringAs<size_t>( "key2" ), 5 );
123 BOOST_CHECK_EQUAL( y.getStringAs<int>( "key2" ), 5 );
124 BOOST_CHECK_EQUAL( y.getStringAs<double>( "key4" ), 1.0 );
125 PropertySet key3val = y.getStringAs<PropertySet>( "key3" );
126 BOOST_CHECK_EQUAL( key3val.size(), 2 );
127 BOOST_CHECK( key3val.hasKey( "key3a" ) );
128 BOOST_CHECK( key3val.hasKey( "key3b" ) );
129 BOOST_CHECK_EQUAL( key3val.getStringAs<std::string>( "key3a" ), std::string("val") );
130 BOOST_CHECK_EQUAL( key3val.getStringAs<double>( "key3b" ), 7.0 );
131 BOOST_CHECK_THROW( y.getAs<int>( "key2" ), Exception );
132 BOOST_CHECK_THROW( y.getStringAs<int>( "key4" ), Exception );
133
134 BOOST_CHECK_EQUAL( boost::any_cast<std::string>(z.get( "key1" )), std::string("val1") );
135 BOOST_CHECK_EQUAL( boost::any_cast<int>(z.get( "key2" )), 5 );
136 BOOST_CHECK_EQUAL( boost::any_cast<PropertySet>(z.get( "key3" )).size(), 2 );
137 BOOST_CHECK_EQUAL( boost::any_cast<double>(z.get( "key4" )), 1.0 );
138 BOOST_CHECK_EQUAL( z.getAs<std::string>( "key1" ), std::string("val1") );
139 BOOST_CHECK_EQUAL( z.getAs<int>( "key2" ), 5 );
140 BOOST_CHECK_EQUAL( z.getAs<PropertySet>( "key3" ).size(), 2 );
141 BOOST_CHECK_EQUAL( z.getAs<double>( "key4" ), 1.0 );
142 BOOST_CHECK_EQUAL( z.getStringAs<std::string>( "key1" ), std::string("val1") );
143 BOOST_CHECK_EQUAL( z.getStringAs<int>( "key2" ), 5 );
144 BOOST_CHECK_EQUAL( z.getStringAs<double>( "key4" ), 1.0 );
145 key3val = z.getAs<PropertySet>( "key3" );
146 BOOST_CHECK_EQUAL( key3val.size(), 2 );
147 BOOST_CHECK( key3val.hasKey( "key3a" ) );
148 BOOST_CHECK( key3val.hasKey( "key3b" ) );
149 BOOST_CHECK_EQUAL( key3val.getStringAs<std::string>( "key3a" ), std::string("val") );
150 BOOST_CHECK_EQUAL( key3val.getStringAs<double>( "key3b" ), 7.0 );
151 BOOST_CHECK_THROW( z.getAs<size_t>( "key2" ), Exception );
152 BOOST_CHECK_THROW( z.getStringAs<size_t>( "key4" ), Exception );
153 }
154
155
156 BOOST_AUTO_TEST_CASE( SetTest ) {
157 PropertySet x;
158 PropertySet y("[key1=val1,key2=5]");
159
160 x.set( "key1", 5 );
161 BOOST_CHECK( x.hasKey( "key1" ) );
162 BOOST_CHECK( !x.hasKey( "key2" ) );
163 BOOST_CHECK_EQUAL( x.getAs<int>( "key1" ), 5 );
164 BOOST_CHECK_THROW( x.getAs<double>( "key1" ), Exception );
165 BOOST_CHECK_THROW( x.getAs<int>( "key2" ), Exception );
166
167 x.set( y );
168 BOOST_CHECK( x.hasKey( "key1" ) );
169 BOOST_CHECK( x.hasKey( "key2" ) );
170 BOOST_CHECK( !x.hasKey( "key" ) );
171 BOOST_CHECK_EQUAL( x.getAs<std::string>( "key1" ), std::string("val1") );
172 BOOST_CHECK_EQUAL( x.getAs<std::string>( "key2" ), std::string("5") );
173 x.setAsString<int>( "key1", 5 );
174 BOOST_CHECK_EQUAL( x.getAs<std::string>( "key1" ), std::string("5") );
175 x.convertTo<size_t>( "key1" );
176 BOOST_CHECK_EQUAL( x.getAs<size_t>( "key1" ), 5 );
177 x.setAsString( "key1", -5 );
178 BOOST_CHECK_EQUAL( x.getAs<std::string>( "key1" ), std::string("-5") );
179 x.convertTo<int>( "key1" );
180 BOOST_CHECK_EQUAL( x.getAs<int>( "key1" ), -5 );
181 x.setAsString( "key1", 1.234 );
182 BOOST_CHECK_EQUAL( x.getAs<std::string>( "key1" ), std::string("1.234") );
183 BOOST_CHECK_THROW( x.convertTo<int>( "key1" ), Exception );
184 x.convertTo<double>( "key1" );
185 BOOST_CHECK_EQUAL( x.getAs<double>( "key1" ), 1.234 );
186 x.setAsString( "key1", "val1");
187 BOOST_CHECK_EQUAL( x.getAs<std::string>( "key1" ), std::string("val1") );
188 BOOST_CHECK_THROW( x.convertTo<int>( "key1" ), Exception );
189 x.convertTo<std::string>( "key1" );
190 BOOST_CHECK_EQUAL( x.getAs<std::string>( "key1" ), std::string("val1") );
191
192 BOOST_CHECK_EQUAL( x.erase( "key1" ), 1 );
193 BOOST_CHECK( !x.hasKey( "key1" ) );
194 }
195
196
197 BOOST_AUTO_TEST_CASE( StreamTest ) {
198 std::stringstream ss1, ss2, ss3;
199 std::string s;
200
201 PropertySet z = PropertySet()("key1",std::string("val1"))("key2",5)("key3",PropertySet("[key3a=val,key3b=7.0]"))("key4",1.0);
202 ss1 << z;
203 ss1 >> s;
204 BOOST_CHECK_EQUAL( s, std::string("[key1=val1,key2=5,key3=[key3a=val,key3b=7.0],key4=1]") );
205 PropertySet y;
206 ss2 << z;
207 ss2 >> y;
208 BOOST_CHECK( y.hasKey( "key1" ) );
209 BOOST_CHECK( y.hasKey( "key2" ) );
210 BOOST_CHECK( y.hasKey( "key3" ) );
211 BOOST_CHECK( y.hasKey( "key4" ) );
212 BOOST_CHECK_EQUAL( y.getAs<std::string>( "key1" ), std::string("val1") );
213 BOOST_CHECK_EQUAL( y.getAs<std::string>( "key2" ), std::string("5") );
214 BOOST_CHECK_EQUAL( y.getAs<std::string>( "key3" ), std::string("[key3a=val,key3b=7.0]") );
215 BOOST_CHECK_EQUAL( y.getAs<std::string>( "key4" ), std::string("1") );
216
217 z.set( "key5", std::vector<int>() );
218 BOOST_CHECK_THROW( ss1 << z, Exception );
219
220 y.clear();
221 BOOST_CHECK_EQUAL( y.size(), 0 );
222 ss3 << "[]";
223 ss3 >> y;
224 BOOST_CHECK_EQUAL( y.size(), 0 );
225 ss3 << "";
226 BOOST_CHECK_THROW( ss3 >> y, Exception );
227 ss3 << "[key1 = val1]";
228 BOOST_CHECK_THROW( ss3 >> y, Exception );
229 ss3 << "[key1=val1";
230 BOOST_CHECK_THROW( ss3 >> y, Exception );
231 }