net_env_project/tests/test_ppu.cpp

131 lines
4.2 KiB
C++
Raw Normal View History

2026-06-18 05:26:08 +00:00
#include <gtest/gtest.h>
#include "app.hpp"
#include <string>
#include <cstring>
using namespace ppu;
// 辅助函数:创建 CanFrame
CanFrame CreateCanFrame(uint32_t id, bool isExtended, bool isCanFd, uint8_t dataLen) {
CanFrame frame;
frame.id = id;
frame.isExtended = isExtended;
frame.isCanFd = isCanFd;
frame.dataLen = dataLen;
frame.timestamp = 1000;
std::memset(frame.data, 0, sizeof(frame.data));
for (uint8_t i = 0; i < dataLen && i < 64; ++i) {
frame.data[i] = i;
}
return frame;
}
// ================= PPU_Check 测试用例 =================
TEST(PPU_Check_Test, Normal_Standard_CAN20) {
CanFrame frame = CreateCanFrame(0x100, false, false, 8);
std::string reason;
EXPECT_TRUE(PPU_Check(frame, reason));
}
TEST(PPU_Check_Test, Normal_Extended_CANFD) {
CanFrame frame = CreateCanFrame(0x1FFFFFFF, true, true, 64);
std::string reason;
EXPECT_TRUE(PPU_Check(frame, reason));
}
TEST(PPU_Check_Test, Boundary_Standard_ID_Max) {
CanFrame frame = CreateCanFrame(0x7FF, false, false, 8);
std::string reason;
EXPECT_TRUE(PPU_Check(frame, reason));
}
TEST(PPU_Check_Test, Boundary_Extended_ID_Max) {
CanFrame frame = CreateCanFrame(0x1FFFFFFF, true, false, 8);
std::string reason;
EXPECT_TRUE(PPU_Check(frame, reason));
}
TEST(PPU_Check_Test, Boundary_DataLen_Zero) {
CanFrame frame = CreateCanFrame(0x100, false, false, 0);
std::string reason;
EXPECT_TRUE(PPU_Check(frame, reason));
}
TEST(PPU_Check_Test, Exception_DataLen_Exceeds_64) {
CanFrame frame = CreateCanFrame(0x100, false, true, 65);
std::string reason;
EXPECT_FALSE(PPU_Check(frame, reason));
EXPECT_NE(reason.find("exceeds maximum (64)"), std::string::npos);
}
TEST(PPU_Check_Test, Exception_CAN20_DataLen_Exceeds_8) {
CanFrame frame = CreateCanFrame(0x100, false, false, 9);
std::string reason;
EXPECT_FALSE(PPU_Check(frame, reason));
EXPECT_NE(reason.find("exceeds 8 bytes"), std::string::npos);
}
TEST(PPU_Check_Test, Exception_Standard_ID_Exceeds_11bit) {
CanFrame frame = CreateCanFrame(0x800, false, false, 8);
std::string reason;
EXPECT_FALSE(PPU_Check(frame, reason));
EXPECT_NE(reason.find("exceeds 11-bit range"), std::string::npos);
}
TEST(PPU_Check_Test, Exception_Extended_ID_Exceeds_29bit) {
CanFrame frame = CreateCanFrame(0x20000000, true, false, 8);
std::string reason;
EXPECT_FALSE(PPU_Check(frame, reason));
EXPECT_NE(reason.find("exceeds 29-bit range"), std::string::npos);
}
// ================= PPU_Convert 测试用例 =================
TEST(PPU_Convert_Test, Normal_CAN20_to_CANFD) {
CanFrame input = CreateCanFrame(0x100, false, false, 8);
CompatibleFrame output;
std::string log;
EXPECT_TRUE(PPU_Convert(input, output, log));
EXPECT_EQ(output.dataLen, 8);
EXPECT_NE(log.find("CAN2.0->CANFD"), std::string::npos);
EXPECT_EQ(output.id, input.id);
}
TEST(PPU_Convert_Test, Normal_CANFD_to_CAN20_No_Truncation) {
CanFrame input = CreateCanFrame(0x100, false, true, 8);
CompatibleFrame output;
std::string log;
EXPECT_TRUE(PPU_Convert(input, output, log));
EXPECT_EQ(output.dataLen, 8);
EXPECT_NE(log.find("CANFD->CAN2.0"), std::string::npos);
}
TEST(PPU_Convert_Test, Normal_CANFD_to_CAN20_With_Truncation) {
CanFrame input = CreateCanFrame(0x100, false, true, 64);
CompatibleFrame output;
std::string log;
EXPECT_TRUE(PPU_Convert(input, output, log));
EXPECT_EQ(output.dataLen, 8);
EXPECT_NE(log.find("CANFD->CAN2.0"), std::string::npos);
}
TEST(PPU_Convert_Test, Exception_Check_Failed) {
CanFrame input = CreateCanFrame(0x100, false, false, 9); // CAN2.0 len > 8
CompatibleFrame output;
std::string log;
EXPECT_FALSE(PPU_Convert(input, output, log));
EXPECT_NE(log.find("Convert rejected"), std::string::npos);
}
TEST(PPU_Convert_Test, Special_Data_Copy_Verification) {
CanFrame input = CreateCanFrame(0x123, true, false, 4);
input.data[0] = 0xAA; input.data[1] = 0xBB; input.data[2] = 0xCC; input.data[3] = 0xDD;
CompatibleFrame output;
std::string log;
EXPECT_TRUE(PPU_Convert(input, output, log));
EXPECT_EQ(output.data[0], 0xAA);
EXPECT_EQ(output.data[1], 0xBB);
EXPECT_EQ(output.data[2], 0xCC);
EXPECT_EQ(output.data[3], 0xDD);
}