tests/test_util.cpp

Go to the documentation of this file.
00001 /* $Id: test_util.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2007 - 2012 by Karol Nowak <grywacz@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-test"
00017 
00018 #include <boost/test/unit_test.hpp>
00019 
00020 #include "util.hpp"
00021 
00022 BOOST_AUTO_TEST_SUITE( util )
00023 
00024 BOOST_AUTO_TEST_CASE( test_lexical_cast )
00025 {
00026     /* First check if lexical_cast returns correct results for correct args */
00027     int result = lexical_cast<int, const std::string&>(std::string("1"));
00028     BOOST_CHECK( result == 1 );
00029 
00030     int result2 = lexical_cast<int, const char*>("2");
00031     BOOST_CHECK( result2 == 2 );
00032 
00033     /* Check that an exception is thrown when an invalid argument is passed */
00034     try {
00035         lexical_cast<int, const std::string&>(std::string("iddqd"));
00036 
00037         /* A bad_lexical_cast should have been thrown already */
00038         BOOST_CHECK( false );
00039     }
00040     catch( const bad_lexical_cast &) {
00041         // Don't do anything, we succeeded.
00042     }
00043 
00044     try {
00045         lexical_cast<int, const char*>("idkfa");
00046 
00047         /* A bad_lexical_cast should have been thrown already */
00048         BOOST_CHECK( false );
00049     }
00050     catch( const bad_lexical_cast &) {
00051         // Don't do anything, we succeeded.
00052     }
00053 }
00054 
00055 BOOST_AUTO_TEST_CASE( test_lexical_cast_default )
00056 {
00057     /* First check if it works with correct values */
00058     int result = lexical_cast_default<int, const std::string&>(std::string("1"));
00059     BOOST_CHECK( result == 1 );
00060 
00061     int result2 = lexical_cast_default<int, const char*>("2");
00062     BOOST_CHECK( result2 == 2 );
00063 
00064     double result3 = lexical_cast_default<double, const std::string&>(std::string("0.5"));
00065     BOOST_CHECK( result3 >= 0.499 && result3 <= 0.511 );
00066 
00067     /* Check if default is returned when argument is empty/invalid */
00068     int result4 = lexical_cast_default<int, const std::string&>(std::string(), 4);
00069     BOOST_CHECK( result4 == 4 );
00070 
00071     int result5 = lexical_cast_default<int, const char*>("", 5);
00072     BOOST_CHECK( result5 == 5 );
00073 
00074     double result6 = lexical_cast_default<double, const std::string&>(std::string(), 0.5);
00075     BOOST_CHECK( result6 >= 0.499 && result6 <= 0.511 );
00076 }
00077 
00078 /* vim: set ts=4 sw=4: */
00079 
00080 BOOST_AUTO_TEST_SUITE_END()
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:12 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs