From 1194dc24044759cde68eab352261ad276a46ce27 Mon Sep 17 00:00:00 2001 From: kifir23917 <159958110+kifir23917@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:48:39 +0200 Subject: [PATCH] only:add tests for `setBitRangeMsb` and `getBitRangeMsb` Lua functions (#7067) * only:add tests for `setBitRangeMsb` and `getBitRangeMsb` Lua functions * fix `setBitRangeMsb` function --- firmware/controllers/lua/lua_lib.h | 2 +- .../tests/lua/test_lua_bit_range_msb.cpp | 52 +++++++++++++++++++ unit_tests/tests/tests.mk | 1 + 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 unit_tests/tests/lua/test_lua_bit_range_msb.cpp diff --git a/firmware/controllers/lua/lua_lib.h b/firmware/controllers/lua/lua_lib.h index 6d7a43cc1c..ceec16cbb4 100644 --- a/firmware/controllers/lua/lua_lib.h +++ b/firmware/controllers/lua/lua_lib.h @@ -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 \ diff --git a/unit_tests/tests/lua/test_lua_bit_range_msb.cpp b/unit_tests/tests/lua/test_lua_bit_range_msb.cpp new file mode 100644 index 0000000000..025ecbee07 --- /dev/null +++ b/unit_tests/tests/lua/test_lua_bit_range_msb.cpp @@ -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); + } +} \ No newline at end of file diff --git a/unit_tests/tests/tests.mk b/unit_tests/tests/tests.mk index 852f2c1ef6..596feb7a82 100644 --- a/unit_tests/tests/tests.mk +++ b/unit_tests/tests/tests.mk @@ -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 \