Skip to content

Commit

Permalink
ztest: Add macros for comparing strings
Browse files Browse the repository at this point in the history
These macros allows us to compare strings in a simpler way.

Signed-off-by: Rubin Gerritsen <[email protected]>
  • Loading branch information
rugeGerritsen authored and fabiobaltieri committed Jun 11, 2024
1 parent 4d2e464 commit a35d5e7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
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);
}

0 comments on commit a35d5e7

Please sign in to comment.