2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
2021-04-28 19:41:25 -07:00
|
|
|
#include "rusefi_lua.h"
|
|
|
|
|
2021-11-22 11:19:19 -08:00
|
|
|
|
|
|
|
TEST(LuaHooks, TestCrc8) {
|
2021-11-22 16:16:57 -08:00
|
|
|
const char* realHDdata = R"(
|
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
return crc8_j1850({0x13, 0x57, 0x13, 0x45, 0x00, 0xe8, 0x5c }, 7)
|
|
|
|
end
|
|
|
|
|
|
|
|
)";
|
|
|
|
EXPECT_EQ(testLuaReturnsNumberOrNil(realHDdata).value_or(0), 0x86);
|
|
|
|
|
|
|
|
const char* crc8scripts = R"(
|
2021-11-22 11:19:19 -08:00
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
return crc8_j1850({0x31,0x32,0x32 }, 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
)";
|
|
|
|
EXPECT_EQ(testLuaReturnsNumberOrNil(crc8scripts).value_or(0), 0x43);
|
|
|
|
}
|
|
|
|
|
2021-12-14 20:16:05 -08:00
|
|
|
TEST(LuaHooks, TestGetCalibration) {
|
2021-12-15 06:23:06 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2021-12-15 05:56:59 -08:00
|
|
|
const char* sourceCode = R"(
|
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
return getCalibration("cranking.rpm")
|
|
|
|
end
|
|
|
|
|
|
|
|
)";
|
2021-12-15 06:23:06 -08:00
|
|
|
EXPECT_EQ(testLuaReturnsNumber(sourceCode), 550);
|
2021-12-14 20:16:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LuaHooks, TestSetCalibration) {
|
2021-12-15 06:23:06 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
|
|
|
const char* sourceCode = R"(
|
|
|
|
|
|
|
|
function testFunc()
|
|
|
|
setCalibration("cranking.rpm", 900, false)
|
|
|
|
return getCalibration("cranking.rpm")
|
|
|
|
end
|
|
|
|
|
|
|
|
)";
|
|
|
|
EXPECT_EQ(testLuaReturnsNumber(sourceCode), 900);
|
2021-12-14 20:16:05 -08:00
|
|
|
}
|
|
|
|
|
2021-04-28 19:41:25 -07:00
|
|
|
|
2021-12-15 06:23:06 -08:00
|
|
|
TEST(LuaHooks, TestGetSensorByIndex) {
|
|
|
|
const char* getSensorTestByIndex = R"(
|
2021-04-28 19:41:25 -07:00
|
|
|
|
2021-12-15 06:23:06 -08:00
|
|
|
function testFunc()
|
|
|
|
return getSensorByIndex(10)
|
|
|
|
end
|
|
|
|
|
|
|
|
)";
|
2021-04-28 19:41:25 -07:00
|
|
|
|
|
|
|
// Test failed sensor, returns nil
|
|
|
|
Sensor::resetMockValue(static_cast<SensorType>(10));
|
2021-10-21 11:33:59 -07:00
|
|
|
EXPECT_EQ(testLuaReturnsNumberOrNil(getSensorTestByIndex), unexpected);
|
2021-04-28 19:41:25 -07:00
|
|
|
|
|
|
|
// Now test with a value, returns value
|
|
|
|
Sensor::setMockValue(10, 33);
|
2021-10-21 11:33:59 -07:00
|
|
|
EXPECT_EQ(testLuaReturnsNumberOrNil(getSensorTestByIndex).value_or(0), 33);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-15 06:23:06 -08:00
|
|
|
TEST(LuaHooks, TestGetSensorByName) {
|
|
|
|
const char* getSensorTestByName = R"(
|
2021-10-21 11:33:59 -07:00
|
|
|
|
2021-12-15 06:23:06 -08:00
|
|
|
function testFunc()
|
|
|
|
return getSensor("CLT")
|
|
|
|
end
|
|
|
|
|
|
|
|
)";
|
2021-10-21 11:33:59 -07:00
|
|
|
|
|
|
|
// Test failed sensor, returns nil
|
|
|
|
Sensor::resetMockValue(SensorType::Clt);
|
|
|
|
EXPECT_EQ(testLuaReturnsNumberOrNil(getSensorTestByName), unexpected);
|
|
|
|
|
|
|
|
// Now test with a value, returns value
|
|
|
|
Sensor::setMockValue((int)SensorType::Clt, 33);
|
|
|
|
EXPECT_EQ(testLuaReturnsNumberOrNil(getSensorTestByName).value_or(0), 33);
|
2021-04-28 19:41:25 -07:00
|
|
|
}
|
2021-04-29 19:22:04 -07:00
|
|
|
|
|
|
|
TEST(LuaHooks, Table3d) {
|
2021-12-15 06:23:06 -08:00
|
|
|
const char* tableTest = R"(
|
|
|
|
function testFunc()
|
|
|
|
return table3d(2, 1000, 40)
|
|
|
|
end
|
|
|
|
)";
|
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
EngineTestHelper eth(TEST_ENGINE);
|
2021-04-29 19:22:04 -07:00
|
|
|
|
2021-11-14 07:39:47 -08:00
|
|
|
setTable(config->scriptTable2, (uint8_t)33);
|
2021-04-29 19:22:04 -07:00
|
|
|
EXPECT_EQ(testLuaReturnsNumber(tableTest), 33);
|
|
|
|
|
2021-11-14 07:39:47 -08:00
|
|
|
setTable(config->scriptTable2, (uint8_t)14);
|
2021-04-29 19:22:04 -07:00
|
|
|
EXPECT_EQ(testLuaReturnsNumber(tableTest), 14);
|
|
|
|
}
|
2021-07-16 11:06:26 -07:00
|
|
|
|
|
|
|
TEST(LuaHooks, CanTxChannel) {
|
|
|
|
// channel 1 valid
|
|
|
|
EXPECT_NO_THROW(testLuaExecString("txCan(1, 0, 0, {0})"));
|
|
|
|
|
|
|
|
// channel 0 invalid
|
|
|
|
EXPECT_ANY_THROW(testLuaExecString("txCan(0, 0, 0, {0})"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LuaHooks, CanTxId) {
|
|
|
|
// std ID ok
|
|
|
|
EXPECT_NO_THROW(testLuaExecString("txCan(1, 2047, 0, {0})"));
|
|
|
|
// std ID too high
|
|
|
|
EXPECT_ANY_THROW(testLuaExecString("txCan(1, 2048, 0, {0})"));
|
|
|
|
|
|
|
|
// ext ID ok
|
|
|
|
EXPECT_NO_THROW(testLuaExecString("txCan(1, 536870911, 1, {0})"));
|
|
|
|
// ext ID too high
|
|
|
|
EXPECT_ANY_THROW(testLuaExecString("txCan(1, 536870912, 1, {0})"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LuaHooks, CanTxDataLength) {
|
|
|
|
// valid: DLC = 0
|
|
|
|
EXPECT_NO_THROW(testLuaExecString("txCan(1, 0, 0, {})"));
|
|
|
|
// valid: DLC = 1
|
|
|
|
EXPECT_NO_THROW(testLuaExecString("txCan(1, 0, 0, {0})"));
|
|
|
|
// valid: DLC = 8
|
|
|
|
EXPECT_NO_THROW(testLuaExecString("txCan(1, 0, 0, {1, 2, 3, 4, 5, 6, 7, 8})"));
|
|
|
|
|
|
|
|
// invalid: DLC = 9
|
|
|
|
EXPECT_ANY_THROW(testLuaExecString("txCan(1, 0, 0, {1, 2, 3, 4, 5, 6, 7, 8, 9})"));
|
|
|
|
|
|
|
|
// invalid: is a table, but contains a not-number
|
|
|
|
EXPECT_ANY_THROW(testLuaExecString("txCan(1, 0, 0, {1, 2, 3, 'hello', 5, 6})"));
|
|
|
|
|
|
|
|
// invalid: not a table
|
|
|
|
EXPECT_ANY_THROW(testLuaExecString("txCan(1, 0, 0, 26)"));
|
|
|
|
}
|
2021-09-03 17:21:39 -07:00
|
|
|
|
2021-11-02 20:35:48 -07:00
|
|
|
TEST(LuaHooks, LuaInterpolate) {
|
2021-12-15 06:23:06 -08:00
|
|
|
EXPECT_EQ(testLuaReturnsNumber(R"(
|
|
|
|
function testFunc()
|
|
|
|
return interpolate(1, 10, 5, 50, 3)
|
|
|
|
end
|
|
|
|
)"), 30);
|
2021-11-02 20:35:48 -07:00
|
|
|
}
|
|
|
|
|
2021-09-03 17:21:39 -07:00
|
|
|
TEST(LuaHooks, TestLuaTimer) {
|
2021-12-15 06:23:06 -08:00
|
|
|
EXPECT_EQ(testLuaReturnsNumber(R"(
|
|
|
|
function testFunc()
|
|
|
|
local a = Timer.new()
|
|
|
|
a:reset()
|
|
|
|
return a:getElapsedSeconds()
|
|
|
|
end
|
|
|
|
)"), 0);
|
2021-09-03 17:21:39 -07:00
|
|
|
}
|
2021-10-22 13:36:29 -07:00
|
|
|
|
|
|
|
static const char* sensorTest = R"(
|
|
|
|
function testFunc()
|
|
|
|
local sens = Sensor.new("CLT")
|
|
|
|
|
|
|
|
-- Check valid sensor
|
|
|
|
sens:set(33)
|
|
|
|
if getSensor("CLT") ~= 33 then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check invalidation
|
|
|
|
sens:invalidate()
|
|
|
|
if getSensor("CLT") then
|
|
|
|
return 2
|
|
|
|
end
|
|
|
|
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
)";
|
|
|
|
|
|
|
|
TEST(LuaHooks, LuaSensor) {
|
|
|
|
EXPECT_EQ(testLuaReturnsNumber(sensorTest), 0);
|
|
|
|
|
|
|
|
// Ensure that the sensor got unregistered on teardown of the Lua interpreter
|
|
|
|
EXPECT_FALSE(Sensor::hasSensor(SensorType::Clt));
|
|
|
|
}
|
2021-11-07 06:53:40 -08:00
|
|
|
|
|
|
|
static const char* pidTest = R"(
|
|
|
|
function testFunc()
|
2021-11-13 00:18:16 -08:00
|
|
|
local pid = Pid.new(0.5, 0, 0, -10, 10)
|
2021-11-07 06:53:40 -08:00
|
|
|
|
|
|
|
-- delta is -4, output -2
|
2021-11-13 00:18:16 -08:00
|
|
|
if pid:get(3, 7) ~= -2 then
|
2021-11-07 06:53:40 -08:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- delta is 6, output 3
|
2021-11-13 00:18:16 -08:00
|
|
|
if pid:get(4, -2) ~= 3 then
|
2021-11-07 06:53:40 -08:00
|
|
|
return 2
|
|
|
|
end
|
|
|
|
|
|
|
|
-- test clamping
|
2021-11-13 00:18:16 -08:00
|
|
|
if pid:get(0, 100) ~= -10 then
|
2021-11-07 06:53:40 -08:00
|
|
|
return 3
|
|
|
|
end
|
|
|
|
|
2021-11-13 00:18:16 -08:00
|
|
|
if pid:get(0, -100) ~= 10 then
|
2021-11-07 06:53:40 -08:00
|
|
|
return 4
|
|
|
|
end
|
|
|
|
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
)";
|
|
|
|
|
|
|
|
TEST(LuaHooks, LuaPid) {
|
|
|
|
EXPECT_EQ(testLuaReturnsNumber(pidTest), 0);
|
|
|
|
}
|