The Battle for Wesnoth  1.19.7+dev
test_formula_function.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
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_concatenate)
126 {
127  BOOST_CHECK_EQUAL(
128  formula("concatenate(100)").evaluate().as_string()
129  , "100");
130 
131  BOOST_CHECK_EQUAL(
132  formula("concatenate(100, 200, 'a')")
133  .evaluate().as_string()
134  , "100200a");
135 
136  BOOST_CHECK_EQUAL(
137  formula("concatenate([1,2,3])")
138  .evaluate().as_string()
139  , "1, 2, 3");
140 
141  BOOST_CHECK_EQUAL(
142  formula(
143  "concatenate([1.0, 1.00, 1.000, 1.2, 1.23, 1.234])")
144  .evaluate().as_string()
145  , "1.000, 1.000, 1.000, 1.200, 1.230, 1.234");
146 }
147 
148 BOOST_AUTO_TEST_CASE(test_formula_function_math)
149 {
150  BOOST_CHECK_EQUAL(formula("abs(5)").evaluate().as_int(), 5);
151  BOOST_CHECK_EQUAL(formula("abs(-5)").evaluate().as_int(), 5);
152  BOOST_CHECK_EQUAL(formula("abs(5.0)").evaluate().as_int(), 5);
153  BOOST_CHECK_EQUAL(formula("abs(-5.0)").evaluate().as_int(), 5);
154 
155  BOOST_CHECK_EQUAL(formula("min(3,5)").evaluate().as_int(), 3);
156  BOOST_CHECK_EQUAL(formula("min(5,2)").evaluate().as_int(), 2);
157  BOOST_CHECK_EQUAL(formula("max(3,5)").evaluate().as_int(), 5);
158  BOOST_CHECK_EQUAL(formula("max(5,2)").evaluate().as_int(), 5);
159  BOOST_CHECK_EQUAL(formula("max(5.5,5)").evaluate().as_decimal(),
160  static_cast<int>(1000.0 * 5.5));
161 
162  BOOST_CHECK_EQUAL(formula("max(4,5,[2,18,7])").evaluate().as_int(), 18);
163 
164  BOOST_CHECK_EQUAL(formula("log(8,2)").evaluate().as_int(), 3);
165  BOOST_CHECK_EQUAL(formula("log(12)").evaluate().as_decimal(),
166  static_cast<int>(round(1000.0 * std::log(12))));
167  BOOST_CHECK_EQUAL(formula("exp(3)").evaluate().as_decimal(),
168  static_cast<int>(round(1000.0 * std::exp(3))));
169 }
170 
171 BOOST_AUTO_TEST_CASE(test_formula_function_trig)
172 {
173  const double pi = 4. * std::atan(1.);
174 
175  map_formula_callable variables;
176 
177  for(std::size_t x = 0; x <= 360; ++x) {
178  variables.add("x", variant(x));
179 
180  BOOST_CHECK_EQUAL(
181  formula("sin(x)")
182  .evaluate(variables).as_decimal()
183  , static_cast<int>(round(1000. * std::sin(x * pi / 180.))));
184 
185  BOOST_CHECK_EQUAL(
186  formula("cos(x)")
187  .evaluate(variables).as_decimal()
188  , static_cast<int>(round(1000. * std::cos(x * pi / 180.))));
189 
190  if(x % 90 == 0 && x % 180 != 0) {
191  BOOST_CHECK(
192  formula("tan(x)")
193  .evaluate(variables).is_null());
194  } else {
195  BOOST_CHECK_EQUAL(
196  formula("tan(x)")
197  .evaluate(variables).as_decimal(),
198  static_cast<int>(round(1000. * std::tan(x * pi / 180.))));
199  }
200  }
201 }
202 
203 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:307
logger & debug()
Definition: log.cpp:325
logger & warn()
Definition: log.cpp:313
bool set_log_domain_severity(const std::string &name, severity severity)
Definition: log.cpp:347
Definition: contexts.hpp:43
BOOST_AUTO_TEST_SUITE(filesystem)
BOOST_AUTO_TEST_CASE(test_formula_function_substring)