Moved unit tests to CXX files to test C++ bindings implicitly.#117
Moved unit tests to CXX files to test C++ bindings implicitly.#117SebastianSchildt merged 4 commits intoCOVESA:mainfrom
Conversation
Signed-off-by: Naresh Nayak <Naresh.Nayak@de.bosch.com>
Signed-off-by: Naresh Nayak <Naresh.Nayak@de.bosch.com>
67c7bd0 to
db25d7f
Compare
|
By just renaming/forcing a CPP compiler, are we sure we will not have the problem the other way around? I.e. C compiler unhappy? |
Signed-off-by: Naresh Nayak <Naresh.Nayak@de.bosch.com>
True. The idea was that, since the examples were built using C, we could identify errors there. However, the unit tests are now run using both a C and a C++ compiler. Hopefully, this will flag errors in both. |
There was a problem hiding this comment.
Pull request overview
This PR enhances the test infrastructure by making all unit tests compilable as both C and C++ code to catch C++ binding compatibility issues early. This addresses the root cause of issue #115, where an implicit conversion from uint64_t to an enum type was not caught until C++ compilation.
Changes:
- Modified all unit test files to support C++ compilation by wrapping cmocka includes in
extern "C"blocks and replacing designated initializers with zero-initialization followed by member assignment - Updated CMakeLists.txt to enable C++ (C++17 standard) and create duplicate test executables compiled as C++ (with
-cppsuffix) for each existing test - Added SPDX-License-Identifier to test files that were missing it for better license clarity
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| CMakeLists.txt | Enabled C++ language support and set C++17 as the standard |
| unit/CMakeLists.txt | Added C++ test executables for all unit tests, creating duplicate tests compiled as C++ |
| unit/test-can.c | Added extern "C" wrapper for cmocka include for C++ compatibility |
| unit/test-aaf.c | Added extern "C" wrapper and SPDX identifier |
| unit/test-avtp.c | Added extern "C" wrapper and SPDX identifier |
| unit/test-crf.c | Added extern "C" wrapper and SPDX identifier |
| unit/test-cvf.c | Added extern "C" wrapper and SPDX identifier |
| unit/test-rvf.c | Added extern "C" wrapper, SPDX identifier, and explicit casts for alloca calls |
| unit/test-tscf.c | Added extern "C" wrapper for cmocka include |
| unit/test-ntscf.c | Added extern "C" wrapper for cmocka include |
| unit/test-vss.c | Added extern "C" wrapper, replaced designated initializers with zero-init + assignment, added explicit cast for strlen |
| unit/test-flexray.c | Added extern "C" wrapper for cmocka include |
| unit/test-lin.c | Added extern "C" wrapper for cmocka include |
| unit/test-most.c | Added extern "C" wrapper for cmocka include |
| unit/test-gpc.c | Added extern "C" wrapper for cmocka include |
| unit/test-sensor.c | Added extern "C" wrapper for cmocka include |
| unit/test-udp.c | Added extern "C" wrapper for cmocka include |
| unit/test-commonheader.c | Added extern "C" wrapper for cmocka include |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Avtp_Vss_SetDatatype(vss_pdu, VSS_STRING); | ||
| Avtp_Vss_SetVssData(vss_pdu, &data); | ||
| uint8_t len_in_mem[] = {0, strlen(str_value)}; | ||
| uint8_t len_in_mem[] = {0, (uint8_t)strlen(str_value)}; |
There was a problem hiding this comment.
The explicit cast to uint8_t could silently truncate if strlen returns a value larger than 255. While this is safe for the current test string "10m/s2" which is only 6 characters, consider adding a comment or assertion to document this size constraint, or using a safer pattern that validates the length before casting.
| test-udp test-commonheader) No newline at end of file | ||
| test-udp test-commonheader test-commonheader-cpp | ||
| test-vss-cpp test-tscf-cpp test-ntscf-cpp test-flexray-cpp test-lin-cpp | ||
| test-most-cpp test-gpc-cpp test-sensor-cpp test-udp-cpp |
There was a problem hiding this comment.
There is a trailing space at the end of this line that should be removed for consistency and to avoid potential linting issues.
| test-most-cpp test-gpc-cpp test-sensor-cpp test-udp-cpp | |
| test-most-cpp test-gpc-cpp test-sensor-cpp test-udp-cpp |
| VssData_t vss_data_get = {0}; | ||
| VssDataInt64Array_t vss_data_int64_arr_get = {0}; | ||
| vss_data_int64_arr_get.data = 0; | ||
|
|
There was a problem hiding this comment.
There is an inconsistent extra blank line here. Other similar code blocks in this file (e.g., lines 550-553, 630-633) don't have a blank line between the struct initialization and its usage. Remove this blank line for consistency.
| VssData_t vss_data_get = {0}; | ||
| VssDataFloatArray_t vss_data_float_arr_get = {0}; | ||
| vss_data_float_arr_get.data = 0; | ||
|
|
There was a problem hiding this comment.
There is an inconsistent extra blank line here. Other similar code blocks in this file (e.g., lines 550-553, 630-633) don't have a blank line between the struct initialization and its usage. Remove this blank line for consistency.
|
I think the additional build targets in CMakeLists.txt that have been added for C++ are redundant and hard to maintain (always need to mirror the C-based build targets). I'd suggest do modify the |
Hi @adriaan-niess. These additional build targets would have identified the error that caused issue #115. These run the tests alone in C++ mode while keeping the library in C, which is the primary deployment scenario for Open1722 for now. The idea of having a parameterised build to choose the compiler is elegant. However, chasing this avenue turned out to be far from trivial with the already convoluted CMake Scripts Open1722 has. I would be happy if you can make a proposal. |
Signed-off-by: Naresh Nayak <Naresh.Nayak@de.bosch.com>
|
One more improvement: I added a function (with help of Copilot) which will automatically generate the CPP test target. So there is no need of a manual "mirroring" of C-based test targets. |
This should avoid issues like #115 in the future.