Commit a35d5e78 authored by Rubin Gerritsen's avatar Rubin Gerritsen Committed by Fabio Baltieri
Browse files

ztest: Add macros for comparing strings



These macros allows us to compare strings in a simpler way.

Signed-off-by: default avatarRubin Gerritsen <rubin.gerritsen@nordicsemi.no>
parent 4d2e4647
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -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__)

/**
 * @}
 */
@@ -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__)

/**
 * @}
 */
@@ -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__)

/**
 * @}
 */
+17 −0
Original line number Diff line number Diff line
@@ -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)
{
+17 −0
Original line number Diff line number Diff line
@@ -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);
}