rusefi/unit_tests/tests/lua/test_lua_vag.cpp

199 lines
4.7 KiB
C++
Raw Normal View History

2022-06-22 19:22:49 -07:00
#include "pch.h"
#include "rusefi_lua.h"
2022-06-23 13:02:09 -07:00
#include "lua_lib.h"
2022-06-22 19:22:49 -07:00
2022-08-30 01:07:48 -07:00
#define VAG_CHECKSUM8 "function xorChecksum8(data) \
2022-06-22 19:22:49 -07:00
return data[1] ~ data[2] ~ data[3] ~ data[4] ~ data[5] ~ data[6] ~ data[7] \
end"
TEST(LuaVag, Checksum) {
2022-08-30 01:07:48 -07:00
const char* realdata = VAG_CHECKSUM8 R"(
2022-06-22 19:22:49 -07:00
function testFunc()
data = { 0xE0, 0x20, 0x20, 0x7E, 0xFE, 0xFF, 0xFF, 0x60 }
2022-08-30 01:07:48 -07:00
return xorChecksum8(data)
2022-06-22 19:22:49 -07:00
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x60);
}
2022-06-23 13:02:09 -07:00
// LSB (Least Significant Byte comes first) "Intel"
2022-06-23 13:02:09 -07:00
TEST(LuaVag, packMotor1) {
2022-08-25 21:04:47 -07:00
const char* realdata = PRINT_ARRAY ARRAY_EQUALS SET_TWO_BYTES R"(
2022-06-23 13:02:09 -07:00
function testFunc()
2022-08-25 21:04:47 -07:00
engineTorque = 15.21
2022-06-23 13:02:09 -07:00
rpm = 1207.1
2022-08-25 21:04:47 -07:00
innerTorqWithoutExt = 21.6
tps = 31.6
2022-08-25 21:21:17 -07:00
torqueLoss = 9.75
2022-08-25 21:04:47 -07:00
requestedTorque = 21.84
2022-06-23 13:02:09 -07:00
data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
2022-08-25 21:04:47 -07:00
data[2] = engineTorque / 0.39
setTwoBytes(data, 2, rpm / 0.25)
data[5] = innerTorqWithoutExt / 0.4
2022-08-25 21:21:17 -07:00
data[6] = tps / 0.4
2022-08-25 21:24:38 -07:00
data[7] = torqueLoss / 0.39
2022-08-26 09:07:13 -07:00
data[8] = requestedTorque / 0.39
2022-08-25 21:04:47 -07:00
2022-08-25 21:21:17 -07:00
print(arrayToString(data))
2022-08-25 21:04:47 -07:00
2022-08-26 09:07:13 -07:00
expected = { 0x00, 0x27, 0xDC, 0x12, 0x36, 0x4F, 0x19, 0x38 }
2022-06-23 13:02:09 -07:00
return equals(data, expected)
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0);
}
2022-08-25 21:04:47 -07:00
#define realMotor1Packet "\ndata = { 0x00, 0x27, 0x8A, 0x1A, 0x36, 0x4F, 0x19, 0x38}\n "
2022-08-25 08:15:54 -07:00
2022-08-25 09:45:47 -07:00
TEST(LuaVag, unpackMotor1_engine_torq) {
2022-08-27 17:44:30 -07:00
const char* script = GET_BIT_RANGE_LSB realMotor1Packet R"(
2022-08-25 09:45:47 -07:00
function testFunc()
engineTorque = getBitRange(data, 8, 8) * 0.39
return engineTorque
end
)";
2022-08-27 17:44:30 -07:00
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(script).value_or(0), 15.21);
2022-08-25 09:45:47 -07:00
}
2022-08-25 08:15:54 -07:00
TEST(LuaVag, unpackMotor1_rpm) {
const char* realdata = GET_BIT_RANGE_LSB realMotor1Packet R"(
function testFunc()
rpm = getBitRange(data, 16, 16) * 0.25
return rpm
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 1698.5);
}
2022-08-25 16:26:18 -07:00
TEST(LuaVag, unpackMotor1_inner_torq) {
const char* realdata = GET_BIT_RANGE_LSB realMotor1Packet R"(
function testFunc()
innerTorqWithoutExt = getBitRange(data, 32, 8) * 0.4
return innerTorqWithoutExt
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 21.6);
}
2022-08-25 08:15:54 -07:00
TEST(LuaVag, unpackMotor1_tps) {
const char* realdata = GET_BIT_RANGE_LSB realMotor1Packet R"(
function testFunc()
tps = getBitRange(data, 40, 8) * 0.4
return tps
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 31.6);
}
2022-08-25 09:45:47 -07:00
TEST(LuaVag, unpackMotor1_torq_loss) {
const char* realdata = GET_BIT_RANGE_LSB realMotor1Packet R"(
function testFunc()
torqueLoss = getBitRange(data, 48, 8) * 0.39
return torqueLoss
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 9.75);
}
TEST(LuaVag, unpackMotor1_torq_req) {
const char* realdata = GET_BIT_RANGE_LSB realMotor1Packet R"(
function testFunc()
requestedTorque = getBitRange(data, 56, 8) * 0.39
return requestedTorque
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 21.84);
}
2022-08-27 17:44:30 -07:00
#define realMotor3Packet "\ndata = { 0x00, 0x62, 0xFA, 0x00, 0x22, 0x00, 0x00, 0xFA}\n "
TEST(LuaVag, packMotor3) {
const char* script = PRINT_ARRAY ARRAY_EQUALS SET_TWO_BYTES R"(
function testFunc()
tps = 100
iat = 25.5
data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
data[2] = (iat + 48) / 0.75
data[3] = tps / 0.4
2022-06-29 01:56:34 -07:00
data[5] = 0x22
2022-08-27 17:44:30 -07:00
data[8] = tps / 0.4
print(arrayToString(data))
2022-06-29 01:56:34 -07:00
expected = { 0x00, 0x62, 0xFA, 0x00, 0x22, 0x00, 0x00, 0xFA }
2022-08-27 17:44:30 -07:00
return equals(data, expected)
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(script).value_or(0), 0);
}
TEST(LuaVag, unpackMotor3_tps) {
const char* script = GET_BIT_RANGE_LSB realMotor3Packet R"(
function testFunc()
tps = getBitRange(data, 56, 8) * 0.40
return tps
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(script).value_or(0), 100);
}
TEST(LuaVag, unpackMotor3_pps) {
const char* script = GET_BIT_RANGE_LSB realMotor3Packet R"(
function testFunc()
pps = getBitRange(data, 16, 8) * 0.40
return pps
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(script).value_or(0), 100);
}
TEST(LuaVag, unpackMotor3_iat) {
const char* script = GET_BIT_RANGE_LSB realMotor3Packet R"(
function testFunc()
iat = getBitRange(data, 8, 8) * 0.75 - 48
return iat
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(script).value_or(0), 25.5);
}
2022-08-30 01:07:48 -07:00
#define realMotor6Packet "\ndata = { 0x3D, 0x54, 0x69, 0x7E, 0xFE, 0xFF, 0xFF, 0x80}\n "
2022-08-27 17:44:30 -07:00
2022-08-30 01:07:48 -07:00
#define VAG_CHECKSUM1 "function xorChecksum1(data) \
return data[8] ~ data[2] ~ data[3] ~ data[4] ~ data[5] ~ data[6] ~ data[7] \
end"
TEST(LuaVag, ChecksumMotor6) {
const char* realdata = VAG_CHECKSUM1 realMotor6Packet R"(
function testFunc()
return xorChecksum1(data)
end
)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0x3D);
}