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