tests/test_formula_function.cpp

Go to the documentation of this file.
00001 /* $Id: test_formula_function.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2010 - 2012 by Mark de Wever <koraq@xs4all.nl>
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 "formula_function.hpp"
00021 
00022 #include <cmath>
00023 
00024 BOOST_AUTO_TEST_SUITE(formula_function)
00025 
00026 BOOST_AUTO_TEST_CASE(test_formula_function_substring)
00027 {
00028     BOOST_CHECK_EQUAL(
00029             game_logic::formula("substring('hello world', 0)")
00030                 .evaluate().as_string()
00031             , "hello world");
00032 
00033     BOOST_CHECK_EQUAL(
00034             game_logic::formula("substring('hello world', 6)")
00035                 .evaluate().as_string()
00036             , "world");
00037 
00038     BOOST_CHECK_EQUAL(
00039             game_logic::formula("substring('hello world', 11)")
00040                 .evaluate().as_string()
00041             , "");
00042 
00043 
00044     BOOST_CHECK_EQUAL(
00045             game_logic::formula("substring('hello world', -1)")
00046                 .evaluate().as_string()
00047             , "d");
00048 
00049     BOOST_CHECK_EQUAL(
00050             game_logic::formula("substring('hello world', -5)")
00051                 .evaluate().as_string()
00052             , "world");
00053 
00054     BOOST_CHECK_EQUAL(
00055             game_logic::formula("substring('hello world', -11)")
00056                 .evaluate().as_string()
00057             , "hello world");
00058 
00059     BOOST_CHECK_EQUAL(
00060             game_logic::formula("substring('hello world', -12)")
00061                 .evaluate().as_string()
00062             , "hello world");
00063 
00064 
00065     BOOST_CHECK_EQUAL(
00066             game_logic::formula("substring('hello world', 0, 0)")
00067                 .evaluate().as_string()
00068             , "");
00069 
00070     BOOST_CHECK_EQUAL(
00071             game_logic::formula("substring('hello world', 0, -1)")
00072                 .evaluate().as_string()
00073             , "");
00074 
00075     BOOST_CHECK_EQUAL(
00076             game_logic::formula("substring('hello world', 5, 1)")
00077                 .evaluate().as_string()
00078             , " ");
00079 
00080     BOOST_CHECK_EQUAL(
00081             game_logic::formula("substring('hello world', 1, 9)")
00082                 .evaluate().as_string()
00083             , "ello worl");
00084 
00085     BOOST_CHECK_EQUAL(
00086             game_logic::formula("substring('hello world', -10, 9)")
00087                 .evaluate().as_string()
00088             , "ello worl");
00089 
00090 }
00091 
00092 BOOST_AUTO_TEST_CASE(test_formula_function_length)
00093 {
00094     BOOST_CHECK_EQUAL(
00095               game_logic::formula("length('')").evaluate().as_int()
00096             , 0);
00097 
00098     BOOST_CHECK_EQUAL(
00099               game_logic::formula("length('hello world')").evaluate().as_int()
00100             , 11);
00101 }
00102 
00103 BOOST_AUTO_TEST_CASE(test_formula_function_concatenate)
00104 {
00105     BOOST_CHECK_EQUAL(
00106               game_logic::formula("concatenate(100)").evaluate().as_string()
00107             , "100");
00108 
00109     BOOST_CHECK_EQUAL(
00110               game_logic::formula("concatenate(100, 200, 'a')")
00111                 .evaluate().as_string()
00112             , "100200a");
00113 
00114     BOOST_CHECK_EQUAL(
00115               game_logic::formula("concatenate([1,2,3])")
00116                 .evaluate().as_string()
00117             , "1, 2, 3");
00118 
00119     BOOST_CHECK_EQUAL(
00120               game_logic::formula(
00121                     "concatenate([1.0, 1.00, 1.000, 1.2, 1.23, 1.234])")
00122                         .evaluate().as_string()
00123             , "1.000, 1.000, 1.000, 1.200, 1.230, 1.234");
00124 }
00125 
00126 BOOST_AUTO_TEST_CASE(test_formula_function_sin_cos)
00127 {
00128     const double pi = 4. * atan(1.);
00129 
00130     game_logic::map_formula_callable variables;
00131 
00132     for(size_t x = 0; x <= 360; ++x) {
00133         variables.add("x", variant(x));
00134 
00135         BOOST_CHECK_EQUAL(
00136               game_logic::formula("sin(x)")
00137                 .evaluate(variables).as_decimal()
00138             , static_cast<int>(1000. * sin(x * pi / 180.)));
00139 
00140         BOOST_CHECK_EQUAL(
00141               game_logic::formula("cos(x)")
00142                 .evaluate(variables).as_decimal()
00143             , static_cast<int>(1000. * cos(x * pi / 180.)));
00144     }
00145 }
00146 
00147 BOOST_AUTO_TEST_SUITE_END()
00148 
 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