1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
#include "cata_catch.h"
#include <cmath>
#include <locale>
#include "avatar.h"
#include "dialogue.h"
#include "global_vars.h"
#include "math_parser.h"
#include "math_parser_func.h"
static const skill_id skill_survival( "survival" );
static const spell_id spell_test_spell_pew( "test_spell_pew" );
// NOLINTNEXTLINE(readability-function-cognitive-complexity): false positive
TEST_CASE( "math_parser_parsing", "[math_parser]" )
{
dialogue d( std::make_unique<talker>(), std::make_unique<talker>() );
math_exp testexp;
CHECK_FALSE( testexp.parse( "" ) );
CHECK( testexp.eval( d ) == Approx( 0.0 ) );
CHECK( testexp.parse( "50" ) );
CHECK( testexp.eval( d ) == Approx( 50 ) );
// binary
CHECK( testexp.parse( "50 + 2" ) );
CHECK( testexp.eval( d ) == Approx( 52 ) );
CHECK( testexp.parse( "50 + 2 * 3" ) );
CHECK( testexp.eval( d ) == Approx( 56 ) );
CHECK( testexp.parse( "50 + 2 * 3 ^ 2" ) );
CHECK( testexp.eval( d ) == Approx( 68 ) );
CHECK( testexp.parse( "50 + ( 2 * 3 ) ^ 2" ) );
CHECK( testexp.eval( d ) == Approx( 86 ) );
CHECK( testexp.parse( "(((50 + (((((( 2 * 4 ))))) ^ 2))))" ) );
CHECK( testexp.eval( d ) == Approx( 114 ) );
CHECK( testexp.parse( "50 % 3" ) );
CHECK( testexp.eval( d ) == Approx( 2 ) );
// boolean
CHECK( testexp.parse( "1 > 2" ) );
CHECK( testexp.eval( d ) == Approx( 0 ) );
CHECK( testexp.parse( "(1 <= 2) * 3" ) );
CHECK( testexp.eval( d ) == Approx( 3 ) );
// unary
CHECK( testexp.parse( "-50" ) );
CHECK( testexp.eval( d ) == Approx( -50 ) );
CHECK( testexp.parse( "+50" ) );
CHECK( testexp.eval( d ) == Approx( 50 ) );
CHECK( testexp.parse( "-3^2" ) );
CHECK( testexp.eval( d ) == Approx( 9 ) );
CHECK( testexp.parse( "-(3^2)" ) );
CHECK( testexp.eval( d ) == Approx( -9 ) );
CHECK( testexp.parse( "-3^3" ) );
CHECK( testexp.eval( d ) == Approx( -27 ) );
CHECK( testexp.parse( "(-3)^2" ) );
CHECK( testexp.eval( d ) == Approx( 9 ) );
CHECK( testexp.parse( "-3^-2" ) );
CHECK( testexp.eval( d ) == Approx( std::pow( -3, -2 ) ) );
CHECK( testexp.parse( "2++3" ) ); // equivalent to 2+(+3)
CHECK( testexp.eval( d ) == Approx( 5 ) );
CHECK( testexp.parse( "2+-3" ) ); // equivalent to 2+(-3)
CHECK( testexp.eval( d ) == Approx( -1 ) );
CHECK( testexp.parse( "!1" ) );
CHECK( testexp.eval( d ) == Approx( 0 ) );
CHECK( testexp.parse( "!(1 == 0)" ) );
CHECK( testexp.eval( d ) == Approx( 1 ) );
//ternary
CHECK( testexp.parse( "0?1:2" ) );
CHECK( testexp.eval( d ) == Approx( 2 ) );
CHECK( testexp.parse( "0==0?1:2" ) );
CHECK( testexp.eval( d ) == Approx( 1 ) );
CHECK( testexp.parse( "1?0?-1:-2:1" ) );
CHECK( testexp.eval( d ) == Approx( -2 ) );
CHECK( testexp.parse( "1?1?-1:-2:1" ) );
CHECK( testexp.eval( d ) == Approx( -1 ) );
CHECK( testexp.parse( "0?0?-1:-2:1" ) );
CHECK( testexp.eval( d ) == Approx( 1 ) );
CHECK( testexp.parse( "0?(0?(-1):-2):1" ) );
CHECK( testexp.eval( d ) == Approx( 1 ) );
CHECK( testexp.parse( "1==1?2:3?4:5" ) );
CHECK( testexp.eval( d ) == Approx( 2 ) );
CHECK( testexp.parse( "0?2:3?4:5" ) );
CHECK( testexp.eval( d ) == Approx( 4 ) );
CHECK( testexp.parse( "0?2:0?4:5" ) );
CHECK( testexp.eval( d ) == Approx( 5 ) );
CHECK( testexp.parse( "(1==1?2:3)?4:5" ) );
CHECK( testexp.eval( d ) == Approx( 4 ) );
CHECK( testexp.parse( "1==1?2:(3?4:5)" ) );
CHECK( testexp.eval( d ) == Approx( 2 ) );
// functions
CHECK( testexp.parse( "_test_()" ) ); // nullary test function
CHECK( testexp.parse( "max()" ) ); // variadic called with zero arguments
CHECK( testexp.parse( "clamp( 1, 2, 3 )" ) ); // arguments passed in the right order 🤦
CHECK( testexp.eval( d ) == Approx( 2 ) );
CHECK( testexp.parse( "sin(1)" ) );
CHECK( testexp.eval( d ) == Approx( std::sin( 1.0 ) ) );
CHECK( testexp.parse( "sin((1))" ) );
CHECK( testexp.eval( d ) == Approx( std::sin( 1.0 ) ) );
CHECK( testexp.parse( "cos( sin( 1 ) )" ) );
CHECK( testexp.eval( d ) == Approx( std::cos( std::sin( 1.0 ) ) ) );
CHECK( testexp.parse( "cos( (sin( 1 )) )" ) );
CHECK( testexp.eval( d ) == Approx( std::cos( std::sin( 1.0 ) ) ) );
CHECK( testexp.parse( "cos( (sin( (1) )) )" ) );
CHECK( testexp.eval( d ) == Approx( std::cos( std::sin( 1.0 ) ) ) );
CHECK( testexp.parse( "sin(-(-(-(-2))))" ) );
CHECK( testexp.eval( d ) == Approx( std::sin( 2 ) ) );
CHECK( testexp.parse( "max( 1, 2, 3, 4, 5, 6 )" ) );
CHECK( testexp.eval( d ) == Approx( 6 ) );
CHECK( testexp.parse( "cos( sin( min( 1 + 2, -50 ) ) )" ) );
CHECK( testexp.eval( d ) == Approx( std::cos( std::sin( std::min( 1 + 2, -50 ) ) ) ) );
// whitespace
CHECK( testexp.parse( " 50 + ( 2 * 3 ) ^ 2 " ) );
CHECK( testexp.eval( d ) == Approx( 86 ) );
// constants
CHECK( testexp.parse( "Ï€" ) );
CHECK( testexp.eval( d ) == Approx( math_constants::pi_v ) );
CHECK( testexp.parse( "pi" ) );
CHECK( testexp.eval( d ) == Approx( math_constants::pi_v ) );
CHECK( testexp.parse( "e^2" ) );
CHECK( testexp.eval( d ) == Approx( std::pow( math_constants::e_v, 2 ) ) );
// from https://raw.githubusercontent.com/ArashPartow/math-parser-benchmark-project/master/bench_expr_complete.txt
CHECK( testexp.parse( "((((((((5+7)*7.123)-3)-((5+7)-(7.123*3)))-((5*(7-(7.123*3)))/((5*7)+(7.123+3))))-((((5+7)-(7.123*3))+((5/7)+(7.123+3)))+(((5*7)+(7.123+3))*((5/7)/(7.123-3)))))-(((((5/7)-(7.321/3))*((5-7)+(7.321+3)))*(((5-7)-(7.321+3))+((5-7)*(7.321/3))))*((((5-7)+(7.321+3))-((5*7)*(7.321+3)))-(((5-7)*(7.321/3))/(5+((7/7.321)+3)))))))" ) );
CHECK( testexp.eval( d ) == Approx( 87139.7243098 ) );
// nan and inf
CHECK( testexp.parse( "1 / 0" ) );
CHECK( std::isinf( testexp.eval( d ) ) );
CHECK( testexp.parse( "-1 ^ 0.5" ) );
CHECK( std::isnan( testexp.eval( d ) ) );
// locale-independent decimal point
std::locale const &oldloc = std::locale();
on_out_of_scope reset_loc( [&oldloc]() {
std::locale::global( oldloc );
} );
try {
std::locale::global( std::locale( "de_DE.UTF-8" ) );
} catch( std::runtime_error &e ) {
WARN( "couldn't set locale for math_parser test: " << e.what() );
}
CAPTURE( std::setlocale( LC_ALL, nullptr ), std::locale().name() );
CHECK( testexp.parse( "2 * 1.5" ) );
CHECK( testexp.eval( d ) == Approx( 3 ) );
// diag functions. _test_diag adds up all the (kw)args except for "test_unused_kwarg"
CHECK( testexp.parse( "_test_diag_('1', 2, 3*4)" ) ); // mixed arg types
CHECK( testexp.eval( d ) == Approx( 15 ) );
CHECK( testexp.parse( "_test_diag_('1+2*3:() sin(1)')" ) ); // string with token delimiters
CHECK( testexp.parse( "_test_diag_(sin(pi), _test_diag_('1'))" ) ); // compounding
CHECK( testexp.eval( d ) == Approx( 1 ) );
CHECK( testexp.parse( "_test_diag_('1':'2')" ) ); // string kwarg
CHECK( testexp.eval( d ) == Approx( 2 ) );
CHECK( testexp.parse( "_test_diag_(1, '2', '1':'2', '3':'4')" ) ); // kwargs after positional
CHECK( testexp.eval( d ) == Approx( 9 ) );
CHECK( testexp.parse( "_test_diag_('1':2)" ) ); // double kwarg
CHECK( testexp.eval( d ) == Approx( 2 ) );
CHECK( testexp.parse( "_test_diag_('1':2*3)" ) ); // sub-expression kwarg
CHECK( testexp.eval( d ) == Approx( 6 ) );
CHECK( testexp.parse( "_test_diag_('1':3.5*sin(pi/2))" ) ); // sub-expression kwarg
CHECK( testexp.eval( d ) == Approx( 3.5 ) );
CHECK( testexp.parse( "_test_diag_('1':0==0?1:2)" ) ); // ternary kwarg
CHECK( testexp.eval( d ) == Approx( 1 ) );
CHECK( testexp.parse( "_test_diag_('1':2*_test_diag_('2':'3'))" ) ); // kwarg compounding
CHECK( testexp.eval( d ) == Approx( 6 ) );
CHECK( testexp.parse( "_test_diag_([1,2,2+1])" ) ); // array arg
CHECK( testexp.eval( d ) == Approx( 6 ) );
CHECK( testexp.parse( "_test_diag_([1,1+1,3], 'blorg': [3+1,5,6])" ) ); // array kwarg
CHECK( testexp.eval( d ) == Approx( 21 ) );
CHECK( testexp.parse( "_test_str_len_(['1','2'], 'test_str_arr': ['one','two'])" ) ); // str array
CHECK( testexp.eval( d ) == Approx( 8 ) );
CHECK( testexp.parse( "_test_diag_([1,2,3], 'blorg': [4,5,_test_diag_([6,7,8], 'blarg':[9])])" ) ); // yo dawg
CHECK( testexp.eval( d ) == Approx( 45 ) );
CHECK( testexp.parse( "_test_diag_([[0,-1],[2,0],[3,4]])" ) ); // nested arrays
CHECK( testexp.parse( "_test_diag_([[0,-1],[2,0],[3,(3+1)]])" ) );
CHECK( testexp.eval( d ) == Approx( 8 ) );
CHECK( testexp.parse( "_test_diag_([],1)" ) ); // empty array
CHECK( testexp.eval( d ) == Approx( 1 ) );
CHECK( testexp.parse( "_test_diag_([0?1:2,3,1?4:5])" ) ); // ternaries in array
CHECK( testexp.eval( d ) == Approx( 9 ) );
CHECK( testexp.parse( "_test_str_len_((['1','2']))" ) ); // pointless parens
CHECK( testexp.eval( d ) == Approx( 2 ) );
CHECK( testexp.parse( "_test_str_len_((['1',('2')]))" ) ); // pointless parens
CHECK( testexp.eval( d ) == Approx( 2 ) );
// failed validation
// NOLINTNEXTLINE(readability-function-cognitive-complexity): false positive
std::string dmsg = capture_debugmsg_during( [&testexp, &d]() {
CHECK_FALSE( testexp.parse( "2+" ) );
CHECK_FALSE( testexp.parse( ")" ) );
CHECK_FALSE( testexp.parse( "(" ) );
CHECK_FALSE( testexp.parse( "[" ) );
CHECK_FALSE( testexp.parse( "]" ) );
CHECK_FALSE( testexp.parse( "()" ) );
CHECK_FALSE( testexp.parse( "[]" ) );
CHECK_FALSE( testexp.parse( "[2]" ) ); // not diag function
CHECK_FALSE( testexp.parse( "(2+2))" ) );
CHECK_FALSE( testexp.parse( "(2+2^)" ) );
CHECK_FALSE( testexp.parse( "((2+2)" ) );
CHECK_FALSE( testexp.parse( "(2+2,3)" ) );
CHECK_FALSE( testexp.parse( "sin(1,2)" ) ); // too many arguments
CHECK_FALSE( testexp.parse( "rng(1)" ) ); // not enough arguments
CHECK_FALSE( testexp.parse( "rng(1,[2,3])" ) ); // not diag function
CHECK_FALSE( testexp.parse( "sin((1+2,3))" ) );
CHECK_FALSE( testexp.parse( "sin(1" ) );
CHECK_FALSE( testexp.parse( "sin" ) );
CHECK_FALSE( testexp.parse( "sin('1')" ) );
CHECK_FALSE( testexp.parse( "sin(+)" ) );
CHECK_FALSE( testexp.parse( "sin(-)" ) );
CHECK_FALSE( testexp.parse( "_test_(-)" ) );
CHECK_FALSE( testexp.parse( "_test_(1)" ) );
CHECK_FALSE( testexp.parse( "'string'" ) );
CHECK_FALSE( testexp.parse( "('wrong')" ) );
CHECK_FALSE( testexp.parse( "_test_diag_('wr'ong')" ) ); // stray ' inside string
CHECK_FALSE( testexp.parse( "_test_diag_('wrong)" ) ); // unterminated string
CHECK_FALSE( testexp.parse( "_test_diag_('1'+'2')" ) ); // no string operators (yet)
CHECK_FALSE( testexp.parse( "_test_diag_(1:'2')" ) ); // kwarg key is not string
CHECK_FALSE( testexp.parse( "_test_diag_('1':'2', 3)" ) ); // positional after kwargs
CHECK_FALSE( testexp.parse( "_test_diag_('test_unused_kwarg': 'mustfail')" ) ); // unused kwarg
CHECK_FALSE( testexp.parse( "_test_diag_('1':'2':)" ) );
CHECK_FALSE( testexp.parse( "_test_diag_('1':'2':'3')" ) );
CHECK_FALSE( testexp.parse( "_test_diag_(:)" ) );
CHECK_FALSE( testexp.parse( "_test_diag_([)" ) );
CHECK_FALSE( testexp.parse( "_test_diag_([" ) );
CHECK_FALSE( testexp.parse( "_test_diag_(2[)" ) );
CHECK_FALSE( testexp.parse( "_test_diag_(2+[)" ) );
CHECK_FALSE( testexp.parse( "_test_diag_(])" ) );
CHECK_FALSE( testexp.parse( "_test_diag_(2])" ) );
CHECK_FALSE( testexp.parse( "_test_diag_(2+])" ) );
CHECK_FALSE( testexp.parse( "_test_diag_(['1','':'1')" ) );
CHECK_FALSE( testexp.parse( "_test_diag_([1,2))" ) ); // mismatched brackets
CHECK_FALSE( testexp.parse( "_test_diag_([1,2]*2)" ) ); // no operators support arrays (yet)
CHECK_FALSE( testexp.parse( "_test_diag_(-[1,2])" ) );
CHECK_FALSE( testexp.parse( "_test_diag_([1,2]?1:2)" ) ); // array can't be condition for ternary
CHECK_FALSE( testexp.parse( "_test_diag_(1?2:[2,3])" ) ); // no arrays in ternaries (yet)
CHECK_FALSE( testexp.parse( "_test_diag_(1?[2,3]:4)" ) );
CHECK_FALSE( testexp.parse( "_test_diag_(['blorg':3])" ) ); // no kwargs in arrays
CHECK_FALSE( testexp.parse( "_test_diag_(1?'a':1:'b':2)" ) ); // no kwargs in ternaries
CHECK_FALSE( testexp.parse( "sin('1':'2')" ) ); // no kwargs in math functions (yet?)
CHECK( testexp.parse( "_test_str_len_('fail')" ) ); // expected array at runtime
CHECK( testexp.eval( d ) == 0 );
CHECK_FALSE( testexp.parse( "'1':'2'" ) );
CHECK_FALSE( testexp.parse( "2 2*2" ) ); // stray space inside variable name
CHECK_FALSE( testexp.parse( "2+++2" ) );
CHECK_FALSE( testexp.parse( "1=2" ) );
CHECK_FALSE( testexp.parse( "1===2" ) );
CHECK_FALSE( testexp.parse( "0?" ) );
CHECK_FALSE( testexp.parse( "0?1" ) );
CHECK_FALSE( testexp.parse( "0?1:" ) );
CHECK( testexp.parse( "2+3" ) );
testexp.assign( d, 10 ); // assignment called on eval tree should not crash
} );
// make sure there were no bad error messages
CHECK( dmsg.find( "Unexpected" ) == std::string::npos );
CHECK( dmsg.find( "That's all we know" ) == std::string::npos );
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity): false positive
TEST_CASE( "math_parser_dialogue_integration", "[math_parser]" )
{
standard_npc dude;
dialogue d( get_talker_for( get_avatar() ), get_talker_for( &dude ) );
math_exp testexp;
global_variables &globvars = get_globals();
// reading scoped variables
globvars.set_global_value( "npctalk_var_x", "100" );
CHECK( testexp.parse( "x" ) );
CHECK( testexp.eval( d ) == Approx( 100 ) );
get_avatar().set_value( "npctalk_var_x", "92" );
CHECK( testexp.parse( "u_x" ) );
CHECK( testexp.eval( d ) == Approx( 92 ) );
dude.set_value( "npctalk_var_x", "21" );
CHECK( testexp.parse( "n_x" ) );
CHECK( testexp.eval( d ) == Approx( 21 ) );
CHECK( testexp.parse( "x + u_x + n_x" ) );
CHECK( testexp.eval( d ) == Approx( 213 ) );
CHECK( testexp.parse( "_ctx" ) );
CHECK( testexp.eval( d ) == Approx( 0 ) );
d.set_value( "npctalk_var_ctx", "14" );
CHECK( testexp.parse( "_ctx" ) );
CHECK( testexp.eval( d ) == Approx( 14 ) );
// reading scoped values with u_val shim
std::string dmsg = capture_debugmsg_during( [&testexp]() {
CHECK_FALSE( testexp.parse( "u_val( 3 )" ) ); // this function doesn't support numbers
CHECK_FALSE( testexp.parse( "u_val(myval)" ) ); // this function doesn't support variables
CHECK_FALSE( testexp.parse( "val( 'stamina' )" ) ); // invalid scope for this function
} );
CHECK( testexp.parse( "u_val('stamina')" ) );
CHECK( testexp.eval( d ) == get_avatar().get_stamina() );
CHECK( testexp.parse( "u_val('spell_level', 'spell: test_spell_pew')" ) );
get_avatar().magic->learn_spell( spell_test_spell_pew, get_avatar(), true );
get_avatar().magic->set_spell_level( spell_test_spell_pew, 4, &get_avatar() );
REQUIRE( d.actor( false )->get_spell_level( spell_test_spell_pew ) != 0 );
CHECK( testexp.eval( d ) == d.actor( false )->get_spell_level( spell_test_spell_pew ) );
CHECK( testexp.parse( "u_val('time: 1 m')" ) ); // test get_member() in shim
CHECK( testexp.eval( d ) == 60 );
// evaluating string variables in dialogue functions
globvars.set_global_value( "npctalk_var_someskill", "survival" );
CHECK( testexp.parse( "u_skill(someskill)" ) );
get_avatar().set_skill_level( skill_survival, 3 );
CHECK( testexp.eval( d ) == 3 );
// assignment to scoped variables
CHECK( testexp.parse( "u_testvar", true ) );
testexp.assign( d, 159 );
CHECK( std::stoi( get_avatar().get_value( "npctalk_var_testvar" ) ) == 159 );
CHECK( testexp.parse( "testvar", true ) );
testexp.assign( d, 259 );
CHECK( std::stoi( globvars.get_global_value( "npctalk_var_testvar" ) ) == 259 );
CHECK( testexp.parse( "n_testvar", true ) );
testexp.assign( d, 359 );
CHECK( std::stoi( dude.get_value( "npctalk_var_testvar" ) ) == 359 );
CHECK( testexp.parse( "_testvar", true ) );
testexp.assign( d, 159 );
CHECK( std::stoi( d.get_value( "npctalk_var_testvar" ) ) == 159 );
// assignment to scoped values with u_val shim
CHECK( testexp.parse( "u_val('stamina')", true ) );
testexp.assign( d, 459 );
CHECK( get_avatar().get_stamina() == 459 );
CHECK( testexp.parse( "n_val('stored_kcal')", true ) );
testexp.assign( d, 559 );
CHECK( dude.get_stored_kcal() == 559 );
std::string morelogs = capture_debugmsg_during( [&testexp, &d]() {
CHECK( testexp.eval( d ) == Approx( 0 ) ); // eval called on assignment tree should not crash
CHECK_FALSE( testexp.parse( "val( 'stamina' ) * 3", true ) ); // eval expression in assignment tree
} );
}
|