test_doc
test_iniparser.cpp
浏览该文件的文档.
1 /*
2  * test_iniparser.cpp
3  *
4  * Created on: 2010-5-26
5  * Author: fasiondog
6  */
7 
8 #ifdef TEST_ALL_IN_ONE
9  #include <boost/test/unit_test.hpp>
10 #else
11  #define BOOST_TEST_MODULE test_iniparser
12  #include <boost/test/unit_test.hpp>
13 #endif
14 
15 #include <cfloat>
16 #include <iostream>
17 #include <boost/filesystem.hpp>
18 #include <boost/filesystem/fstream.hpp>
19 #include <hikyuu_utils/iniparser/IniParser.h>
20 
21 using namespace boost::unit_test;
22 using namespace boost::filesystem;
23 using namespace hku;
24 
35 BOOST_AUTO_TEST_CASE( test_IniParser_read ) {
36  IniParser ini_parser;
37 
39  BOOST_CHECK_THROW(ini_parser.read("sdjfljoiEI487sdbc.txt"), std::invalid_argument);
40 
42  std::string test_filename("test_iniparser_read.ini");
43  ofstream testini;
44  testini.open(test_filename, ofstream::trunc);
45  testini << "Missing section Header\n";
46  testini.close();
47  BOOST_CHECK_THROW(ini_parser.read(test_filename), std::logic_error);
48 
50  testini.open(test_filename, ofstream::trunc);
51  testini << "[section" << std::endl;
52  testini.close();
53  BOOST_CHECK_THROW(ini_parser.read(test_filename), std::logic_error);
54 
56  testini.open(test_filename, ofstream::trunc);
57  testini << "[section;]" << std::endl;
58  testini.close();
59  BOOST_CHECK_THROW(ini_parser.read(test_filename), std::logic_error);
60 
62  testini.open(test_filename, ofstream::trunc);
63  testini << "[section]\n" << "= value";
64  testini.close();
65  BOOST_CHECK_THROW(ini_parser.read(test_filename), std::logic_error);
66 
68  testini.open(test_filename, ofstream::trunc);
69  testini << "[section]\n" << "key=";
70  testini.close();
71  BOOST_CHECK_THROW(ini_parser.read(test_filename), std::logic_error);
72 
74  testini.open(test_filename, ofstream::trunc);
75  testini << "[section]\n" << "key";
76  testini.close();
77  BOOST_CHECK_THROW(ini_parser.read(test_filename), std::logic_error);
78 
80  testini.open(test_filename, ofstream::trunc);
81  testini << "[section1]";
82  testini.close();
83  ini_parser.read(test_filename);
84  BOOST_CHECK(ini_parser.hasSection("section1"));
85 
88  remove(test_filename);
89 
90 }
91 
95 BOOST_AUTO_TEST_CASE( test_IniParser_hasSection ) {
96  IniParser ini_parser;
97  std::string test_filename("test_iniparser.ini");
98  ofstream testini(test_filename, ofstream::trunc);
99  testini << "[test1]\n" << "key1=value1\n";
100  testini.close();
101 
102  ini_parser.read(test_filename);
103 
105  BOOST_CHECK(ini_parser.hasSection("test1"));
106 
108  BOOST_CHECK(!ini_parser.hasSection("test2"));
109 
110  remove(test_filename);
111 }
112 
116 BOOST_AUTO_TEST_CASE( test_IniParser_hasOption ) {
117  std::string test_filename("test_iniparser.ini");
118  ofstream testini(test_filename, ofstream::trunc);
119  testini << "[section1]\n" << "key1=value1\n\n"
120  << "[section1]\n" << "key3=value1\n";
121  testini.close();
122 
123  IniParser ini_parser;
124  ini_parser.read(test_filename);
125 
127  BOOST_CHECK(ini_parser.hasOption("section1", "key1"));
128  BOOST_CHECK(ini_parser.hasOption("section1", "key3"));
129 
131  BOOST_CHECK(!ini_parser.hasOption("section1", "key2"));
132 
134  BOOST_CHECK(!ini_parser.hasOption("section2", "key1"));
135 
136  remove(test_filename);
137 }
138 
139 
141 BOOST_AUTO_TEST_CASE(test_IniParser_getSectionList) {
142  std::string test_filename("test_iniparser.ini");
143  ofstream testini(test_filename, ofstream::trunc);
144  testini << "[section1]\n" << "key1=value1\n\n"
145  << "[section1]\n" << "key3=value1\n"
146  << "[section2]\n key1=value1\n";
147  testini.close();
148 
149  IniParser ini_parser;
150  ini_parser.read(test_filename);
151 
153  IniParser::StringListPtr output = ini_parser.getSectionList();
154  IniParser::StringList::iterator iter = output->begin();
155  BOOST_CHECK(output->size() == 2);
156  BOOST_CHECK((*iter++) == "section1");
157  BOOST_CHECK((*iter) == "section2");
158  remove(test_filename);
159 
161  testini.open(test_filename, ofstream::trunc);
162  testini.close();
163  ini_parser.clear();
164  ini_parser.read(test_filename);
165  output = ini_parser.getSectionList();
166  BOOST_CHECK(output->empty());
167  remove(test_filename);
168 }
169 
171 BOOST_AUTO_TEST_CASE(test_IniParser_getOptionList) {
172  std::string test_filename("test_iniparser.ini");
173  ofstream testini(test_filename, ofstream::trunc);
174  testini << "[section1]\n" << "key1=value1\n\n"
175  << "[section1]\n" << "key2=value1\n"
176  << "[section2]\n key1=value1\n\n"
177  << "[section3]";
178  testini.close();
179 
180  IniParser ini_parser;
181  ini_parser.read(test_filename);
182  IniParser::StringListPtr output;
183 
185  output = ini_parser.getOptionList("section3");
186  BOOST_CHECK(output->empty());
187 
189  output = ini_parser.getOptionList("section2");
190  BOOST_CHECK(output->size() == 1);
191  BOOST_CHECK(output->front() == "key1");
192 
194  output = ini_parser.getOptionList("section1");
195  BOOST_CHECK(output->size() == 2);
196  IniParser::StringList::iterator iter = output->begin();
197  BOOST_CHECK((*iter++) == "key1");
198  BOOST_CHECK((*iter) == "key2");
199 
200  remove(test_filename);
201 }
202 
204 BOOST_AUTO_TEST_CASE(test_IniParser_get) {
205  std::string test_filename("test_iniparser.ini");
206  ofstream testini(test_filename, ofstream::trunc);
207  testini << "[section1]\n" << "key1 = value1\n"
208  << "key3 = value3\n\n"
209  << "[section2]\n" << "key1 = value1\n";
210  testini.close();
211 
212  IniParser ini_parser;
213  ini_parser.read(test_filename);
214 
216  BOOST_CHECK(ini_parser.get("section1", "key1") == "value1");
217  BOOST_CHECK(ini_parser.get("section2", "key1") == "value1");
218 
220  BOOST_CHECK(ini_parser.get("section1", "key1", "value") == "value1");
221  BOOST_CHECK(ini_parser.get("section2", "key1", "value") != "value");
222 
224  BOOST_CHECK_THROW(ini_parser.get("section1", "key2"), std::invalid_argument);
225 
227  BOOST_CHECK(ini_parser.get("section1", "key2", "value2") == "value2");
228 
230  BOOST_CHECK_THROW(ini_parser.get("section3", "key1"), std::invalid_argument);
231 
233  BOOST_CHECK_THROW(ini_parser.get("section3", "key1", "value1"), std::invalid_argument);
234 
235  remove(test_filename);
236 }
237 
239 BOOST_AUTO_TEST_CASE( test_IniParser_getInt ) {
240  std::string test_filename("test_iniparser.ini");
241  ofstream testini(test_filename, ofstream::trunc);
242  testini << "[section1]\n" << "key1 = 1\n"
243  << "key3 = -3\n\n"
244  << "[section2]\n" << "key1 = 10\n\n"
245  << "key2 = A\n";
246  testini.close();
247 
248  IniParser ini_parser;
249  ini_parser.read(test_filename);
250 
252  BOOST_CHECK(ini_parser.getInt("section1", "key1") == 1);
253  BOOST_CHECK(ini_parser.getInt("section1", "key3") == -3);
254  BOOST_CHECK(ini_parser.getInt("section2", "key1") == 10);
255 
257  BOOST_CHECK(ini_parser.getInt("section1", "key1", "2") == 1);
258  BOOST_CHECK(ini_parser.getInt("section2", "key1", "20") != 20);
259 
261  BOOST_CHECK_THROW(ini_parser.getInt("section2", "key2"), std::domain_error);
262 
264  BOOST_CHECK_THROW(ini_parser.getInt("section1", "key1", "tow"), std::invalid_argument);
265  BOOST_CHECK_THROW(ini_parser.getInt("section2", "key1", "twenty"), std::invalid_argument);
266 
268  BOOST_CHECK_THROW(ini_parser.getInt("section1", "key2"), std::invalid_argument);
269 
271  BOOST_CHECK(ini_parser.getInt("section1", "key2", "10") == 10);
272 
274  BOOST_CHECK_THROW(ini_parser.getInt("section1", "key2", "10.0"), std::invalid_argument);
275 
277  BOOST_CHECK_THROW(ini_parser.getInt("section3", "key1"), std::invalid_argument);
278 
280  BOOST_CHECK_THROW(ini_parser.getInt("section3", "key1", "1"), std::invalid_argument);
281 
282  remove(test_filename);
283 }
284 
286 BOOST_AUTO_TEST_CASE( test_IniParser_getFloat ) {
287  std::string test_filename("test_iniparser.ini");
288  ofstream testini(test_filename, ofstream::trunc);
289  testini << "[section1]\n" << "key1 = 1.123456\n"
290  << "key3 = -3.145789\n\n"
291  << "[section2]\n" << "key1 = 10\n\n"
292  << "key2 = A\n";
293  testini.close();
294 
295  IniParser ini_parser;
296  ini_parser.read(test_filename);
297 
299  BOOST_CHECK_CLOSE(ini_parser.getFloat("section1", "key1"), 1.123456, 0.00001);
300  BOOST_CHECK_CLOSE(ini_parser.getFloat("section1", "key3"), -3.145789, 0.00001);
301  BOOST_CHECK_CLOSE(ini_parser.getFloat("section2", "key1"), 10, 0.00001);
302 
304  BOOST_CHECK_CLOSE(ini_parser.getFloat("section1", "key1", "2.01"), 1.123456, 0.00001);
305  BOOST_CHECK_CLOSE(ini_parser.getFloat("section2", "key1", "20.20"), 10, 0.00001);
306 
308  BOOST_CHECK_THROW(ini_parser.getFloat("section2", "key2"), std::domain_error);
309 
311  BOOST_CHECK_THROW(ini_parser.getFloat("section1", "key1", "tow"), std::invalid_argument);
312  BOOST_CHECK_THROW(ini_parser.getFloat("section2", "key1", "twenty"), std::invalid_argument);
313 
315  BOOST_CHECK_THROW(ini_parser.getFloat("section1", "key2"), std::invalid_argument);
316 
318  BOOST_CHECK_CLOSE(ini_parser.getFloat("section1", "key2", "10"), 10.0, 0.00001);
319 
321  BOOST_CHECK_THROW(ini_parser.getFloat("section1", "key2", "1A0"), std::invalid_argument);
322 
324  BOOST_CHECK_THROW(ini_parser.getFloat("section3", "key1"), std::invalid_argument);
325 
327  BOOST_CHECK_THROW(ini_parser.getFloat("section3", "key1", "1"), std::invalid_argument);
328 
330  BOOST_CHECK_CLOSE(ini_parser.getFloat("section1", "key2", "-3.4e-38"), -3.4e-38F, 0.1e-30);
331  BOOST_CHECK_CLOSE(ini_parser.getFloat("section1", "key2", "3.402823466e+38"), 3.402823466e+38F, 0.1e-38);
332 
334 #ifdef __MSVC__
335  BOOST_CHECK_THROW(ini_parser.getFloat("section1", "key2", "3.41e+38"), std::invalid_argument);
336 #endif
337 
338  remove(test_filename);
339 }
340 
342 BOOST_AUTO_TEST_CASE( test_IniParser_getDouble ) {
343  std::string test_filename("test_iniparser.ini");
344  ofstream testini(test_filename, ofstream::trunc);
345  testini << "[section1]\n" << "key1 = 1.123456\n"
346  << "key3 = -3.145789\n\n"
347  << "[section2]\n" << "key1 = 10\n\n"
348  << "key2 = A\n";
349  testini.close();
350 
351  IniParser ini_parser;
352  ini_parser.read(test_filename);
353 
355  BOOST_CHECK_CLOSE(ini_parser.getDouble("section1", "key1"), 1.123456, 0.00001);
356  BOOST_CHECK_CLOSE(ini_parser.getDouble("section1", "key3"), -3.145789, 0.00001);
357  BOOST_CHECK_CLOSE(ini_parser.getDouble("section2", "key1"), 10, 0.00001);
358 
360  BOOST_CHECK_CLOSE(ini_parser.getDouble("section1", "key1", "2.01"), 1.123456, 0.00001);
361  BOOST_CHECK_CLOSE(ini_parser.getDouble("section2", "key1", "20.20"), 10, 0.00001);
362 
364  BOOST_CHECK_THROW(ini_parser.getDouble("section2", "key2"), std::domain_error);
365 
367  BOOST_CHECK_THROW(ini_parser.getDouble("section1", "key1", "tow"), std::invalid_argument);
368  BOOST_CHECK_THROW(ini_parser.getDouble("section2", "key1", "twenty"), std::invalid_argument);
369 
371  BOOST_CHECK_THROW(ini_parser.getDouble("section1", "key2"), std::invalid_argument);
372 
374  BOOST_CHECK_CLOSE(ini_parser.getDouble("section1", "key2", "10"), 10.0, 0.00001);
375 
377  BOOST_CHECK_THROW(ini_parser.getDouble("section1", "key2", "1A0"), std::invalid_argument);
378 
380  BOOST_CHECK_THROW(ini_parser.getDouble("section3", "key1"), std::invalid_argument);
381 
383  BOOST_CHECK_THROW(ini_parser.getDouble("section3", "key1", "1"), std::invalid_argument);
384 
385  remove(test_filename);
386 }
387 
389 BOOST_AUTO_TEST_CASE( test_IniParser_getBool ) {
390  std::string test_filename("test_iniparser.ini");
391  ofstream testini(test_filename, ofstream::trunc);
392  testini << "[section1]\n"
393  << "key1 = 1\n"
394  << "key2 = 0\n"
395  << "key3 = TRUE\n"
396  << "key4 = true\n"
397  << "key5 = True\n"
398  << "key6 = FALSE\n"
399  << "key7 = false\n"
400  << "key8 = False\n"
401  << "key9 = YES\n"
402  << "key10 = Yes\n"
403  << "key11 = yes\n"
404  << "key12 = NO\n"
405  << "key13 = No\n"
406  << "key14 = no\n"
407  << "key15 = ON\n"
408  << "key16 = On\n"
409  << "key17 = on\n"
410  << "key18 = OFF\n"
411  << "key19 = Off\n"
412  << "key20 = off\n"
413  << "\n"
414  << "[section2]\n" << "key1 = false\n\n"
415  << "key2 = A\n";
416  testini.close();
417 
418  IniParser ini_parser;
419  ini_parser.read(test_filename);
420 
422  BOOST_CHECK(ini_parser.getBool("section1", "key1"));
423  BOOST_CHECK(!ini_parser.getBool("section1", "key2"));
424  BOOST_CHECK(ini_parser.getBool("section1", "key3"));
425  BOOST_CHECK(ini_parser.getBool("section1", "key4"));
426  BOOST_CHECK(ini_parser.getBool("section1", "key5"));
427  BOOST_CHECK(!ini_parser.getBool("section1", "key6"));
428  BOOST_CHECK(!ini_parser.getBool("section1", "key7"));
429  BOOST_CHECK(!ini_parser.getBool("section1", "key8"));
430  BOOST_CHECK(ini_parser.getBool("section1", "key9"));
431  BOOST_CHECK(ini_parser.getBool("section1", "key10"));
432  BOOST_CHECK(ini_parser.getBool("section1", "key11"));
433  BOOST_CHECK(!ini_parser.getBool("section1", "key12"));
434  BOOST_CHECK(!ini_parser.getBool("section1", "key13"));
435  BOOST_CHECK(!ini_parser.getBool("section1", "key14"));
436  BOOST_CHECK(ini_parser.getBool("section1", "key15"));
437  BOOST_CHECK(ini_parser.getBool("section1", "key16"));
438  BOOST_CHECK(ini_parser.getBool("section1", "key17"));
439  BOOST_CHECK(!ini_parser.getBool("section1", "key18"));
440  BOOST_CHECK(!ini_parser.getBool("section1", "key19"));
441  BOOST_CHECK(!ini_parser.getBool("section1", "key20"));
442 
444  BOOST_CHECK(ini_parser.getBool("section1", "key1", "0"));
445  BOOST_CHECK(!ini_parser.getBool("section1", "key2", "1"));
446 
448  BOOST_CHECK_THROW(ini_parser.getBool("section2", "key2"), std::domain_error);
449 
451  BOOST_CHECK_THROW(ini_parser.getBool("section1", "key1", "tow"), std::invalid_argument);
452  BOOST_CHECK_THROW(ini_parser.getBool("section1", "key1", "twenty"), std::invalid_argument);
453 
455  BOOST_CHECK_THROW(ini_parser.getBool("section1", "key22"), std::invalid_argument);
456 
458  BOOST_CHECK(!ini_parser.getBool("section1", "key22", "0"));
459  BOOST_CHECK(!ini_parser.getBool("section1", "key22", "false"));
460  BOOST_CHECK(!ini_parser.getBool("section1", "key22", "False"));
461  BOOST_CHECK(!ini_parser.getBool("section1", "key22", "FALSE"));
462  BOOST_CHECK(ini_parser.getBool("section1", "key22", "1"));
463  BOOST_CHECK(ini_parser.getBool("section1", "key22", "true"));
464  BOOST_CHECK(ini_parser.getBool("section1", "key22", "TRUE"));
465  BOOST_CHECK(ini_parser.getBool("section1", "key22", "True"));
466  BOOST_CHECK(ini_parser.getBool("section1", "key22", "YES"));
467  BOOST_CHECK(ini_parser.getBool("section1", "key22", "Yes"));
468  BOOST_CHECK(ini_parser.getBool("section1", "key22", "yes"));
469  BOOST_CHECK(ini_parser.getBool("section1", "key22", "ON"));
470  BOOST_CHECK(ini_parser.getBool("section1", "key22", "On"));
471  BOOST_CHECK(ini_parser.getBool("section1", "key22", "on"));
472  BOOST_CHECK(!ini_parser.getBool("section1", "key22", "NO"));
473  BOOST_CHECK(!ini_parser.getBool("section1", "key22", "No"));
474  BOOST_CHECK(!ini_parser.getBool("section1", "key22", "no"));
475  BOOST_CHECK(!ini_parser.getBool("section1", "key22", "OFF"));
476  BOOST_CHECK(!ini_parser.getBool("section1", "key22", "Off"));
477  BOOST_CHECK(!ini_parser.getBool("section1", "key22", "off"));
478 
480  BOOST_CHECK_THROW(ini_parser.getBool("section1", "key22", "10"), std::invalid_argument);
481 
483  BOOST_CHECK_THROW(ini_parser.getBool("section3", "key1"), std::invalid_argument);
484 
486  BOOST_CHECK_THROW(ini_parser.getBool("section3", "key1", "1"), std::invalid_argument);
487 
488  remove(test_filename);
489 }
490 
BOOST_AUTO_TEST_CASE(test_IniParser_read)
测试IniParser读取文件操作