Add unit tests for sSerialAttrToStr#10
Add unit tests for sSerialAttrToStr#10zknpr wants to merge 1 commit intosecurity-fixes-and-community-prsfrom
Conversation
- Create `tests/test_serial.c` with test cases covering standard, complex, edge, and overflow scenarios. - Update `CMakeLists.txt` to enable testing and add `test_serial` target. - Verify tests pass and handle potential truncation correctly. Co-authored-by: zknpr <96851588+zknpr@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Summary of ChangesHello @zknpr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the reliability of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces unit tests for the sSerialAttrToStr function, which is a great addition for improving code quality and reliability. The changes to CMakeLists.txt correctly integrate the new tests into the build system. My review focuses on improving the structure and robustness of the new test file tests/test_serial.c. I've suggested refactoring the test runner to ensure all tests are executed and to provide a clear summary of results, which is a common best practice for unit testing.
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include <assert.h> | ||
| #include "serial.h" | ||
|
|
||
| void test_standard_case() { | ||
| xSerialIos ios; | ||
| ios.baud = 9600; | ||
| ios.dbits = SERIAL_DATABIT_8; | ||
| ios.parity = SERIAL_PARITY_NONE; | ||
| ios.sbits = SERIAL_STOPBIT_ONE; | ||
| ios.flow = SERIAL_FLOW_NONE; | ||
|
|
||
| const char *result = sSerialAttrToStr(&ios); | ||
| // Format: BBBBBBBBBB-DPSF | ||
| // " 9600-8N1 " | ||
| printf("Test Standard Case: '%s'\n", result); | ||
| assert(strcmp(result, " 9600-8N1 ") == 0); | ||
| } | ||
|
|
||
| void test_complex_case() { | ||
| xSerialIos ios; | ||
| ios.baud = 115200; | ||
| ios.dbits = SERIAL_DATABIT_8; | ||
| ios.parity = SERIAL_PARITY_EVEN; | ||
| ios.sbits = SERIAL_STOPBIT_TWO; | ||
| ios.flow = SERIAL_FLOW_RTSCTS; | ||
|
|
||
| const char *result = sSerialAttrToStr(&ios); | ||
| // " 115200-8E2H" | ||
| printf("Test Complex Case: '%s'\n", result); | ||
| assert(strcmp(result, " 115200-8E2H") == 0); | ||
| } | ||
|
|
||
| void test_rs485_case() { | ||
| xSerialIos ios; | ||
| ios.baud = 19200; | ||
| ios.dbits = SERIAL_DATABIT_8; | ||
| ios.parity = SERIAL_PARITY_NONE; | ||
| ios.sbits = SERIAL_STOPBIT_ONE; | ||
| ios.flow = SERIAL_FLOW_RS485_RTS_AFTER_SEND; | ||
|
|
||
| const char *result = sSerialAttrToStr(&ios); | ||
| // " 19200-8N1R" | ||
| printf("Test RS485 Case: '%s'\n", result); | ||
| assert(strcmp(result, " 19200-8N1R") == 0); | ||
| } | ||
|
|
||
| void test_stopbit_onehalf_case() { | ||
| xSerialIos ios; | ||
| ios.baud = 9600; | ||
| ios.dbits = SERIAL_DATABIT_8; | ||
| ios.parity = SERIAL_PARITY_NONE; | ||
| ios.sbits = SERIAL_STOPBIT_ONEHALF; | ||
| ios.flow = SERIAL_FLOW_NONE; | ||
|
|
||
| const char *result = sSerialAttrToStr(&ios); | ||
| // " 9600-8N3 " (Wait, 1.5 is enum value 3) | ||
| printf("Test StopBit 1.5 Case: '%s'\n", result); | ||
| assert(strcmp(result, " 9600-8N3 ") == 0); | ||
| } | ||
|
|
||
| void test_truncation_case() { | ||
| xSerialIos ios; | ||
| ios.baud = 1234567890; // 10 digits | ||
| ios.dbits = SERIAL_DATABIT_8; | ||
| ios.parity = SERIAL_PARITY_NONE; | ||
| ios.sbits = SERIAL_STOPBIT_ONE; | ||
| ios.flow = SERIAL_FLOW_NONE; | ||
|
|
||
| const char *result = sSerialAttrToStr(&ios); | ||
| // "1234567890-8N1 " | ||
| printf("Test Truncation Case (10 digits): '%s'\n", result); | ||
| assert(strcmp(result, "1234567890-8N1 ") == 0); | ||
| } | ||
|
|
||
| void test_overflow_case() { | ||
| xSerialIos ios; | ||
| ios.dbits = SERIAL_DATABIT_8; | ||
| ios.parity = SERIAL_PARITY_NONE; | ||
| ios.sbits = SERIAL_STOPBIT_ONE; | ||
| ios.flow = SERIAL_FLOW_NONE; | ||
|
|
||
| // Check if long is large enough to hold the value | ||
| if (sizeof(long) > 4) { | ||
| ios.baud = 12345678901L; // 11 digits (if long is 64-bit) | ||
| const char *result = sSerialAttrToStr(&ios); | ||
| printf("Test Overflow Case (11 digits): '%s'\n", result); | ||
| // Expected: "12345678901-8N1" (15 chars) | ||
| // because snprintf truncates to size-1. | ||
| assert(strcmp(result, "12345678901-8N1") == 0); | ||
| } else { | ||
| printf("Skipping Test Overflow Case (long is 32-bit)\n"); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
| test_standard_case(); | ||
| test_complex_case(); | ||
| test_rs485_case(); | ||
| test_stopbit_onehalf_case(); | ||
| test_truncation_case(); | ||
| test_overflow_case(); | ||
|
|
||
| printf("All tests passed!\n"); | ||
| return 0; | ||
| } |
There was a problem hiding this comment.
The current test implementation uses assert(), which will cause the test executable to abort on the first failure. This prevents subsequent tests from running, making it harder to get a full picture of any issues. It's better to have a test runner that executes all tests and reports a summary of successes and failures at the end.
I suggest a small refactoring to introduce a simple test harness. This involves:
- Replacing
assert()with a custom macro that records failures but doesn't halt execution. - Updating
main()to iterate through all tests and provide a summary report, returning a non-zero exit code if any test fails (which is standard forctest). - Removing diagnostic
printf()calls from individual test cases to keep the test output clean forctest.
This approach makes the test suite more robust and provides more comprehensive feedback.
#include <stdio.h>
#include <string.h>
#include "serial.h"
// --- Simple Test Harness ---
static int tests_run = 0;
static int tests_failed = 0;
// A simple assertion macro that reports failures without aborting.
#define ASSERT_STR_EQ(actual, expected, test_name) \
do { \
tests_run++; \
if (strcmp(actual, expected) != 0) { \
fprintf(stderr, "FAIL in %s: expected \"%s\", got \"%s\"\n", test_name, expected, actual); \
tests_failed++; \
} \
} while (0)
// --- End Test Harness ---
void test_standard_case() {
xSerialIos ios;
ios.baud = 9600;
ios.dbits = SERIAL_DATABIT_8;
ios.parity = SERIAL_PARITY_NONE;
ios.sbits = SERIAL_STOPBIT_ONE;
ios.flow = SERIAL_FLOW_NONE;
const char *result = sSerialAttrToStr(&ios);
ASSERT_STR_EQ(result, " 9600-8N1 ", "test_standard_case");
}
void test_complex_case() {
xSerialIos ios;
ios.baud = 115200;
ios.dbits = SERIAL_DATABIT_8;
ios.parity = SERIAL_PARITY_EVEN;
ios.sbits = SERIAL_STOPBIT_TWO;
ios.flow = SERIAL_FLOW_RTSCTS;
const char *result = sSerialAttrToStr(&ios);
ASSERT_STR_EQ(result, " 115200-8E2H", "test_complex_case");
}
void test_rs485_case() {
xSerialIos ios;
ios.baud = 19200;
ios.dbits = SERIAL_DATABIT_8;
ios.parity = SERIAL_PARITY_NONE;
ios.sbits = SERIAL_STOPBIT_ONE;
ios.flow = SERIAL_FLOW_RS485_RTS_AFTER_SEND;
const char *result = sSerialAttrToStr(&ios);
ASSERT_STR_EQ(result, " 19200-8N1R", "test_rs485_case");
}
void test_stopbit_onehalf_case() {
xSerialIos ios;
ios.baud = 9600;
ios.dbits = SERIAL_DATABIT_8;
ios.parity = SERIAL_PARITY_NONE;
ios.sbits = SERIAL_STOPBIT_ONEHALF;
ios.flow = SERIAL_FLOW_NONE;
const char *result = sSerialAttrToStr(&ios);
ASSERT_STR_EQ(result, " 9600-8N3 ", "test_stopbit_onehalf_case");
}
void test_truncation_case() {
xSerialIos ios;
ios.baud = 1234567890; // 10 digits
ios.dbits = SERIAL_DATABIT_8;
ios.parity = SERIAL_PARITY_NONE;
ios.sbits = SERIAL_STOPBIT_ONE;
ios.flow = SERIAL_FLOW_NONE;
const char *result = sSerialAttrToStr(&ios);
ASSERT_STR_EQ(result, "1234567890-8N1 ", "test_truncation_case");
}
void test_overflow_case() {
xSerialIos ios;
ios.dbits = SERIAL_DATABIT_8;
ios.parity = SERIAL_PARITY_NONE;
ios.sbits = SERIAL_STOPBIT_ONE;
ios.flow = SERIAL_FLOW_NONE;
// Check if long is large enough to hold the value
if (sizeof(long) > 4) {
ios.baud = 12345678901L; // 11 digits (if long is 64-bit)
const char *result = sSerialAttrToStr(&ios);
ASSERT_STR_EQ(result, "12345678901-8N1", "test_overflow_case");
} else {
printf("Skipping Test Overflow Case (long is 32-bit)\n");
}
}
int main() {
test_standard_case();
test_complex_case();
test_rs485_case();
test_stopbit_onehalf_case();
test_truncation_case();
test_overflow_case();
if (tests_failed > 0) {
fprintf(stderr, "\n%d of %d tests failed.\n", tests_failed, tests_run);
return 1;
}
printf("All tests passed!\n");
return 0;
}|
Tests merged into v1.5.3 as combined test suite. Thank you Jules! |
You're welcome! Glad to hear the tests were useful and merged successfully. |
Add comprehensive test suite for serial port configuration functions: - sSerialFlowToStr: flow control type conversion - sSerialDataBitsToStr: data bits conversion - sSerialStopBitsToStr: stop bits conversion - sSerialParityToStr: parity conversion Test infrastructure: - tests/test_serial.c: All serial function tests - tests/CMakeLists.txt: CTest integration - BUILD_TESTING option in main CMakeLists.txt Based on Jules AI contributions from PRs #10, #11, #14, #15 Reviewed by: Gemini Code Assist Co-Authored-By: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This PR adds unit tests for
sSerialAttrToStrinsrc/serial.c. It introduces a newtestsdirectory and integrates testing into the CMake build system.Changes:
tests/test_serial.c: Contains test cases forsSerialAttrToStr.CMakeLists.txt: Enabled testing and addedtest_serialexecutable.Verification:
PR created automatically by Jules for task 4500608520212156328 started by @zknpr