Overpass API regression suite coverage (Saxony) / overpass_api/statements/binary_operators.cc

overpass_api/statements/binary_operators.cc

183 of 200 instrumented lines run by the suite (91.5%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include "../data/tag_store.h"
20#include "../data/utils.h"
21#include "binary_operators.h"
241148131 qEvaluator_Pair_Operator::Evaluator_Pair_Operator(int line_number_) : Evaluator(line_number_), lhs(0), rhs(0) {}
27void Evaluator_Pair_Operator::add_statement(Statement* statement, std::string text)
282296131 q{
292296131 q Evaluator* tag_value_ = dynamic_cast< Evaluator* >(statement);
302296131 q if (!tag_value_)
310 substatement_error(get_name(), statement);
322296131 q else if (!lhs)
331148131 q lhs = tag_value_;
341148131 q else if (!rhs)
351148131 q rhs = tag_value_;
360 else
370 add_static_error(get_name() + " must have exactly two evaluator substatements.");
382296131 q}
41void Evaluator_Pair_Operator::add_substatements(Statement* result, const std::string& operator_name,
42 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context,
43 Statement::Factory& stmt_factory, Error_Output* error_output)
441148131 q{
451148131 q if (result)
461148131 q {
471148131 q Statement* lhs = stmt_factory.create_evaluator(
481148131 q tree_it.lhs(), tree_context, Statement::Single_Return_Type_Checker(Statement::string));
491148131 q if (lhs)
501148131 q result->add_statement(lhs, "");
510 else if (error_output)
520 error_output->add_parse_error(std::string("Operator \"") + operator_name
530 + "\" needs a left-hand-side argument", tree_it->line_col.first);
551148131 q Statement* rhs = stmt_factory.create_evaluator(
561148131 q tree_it.rhs(), tree_context, Statement::Single_Return_Type_Checker(Statement::string));
571148131 q if (rhs)
581148131 q result->add_statement(rhs, "");
590 else if (error_output)
600 error_output->add_parse_error(std::string("Operator \"") + operator_name
610 + "\" needs a right-hand-side argument", tree_it->line_col.first);
621148131 q }
631148131 q}
66Eval_Task* Evaluator_Pair_Operator::get_string_task(Prepare_Task_Context& context, const std::string* key)
68299128 q Eval_Task* lhs_task = lhs ? lhs->get_string_task(context, key) : 0;
69299128 q Eval_Task* rhs_task = rhs ? rhs->get_string_task(context, key) : 0;
70299128 q return new Binary_Eval_Task(lhs_task, rhs_task, this);
74std::string Binary_Eval_Task::eval(const std::string* key) const
767137 q return evaluator->process(lhs ? lhs->eval(key) : "", rhs ? rhs->eval(key) : "");
80std::string Binary_Eval_Task::eval(const Element_With_Context< Node_Skeleton >& data, const std::string* key) const
81100164836 q{
82100164836 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
83100164836 q}
86std::string Binary_Eval_Task::eval(const Element_With_Context< Attic< Node_Skeleton > >& data, const std::string* key) const
871180412 q{
881180412 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
891180412 q}
92std::string Binary_Eval_Task::eval(const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
9357796032 q{
9457796032 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
9557796032 q}
98std::string Binary_Eval_Task::eval(const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
991767014 q{
1001767014 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
1011767014 q}
104std::string Binary_Eval_Task::eval(const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
10568376325 q{
10668376325 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
10768376325 q}
110std::string Binary_Eval_Task::eval(const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
111295935 q{
112295935 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
113295935 q}
116std::string Binary_Eval_Task::eval(const Element_With_Context< Area_Skeleton >& data, const std::string* key) const
11842 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
122std::string Binary_Eval_Task::eval(const Element_With_Context< Derived_Skeleton >& data, const std::string* key) const
123111389711 q{
124111389711 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
125111389711 q}
128std::string Binary_Eval_Task::eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
129685794 q{
130685794 q return evaluator->process(lhs ? lhs->eval(pos, data, key) : "", rhs ? rhs->eval(pos, data, key) : "");
131685794 q}
134std::string Binary_Eval_Task::eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
13568891 q{
13668891 q return evaluator->process(lhs ? lhs->eval(pos, data, key) : "", rhs ? rhs->eval(pos, data, key) : "");
13768891 q}
140std::string Binary_Eval_Task::eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
1415168843 q{
1425168843 q return evaluator->process(lhs ? lhs->eval(pos, data, key) : "", rhs ? rhs->eval(pos, data, key) : "");
1435168843 q}
146std::string Binary_Eval_Task::eval(uint pos, const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
1470{
1480 return evaluator->process(lhs ? lhs->eval(pos, data, key) : "", rhs ? rhs->eval(pos, data, key) : "");
1490}
152Requested_Context Evaluator_Pair_Operator::request_context() const
154299128 q if (lhs && rhs)
156299128 q Requested_Context result = lhs->request_context();
157299128 q result.add(rhs->request_context());
158299128 q return result;
1600 else if (lhs)
1610 return lhs->request_context();
1620 else if (rhs)
1630 return rhs->request_context();
1650 return Requested_Context();
169//-----------------------------------------------------------------------------
172Operator_Stmt_Maker< Evaluator_And > Evaluator_And::statement_maker;
173Operator_Eval_Maker< Evaluator_And > Evaluator_And::evaluator_maker;
176std::string Evaluator_And::process(const std::string& lhs_s, const std::string& rhs_s) const
17710111811 q{
17810111811 q return string_represents_boolean_true(lhs_s) && string_represents_boolean_true(rhs_s) ? "1" : "0";
17910111811 q}
182//-----------------------------------------------------------------------------
185Operator_Stmt_Maker< Evaluator_Or > Evaluator_Or::statement_maker;
186Operator_Eval_Maker< Evaluator_Or > Evaluator_Or::evaluator_maker;
189std::string Evaluator_Or::process(const std::string& lhs_s, const std::string& rhs_s) const
190457159 q{
191457159 q return string_represents_boolean_true(lhs_s) || string_represents_boolean_true(rhs_s) ? "1" : "0";
192457159 q}
195//-----------------------------------------------------------------------------
198Operator_Stmt_Maker< Evaluator_Equal > Evaluator_Equal::statement_maker;
199Operator_Eval_Maker< Evaluator_Equal > Evaluator_Equal::evaluator_maker;
202std::string Evaluator_Equal::process(const std::string& lhs_s, const std::string& rhs_s) const
20322231428 q{
20422231428 q int64 lhs_l = 0;
20522231428 q int64 rhs_l = 0;
20622231428 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
20785599 q return lhs_l == rhs_l ? "1" : "0";
20921375522 q double lhs_d = 0;
21021375522 q double rhs_d = 0;
21121375522 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
212575 q return lhs_d == rhs_d ? "1" : "0";
21421369817 q return lhs_s == rhs_s ? "1" : "0";
21521375522 q}
218//-----------------------------------------------------------------------------
221Operator_Stmt_Maker< Evaluator_Not_Equal > Evaluator_Not_Equal::statement_maker;
222Operator_Eval_Maker< Evaluator_Not_Equal > Evaluator_Not_Equal::evaluator_maker;
225std::string Evaluator_Not_Equal::process(const std::string& lhs_s, const std::string& rhs_s) const
22649693611 q{
22749693611 q int64 lhs_l = 0;
22849693611 q int64 rhs_l = 0;
22949693611 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
2304937923 q return lhs_l == rhs_l ? "0" : "1";
23231449 q double lhs_d = 0;
23331449 q double rhs_d = 0;
23431449 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
235123 q return lhs_d == rhs_d ? "0" : "1";
23731326 q return lhs_s == rhs_s ? "0" : "1";
23831449 q}
241//-----------------------------------------------------------------------------
244Operator_Stmt_Maker< Evaluator_Less > Evaluator_Less::statement_maker;
245Operator_Eval_Maker< Evaluator_Less > Evaluator_Less::evaluator_maker;
248std::string Evaluator_Less::process(const std::string& lhs_s, const std::string& rhs_s) const
2496611019 q{
2506611019 q int64 lhs_l = 0;
2516611019 q int64 rhs_l = 0;
2526611019 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
253574 q return lhs_l < rhs_l ? "1" : "0";
2556605316 q double lhs_d = 0;
2566605316 q double rhs_d = 0;
2576605316 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
25864658 q return lhs_d < rhs_d ? "1" : "0";
260595888 q return lhs_s < rhs_s ? "1" : "0";
2616605316 q}
264//-----------------------------------------------------------------------------
267Operator_Stmt_Maker< Evaluator_Less_Equal > Evaluator_Less_Equal::statement_maker;
268Operator_Eval_Maker< Evaluator_Less_Equal > Evaluator_Less_Equal::evaluator_maker;
271std::string Evaluator_Less_Equal::process(const std::string& lhs_s, const std::string& rhs_s) const
27290976 q{
27390976 q int64 lhs_l = 0;
27490976 q int64 rhs_l = 0;
27590976 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
27661 q return lhs_l <= rhs_l ? "1" : "0";
27890915 q double lhs_d = 0;
27990915 q double rhs_d = 0;
28090915 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
28133 q return lhs_d <= rhs_d ? "1" : "0";
28390882 q return lhs_s <= rhs_s ? "1" : "0";
28490915 q}
287//-----------------------------------------------------------------------------
290Operator_Stmt_Maker< Evaluator_Greater > Evaluator_Greater::statement_maker;
291Operator_Eval_Maker< Evaluator_Greater > Evaluator_Greater::evaluator_maker;
294std::string Evaluator_Greater::process(const std::string& lhs_s, const std::string& rhs_s) const
29534854643 q{
29634854643 q int64 lhs_l = 0;
29734854643 q int64 rhs_l = 0;
29834854643 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
29930600622 q return lhs_l > rhs_l ? "1" : "0";
3014254023 q double lhs_d = 0;
3024254023 q double rhs_d = 0;
3034254023 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
3043992513 q return lhs_d > rhs_d ? "1" : "0";
306261511 q return lhs_s > rhs_s ? "1" : "0";
3074254023 q}
310//-----------------------------------------------------------------------------
313Operator_Stmt_Maker< Evaluator_Greater_Equal > Evaluator_Greater_Equal::statement_maker;
314Operator_Eval_Maker< Evaluator_Greater_Equal > Evaluator_Greater_Equal::evaluator_maker;
317std::string Evaluator_Greater_Equal::process(const std::string& lhs_s, const std::string& rhs_s) const
3181436411 q{
3191436411 q int64 lhs_l = 0;
3201436411 q int64 rhs_l = 0;
3211436411 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
32264422 q return lhs_l >= rhs_l ? "1" : "0";
32479229 q double lhs_d = 0;
32579229 q double rhs_d = 0;
32679229 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
3275487 q return lhs_d >= rhs_d ? "1" : "0";
32973743 q return lhs_s >= rhs_s ? "1" : "0";
33079229 q}
333//-----------------------------------------------------------------------------
336Operator_Stmt_Maker< Evaluator_Plus > Evaluator_Plus::statement_maker;
337Operator_Eval_Maker< Evaluator_Plus > Evaluator_Plus::evaluator_maker;
340std::string Evaluator_Plus::process(const std::string& lhs_s, const std::string& rhs_s) const
341182475243 q{
342182475243 q int64 lhs_l = 0;
343182475243 q int64 rhs_l = 0;
344182475243 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
3451174286 q return to_string(lhs_l + rhs_l);
347170732440 q double lhs_d = 0;
348170732440 q double rhs_d = 0;
349170732440 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
35056953 q return to_string(lhs_d + rhs_d);
352170162938 q return lhs_s + rhs_s;
353170732440 q}
356//-----------------------------------------------------------------------------
359Operator_Stmt_Maker< Evaluator_Minus > Evaluator_Minus::statement_maker;
360Operator_Eval_Maker< Evaluator_Minus > Evaluator_Minus::evaluator_maker;
363std::string Evaluator_Minus::process(const std::string& lhs_s, const std::string& rhs_s) const
3643776547 q{
3653776547 q int64 lhs_l = 0;
3663776547 q int64 rhs_l = 0;
3673776547 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
368672 q return to_string(lhs_l - rhs_l);
3703775875 q double lhs_d = 0;
3713775875 q double rhs_d = 0;
3723775875 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
3733775853 q return to_string(lhs_d - rhs_d);
37522 q return "NaN";
3763775875 q}
379//-----------------------------------------------------------------------------
382Operator_Stmt_Maker< Evaluator_Times > Evaluator_Times::statement_maker;
383Operator_Eval_Maker< Evaluator_Times > Evaluator_Times::evaluator_maker;
386std::string Evaluator_Times::process(const std::string& lhs_s, const std::string& rhs_s) const
3873712006 q{
3883712006 q int64 lhs_l = 0;
3893712006 q int64 rhs_l = 0;
3903712006 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
3913711441 q return to_string(lhs_l * rhs_l);
393565 q double lhs_d = 0;
394565 q double rhs_d = 0;
395565 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
396482 q return to_string(lhs_d * rhs_d);
39883 q return "NaN";
402//-----------------------------------------------------------------------------
405Operator_Stmt_Maker< Evaluator_Divided > Evaluator_Divided::statement_maker;
406Operator_Eval_Maker< Evaluator_Divided > Evaluator_Divided::evaluator_maker;
409std::string Evaluator_Divided::process(const std::string& lhs_s, const std::string& rhs_s) const
41015095612 q{
411 // On purpose no int64 detection
41315095612 q double lhs_d = 0;
41415095612 q double rhs_d = 0;
41515095612 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
4168030110 q return to_string(lhs_d / rhs_d);
418706552 q return "NaN";
41915095612 q}