The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_formula_function.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-test"
16 
17 #include <boost/test/unit_test.hpp>
18 
19 #include "formula/function.hpp"
20 #include "log.hpp"
21 
22 #include <cmath>
23 
24 using namespace game_logic;
25 
27 
28 BOOST_AUTO_TEST_CASE(test_formula_function_substring)
29 {
30  BOOST_CHECK_EQUAL(
31  game_logic::formula("substring('hello world', 0)")
32  .evaluate().as_string()
33  , "hello world");
34 
35  BOOST_CHECK_EQUAL(
36  game_logic::formula("substring('hello world', 6)")
37  .evaluate().as_string()
38  , "world");
39 
40  lg::set_log_domain_severity("scripting/formula", lg::err());
41 
42  BOOST_CHECK_EQUAL(
43  game_logic::formula("substring('hello world', 11)")
44  .evaluate().as_string()
45  , "");
46 
47  lg::set_log_domain_severity("scripting/formula", lg::debug());
48 
49  BOOST_CHECK_EQUAL(
50  game_logic::formula("substring('hello world', -1)")
51  .evaluate().as_string()
52  , "d");
53 
54  BOOST_CHECK_EQUAL(
55  game_logic::formula("substring('hello world', -5)")
56  .evaluate().as_string()
57  , "world");
58 
59  BOOST_CHECK_EQUAL(
60  game_logic::formula("substring('hello world', -11)")
61  .evaluate().as_string()
62  , "hello world");
63 
64  lg::set_log_domain_severity("scripting/formula", lg::err());
65 
66  BOOST_CHECK_EQUAL(
67  game_logic::formula("substring('hello world', -12)")
68  .evaluate().as_string()
69  , "hello world");
70 
71  lg::set_log_domain_severity("scripting/formula", lg::debug());
72 
73  BOOST_CHECK_EQUAL(
74  game_logic::formula("substring('hello world', 0, 0)")
75  .evaluate().as_string()
76  , "");
77 
78  BOOST_CHECK_EQUAL(
79  game_logic::formula("substring('hello world', 0, -1)")
80  .evaluate().as_string()
81  , "h");
82 
83  lg::set_log_domain_severity("scripting/formula", lg::debug());
84 
85  BOOST_CHECK_EQUAL(
86  game_logic::formula("substring('hello world', 5, 1)")
87  .evaluate().as_string()
88  , " ");
89 
90  BOOST_CHECK_EQUAL(
91  game_logic::formula("substring('hello world', 1, 9)")
92  .evaluate().as_string()
93  , "ello worl");
94 
95  BOOST_CHECK_EQUAL(
96  game_logic::formula("substring('hello world', -10, 9)")
97  .evaluate().as_string()
98  , "ello worl");
99 
100  BOOST_CHECK_EQUAL(
101  game_logic::formula("substring('hello world', -1, -5)")
102  .evaluate().as_string()
103  , "world");
104 
105  BOOST_CHECK_EQUAL(
106  game_logic::formula("substring('hello world', 4, -5)")
107  .evaluate().as_string()
108  , "hello");
109 
110 }
111 
112 BOOST_AUTO_TEST_CASE(test_formula_function_length)
113 {
114  BOOST_CHECK_EQUAL(
115  game_logic::formula("length('')").evaluate().as_int()
116  , 0);
117 
118  BOOST_CHECK_EQUAL(
119  game_logic::formula("length('hello world')").evaluate().as_int()
120  , 11);
121 }
122 
123 BOOST_AUTO_TEST_CASE(test_formula_function_concatenate)
124 {
125  BOOST_CHECK_EQUAL(
126  game_logic::formula("concatenate(100)").evaluate().as_string()
127  , "100");
128 
129  BOOST_CHECK_EQUAL(
130  game_logic::formula("concatenate(100, 200, 'a')")
131  .evaluate().as_string()
132  , "100200a");
133 
134  BOOST_CHECK_EQUAL(
135  game_logic::formula("concatenate([1,2,3])")
136  .evaluate().as_string()
137  , "1, 2, 3");
138 
139  BOOST_CHECK_EQUAL(
141  "concatenate([1.0, 1.00, 1.000, 1.2, 1.23, 1.234])")
142  .evaluate().as_string()
143  , "1.000, 1.000, 1.000, 1.200, 1.230, 1.234");
144 }
145 
146 BOOST_AUTO_TEST_CASE(test_formula_function_math)
147 {
148  BOOST_CHECK_EQUAL(formula("abs(5)").evaluate().as_int(), 5);
149  BOOST_CHECK_EQUAL(formula("abs(-5)").evaluate().as_int(), 5);
150  BOOST_CHECK_EQUAL(formula("abs(5.0)").evaluate().as_int(), 5);
151  BOOST_CHECK_EQUAL(formula("abs(-5.0)").evaluate().as_int(), 5);
152 
153  BOOST_CHECK_EQUAL(formula("min(3,5)").evaluate().as_int(), 3);
154  BOOST_CHECK_EQUAL(formula("min(5,2)").evaluate().as_int(), 2);
155  BOOST_CHECK_EQUAL(formula("max(3,5)").evaluate().as_int(), 5);
156  BOOST_CHECK_EQUAL(formula("max(5,2)").evaluate().as_int(), 5);
157  BOOST_CHECK_EQUAL(formula("max(5.5,5)").evaluate().as_decimal(),
158  static_cast<int>(1000.0 * 5.5));
159 
160  BOOST_CHECK_EQUAL(formula("max(4,5,[2,18,7])").evaluate().as_int(), 18);
161 
162  BOOST_CHECK_EQUAL(formula("log(8,2)").evaluate().as_int(), 3);
163  BOOST_CHECK_EQUAL(formula("log(12)").evaluate().as_decimal(),
164  static_cast<int>(round(1000.0 * log(12))));
165  BOOST_CHECK_EQUAL(formula("exp(3)").evaluate().as_decimal(),
166  static_cast<int>(round(1000.0 * exp(3))));
167 }
168 
169 BOOST_AUTO_TEST_CASE(test_formula_function_trig)
170 {
171  const double pi = 4. * atan(1.);
172 
174 
175  for(size_t x = 0; x <= 360; ++x) {
176  variables.add("x", variant(x));
177 
178  BOOST_CHECK_EQUAL(
179  game_logic::formula("sin(x)")
180  .evaluate(variables).as_decimal()
181  , static_cast<int>(round(1000. * sin(x * pi / 180.))));
182 
183  BOOST_CHECK_EQUAL(
184  game_logic::formula("cos(x)")
185  .evaluate(variables).as_decimal()
186  , static_cast<int>(round(1000. * cos(x * pi / 180.))));
187 
188  if(x % 90 == 0 && x % 180 != 0) {
189  BOOST_CHECK(
190  game_logic::formula("tan(x)")
191  .evaluate(variables).is_null());
192  } else {
193  BOOST_CHECK_EQUAL(
194  game_logic::formula("tan(x)")
195  .evaluate(variables).as_decimal(),
196  static_cast<int>(round(1000. * tan(x * pi / 180.))));
197  }
198  }
199 }
200 
201 BOOST_AUTO_TEST_SUITE_END()
202 
BOOST_AUTO_TEST_SUITE(test_map_location)
bool set_log_domain_severity(std::string const &name, int severity)
Definition: log.cpp:117
BOOST_AUTO_TEST_CASE(test_formula_function_substring)
logger & debug()
Definition: log.cpp:97
logger & err()
Definition: log.cpp:79
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
map_formula_callable & add(const std::string &key, const variant &value)
Definition: formula.cpp:41
Standard logging facilities (interface).