1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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. *
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. *
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 ******************************************************************************/
20 #include "base/convert.hpp"
21 #include "base/object.hpp"
22 #include <BoostTestTargetConfig.h>
25 using namespace icinga;
27 BOOST_AUTO_TEST_SUITE(base_convert)
29 BOOST_AUTO_TEST_CASE(tolong)
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);
35 BOOST_CHECK(Convert::ToLong(Value(-7)) == -7);
38 BOOST_AUTO_TEST_CASE(todouble)
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);
46 BOOST_AUTO_TEST_CASE(tostring)
48 BOOST_CHECK(Convert::ToString(7) == "7");
49 BOOST_CHECK(Convert::ToString(7.5) == "7.500000");
50 BOOST_CHECK(Convert::ToString("hello") == "hello");
53 BOOST_CHECK(Convert::ToString(str) == "hello");
55 BOOST_CHECK(Convert::ToString(Value(7)) == "7");
56 BOOST_CHECK(Convert::ToString(Value(7.5)) == "7.500000");
57 BOOST_CHECK(Convert::ToString(Value("hello")) == "hello");
58 BOOST_CHECK(Convert::ToString(Value("hello hello")) == "hello hello");
61 BOOST_AUTO_TEST_CASE(tobool)
63 BOOST_CHECK(Convert::ToBool("a") == true);
64 BOOST_CHECK(Convert::ToBool("0") == true);
65 BOOST_CHECK(Convert::ToBool("1") == true);
66 BOOST_CHECK(Convert::ToBool("2") == true);
67 BOOST_CHECK(Convert::ToBool(1) == true);
68 BOOST_CHECK(Convert::ToBool(0) == false);
69 BOOST_CHECK(Convert::ToBool(Value(true)) == true);
70 BOOST_CHECK(Convert::ToBool(Value(false)) == false);
73 BOOST_AUTO_TEST_SUITE_END()