Implement setBitRangeMoto method (#7056)
* only:enhance tests a little bit * implement `setBitRangeMsb` method * implement `setBitRangeMoto` method
This commit is contained in:
parent
939ba6d170
commit
c3d2cfc33c
|
@ -232,6 +232,22 @@ int getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth) {
|
|||
return getBitRangeCommon(data, bitIndex, bitWidth, -1);
|
||||
}
|
||||
|
||||
void setBitRangeMsb(uint8_t data[], const int totalBitIndex, const int bitWidth, const int value) {
|
||||
int leftBitWidh = bitWidth;
|
||||
const int byteIndex = totalBitIndex >> 3;
|
||||
const int bitInByteIndex = totalBitIndex - byteIndex * 8;
|
||||
if (bitInByteIndex + leftBitWidh > 8) {
|
||||
const int bitsToHandleNow = 8 - bitInByteIndex;
|
||||
setBitRangeMsb(data, totalBitIndex - bitsToHandleNow, leftBitWidh - bitsToHandleNow, value >> bitsToHandleNow);
|
||||
leftBitWidh = bitsToHandleNow;
|
||||
}
|
||||
const int mask = (1 << leftBitWidh) - 1;
|
||||
data[byteIndex] = data[byteIndex] & (~(mask << bitInByteIndex));
|
||||
const int maskedValue = value & mask;
|
||||
const int shiftedValue = maskedValue << bitInByteIndex;
|
||||
data[byteIndex] = data[byteIndex] | shiftedValue;
|
||||
}
|
||||
|
||||
int motorolaMagicFromDbc(int b, int length) {
|
||||
// https://github.com/ebroecker/canmatrix/wiki/signal-Byteorder
|
||||
// convert from lsb0 bit numbering to msb0 bit numbering (or msb0 to lsb0)
|
||||
|
@ -244,6 +260,11 @@ int motorolaMagicFromDbc(int b, int length) {
|
|||
}
|
||||
|
||||
int getBitRangeMoto(const uint8_t data[], int bitIndex, int bitWidth) {
|
||||
int b = motorolaMagicFromDbc(bitIndex, bitWidth);
|
||||
const int b = motorolaMagicFromDbc(bitIndex, bitWidth);
|
||||
return getBitRangeMsb(data, b, bitWidth);
|
||||
}
|
||||
|
||||
void setBitRangeMoto(uint8_t data[], const int totalBitIndex, const int bitWidth, const int value) {
|
||||
const int b = motorolaMagicFromDbc(totalBitIndex, bitWidth);
|
||||
return setBitRangeMsb(data, b, bitWidth, value);
|
||||
}
|
|
@ -134,6 +134,8 @@ int getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth);
|
|||
for instance DBC 8|16@0
|
||||
*/
|
||||
int getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth);
|
||||
void setBitRangeMsb(uint8_t data[], int totalBitIndex, int bitWidth, int value);
|
||||
|
||||
int motorolaMagicFromDbc(int b, int length);
|
||||
int getBitRangeMoto(const uint8_t data[], int bitIndex, int bitWidth);
|
||||
void setBitRangeMoto(uint8_t data[], int totalBitIndex, int bitWidth, int value);
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
//
|
||||
// Created by kifir on 11/8/24.
|
||||
//
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
#include "lua_lib.h"
|
||||
|
||||
// inspired by TEST(LuaE65, offtopicTestGetBitRangeMsb) from test_lua_e65.cpp
|
||||
TEST(BitRangeMsbTest, offtopicTestGetBitRangeMsb) {
|
||||
// 1001 1111 0100 0001 0011 0010 0010 0000 0010 0011 0011 0000 1111 1111 0100 0011
|
||||
// ^------------^
|
||||
uint8_t data[] = { 0x9F, 0x41, 0x32, 0x20, 0x23, 0x30, 0xFF, 0x43 };
|
||||
|
||||
EXPECT_EQ(getBitRangeMsb(data, 12, 12), 0x9F4); // 1001 1111 0100
|
||||
}
|
||||
|
||||
// inspired by TEST(LuaE65, offtopicTestGetBitRangeMsb2) from test_lua_e65.cpp
|
||||
TEST(BitRangeMsbTest, offtopicTestGetBitRangeMsb2) {
|
||||
// 0111 0000 0000 0100 0001 1111
|
||||
// ^-----------------^
|
||||
uint8_t data[] = { 0x70, 0x04, 0x1F};
|
||||
EXPECT_EQ(getBitRangeMsb(data, 16, 16), 0x41F); // 0111 0000 0000 0100
|
||||
}
|
||||
|
||||
// inspired by TEST(LuaE65, offtopicTestSetBitRangeMsb2) from test_lua_e65.cpp
|
||||
TEST(BitRangeMsbTest, offtopicTestSetBitRangeMsb2) {
|
||||
// 1000 0000 0111
|
||||
// v------------v
|
||||
// 0110 1000 0000 0111
|
||||
uint8_t data[] = { 0x68, 0x07 };
|
||||
setBitRangeMsb(data, 8, 13, 0x807); // 1000 0000 0111
|
||||
EXPECT_THAT(data, testing::ElementsAre(0x68, 0x07));
|
||||
|
||||
EXPECT_EQ(getBitRangeMsb(data, 8, 13), 0x807);
|
||||
}
|
||||
|
||||
// inspired by TEST(LuaE65, offtopicTestSetBitRangeMsb3) from test_lua_e65.cpp
|
||||
TEST(BitRangeMsbTest, offtopicTestSetBitRangeMsb3) {
|
||||
// 0011 0000 0100
|
||||
// v------------v
|
||||
// 0110 1000 0000 0111
|
||||
uint8_t data[] = { 0x68, 0x07 };
|
||||
setBitRangeMsb(data, 8, 13, 0x304); // 0011 0000 0100
|
||||
EXPECT_THAT(data, testing::ElementsAre(0x63, 0x04));
|
||||
|
||||
EXPECT_EQ(getBitRangeMsb(data, 8, 13), 0x304);
|
||||
}
|
||||
|
||||
// inspired by TEST(LuaE65, offtopicTestGetBitRangeMsb2) from test_lua_e65.cpp
|
||||
TEST(BitRangeMsbTest, getBitRangeMsbTest) {
|
||||
// 1001 1111 0000 0001 0011 0010 0010 0000 0010 0011 0110 0111 0100 0000 0000 0000
|
||||
// ^------------^
|
||||
uint8_t data[] = { 0x9F, 0x01, 0x32, 0x20, 0x23, 0x67, 0x40, 0x00 };
|
||||
|
||||
EXPECT_EQ(getBitRangeMsb(data, 6 * 8, 13), 0x740); // 0111 0100 0000
|
||||
}
|
||||
|
||||
// inspired by TEST(LuaE65, setBitRangeMsbTest) from test_lua_e65.cpp
|
||||
TEST(BitRangeMsbTest, setBitRangeMsbTest) {
|
||||
// v--------------v
|
||||
// 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
|
||||
uint8_t data[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
setBitRangeMsb(data, 6 * 8, 13, 0x740); // 0111 0100 0000
|
||||
EXPECT_THAT(
|
||||
data,
|
||||
// 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0111 0100 0000 0000 0000
|
||||
testing::ElementsAre(0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x40, 0x00)
|
||||
);
|
||||
|
||||
EXPECT_EQ(getBitRangeMsb(data, 6 * 8, 13), 0x740);
|
||||
}
|
||||
|
||||
TEST(BitRangeMsbTest, getBitRangeMsbTest1) {
|
||||
// 0000 0000 1111 1111 0000 0000
|
||||
uint8_t data[] = {0x00, 0xFF, 0x00};
|
||||
|
||||
// 0000 0000 1111 1111 0000 0000
|
||||
// ^-------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 8, 8), 0xFF); // 1111 1111
|
||||
|
||||
// 0000 0000 1111 1111 0000 0000
|
||||
// ^--------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 9, 8), 0x7F); // 0111 1111
|
||||
|
||||
// 0000 0000 1111 1111 0000 0000
|
||||
// ^--------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 23, 8), 0xFE); // 1111 1110
|
||||
|
||||
// 0000 0000 1111 1111 0000 0000
|
||||
// ^---------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 23, 9), 0x1FE); // 0001 1111 1110
|
||||
|
||||
// 0000 0000 1111 1111 0000 0000
|
||||
// ^---------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 8, 9), 0x0FF); // 0 1111 1111
|
||||
|
||||
// 0000 0000 1111 1111 0000 0000
|
||||
// ^-----------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 23, 10), 0x1FE); // 0001 1111 1110
|
||||
}
|
||||
|
||||
TEST(BitRangeMsbTest, getBitRangeMsbTest2) {
|
||||
// 1111 1111 0111 1110 1111 1111
|
||||
uint8_t data[] = {0xFF, 0x7E, 0xFF};
|
||||
|
||||
// 1111 1111 0111 1110 1111 1111
|
||||
// ^-------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 8, 8), 0x7E); // 0111 1110
|
||||
|
||||
// 1111 1111 0111 1110 1111 1111
|
||||
// ^--------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 9, 8), 0xBF); // 1011 1111
|
||||
|
||||
// 1111 1111 0111 1110 1111 1111
|
||||
// ^--------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 23, 8), 0xFD); // 1111 1101
|
||||
|
||||
// 1111 1111 0111 1110 1111 1111
|
||||
// ^---------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 23, 9), 0x0FD); // 0000 1111 1101
|
||||
|
||||
// 1111 1111 0111 1110 1111 1111
|
||||
// ^---------^
|
||||
EXPECT_EQ(getBitRangeMsb(data, 8, 9), 0x17E); // 0001 0111 1110
|
||||
|
||||
// 1111 1111 0111 1110 1111 1111
|
||||
// ^-----------^
|
||||
EXPECT_NE(getBitRangeMsb(data, 23, 10), 0x2FD); // 0010 1111 1101
|
||||
}
|
|
@ -2,13 +2,19 @@
|
|||
#include "rusefi_lua.h"
|
||||
#include "lua_lib.h"
|
||||
|
||||
// `motorolaMagicFromDbc` function calculates data payload LST (Least Significant Bit) index from the corresponding
|
||||
// `Bit start | length` values in DBC file. The following test checks that for some sample DBC file
|
||||
// `motorolaMagicFromDbc` function returns values that match pictures we see if we open this DBC file with `Kvazer
|
||||
// Database Editor` program.
|
||||
TEST(dbcMotorola, testFromDbc) {
|
||||
ASSERT_EQ(24, motorolaMagicFromDbc(27, 4));
|
||||
ASSERT_EQ(24, motorolaMagicFromDbc(30, 7));
|
||||
ASSERT_EQ(24, motorolaMagicFromDbc(31, 8));
|
||||
ASSERT_EQ(24, motorolaMagicFromDbc(17, 10));
|
||||
|
||||
ASSERT_EQ(24, motorolaMagicFromDbc(17, 10));
|
||||
ASSERT_EQ(24, motorolaMagicFromDbc(16, 9));
|
||||
ASSERT_EQ(22, motorolaMagicFromDbc(13, 8));
|
||||
ASSERT_EQ(22, motorolaMagicFromDbc(14, 9));
|
||||
ASSERT_EQ(23, motorolaMagicFromDbc(14, 8));
|
||||
}
|
||||
|
||||
TEST(dbcMotorola, testFromIntelIntoMotorolaDbc) {
|
||||
|
@ -16,13 +22,18 @@ TEST(dbcMotorola, testFromIntelIntoMotorolaDbc) {
|
|||
}
|
||||
|
||||
TEST(dbcMotorola, accessRange) {
|
||||
uint8_t data[] = { 0x00, 0x00, 0x03, 0xFF, 0x00, 0x00, 0x00, 0x00};
|
||||
uint8_t data[] = { 0x00, 0x00, 0x03, 0xFF, 0x00, 0x00, 0x00, 0x00};
|
||||
|
||||
EXPECT_NEAR_M3(getBitRangeMoto(data, 17, 10), 0x03FF);
|
||||
EXPECT_EQ(getBitRangeMoto(data, 17, 10), 0x03FF);
|
||||
|
||||
EXPECT_NEAR_M3(getBitRangeLsb(data, 16, 2), 3);
|
||||
EXPECT_NEAR_M3(getBitRangeLsb(data, 24, 8), 0xFF);
|
||||
EXPECT_EQ(getBitRangeLsb(data, 16, 2), 3);
|
||||
EXPECT_EQ(getBitRangeLsb(data, 24, 8), 0xFF);
|
||||
|
||||
// todo: setBitRangeMoto(data, 17, 10, 0x03FF);
|
||||
// todo: assert data is empty now?
|
||||
setBitRangeMoto(data, 17, 10, 0x0234);
|
||||
EXPECT_THAT(data, testing::ElementsAre(0x00, 0x00, 0x02, 0x34, 0x00, 0x00, 0x00, 0x00));
|
||||
|
||||
EXPECT_EQ(getBitRangeMoto(data, 17, 10), 0x0234);
|
||||
|
||||
EXPECT_EQ(getBitRangeLsb(data, 16, 2), 2);
|
||||
EXPECT_EQ(getBitRangeLsb(data, 24, 8), 0x34);
|
||||
}
|
||||
|
|
|
@ -71,7 +71,8 @@ TESTS_SRC_CPP = \
|
|||
tests/shift_torque_reduction/test_shift_torque_reduction_angle_advance.cpp \
|
||||
tests/test_fft.cpp \
|
||||
tests/lua/test_lua_basic.cpp \
|
||||
tests/lua/test_lua_dbc.cpp \
|
||||
tests/lua/test_bit_range_msb.cpp \
|
||||
tests/lua/test_lua_dbc.cpp \
|
||||
tests/lua/test_lookup.cpp \
|
||||
tests/lua/test_lua_e38.cpp \
|
||||
tests/lua/test_lua_canam.cpp \
|
||||
|
|
Loading…
Reference in New Issue