/* * Copyright (c) 2023 Fancy Code. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ILog.h" #include "LinuxApiMock.h" #include "UartDevice.h" #include "UartDeviceTestTool.h" #include #include #include namespace UartDeviceMockTest { const char *gDeviceName = "dev/s1"; static uart_info gUartDevice = { gDeviceName, 1152000, 'N', 8, 1, 'N', }; class UartDeviceMockTest : public testing::Test, public UartDeviceTestTool { public: UartDeviceMockTest() {} virtual ~UartDeviceMockTest() {} static void SetUpTestCase() { CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); } static void TearDownTestCase() { ILogUnInit(); } virtual void SetUp() { mLinuxTest = LinuxTest::CreateLinuxTest(); std::shared_ptr test = mLinuxTest; LinuxApiMock::GetInstance(&test); LinuxApiMock::GetInstance()->Init(); RegisterUartDevice(mLinuxTest, gUartDevice); } virtual void TearDown() { LinuxApiMock::GetInstance()->UnInit(); mLinuxTest = std::make_shared(); std::shared_ptr test = std::make_shared(); LinuxApiMock::GetInstance(&test); } public: std::shared_ptr mLinuxTest; }; // ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_EXAMPLE_Demo TEST_F(UartDeviceMockTest, UNIT_UartDevice_EXAMPLE_Demo) { void *object = CreateUartDevice(gUartDevice); IUartOpen(object); const char *SEND_BUF = "TEST"; ssize_t sendResult = IUartSend(object, SEND_BUF, strlen(SEND_BUF)); EXPECT_EQ(sendResult, static_cast(strlen(SEND_BUF))); // IUartRecv(object, nullptr, 0, 0); // IUartTcflush(object); IUartDeviceFree(object); } // ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_ParameterEarror /** * @brief Construct a new test f object * Is the interface accurate when testing parameter errors in this test case. */ TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_ParameterEarror) { char *deviceName = nullptr; static uart_info uartDevice = { deviceName, 1152000, 'N', 8, 1, 'N', }; RegisterUartDevice(mLinuxTest, uartDevice); void *object = CreateUartDevice(uartDevice); IUartOpen(object); IUartDeviceFree(object); EXPECT_EQ(object, nullptr); } // ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_ParameterEarror2 /** * @brief Construct a new test f object * Is the interface accurate when testing parameter errors in this test case. */ TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_ParameterEarror2) { // TODO: // const char *SEND_BUF = "TEST"; // char *deviceName = (char *)malloc(strlen(SEND_BUF) + 1); // memset(deviceName, 0, strlen(SEND_BUF) + 1); // static uart_info uartDevice = { // "deviceName", // 1152000, // 'N', // 8, // 1, // 'N', // }; // RegisterUartDevice(mLinuxTest, uartDevice); // void *object = CreateUartDevice(uartDevice); // IUartOpen(object); // IUartDeviceFree(object); // free(deviceName); // deviceName = nullptr; } // ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_WriteAllData /** * @brief Construct a new test f object * This test case verifies whether the application will call the write function twice until all data is written when the * write function does not write the length of the data. */ TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_WriteAllData) { const char *SEND_BUF = "TEST"; constexpr ssize_t SEND_LENGTH = 1; // less than static_cast(strlen(SEND_BUF)) SetSendApiOnce(mLinuxTest, gUartDevice, SEND_LENGTH); void *object = CreateUartDevice(gUartDevice); IUartOpen(object); ssize_t sendResult = IUartSend(object, SEND_BUF, strlen(SEND_BUF)); EXPECT_EQ(sendResult, static_cast(strlen(SEND_BUF))); IUartDeviceFree(object); } // ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_EXAMPLE_MultiThreadTest /** * @brief Construct a new test f object * This test case tests whether the correct return value can be obtained when simulating multi-threaded operations on * the interface. */ TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_MultiThreadTest) { auto openThread = [](void) { LogInfo("===========openThread \n"); void *object = CreateUartDevice(gUartDevice); IUartOpen(object); IUartDeviceFree(object); }; std::thread test1 = std::thread(openThread); test1.detach(); std::thread test2 = std::thread(openThread); test2.detach(); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } } // namespace UartDeviceMockTest