Skip to content
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

zcbor_common.h: Add ZCBOR_VERSION macro #386

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/zcbor_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
extern "C" {
#endif

#ifndef STRINGIFY
#define STRINGIFY(x) #x
#endif

#define ZCBOR_VERSION_MAJOR 0
#define ZCBOR_VERSION_MINOR 8
#define ZCBOR_VERSION_BUGFIX 99

/** The version string with dots and not prefix. */
#define ZCBOR_VERSION_STR STRINGIFY(ZCBOR_VERSION_MAJOR) \
"." STRINGIFY(ZCBOR_VERSION_MINOR) \
"." STRINGIFY(ZCBOR_VERSION_BUGFIX)

/** Monotonically increasing integer representing the version. */
#define ZCBOR_VERSION ((ZCBOR_VERSION_MAJOR << 24) \
+ (ZCBOR_VERSION_MINOR << 16) \
+ (ZCBOR_VERSION_BUGFIX << 8))

/** Convenience type that allows pointing to strings directly inside the payload
* without the need to copy out.
*/
Expand Down
17 changes: 15 additions & 2 deletions tests/unit/test1_unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ target_include_directories(zcbor PRIVATE ../../../include)

target_link_libraries(app PRIVATE zcbor)

zephyr_compile_definitions(ZCBOR_STOP_ON_ERROR)

target_compile_options(zcbor PRIVATE -Wpedantic -Wconversion -Wall -Wextra -Wdouble-promotion)

file(READ ${CMAKE_CURRENT_LIST_DIR}/../../../zcbor/VERSION ZCBOR_VERSION_STR)
string(REPLACE "." ";" ZCBOR_VERSION_SPLIT ${ZCBOR_VERSION_STR})
list(GET ZCBOR_VERSION_SPLIT 0 ZCBOR_VERSION_MAJOR)
list(GET ZCBOR_VERSION_SPLIT 1 ZCBOR_VERSION_MINOR)
list(GET ZCBOR_VERSION_SPLIT 2 ZCBOR_VERSION_BUGFIX)
math(EXPR ZCBOR_VERSION "(${ZCBOR_VERSION_MAJOR} << 24) + (${ZCBOR_VERSION_MINOR} << 16) + (${ZCBOR_VERSION_BUGFIX} << 8)")

zephyr_compile_definitions(
ZCBOR_STOP_ON_ERROR
TEST_ZCBOR_VERSION_STR="${ZCBOR_VERSION_STR}"
TEST_ZCBOR_VERSION=${ZCBOR_VERSION}
TEST_ZCBOR_VERSION_MAJOR=${ZCBOR_VERSION_MAJOR}
TEST_ZCBOR_VERSION_MINOR=${ZCBOR_VERSION_MINOR}
TEST_ZCBOR_VERSION_BUGFIX=${ZCBOR_VERSION_BUGFIX})
14 changes: 14 additions & 0 deletions tests/unit/test1_unit_tests/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1420,4 +1420,18 @@ ZTEST(zcbor_unit_tests, test_canonical_check)
}


ZTEST(zcbor_unit_tests, test_zcbor_version)
{
const char zcbor_version_str[] = ZCBOR_VERSION_STR;
const char zcbor_version_expected[] = TEST_ZCBOR_VERSION_STR;


zassert_mem_equal(zcbor_version_expected, zcbor_version_str, sizeof(zcbor_version_expected));
zassert_equal(TEST_ZCBOR_VERSION, ZCBOR_VERSION, NULL);
zassert_equal(TEST_ZCBOR_VERSION_MAJOR, ZCBOR_VERSION_MAJOR, NULL);
zassert_equal(TEST_ZCBOR_VERSION_MINOR, ZCBOR_VERSION_MINOR, NULL);
zassert_equal(TEST_ZCBOR_VERSION_BUGFIX, ZCBOR_VERSION_BUGFIX, NULL);
}


ZTEST_SUITE(zcbor_unit_tests, NULL, NULL, NULL, NULL, NULL);