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

ztest: Add macros for comparing strings #73841

Merged
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
31 changes: 31 additions & 0 deletions subsys/testsuite/ztest/include/zephyr/ztest_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,16 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
#define zassert_mem_equal__(buf, exp, size, ...) \
zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)

/**
* @brief Assert that 2 strings have the same contents
*
* @param s1 The first string
* @param s2 The second string
* @param ... Optional message and variables to print if the expectation fails
*/
#define zassert_str_equal(s1, s2, ...) \
zassert(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)

/**
* @}
*/
Expand Down Expand Up @@ -557,6 +567,16 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
#define zassume_mem_equal__(buf, exp, size, ...) \
zassume(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)

/**
* @brief Assumes that 2 strings have the same contents
*
* @param s1 The first string
* @param s2 The second string
* @param ... Optional message and variables to print if the expectation fails
*/
#define zassume_str_equal(s1, s2, ...) \
zassume(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)

/**
* @}
*/
Expand Down Expand Up @@ -691,6 +711,17 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
#define zexpect_mem_equal(buf, exp, size, ...) \
zexpect(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)

/**
* @brief Expect that 2 strings have the same contents, otherwise mark test as failed but
* continue its execution.
*
* @param s1 The first string
* @param s2 The second string
* @param ... Optional message and variables to print if the expectation fails
*/
#define zexpect_str_equal(s1, s2, ...) \
zexpect(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)

/**
* @}
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/ztest/base/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ ZTEST(framework_tests, test_assert_mem_equal)
zassert_mem_equal(actual, expected, sizeof(expected), NULL);
}

ZTEST(framework_tests, test_assert_str_equal)
{
const char *s1 = "asdf";
const char s2[] = {'a', 's', 'd', 'f', '\0'};

zassert_str_equal(s1, s2);
}

ZTEST_EXPECT_FAIL(framework_tests, test_assert_str_equal_fail);
ZTEST(framework_tests, test_assert_str_equal_fail)
{
const char *s1 = "asdf";
const char s2[] = {'a', 's', 'd', 'f', 'q', '\0'};

zassert_str_equal(s1, s2);
}

ZTEST_EXPECT_SKIP(framework_tests, test_skip_config);
ZTEST(framework_tests, test_skip_config)
{
Expand Down
17 changes: 17 additions & 0 deletions tests/ztest/zexpect/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,20 @@ ZTEST(expect, test_fail_expect_between_inclusive)
zexpect_between_inclusive(5, 0, 4);
zexpect_between_inclusive(5, 6, 10);
}

ZTEST(expect, test_expect_str_equal)
{
const char *s1 = "asdf";
const char s2[] = {'a', 's', 'd', 'f', '\0'};

zexpect_str_equal(s1, s2);
}

ZTEST_EXPECT_FAIL(expect, test_expect_str_equal_fail);
ZTEST(expect, test_expect_str_equal_fail)
{
const char *s1 = "asdf";
const char s2[] = {'a', 's', 'd', 'f', 'q', '\0'};

zexpect_str_equal(s1, s2);
}
Loading