rusefi/unit_tests/test_logic_expression.cpp

179 lines
4.0 KiB
C++
Raw Normal View History

2014-10-03 09:03:02 -07:00
/**
* @file test_logic_expression.cpp
*
* https://sourceforge.net/p/rusefi/tickets/102/
*
* @date Oct 3, 2014
* @author Andrey Belomutskiy, (c) 2012-2014
*/
#include "main.h"
#include "test_logic_expression.h"
2014-10-03 12:05:03 -07:00
#include "logic_expression.h"
2014-10-09 00:02:51 -07:00
#include "le_functions.h"
2014-10-04 08:03:38 -07:00
#include "cli_registry.h"
2014-10-05 07:03:00 -07:00
#include "engine.h"
2014-11-17 13:03:20 -08:00
#define TEST_POOL_SIZE 256
2014-10-05 07:03:00 -07:00
static float mockCoolant;
static float mockFan;
2014-10-09 00:02:51 -07:00
static float mockRpm;
static float mockTimeSinceBoot;
2014-10-05 07:03:00 -07:00
float getLEValue(Engine *engine, le_action_e action) {
switch(action) {
case LE_METHOD_FAN:
return mockFan;
case LE_METHOD_COOLANT:
return mockCoolant;
2014-10-09 00:02:51 -07:00
case LE_METHOD_RPM:
return mockRpm;
case LE_METHOD_TIME_SINCE_BOOT:
return mockTimeSinceBoot;
2014-10-05 07:03:00 -07:00
default:
2014-10-09 00:02:51 -07:00
firmwareError("No mock value for %d", action);
2014-10-06 05:03:03 -07:00
return NAN;
2014-10-05 07:03:00 -07:00
}
}
2014-10-03 09:03:02 -07:00
2014-10-04 11:02:53 -07:00
static void testParsing(void) {
2014-10-04 08:03:38 -07:00
char buffer[64];
2014-10-04 11:02:53 -07:00
assertTrue(strEqualCaseInsensitive("hello", "HELlo"));
assertFalse(strEqualCaseInsensitive("hello", "HElo2"));
2014-10-04 08:03:38 -07:00
const char *ptr;
2014-11-17 12:03:37 -08:00
ptr = getNextToken(" hello ", buffer);
2014-10-04 08:03:38 -07:00
assertTrue(strEqual("hello", buffer));
2014-11-17 12:03:37 -08:00
ptr = getNextToken("hello", buffer);
2014-10-04 08:03:38 -07:00
assertTrue(strEqual("hello", buffer));
2014-11-17 12:03:37 -08:00
ptr = getNextToken(" hello world ", buffer);
2014-10-04 08:03:38 -07:00
assertTrue(strEqual("hello", buffer));
2014-11-17 12:03:37 -08:00
ptr = getNextToken(ptr, buffer);
2014-10-04 08:03:38 -07:00
assertTrue(strEqual("world", buffer));
2014-10-04 11:02:53 -07:00
assertTrue(isNumeric("123"));
assertFalse(isNumeric("a123"));
2014-11-17 13:03:20 -08:00
LEElement thepool[TEST_POOL_SIZE];
LEElementPool pool(thepool, TEST_POOL_SIZE);
2014-10-04 11:02:53 -07:00
LEElement *element;
2014-11-18 13:03:12 -08:00
element = pool.parseExpression("1 3 AND not");
2014-10-04 11:02:53 -07:00
assertTrue(element != NULL);
assertEquals(element->action, LE_NUMERIC_VALUE);
assertEquals(element->fValue, 1.0);
element = element->next;
assertEquals(element->action, LE_NUMERIC_VALUE);
assertEquals(element->fValue, 3.0);
element = element->next;
assertEquals(element->action, LE_OPERATOR_AND);
2014-10-05 07:03:00 -07:00
element = element->next;
assertEquals(element->action, LE_OPERATOR_NOT);
2014-10-04 11:02:53 -07:00
element = element->next;
assertTrue(element == NULL);
}
2014-10-05 07:03:00 -07:00
static void testExpression(const char *line, float expected) {
2014-11-17 13:03:20 -08:00
LEElement thepool[TEST_POOL_SIZE];
LEElementPool pool(thepool, TEST_POOL_SIZE);
2014-10-05 07:03:00 -07:00
pool.reset();
2014-11-18 13:03:12 -08:00
LEElement * element = pool.parseExpression(line);
2014-10-09 00:02:51 -07:00
print("Parsing [%s]", line);
2014-10-05 07:03:00 -07:00
assertTrueM("Not NULL expected", element != NULL);
LECalculator c;
c.add(element);
2014-10-06 05:03:03 -07:00
assertEqualsM(line, expected, c.getValue(NULL));
2014-10-05 07:03:00 -07:00
}
2014-10-04 11:02:53 -07:00
void testLogicExpressions(void) {
printf("*************************************************** testLogicExpressions\r\n");
testParsing();
2014-10-03 12:05:03 -07:00
LECalculator c;
LEElement value1;
value1.init(LE_NUMERIC_VALUE, 123.0);
c.add(&value1);
2014-10-06 05:03:03 -07:00
assertEqualsM("123", 123.0, c.getValue(NULL));
2014-10-03 12:05:03 -07:00
LEElement value2;
value2.init(LE_NUMERIC_VALUE, 321.0);
c.add(&value2);
LEElement value3;
value3.init(LE_OPERATOR_AND);
c.add(&value3);
2014-10-06 05:03:03 -07:00
assertEqualsM("123 and 321", 1.0, c.getValue(NULL));
2014-10-03 12:05:03 -07:00
2014-10-03 09:03:02 -07:00
/**
* fuel_pump = (time_since_boot < 4 seconds) OR (rpm > 0)
2014-10-06 02:03:01 -07:00
* fuel_pump = time_since_boot 4 less rpm 0 > OR
2014-10-03 09:03:02 -07:00
*/
2014-10-03 15:03:01 -07:00
c.reset();
2014-11-17 13:03:20 -08:00
LEElement thepool[TEST_POOL_SIZE];
LEElementPool pool(thepool, TEST_POOL_SIZE);
2014-10-03 15:03:01 -07:00
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);
2014-10-05 07:03:00 -07:00
pool.reset();
LEElement *element;
2014-11-18 13:03:12 -08:00
element = pool.parseExpression("fan no_such_method");
2014-10-05 07:03:00 -07:00
assertTrueM("NULL expected", element == NULL);
2014-10-03 09:03:02 -07:00
/**
* fan = (not fan && coolant > 90) OR (fan && coolant > 85)
* fan = fan NOT coolant 90 AND more fan coolant 85 more AND OR
*/
2014-10-05 07:03:00 -07:00
mockFan = 0;
mockCoolant = 100;
testExpression("coolant", 100);
testExpression("fan", 0);
testExpression("fan not", 1);
testExpression("coolant 90 >", 1);
testExpression("fan not coolant 90 > and", 1);
2014-12-04 18:03:12 -08:00
testExpression("100 200 1 if", 200);
2014-12-04 17:03:09 -08:00
testExpression("10 99 max", 99);
2014-10-05 07:03:00 -07:00
testExpression("fan NOT coolant 90 > AND fan coolant 85 > AND OR", 1);
2014-10-09 00:02:51 -07:00
mockRpm = 900;
testExpression(FUEL_PUMP_LOGIC, 1);
2014-10-03 09:03:02 -07:00
}