-
Notifications
You must be signed in to change notification settings - Fork 0
test: Add unit tests for serialOpen, serialClose, serialRead, and ser… #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
36dbf86
test: Add unit tests for serialOpen, serialClose, serialRead, and ser…
Katze719 9967cec
fix warnings
Katze719 d2bf98d
refactor: Update CMakeLists.txt to refine source file inclusion and a…
Katze719 ba763c9
refactor: Remove commented test section headers from serial test files
Katze719 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #include <cpp_core/interface/serial_close.h> | ||
| #include <cpp_core/status_codes.h> | ||
|
|
||
| #include <limits> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "test_helpers/error_capture.hpp" | ||
|
|
||
| class SerialCloseTest : public ::testing::Test | ||
| { | ||
| protected: | ||
| void SetUp() override | ||
| { | ||
| ErrorCapture::instance = &error_capture; | ||
| error_callback = &ErrorCapture::callback; | ||
| } | ||
|
|
||
| void TearDown() override | ||
| { | ||
| ErrorCapture::instance = nullptr; | ||
| } | ||
|
|
||
| ErrorCapture error_capture; | ||
| ErrorCallbackT error_callback = nullptr; | ||
| }; | ||
|
|
||
| TEST_F(SerialCloseTest, CloseInvalidHandleZero) | ||
| { | ||
| int result = serialClose(0, error_callback); | ||
|
|
||
| EXPECT_EQ(result, static_cast<int>(cpp_core::StatusCodes::kSuccess)); | ||
| } | ||
|
|
||
| TEST_F(SerialCloseTest, CloseInvalidHandleNegative) | ||
| { | ||
| int result = serialClose(-1, error_callback); | ||
|
|
||
| EXPECT_EQ(result, static_cast<int>(cpp_core::StatusCodes::kSuccess)); | ||
| } | ||
|
|
||
| TEST_F(SerialCloseTest, CloseInvalidHandleNegativeLarge) | ||
| { | ||
| int result = serialClose(-12345, error_callback); | ||
|
|
||
| EXPECT_EQ(result, static_cast<int>(cpp_core::StatusCodes::kSuccess)); | ||
| } | ||
|
|
||
| TEST_F(SerialCloseTest, CloseInvalidHandleTooLarge) | ||
| { | ||
| auto too_large_handle = static_cast<int64_t>(std::numeric_limits<int>::max()) + 1; | ||
| int result = serialClose(too_large_handle, error_callback); | ||
|
|
||
| EXPECT_EQ(result, static_cast<int>(cpp_core::StatusCodes::kInvalidHandleError)); | ||
| } | ||
|
|
||
| TEST_F(SerialCloseTest, CloseInvalidHandleIntMaxBoundary) | ||
| { | ||
| auto handle = static_cast<int64_t>(std::numeric_limits<int>::max()); | ||
| int result = serialClose(handle, error_callback); | ||
|
|
||
| // Should fail because this fd doesn't exist, but not with InvalidHandleError | ||
| EXPECT_NE(result, static_cast<int>(cpp_core::StatusCodes::kInvalidHandleError)); | ||
| } | ||
|
|
||
| TEST_F(SerialCloseTest, CloseNoErrorCallback) | ||
| { | ||
| int result = serialClose(0, nullptr); | ||
|
|
||
| EXPECT_EQ(result, static_cast<int>(cpp_core::StatusCodes::kSuccess)); | ||
| } | ||
|
|
||
| TEST_F(SerialCloseTest, CloseInvalidHandle) | ||
| { | ||
| // Test closing a real invalid fd (one that never existed) | ||
| // We should get an error but not crash | ||
| int result = serialClose(9999, error_callback); | ||
|
|
||
| // This will fail because the fd doesn't exist | ||
| EXPECT_EQ(result, static_cast<int>(cpp_core::StatusCodes::kCloseHandleError)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| #include <cpp_core/interface/serial_open.h> | ||
| #include <cpp_core/status_codes.h> | ||
|
|
||
| #include <array> | ||
| #include <string> | ||
|
|
||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "test_helpers/error_capture.hpp" | ||
|
|
||
| class SerialOpenTest : public ::testing::Test | ||
| { | ||
| protected: | ||
| void SetUp() override | ||
| { | ||
| ErrorCapture::instance = &error_capture; | ||
| error_callback = &ErrorCapture::callback; | ||
| } | ||
|
|
||
| void TearDown() override | ||
| { | ||
| ErrorCapture::instance = nullptr; | ||
| } | ||
|
|
||
| ErrorCapture error_capture; | ||
| ErrorCallbackT error_callback = nullptr; | ||
| }; | ||
|
|
||
| TEST_F(SerialOpenTest, NullPortParameter) | ||
| { | ||
| intptr_t result = serialOpen(nullptr, 9600, 8, 0, 1, error_callback); | ||
|
|
||
| EXPECT_EQ(result, static_cast<intptr_t>(cpp_core::StatusCodes::kNotFoundError)); | ||
| EXPECT_NE(error_capture.last_message.find("nullptr"), std::string::npos); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, BaudrateTooLow) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 100, 8, 0, 1, error_callback); | ||
|
|
||
| EXPECT_EQ(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| EXPECT_NE(error_capture.last_message.find("baudrate"), std::string::npos); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, BaudrateTooLowBoundary) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 299, 8, 0, 1, error_callback); | ||
|
|
||
| EXPECT_EQ(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, BaudrateBoundaryValid) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 300, 8, 0, 1, error_callback); | ||
|
|
||
| // /dev/null is not a real serial port, but should pass baudrate validation | ||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, DataBitsTooLow) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 4, 0, 1, error_callback); | ||
|
|
||
| EXPECT_EQ(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| EXPECT_NE(error_capture.last_message.find("data bits"), std::string::npos); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, DataBitsTooHigh) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 9, 0, 1, error_callback); | ||
|
|
||
| EXPECT_EQ(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, ValidDataBits5) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 5, 0, 1, error_callback); | ||
|
|
||
| // Should pass data bits validation | ||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, ValidDataBits6) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 6, 0, 1, error_callback); | ||
|
|
||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, ValidDataBits7) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 7, 0, 1, error_callback); | ||
|
|
||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, ValidDataBits8) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 8, 0, 1, error_callback); | ||
|
|
||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, InvalidParity) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 8, 5, 1, error_callback); | ||
|
|
||
| // Invalid parity should return an error | ||
| EXPECT_LT(result, 0); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, ValidParityNone) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 8, 0, 1, error_callback); | ||
|
|
||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, ValidParityEven) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 8, 1, 1, error_callback); | ||
|
|
||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, ValidParityOdd) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 8, 2, 1, error_callback); | ||
|
|
||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, InvalidStopBits) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 8, 0, 3, error_callback); | ||
|
|
||
| // Invalid stop bits should return an error | ||
| EXPECT_LT(result, 0); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, ValidStopBits0) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 8, 0, 0, error_callback); | ||
|
|
||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, ValidStopBits1) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 8, 0, 1, error_callback); | ||
|
|
||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, ValidStopBits2) | ||
| { | ||
| const char *port = "/dev/null"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 8, 0, 2, error_callback); | ||
|
|
||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, NonExistentPort) | ||
| { | ||
| const char *port = "/dev/ttyNONEXISTENT99999"; | ||
| intptr_t result = serialOpen(const_cast<void *>(static_cast<const void *>(port)), 9600, 8, 0, 1, error_callback); | ||
|
|
||
| EXPECT_EQ(result, static_cast<intptr_t>(cpp_core::StatusCodes::kNotFoundError)); | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, VariousBaudrates) | ||
| { | ||
| const char *port = "/dev/null"; | ||
|
|
||
| // Test common baudrates | ||
| const std::array<int, 11> baudrates = {300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800}; | ||
|
|
||
| for (int baudrate : baudrates) | ||
| { | ||
| intptr_t result = | ||
| serialOpen(const_cast<void *>(static_cast<const void *>(port)), baudrate, 8, 0, 1, error_callback); | ||
| EXPECT_NE(result, static_cast<intptr_t>(cpp_core::StatusCodes::kSetStateError)) | ||
| << "Baudrate " << baudrate << " should be valid"; | ||
| } | ||
| } | ||
|
|
||
| TEST_F(SerialOpenTest, NoErrorCallbackNullPort) | ||
| { | ||
| intptr_t result = serialOpen(nullptr, 9600, 8, 0, 1, nullptr); | ||
|
|
||
| EXPECT_EQ(result, static_cast<intptr_t>(cpp_core::StatusCodes::kNotFoundError)); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.