The Battle for Wesnoth  1.15.11+dev
test_formula_function.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://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 wfl;
25 
27 
28 BOOST_AUTO_TEST_CASE(test_formula_function_substring)
29 {
30  BOOST_CHECK_EQUAL(
31  formula("substring('hello world', 0)")
32  .evaluate().as_string()
33  , "hello world");
34 
35  BOOST_CHECK_EQUAL(
36  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  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  formula("substring('hello world', -1)")
51  .evaluate().as_string()
52  , "d");
53 
54  BOOST_CHECK_EQUAL(
55  formula("substring('hello world', -5)")
56  .evaluate().as_string()
57  , "world");
58 
59  BOOST_CHECK_EQUAL(
60  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  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  formula("substring('hello world', 0, 0)")
75  .evaluate().as_string()
76  , "");
77 
78  BOOST_CHECK_EQUAL(
79  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  formula("substring('hello world', 5, 1)")
87  .evaluate().as_string()
88  , " ");
89 
90  BOOST_CHECK_EQUAL(
91  formula("substring('hello world', 1, 9)")
92  .evaluate().as_string()
93  , "ello worl");
94 
95  BOOST_CHECK_EQUAL(
96  formula("substring('hello world', -10, 9)")
97  .evaluate().as_string()
98  , "ello worl");
99 
100  BOOST_CHECK_EQUAL(
101  formula("substring('hello world', -1, -5)")
102  .evaluate().as_string()
103  , "world");
104 
105  BOOST_CHECK_EQUAL(
106  formula("substring('hello world', 4, -5)")
107  .evaluate().as_string()
108  , "hello");
109 
111 }
112 
113 BOOST_AUTO_TEST_CASE(test_formula_function_length)
114 {
115  BOOST_CHECK_EQUAL(
116  formula("length('')").evaluate().as_int()
117  , 0);
118 
119  BOOST_CHECK_EQUAL(
120  formula("length('hello world')").evaluate().as_int()
121  , 11);
122 }
123 
124 BOOST_AUTO_TEST_CASE(test_formula_function_concatenate)
125 {
126  BOOST_CHECK_EQUAL(
127  formula("concatenate(100)").evaluate().as_string()
128  , "100");
129 
130  BOOST_CHECK_EQUAL(
131  formula("concatenate(100, 200, 'a')")
132  .evaluate().as_string()
133  , "100200a");
134 
135  BOOST_CHECK_EQUAL(
136  formula("concatenate([1,2,3])")
137  .evaluate().as_string()
138  , "1, 2, 3");
139 
140  BOOST_CHECK_EQUAL(
141  formula(
142  "concatenate([1.0, 1.00, 1.000, 1.2, 1.23, 1.234])")
143  .evaluate().as_string()
144  , "1.000, 1.000, 1.000, 1.200, 1.230, 1.234");
145 }
146 
147 BOOST_AUTO_TEST_CASE(test_formula_function_math)
148 {
149  BOOST_CHECK_EQUAL(formula("abs(5)").evaluate().as_int(), 5);
150  BOOST_CHECK_EQUAL(formula("abs(-5)").evaluate().as_int(), 5);
151  BOOST_CHECK_EQUAL(formula("abs(5.0)").evaluate().as_int(), 5);
152  BOOST_CHECK_EQUAL(formula("abs(-5.0)").evaluate().as_int(), 5);
153 
154  BOOST_CHECK_EQUAL(formula("min(3,5)").evaluate().as_int(), 3);
155  BOOST_CHECK_EQUAL(formula("min(5,2)").evaluate().as_int(), 2);
156  BOOST_CHECK_EQUAL(formula("max(3,5)").evaluate().as_int(), 5);
157  BOOST_CHECK_EQUAL(formula("max(5,2)").evaluate().as_int(), 5);
158  BOOST_CHECK_EQUAL(formula("max(5.5,5)").evaluate().as_decimal(),
159  static_cast<int>(1000.0 * 5.5));
160 
161  BOOST_CHECK_EQUAL(formula("max(4,5,[2,18,7])").evaluate().as_int(), 18);
162 
163  BOOST_CHECK_EQUAL(formula("log(8,2)").evaluate().as_int(), 3);
164  BOOST_CHECK_EQUAL(formula("log(12)").evaluate().as_decimal(),
165  static_cast<int>(round(1000.0 * std::log(12))));
166  BOOST_CHECK_EQUAL(formula("exp(3)").evaluate().as_decimal(),
167  static_cast<int>(round(1000.0 * std::exp(3))));
168 }
169 
170 BOOST_AUTO_TEST_CASE(test_formula_function_trig)
171 {
172  const double pi = 4. * std::atan(1.);
173 
174  map_formula_callable variables;
175 
176  for(std::size_t x = 0; x <= 360; ++x) {
177  variables.add("x", variant(x));
178 
179  BOOST_CHECK_EQUAL(
180  formula("sin(x)")
181  .evaluate(variables).as_decimal()
182  , static_cast<int>(round(1000. * std::sin(x * pi / 180.))));
183 
184  BOOST_CHECK_EQUAL(
185  formula("cos(x)")
186  .evaluate(variables).as_decimal()
187  , static_cast<int>(round(1000. * std::cos(x * pi / 180.))));
188 
189  if(x % 90 == 0 && x % 180 != 0) {
190  BOOST_CHECK(
191  formula("tan(x)")
192  .evaluate(variables).is_null());
193  } else {
194  BOOST_CHECK_EQUAL(
195  formula("tan(x)")
196  .evaluate(variables).as_decimal(),
197  static_cast<int>(round(1000. * std::tan(x * pi / 180.))));
198  }
199  }
200 }
201 
202 BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(filesystem)
map_formula_callable & add(const std::string &key, const variant &value)
Definition: callable.hpp:252
BOOST_AUTO_TEST_CASE(test_formula_function_substring)
logger & debug()
Definition: log.cpp:94
logger & err()
Definition: log.cpp:76
bool set_log_domain_severity(const std::string &name, int severity)
Definition: log.cpp:116
logger & warn()
Definition: log.cpp:82
Definition: contexts.hpp:43
Standard logging facilities (interface).