Commit aa2e315a authored by Krzysztof Chruściński's avatar Krzysztof Chruściński Committed by Carles Cufi
Browse files

tests: unit: cbprintf: Add tests for string validation



Add tests for Z_CBPRINTF_NONE_CHAR_PTR_COUNT, Z_CBPRINTF_P_COUNT
and Z_CBPRINTF_POINTERS_VALIDATE.

Signed-off-by: default avatarKrzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
parent f9a54755
Loading
Loading
Loading
Loading
+99 −0
Original line number Diff line number Diff line
@@ -1338,6 +1338,105 @@ ZTEST(prf, test_nop)
{
}

ZTEST(prf, test_is_none_char_ptr)
{
	char c = 0;
	const char cc = 0;
	volatile char vc = 0;
	volatile const char vcc = 0;

	unsigned char uc = 0;
	const unsigned char cuc = 0;
	volatile unsigned char vuc = 0;
	volatile const unsigned char vcuc = 0;

	short s = 0;
	unsigned short us = 0;

	int i = 0;
	unsigned int ui = 0;

	long l = 0;
	unsigned long ul = 0;

	long long ll = 0;
	unsigned long long ull = 0;

	float f = 0.1;
	double d = 0.1;

	_Pragma("GCC diagnostic push")
	_Pragma("GCC diagnostic ignored \"-Wpointer-arith\"")
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(c), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(cc), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(vc), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(vcc), 0);

	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&c), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&cc), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&vc), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&vcc), 0);

	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(uc), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(cuc), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(vuc), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(vcuc), 0);

	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&uc), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&cuc), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&vuc), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&vcuc), 0);

	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(s), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(us), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&s), 1);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&us), 1);

	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(i), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(ui), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&i), 1);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&ui), 1);

	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(l), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(ul), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&l), 1);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&ul), 1);

	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(ll), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(ull), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&ll), 1);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&ull), 1);

	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(f), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(d), 0);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&f), 1);
	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR(&d), 1);

	zassert_equal(Z_CBPRINTF_IS_NONE_CHAR_PTR((void *)&c), 1);

	_Pragma("GCC diagnostic pop")
}

ZTEST(prf, test_p_count)
{
	zassert_equal(Z_CBPRINTF_P_COUNT("no pointers"), 0);
	zassert_equal(Z_CBPRINTF_P_COUNT("no %%p pointers"), 0);

	zassert_equal(Z_CBPRINTF_P_COUNT("%d %%p %x %s %p %f"), 1);
	zassert_equal(Z_CBPRINTF_P_COUNT("%p %p %llx %p "), 3);
}

ZTEST(prf, test_pointers_validate)
{
	_Pragma("GCC diagnostic push")
	_Pragma("GCC diagnostic ignored \"-Wpointer-arith\"")
	zassert_equal(Z_CBPRINTF_POINTERS_VALIDATE("no arguments"), true);
	/* const char fails validation */
	zassert_equal(Z_CBPRINTF_POINTERS_VALIDATE("%p", "string"), false);
	zassert_equal(Z_CBPRINTF_POINTERS_VALIDATE("%p", (void *)"string"), true);
	_Pragma("GCC diagnostic pop")
}

static void *cbprintf_setup(void)
{
	if (sizeof(int) == 4) {