src.dualinventive.com/dinet/libdi/tests/can_bitops.cpp

151 lines
5.5 KiB
C++

/**
* @file tests/can_bitops.cpp
* @brief brief
* @date 19 July, 2016
* @author jjacobs
* @copyright 2016 Dual Inventive Technology Centre B.V.
*
* Test can bit operation macros for manipulating the extend CAN frame ID
*/
#include <di/can.h>
#include <gtest/gtest.h>
/**
* Check all specification values
*/
TEST(can_bitops, check_spec_values) {
/* Msg Type */
EXPECT_EQ(0U, DI_CAN_MSGTYPE_RAW);
EXPECT_EQ(1U, DI_CAN_MSGTYPE_RPC);
EXPECT_EQ(2U, DI_CAN_MSGTYPE_NET);
/* Transfer type ID */
EXPECT_EQ(0U, DI_CAN_TRANSFERTYPE_REPLY_ERR);
EXPECT_EQ(1U, DI_CAN_TRANSFERTYPE_REPLY);
EXPECT_EQ(2U, DI_CAN_TRANSFERTYPE_REQUEST);
EXPECT_EQ(3U, DI_CAN_TRANSFERTYPE_PUBLISH);
/* Frame Index */
EXPECT_EQ(0U, DI_CAN_FRAME_ID_FIRST);
EXPECT_EQ(511U, DI_CAN_FRAME_ID_MAX);
EXPECT_EQ(512U, DI_CAN_FRAME_ID_MAX_COUNT);
/* Transaction ID */
EXPECT_EQ(0U, DI_CAN_TRANSACTION_FIRST);
EXPECT_EQ(15U, DI_CAN_TRANSACTION_LAST);
/* Node ID */
EXPECT_EQ(0x00000000, DI_CAN_NODEID_UNSET);
EXPECT_EQ(0xffffffff, DI_CAN_NODEID_BROADCAST);
/* Maximum payload framing size (512 frames * 8 bytes) - (SFT + MFT metadata frame) */
EXPECT_EQ(4080U, DI_CAN_FRAMING_SIZE_MAX);
}
/**
* Test proper functioning of DI_CAN macros which creates the canid bitfields
*/
TEST(can_bitops, DI_CAN_SET__DI_CAN_GET) {
uint32_t canid;
uint32_t canid_generated;
struct {
uint32_t canid;
uint8_t msgtype;
uint16_t datatype;
uint8_t transfertype;
uint16_t frame_index;
uint8_t transaction_id;
uint8_t last;
} expect[] = {
/* Check Msg Type */
{ 0x00000000, DI_CAN_MSGTYPE_RAW, 0, 0, 0, 0, 0 },
{ 0x08000000, DI_CAN_MSGTYPE_RPC, 0, 0, 0, 0, 0 },
{ 0x10000000, DI_CAN_MSGTYPE_NET, 0, 0, 0, 0, 0 },
{ 0x10000001, DI_CAN_MSGTYPE_NET, 0, 0, 0, 0, DI_CAN_LAST_FRAME_TRUE },
/* Check Data Type ID */
{ 0x07ff0000, DI_CAN_MSGTYPE_RAW, 0x7ff, 0, 0, 0, 0 },
{ 0x0fff0000, DI_CAN_MSGTYPE_RPC, 0x7ff, 0, 0, 0, 0 },
{ 0x17ff0000, DI_CAN_MSGTYPE_NET, 0x7ff, 0, 0, 0, 0 },
/* Transfer Type ID */
{ 0x00000000, 0, 0, DI_CAN_TRANSFERTYPE_REPLY_ERR, 0, 0, 0 },
{ 0x00004000, 0, 0, DI_CAN_TRANSFERTYPE_REPLY, 0, 0, 0 },
{ 0x00008000, 0, 0, DI_CAN_TRANSFERTYPE_REQUEST, 0, 0, 0 },
{ 0x0000c000, 0, 0, DI_CAN_TRANSFERTYPE_PUBLISH, 0, 0, 0 },
{ 0x07ff0000, DI_CAN_MSGTYPE_RAW, 0x7ff, DI_CAN_TRANSFERTYPE_REPLY_ERR, 0, 0, 0 },
{ 0x07ff4000, DI_CAN_MSGTYPE_RAW, 0x7ff, DI_CAN_TRANSFERTYPE_REPLY, 0, 0, 0 },
{ 0x07ff8000, DI_CAN_MSGTYPE_RAW, 0x7ff, DI_CAN_TRANSFERTYPE_REQUEST, 0, 0, 0 },
{ 0x07ffc000, DI_CAN_MSGTYPE_RAW, 0x7ff, DI_CAN_TRANSFERTYPE_PUBLISH, 0, 0, 0 },
{ 0x0fff0000, DI_CAN_MSGTYPE_RPC, 0x7ff, DI_CAN_TRANSFERTYPE_REPLY_ERR, 0, 0, 0 },
{ 0x17ff0000, DI_CAN_MSGTYPE_NET, 0x7ff, DI_CAN_TRANSFERTYPE_REPLY_ERR, 0, 0, 0 },
/* Frame Index */
{ 0x00000020, 0, 0, 0, 0x001, 0, 0 },
{ 0x00002000, 0, 0, 0, 0x100, 0, 0 },
{ 0x00002020, 0, 0, 0, 0x101, 0, 0 },
{ 0x00003fe0, 0, 0, 0, 0x1ff, 0, 0 },
{ 0x08003fe0, DI_CAN_MSGTYPE_RPC, 0, 0, 0x1ff, 0, 0 },
{ 0x10003fe0, DI_CAN_MSGTYPE_NET, 0, 0, 0x1ff, 0, 0 },
/* Transaction ID */
{ 0x00000000, 0, 0, 0, 0, DI_CAN_TRANSACTION_FIRST, 0 },
{ 0x00000004, 0, 0, 0, 0, 0x2, 0 },
{ 0x00000012, 0, 0, 0, 0, 0x9, 0 },
{ 0x0000001e, 0, 0, 0, 0, DI_CAN_TRANSACTION_LAST, 0 },
{ 0x00000000, DI_CAN_MSGTYPE_RAW, 0, 0, 0, DI_CAN_TRANSACTION_FIRST, 0 },
{ 0x08000000, DI_CAN_MSGTYPE_RPC, 0, 0, 0, DI_CAN_TRANSACTION_FIRST, 0 },
{ 0x10000000, DI_CAN_MSGTYPE_NET, 0, 0, 0, DI_CAN_TRANSACTION_FIRST, 0 },
{ 0x1000001f, DI_CAN_MSGTYPE_NET, 0, 0, 0, DI_CAN_TRANSACTION_LAST, DI_CAN_LAST_FRAME_TRUE },
/* Last frame */
{ 0x00000000, 0, 0, 0, 0, 0, DI_CAN_LAST_FRAME_FALSE },
{ 0x0000000D, 0, 0, 0, 0, 6, DI_CAN_LAST_FRAME_TRUE }
};
// Check expected canid against all fields
for (size_t n = 0; n < sizeof(expect)/sizeof(expect[0]); n++) {
canid = expect[n].canid;
// Generate the expected canid, so we can catch errors and print a usefull message
canid_generated = 0;
DI_CAN_SET_MSGTYPE(canid_generated, expect[n].msgtype);
DI_CAN_SET_DATATYPE(canid_generated, expect[n].datatype);
DI_CAN_SET_TRANSFERTYPE(canid_generated, expect[n].transfertype);
DI_CAN_SET_FRAME(canid_generated, expect[n].frame_index);
DI_CAN_SET_TRANSACTION(canid_generated, expect[n].transaction_id);
DI_CAN_SET_LAST_FRAME(canid_generated, expect[n].last);
if (canid != canid_generated) {
EXPECT_TRUE(false) << "=== Error in canid test vector ===" << std::endl << "index: " << n << std::endl
<< std::setfill('0')
<< " got canid: 0x" << std::hex << std::setw(8) << canid << std::endl
<< "expect canid: 0x" << std::hex << std::setw(8) << canid_generated << std::endl;
continue;
}
ASSERT_EQ(expect[n].msgtype, DI_CAN_GET_MSGTYPE(canid));
ASSERT_EQ(expect[n].datatype, DI_CAN_GET_DATATYPE(canid));
ASSERT_EQ(expect[n].transfertype, DI_CAN_GET_TRANSFERTYPE(canid));
ASSERT_EQ(expect[n].frame_index, DI_CAN_GET_FRAME(canid));
ASSERT_EQ(expect[n].transaction_id, DI_CAN_GET_TRANSACTION(canid));
ASSERT_EQ(expect[n].last, DI_CAN_GET_LAST_FRAME(canid));
}
}
/**
* Test if the DI_CAN_SET_FRAME_NEXT increments the correct bits in the
* canid Frame Index field.
*/
TEST(can_bitops, DI_CAN_SET_FRAME_NEXT) {
uint32_t canid = 0;
DI_CAN_SET_FRAME(canid, 1);
EXPECT_EQ(0x00000020, canid);
EXPECT_EQ(1U, DI_CAN_GET_FRAME(canid));
DI_CAN_SET_FRAME_NEXT(canid);
EXPECT_EQ(0x00000040, canid);
EXPECT_EQ(2U, DI_CAN_GET_FRAME(canid));
DI_CAN_SET_FRAME(canid, DI_CAN_FRAME_ID_MAX);
EXPECT_EQ(0x00003fe0, canid);
EXPECT_EQ(511U, DI_CAN_GET_FRAME(canid));
}