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

overpass_api/statements/binary_operators.cc

180 of 200 instrumented lines run by the suite (90.0%) 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"
241076117 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)
282152117 q{
292152117 q Evaluator* tag_value_ = dynamic_cast< Evaluator* >(statement);
302152117 q if (!tag_value_)
310 substatement_error(get_name(), statement);
322152117 q else if (!lhs)
331076117 q lhs = tag_value_;
341076117 q else if (!rhs)
351076117 q rhs = tag_value_;
360 else
370 add_static_error(get_name() + " must have exactly two evaluator substatements.");
382152117 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)
441076117 q{
451076117 q if (result)
461076117 q {
471076117 q Statement* lhs = stmt_factory.create_evaluator(
481076117 q tree_it.lhs(), tree_context, Statement::Single_Return_Type_Checker(Statement::string));
491076117 q if (lhs)
501076117 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);
551076117 q Statement* rhs = stmt_factory.create_evaluator(
561076117 q tree_it.rhs(), tree_context, Statement::Single_Return_Type_Checker(Statement::string));
571076117 q if (rhs)
581076117 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);
621076117 q }
631076117 q}
66Eval_Task* Evaluator_Pair_Operator::get_string_task(Prepare_Task_Context& context, const std::string* key)
68286116 q Eval_Task* lhs_task = lhs ? lhs->get_string_task(context, key) : 0;
69286116 q Eval_Task* rhs_task = rhs ? rhs->get_string_task(context, key) : 0;
70286116 q return new Binary_Eval_Task(lhs_task, rhs_task, this);
74std::string Binary_Eval_Task::eval(const std::string* key) const
765831 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
8116038640 q{
8216038640 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
8316038640 q}
86std::string Binary_Eval_Task::eval(const Element_With_Context< Attic< Node_Skeleton > >& data, const std::string* key) const
87875214 q{
88875214 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
89875214 q}
92std::string Binary_Eval_Task::eval(const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
93135268550 q{
94135268550 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
95135268550 q}
98std::string Binary_Eval_Task::eval(const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
9920534118 q{
10020534118 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
10120534118 q}
104std::string Binary_Eval_Task::eval(const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
10520183630 q{
10620183630 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
10720183630 q}
110std::string Binary_Eval_Task::eval(const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
11152506 q{
11252506 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
11352506 q}
116std::string Binary_Eval_Task::eval(const Element_With_Context< Area_Skeleton >& data, const std::string* key) const
118842 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
1231349 q{
1241349 q return evaluator->process(lhs ? lhs->eval(data, key) : "", rhs ? rhs->eval(data, key) : "");
1251349 q}
128std::string Binary_Eval_Task::eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
129515616 q{
130515616 q return evaluator->process(lhs ? lhs->eval(pos, data, key) : "", rhs ? rhs->eval(pos, data, key) : "");
131515616 q}
134std::string Binary_Eval_Task::eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
1350{
1360 return evaluator->process(lhs ? lhs->eval(pos, data, key) : "", rhs ? rhs->eval(pos, data, key) : "");
1370}
140std::string Binary_Eval_Task::eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
14127401 q{
14227401 q return evaluator->process(lhs ? lhs->eval(pos, data, key) : "", rhs ? rhs->eval(pos, data, key) : "");
14327401 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
154286116 q if (lhs && rhs)
156286116 q Requested_Context result = lhs->request_context();
157286116 q result.add(rhs->request_context());
158286116 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
177146149 q{
178146149 q return string_represents_boolean_true(lhs_s) && string_represents_boolean_true(rhs_s) ? "1" : "0";
179146149 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
190253749 q{
191253749 q return string_represents_boolean_true(lhs_s) || string_represents_boolean_true(rhs_s) ? "1" : "0";
192253749 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
20311732325 q{
20411732325 q int64 lhs_l = 0;
20511732325 q int64 rhs_l = 0;
20611732325 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
20754 q return lhs_l == rhs_l ? "1" : "0";
20911731821 q double lhs_d = 0;
21011731821 q double rhs_d = 0;
21111731821 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
212114 q return lhs_d == rhs_d ? "1" : "0";
21411730717 q return lhs_s == rhs_s ? "1" : "0";
21511731821 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
22611799510 q{
22711799510 q int64 lhs_l = 0;
22811799510 q int64 rhs_l = 0;
22911799510 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
23021 q return lhs_l == rhs_l ? "0" : "1";
2321179939 q double lhs_d = 0;
2331179939 q double rhs_d = 0;
2341179939 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
23531 q return lhs_d == rhs_d ? "0" : "1";
2371179908 q return lhs_s == rhs_s ? "0" : "1";
2381179939 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
2492657616 q{
2502657616 q int64 lhs_l = 0;
2512657616 q int64 rhs_l = 0;
2522657616 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
25353 q return lhs_l < rhs_l ? "1" : "0";
2552657113 q double lhs_d = 0;
2562657113 q double rhs_d = 0;
2572657113 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
25848356 q return lhs_d < rhs_d ? "1" : "0";
260217367 q return lhs_s < rhs_s ? "1" : "0";
2612657113 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
272241948 q{
273241948 q int64 lhs_l = 0;
274241948 q int64 rhs_l = 0;
275241948 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
27662 q return lhs_l <= rhs_l ? "1" : "0";
278241886 q double lhs_d = 0;
279241886 q double rhs_d = 0;
280241886 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
28146234 q return lhs_d <= rhs_d ? "1" : "0";
283195652 q return lhs_s <= rhs_s ? "1" : "0";
284241886 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
295123612243 q{
296123612243 q int64 lhs_l = 0;
297123612243 q int64 rhs_l = 0;
298123612243 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
29998809623 q return lhs_l > rhs_l ? "1" : "0";
30124802625 q double lhs_d = 0;
30224802625 q double rhs_d = 0;
30324802625 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
3043911518 q return lhs_d > rhs_d ? "1" : "0";
3062089117 q return lhs_s > rhs_s ? "1" : "0";
30724802625 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
318225410 q{
319225410 q int64 lhs_l = 0;
320225410 q int64 rhs_l = 0;
321225410 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
32222223 q return lhs_l >= rhs_l ? "1" : "0";
324327 q double lhs_d = 0;
325327 q double rhs_d = 0;
326327 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
327306 q return lhs_d >= rhs_d ? "1" : "0";
32921 q return lhs_s >= rhs_s ? "1" : "0";
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
34140491540 q{
34240491540 q int64 lhs_l = 0;
34340491540 q int64 rhs_l = 0;
34440491540 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
34566334 q return to_string(lhs_l + rhs_l);
34739828239 q double lhs_d = 0;
34839828239 q double rhs_d = 0;
34939828239 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
350140306 q return to_string(lhs_d + rhs_d);
35238425236 q return lhs_s + rhs_s;
35339828239 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
36429269 q{
36529269 q int64 lhs_l = 0;
36629269 q int64 rhs_l = 0;
36729269 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
368752 q return to_string(lhs_l - rhs_l);
37028517 q double lhs_d = 0;
37128517 q double rhs_d = 0;
37228517 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
37325354 q return to_string(lhs_d - rhs_d);
3753164 q return "NaN";
37628517 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
38739818 q{
38839818 q int64 lhs_l = 0;
38939818 q int64 rhs_l = 0;
39039818 q if (try_int64(lhs_s, lhs_l) && try_int64(rhs_s, rhs_l))
39119942 q return to_string(lhs_l * rhs_l);
39319876 q double lhs_d = 0;
39419876 q double rhs_d = 0;
39519876 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
396593 q return to_string(lhs_d * rhs_d);
39819283 q return "NaN";
39919876 q}
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
4101255316 q{
411 // On purpose no int64 detection
4131255316 q double lhs_d = 0;
4141255316 q double rhs_d = 0;
4151255316 q if (try_double(lhs_s, lhs_d) && try_double(rhs_s, rhs_d))
4161241913 q return to_string(lhs_d / rhs_d);
4181343 q return "NaN";
4191255316 q}