]> granicus.if.org Git - icinga2/blob - test/base-convert.cpp
Update INSTALL file.
[icinga2] / test / base-convert.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/convert.h"
21 #include "base/object.h"
22 #include <boost/test/unit_test.hpp>
23 #include <iostream>
24
25 using namespace icinga;
26
27 BOOST_AUTO_TEST_SUITE(base_convert)
28
29 BOOST_AUTO_TEST_CASE(tolong)
30 {
31         BOOST_CHECK_THROW(Convert::ToLong(" 7"), boost::exception);
32         BOOST_CHECK(Convert::ToLong("-7") == -7);
33         BOOST_CHECK_THROW(Convert::ToLong("7a"), boost::exception);
34
35         BOOST_CHECK(Convert::ToLong(Value(-7)) == -7);
36 }
37
38 BOOST_AUTO_TEST_CASE(todouble)
39 {
40         BOOST_CHECK_THROW(Convert::ToDouble(" 7.3"), boost::exception);
41         BOOST_CHECK(Convert::ToDouble("-7.3") == -7.3);
42         BOOST_CHECK_THROW(Convert::ToDouble("7.3a"), boost::exception);
43         BOOST_CHECK(Convert::ToDouble(Value(-7.3)) == -7.3);
44 }
45
46 BOOST_AUTO_TEST_CASE(tostring)
47 {
48         BOOST_CHECK(Convert::ToString(7) == "7");
49         BOOST_CHECK(Convert::ToString(7.5) == "7.5");
50         BOOST_CHECK(Convert::ToString("hello") == "hello");
51
52         BOOST_CHECK(Convert::ToString(Value(7)) == "7");
53         BOOST_CHECK(Convert::ToString(Value(7.5)) == "7.5");
54         BOOST_CHECK(Convert::ToString(Value("hello")) == "hello");
55         BOOST_CHECK(Convert::ToString(Value("hello hello")) == "hello hello");
56 }
57
58 BOOST_AUTO_TEST_CASE(tobool)
59 {
60         BOOST_CHECK_THROW(Convert::ToBool("a"), boost::exception);
61         BOOST_CHECK(Convert::ToBool("0") == false);
62         BOOST_CHECK(Convert::ToBool("1") == true);
63         BOOST_CHECK(Convert::ToBool("2") == true);
64         BOOST_CHECK(Convert::ToBool(Value(true)) == true);
65         BOOST_CHECK(Convert::ToBool(Value(false)) == false);
66 }
67
68 BOOST_AUTO_TEST_SUITE_END()