]> git.stg.codes - stg.git/blob - tests/test_conffiles.cpp
Make time-sensitive tests in local machine time.
[stg.git] / tests / test_conffiles.cpp
1 #define BOOST_TEST_MODULE STGConfFiles
2
3 #include "stg/conffiles.h"
4
5 #pragma GCC diagnostic push
6 #pragma GCC diagnostic ignored "-Wold-style-cast"
7 #pragma GCC diagnostic ignored "-Wunused-parameter"
8 #pragma GCC diagnostic ignored "-Wsign-compare"
9 #pragma GCC diagnostic ignored "-Wparentheses"
10 #include <boost/test/unit_test.hpp>
11 #pragma GCC diagnostic pop
12
13 #include <string>
14 #include <fstream>
15
16 #include <unistd.h> // unlink
17
18 BOOST_AUTO_TEST_SUITE(ConfFiles)
19
20 BOOST_AUTO_TEST_CASE(ReadWrite)
21 {
22     {
23         CONFIGFILE cf("/tmp/test.cf", true);
24
25         BOOST_CHECK_EQUAL(cf.Error(), 0);
26
27         cf.WriteString("a", "a-string");
28         cf.WriteInt("b", 0);
29         cf.WriteDouble("e", 2.718281828);
30
31         BOOST_CHECK_EQUAL(cf.Flush(), 0);
32     }
33
34     {
35         CONFIGFILE cf("/tmp/test.cf");
36
37         BOOST_CHECK_EQUAL(cf.Error(), 0);
38
39         std::string svalue;
40         BOOST_CHECK_EQUAL(cf.ReadString("a", &svalue, "a-default"), 0);
41         int ivalue;
42         BOOST_CHECK_EQUAL(cf.ReadInt("b", &ivalue, -1), 0);
43         double dvalue = 0;
44         BOOST_CHECK_EQUAL(cf.ReadDouble("e", &dvalue, 0), 0);
45
46         BOOST_CHECK_EQUAL(svalue, "a-string");
47         BOOST_CHECK_EQUAL(ivalue, 0);
48         BOOST_CHECK(dvalue != 0);
49     }
50
51     BOOST_CHECK_EQUAL(unlink("/tmp/test.cf"), 0);
52 }
53
54 BOOST_AUTO_TEST_CASE(EmptyLinesAndComments)
55 {
56     {
57         std::ofstream f("/tmp/test.cf");
58
59         BOOST_CHECK(static_cast<bool>(f));
60
61         f << "\n"
62           << "a=a-string# a string\n"
63           << "              \n"
64           << "b=0\n"
65           << "#abc\n"
66           << "e=2.718281828\n";
67     }
68
69     {
70         CONFIGFILE cf("/tmp/test.cf");
71
72         BOOST_CHECK_EQUAL(cf.Error(), 0);
73
74         std::string svalue;
75         BOOST_CHECK_EQUAL(cf.ReadString("a", &svalue, "a-default"), 0);
76         int ivalue;
77         BOOST_CHECK_EQUAL(cf.ReadInt("b", &ivalue, -1), 0);
78         double dvalue = 0;
79         BOOST_CHECK_EQUAL(cf.ReadDouble("e", &dvalue, 0), 0);
80
81         BOOST_CHECK_EQUAL(svalue, "a-string");
82         BOOST_CHECK_EQUAL(ivalue, 0);
83         BOOST_CHECK(dvalue != 0);
84     }
85
86     BOOST_CHECK_EQUAL(unlink("/tmp/test.cf"), 0);
87 }
88
89 BOOST_AUTO_TEST_SUITE_END()