only:add tests for `setBitRangeMsb` and `getBitRangeMsb` Lua functions (#7067)

* only:add tests for `setBitRangeMsb` and `getBitRangeMsb` Lua functions

* fix `setBitRangeMsb` function
This commit is contained in:
kifir23917 2024-11-12 16:48:39 +02:00 committed by GitHub
parent 69ab92595e
commit 1194dc2404
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 1 deletions

View File

@ -148,7 +148,7 @@ function setBitRangeMsb(data, totalBitIndex, bitWidth, value) \
local bitInByteIndex = totalBitIndex - byteIndex * 8 \
if (bitInByteIndex + bitWidth > 8) then \
local bitsToHandleNow = 8 - bitInByteIndex \
setBitRangeMsb(data, totalBitIndex - bitsToHandleNow, bitWidth - bitsToHandleNow, value >> bitsToHandleNow) \
setBitRangeMsb(data, (byteIndex - 1) * 8, bitWidth - bitsToHandleNow, value >> bitsToHandleNow) \
bitWidth = bitsToHandleNow \
end \
local mask = (1 << bitWidth) - 1 \

View File

@ -0,0 +1,52 @@
//
// Created by kifir on 11/12/24.
//
#include "pch.h"
#include "rusefi_lua.h"
#include "lua_lib.h"
namespace {
class LuaBitRangeMsbTest : public testing::Test {
protected:
void checkSetAndGet(int bitStart, int length, int testValue);
};
void LuaBitRangeMsbTest::checkSetAndGet(const int bitStart, const int length, const int testValue) {
const char* const luaCodeFormatString = SET_BIT_RANGE_MSB GET_BIT_RANGE_MSB R"(
function testFunc()
data = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
setBitRangeMsb(data, %d, %d, %d)
return getBitRangeMsb(data, %d, %d)
end)";
char luaCode[4096];
std::snprintf(luaCode, sizeof(luaCode), luaCodeFormatString, bitStart, length, testValue, bitStart, length);
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(luaCode).value_or(0), testValue)
<< "bitStart=" << bitStart << ", length=" << length << ", testValue=" << testValue << std::endl << luaCode;
}
TEST_F(LuaBitRangeMsbTest, checkVariousBitRanges) {
checkSetAndGet(24, 4, 0xB);
checkSetAndGet(24, 7, 0x59);
checkSetAndGet(24, 8, 0xB3);
checkSetAndGet(24, 10, 0x2C9);
checkSetAndGet(24, 9, 0x173);
checkSetAndGet(22, 8, 0xB3);
checkSetAndGet(22, 9, 0x167);
checkSetAndGet(23, 8, 0xB3);
}
TEST_F(LuaBitRangeMsbTest, checkWithTotalBitIndex22AndBitWidth8) {
const char* realdata = SET_BIT_RANGE_MSB GET_BIT_RANGE_MSB R"(
function testFunc()
data = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
setBitRangeMsb(data, 22, 8, 0xB3)
return getBitRangeMsb(data, 22, 8)
end)";
EXPECT_NEAR_M3(testLuaReturnsNumberOrNil(realdata).value_or(0), 0xB3);
}
}

View File

@ -74,6 +74,7 @@ TESTS_SRC_CPP = \
tests/lua/test_bit_range_msb.cpp \
tests/lua/test_lua_dbc.cpp \
tests/lua/test_motorola_dbc.cpp \
tests/lua/test_lua_bit_range_msb.cpp \
tests/lua/test_lookup.cpp \
tests/lua/test_lua_e38.cpp \
tests/lua/test_lua_canam.cpp \