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