rusefi-1/unit_tests/tests/test_logic_expression.cpp

235 lines
5.8 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file test_logic_expression.cpp
*
* https://sourceforge.net/p/rusefi/tickets/102/
*
* @date Oct 3, 2014
2018-07-28 09:42:37 -07:00
* @author Andrey Belomutskiy, (c) 2012-2018
2015-07-10 06:01:56 -07:00
*/
2018-09-16 19:39:46 -07:00
#include "global.h"
2015-07-10 06:01:56 -07:00
#include "fsio_impl.h"
#include "cli_registry.h"
2017-06-04 12:25:37 -07:00
#include "engine_test_helper.h"
2015-07-10 06:01:56 -07:00
#define TEST_POOL_SIZE 256
static float mockFan;
static float mockRpm;
2017-06-25 22:49:11 -07:00
static float mockCrankingRpm;
2015-07-10 06:01:56 -07:00
static float mockTimeSinceBoot;
2017-06-04 12:25:37 -07:00
float getEngineValue(le_action_e action DECLARE_ENGINE_PARAMETER_SUFFIX) {
2015-07-10 06:01:56 -07:00
switch(action) {
case LE_METHOD_FAN:
return mockFan;
case LE_METHOD_COOLANT:
return engine->sensors.clt;
2015-07-10 06:01:56 -07:00
case LE_METHOD_RPM:
return mockRpm;
2017-06-25 22:49:11 -07:00
case LE_METHOD_CRANKING_RPM:
return mockCrankingRpm;
2015-07-10 06:01:56 -07:00
case LE_METHOD_TIME_SINCE_BOOT:
return mockTimeSinceBoot;
case LE_METHOD_FAN_ON_SETTING:
return 0;
case LE_METHOD_FAN_OFF_SETTING:
return 0;
case LE_METHOD_VBATT:
return 12;
2015-07-10 06:01:56 -07:00
default:
2016-10-10 11:02:17 -07:00
firmwareError(OBD_PCM_Processor_Fault, "No mock value for %d", action);
2015-07-10 06:01:56 -07:00
return NAN;
}
}
static void testParsing(void) {
char buffer[64];
2019-01-14 12:37:05 -08:00
ASSERT_TRUE(strEqualCaseInsensitive("hello", "HELlo"));
ASSERT_FALSE(strEqualCaseInsensitive("hello", "HElo2"));
2015-07-10 06:01:56 -07:00
const char *ptr;
ptr = getNextToken(" hello ", buffer, sizeof(buffer));
2019-01-14 12:37:05 -08:00
ASSERT_TRUE(strEqual("hello", buffer));
2015-07-10 06:01:56 -07:00
ptr = getNextToken("hello", buffer, sizeof(buffer));
2019-01-14 12:37:05 -08:00
ASSERT_TRUE(strEqual("hello", buffer));
2015-07-10 06:01:56 -07:00
ptr = getNextToken(" hello world ", buffer, sizeof(buffer));
2019-01-14 12:37:05 -08:00
ASSERT_TRUE(strEqual("hello", buffer));
2015-07-10 06:01:56 -07:00
ptr = getNextToken(ptr, buffer, sizeof(buffer));
2019-01-14 12:37:05 -08:00
ASSERT_TRUE(strEqual("world", buffer));
2015-07-10 06:01:56 -07:00
2019-01-14 12:37:05 -08:00
ASSERT_TRUE(isNumeric("123"));
ASSERT_FALSE(isNumeric("a123"));
2015-07-10 06:01:56 -07:00
LEElement thepool[TEST_POOL_SIZE];
LEElementPool pool(thepool, TEST_POOL_SIZE);
LEElement *element;
element = pool.parseExpression("1 3 AND not");
2019-01-14 12:37:05 -08:00
ASSERT_TRUE(element != NULL);
2015-07-10 06:01:56 -07:00
2019-01-14 12:45:35 -08:00
ASSERT_EQ(element->action, LE_NUMERIC_VALUE);
ASSERT_EQ(element->fValue, 1.0);
2015-07-10 06:01:56 -07:00
element = element->next;
2019-01-14 12:45:35 -08:00
ASSERT_EQ(element->action, LE_NUMERIC_VALUE);
ASSERT_EQ(element->fValue, 3.0);
2015-07-10 06:01:56 -07:00
element = element->next;
2019-01-14 12:45:35 -08:00
ASSERT_EQ(element->action, LE_OPERATOR_AND);
2015-07-10 06:01:56 -07:00
element = element->next;
2019-01-14 12:45:35 -08:00
ASSERT_EQ(element->action, LE_OPERATOR_NOT);
2015-07-10 06:01:56 -07:00
element = element->next;
2019-01-14 12:37:05 -08:00
ASSERT_TRUE(element == NULL);
2015-07-10 06:01:56 -07:00
}
static void testExpression2(float selfValue, const char *line, float expected, Engine *engine) {
2015-07-10 06:01:56 -07:00
LEElement thepool[TEST_POOL_SIZE];
LEElementPool pool(thepool, TEST_POOL_SIZE);
LEElement * element = pool.parseExpression(line);
print("Parsing [%s]", line);
2019-01-14 15:56:32 -08:00
ASSERT_TRUE(element != NULL) << "Not NULL expected";
2015-07-10 06:01:56 -07:00
LECalculator c;
2017-06-04 12:25:37 -07:00
EXPAND_Engine;
ASSERT_EQ(expected, c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX)) << line;
}
2017-06-04 12:25:37 -07:00
static void testExpression2(float selfValue, const char *line, float expected) {
WITH_ENGINE_TEST_HELPER(FORD_INLINE_6_1995);
testExpression2(selfValue, line, expected, engine);
2016-03-02 19:02:37 -08:00
}
static void testExpression(const char *line, float expected) {
testExpression2(0, line, expected);
2015-07-10 06:01:56 -07:00
}
2019-01-14 12:31:56 -08:00
TEST(misc, testLogicExpressions) {
2015-07-10 06:01:56 -07:00
printf("*************************************************** testLogicExpressions\r\n");
testParsing();
{
2015-07-10 06:01:56 -07:00
2019-01-19 17:42:29 -08:00
WITH_ENGINE_TEST_HELPER(FORD_INLINE_6_1995);
2017-06-04 12:25:37 -07:00
2015-07-10 06:01:56 -07:00
LECalculator c;
LEElement value1;
value1.init(LE_NUMERIC_VALUE, 123.0);
c.add(&value1);
2017-06-04 12:25:37 -07:00
assertEqualsM("123", 123.0, c.getValue(0 PASS_ENGINE_PARAMETER_SUFFIX));
2015-07-10 06:01:56 -07:00
LEElement value2;
value2.init(LE_NUMERIC_VALUE, 321.0);
c.add(&value2);
LEElement value3;
value3.init(LE_OPERATOR_AND);
c.add(&value3);
2017-06-04 12:25:37 -07:00
assertEqualsM("123 and 321", 1.0, c.getValue(0 PASS_ENGINE_PARAMETER_SUFFIX));
2015-07-10 06:01:56 -07:00
/**
* fuel_pump = (time_since_boot < 4 seconds) OR (rpm > 0)
* fuel_pump = time_since_boot 4 less rpm 0 > OR
*/
c.reset();
LEElement thepool[TEST_POOL_SIZE];
LEElementPool pool(thepool, TEST_POOL_SIZE);
LEElement *e = pool.next();
e->init(LE_METHOD_TIME_SINCE_BOOT);
e = pool.next();
e->init(LE_NUMERIC_VALUE, 4);
e = pool.next();
e->init(LE_OPERATOR_LESS);
e = pool.next();
e->init(LE_METHOD_RPM);
e = pool.next();
e->init(LE_NUMERIC_VALUE, 0);
e = pool.next();
e->init(LE_OPERATOR_MORE);
e = pool.next();
e->init(LE_OPERATOR_OR);
pool.reset();
LEElement *element;
element = pool.parseExpression("fan no_such_method");
2019-01-14 15:56:32 -08:00
ASSERT_TRUE(element == NULL) << "NULL expected";
2015-07-10 06:01:56 -07:00
}
2015-07-10 06:01:56 -07:00
/**
* fan = (not fan && coolant > 90) OR (fan && coolant > 85)
* fan = fan NOT coolant 90 AND more fan coolant 85 more AND OR
*/
mockFan = 0;
{
WITH_ENGINE_TEST_HELPER(FORD_INLINE_6_1995);
engine->sensors.mockClt = 100;
engine->periodicSlowCallback(PASS_ENGINE_PARAMETER_SIGNATURE);
testExpression2(0, "coolant 1 +", 101, engine);
}
2015-07-10 06:01:56 -07:00
testExpression("fan", 0);
testExpression("fan not", 1);
testExpression("coolant 90 >", 1);
testExpression("fan not coolant 90 > and", 1);
testExpression("100 200 1 if", 200);
testExpression("10 99 max", 99);
2016-03-02 19:02:37 -08:00
testExpression2(123, "10 self max", 123);
2015-07-10 06:01:56 -07:00
testExpression("fan NOT coolant 90 > AND fan coolant 85 > AND OR", 1);
{
WITH_ENGINE_TEST_HELPER(FORD_INLINE_6_1995);
2015-07-10 06:01:56 -07:00
LEElement thepool[TEST_POOL_SIZE];
LEElementPool pool(thepool, TEST_POOL_SIZE);
LEElement * element = pool.parseExpression("fan NOT coolant 90 > AND fan coolant 85 > AND OR");
2019-01-14 15:56:32 -08:00
ASSERT_TRUE(element != NULL) << "Not NULL expected";
2015-07-10 06:01:56 -07:00
LECalculator c;
2019-01-14 15:38:20 -08:00
ASSERT_EQ( 1, c.getValue2(0, element PASS_ENGINE_PARAMETER_SUFFIX)) << "that expression";
2015-07-10 06:01:56 -07:00
2019-01-14 12:45:35 -08:00
ASSERT_EQ(12, c.currentCalculationLogPosition);
ASSERT_EQ(102, c.calcLogAction[0]);
ASSERT_EQ(0, c.calcLogValue[0]);
2015-07-10 06:01:56 -07:00
}
testExpression("fan_off_setting", 0);
testExpression("coolant fan_off_setting >", 1);
testExpression("0 1 &", 0);
testExpression("0 1 |", 1);
testExpression("0 1 >", 0);
testExpression(FAN_CONTROL_LOGIC, 1);
2017-06-25 22:49:11 -07:00
{
mockRpm = 900;
mockCrankingRpm = 200;
testExpression("rpm", 900);
testExpression("cranking_rpm", 200);
testExpression(STARTER_BLOCK, 0);
testExpression("rpm cranking_rpm > ", 1);
}
2015-07-10 06:01:56 -07:00
mockRpm = 900;
2017-06-25 22:49:11 -07:00
testExpression("fan NOT coolant 90 > AND fan coolant 85 > AND OR", 1);
2015-07-10 06:01:56 -07:00
}