AI 自动生成测试用例
This commit is contained in:
parent
5e3cb998eb
commit
626628a09e
|
|
@ -0,0 +1,25 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10.0)
|
||||||
|
project(test_net_env_project)
|
||||||
|
include(FetchContent)
|
||||||
|
if (MSVC)
|
||||||
|
add_compile_options(/utf-8)
|
||||||
|
endif()
|
||||||
|
FetchContent_Declare(
|
||||||
|
googletest
|
||||||
|
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
|
||||||
|
)
|
||||||
|
# For Windows: Prevent overriding the parent project's compiler/linker settings
|
||||||
|
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||||
|
FetchContent_MakeAvailable(googletest)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
|
||||||
|
|
||||||
|
include(CTest)
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
add_executable(test_net_env_project test_app.cpp ../src/dau.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(test_net_env_project gtest gmock gtest_main)
|
||||||
|
include(GoogleTest)
|
||||||
|
gtest_discover_tests(test_net_env_project)
|
||||||
|
|
@ -0,0 +1,104 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include "app.hpp"
|
||||||
|
#include <string>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
using namespace dau;
|
||||||
|
|
||||||
|
class DAUTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
void SetUp() override {
|
||||||
|
DAU_SetSleep(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(DAUTest, testDAUProcessNormalInput) {
|
||||||
|
RawDataFrame input{};
|
||||||
|
ValidData output{};
|
||||||
|
std::string errorLog;
|
||||||
|
|
||||||
|
input.len = 8;
|
||||||
|
for(int i = 0; i < 8; ++i) input.data[i] = i;
|
||||||
|
input.timestamp = 12345;
|
||||||
|
input.busType = static_cast<BusType>(1);
|
||||||
|
|
||||||
|
uint32_t initialLoad = DAU_GetBusLoad();
|
||||||
|
|
||||||
|
EXPECT_TRUE(DAU_Process(input, output, errorLog));
|
||||||
|
EXPECT_EQ(output.dataLen, 8);
|
||||||
|
EXPECT_EQ(output.timestamp, 12345);
|
||||||
|
EXPECT_EQ(output.sourceBus, static_cast<BusType>(1));
|
||||||
|
EXPECT_EQ(DAU_GetBusLoad(), initialLoad + 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DAUTest, testDAUProcessZeroLength) {
|
||||||
|
RawDataFrame input{};
|
||||||
|
ValidData output{};
|
||||||
|
std::string errorLog;
|
||||||
|
input.len = 0;
|
||||||
|
|
||||||
|
EXPECT_FALSE(DAU_Process(input, output, errorLog));
|
||||||
|
EXPECT_NE(errorLog.find("Invalid data length"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DAUTest, testDAUProcessMaxLength) {
|
||||||
|
RawDataFrame input{};
|
||||||
|
ValidData output{};
|
||||||
|
std::string errorLog;
|
||||||
|
input.len = 64;
|
||||||
|
for(int i = 0; i < 64; ++i) input.data[i] = 0x01;
|
||||||
|
|
||||||
|
EXPECT_TRUE(DAU_Process(input, output, errorLog));
|
||||||
|
EXPECT_EQ(output.dataLen, 64);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DAUTest, testDAUProcessExceedMaxLength) {
|
||||||
|
RawDataFrame input{};
|
||||||
|
ValidData output{};
|
||||||
|
std::string errorLog;
|
||||||
|
input.len = 65;
|
||||||
|
|
||||||
|
EXPECT_FALSE(DAU_Process(input, output, errorLog));
|
||||||
|
EXPECT_NE(errorLog.find("Invalid data length"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DAUTest, testDAUProcessErrorFrame) {
|
||||||
|
RawDataFrame input{};
|
||||||
|
ValidData output{};
|
||||||
|
std::string errorLog;
|
||||||
|
input.len = 8;
|
||||||
|
memset(input.data, 0xFF, 8);
|
||||||
|
|
||||||
|
EXPECT_FALSE(DAU_Process(input, output, errorLog));
|
||||||
|
EXPECT_NE(errorLog.find("Error frame detected"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DAUTest, testDAUProcessSleepMode) {
|
||||||
|
DAU_SetSleep(true);
|
||||||
|
RawDataFrame input{};
|
||||||
|
ValidData output{};
|
||||||
|
std::string errorLog;
|
||||||
|
input.len = 8;
|
||||||
|
|
||||||
|
EXPECT_FALSE(DAU_Process(input, output, errorLog));
|
||||||
|
EXPECT_NE(errorLog.find("Sleep mode"), std::string::npos);
|
||||||
|
|
||||||
|
DAU_SetSleep(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DAUTest, testDAUSetSleepAndWakeUp) {
|
||||||
|
DAU_SetSleep(true);
|
||||||
|
RawDataFrame input{};
|
||||||
|
ValidData output{};
|
||||||
|
std::string errorLog;
|
||||||
|
input.len = 8;
|
||||||
|
EXPECT_FALSE(DAU_Process(input, output, errorLog));
|
||||||
|
|
||||||
|
DAU_SetSleep(false);
|
||||||
|
EXPECT_TRUE(DAU_Process(input, output, errorLog));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DAUTest, testDAUGetBusLoad) {
|
||||||
|
uint32_t load = DAU_GetBusLoad();
|
||||||
|
EXPECT_LE(load, 100);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue