Skip to content

Commit

Permalink
pta: test: add selftest cases for rounded operations
Browse files Browse the repository at this point in the history
Add selftest cases for ROUNDUP() and ROUNDDOWN() operations.

Signed-off-by: Etienne Carriere <[email protected]>
  • Loading branch information
etienne-lms committed Dec 16, 2024
1 parent b5160f0 commit e1cceb7
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion core/pta/tests/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,58 @@
*/
#define LOG(...)

static int self_test_rounded_operations(void)
{
const size_t tested_size[] = { 6, 7, 8 };

size_t awaited = 0;
size_t value = 0;
size_t size = 0;
size_t n = 0;
int rc = 0;

for (n = 0; n < ARRAY_SIZE(tested_size); n++) {
size = tested_size[n];
for (value = 0; value <= 2 * size; value++) {
if (!value)
awaited = 0;
else if (value <= size)
awaited = size;
else
awaited = 2 * size;

if (ROUNDUP(value, size) != awaited) {
EMSG("Roundup (%zu %zu): %zu != %zu",
value, size, ROUNDUP(value, size), awaited);
rc = 1;
break;
}
}
}

for (n = 0; n < ARRAY_SIZE(tested_size); n++) {
size = tested_size[n];
for (value = 0; value <= 2 * size; value++) {
if (value < size)
awaited = 0;
else if (value < size * 2)
awaited = size;
else
awaited = 2 * size;

if (ROUNDDOWN(value, size) != awaited) {
EMSG("Rounddown (%zu %zu): %zu != %zu",
value, size, ROUNDDOWN(value, size),
awaited);
rc = 1;
break;
}
}
}

return rc;
}

static int self_test_add_overflow(void)
{
uint32_t r_u32;
Expand Down Expand Up @@ -558,7 +610,7 @@ TEE_Result core_self_tests(uint32_t nParamTypes __unused,
if (self_test_mul_signed_overflow() || self_test_add_overflow() ||
self_test_sub_overflow() || self_test_mul_unsigned_overflow() ||
self_test_division() || self_test_malloc() ||
self_test_nex_malloc()) {
self_test_nex_malloc() || self_test_rounded_operations()) {
EMSG("some self_test_xxx failed! you should enable local LOG");
return TEE_ERROR_GENERIC;
}
Expand Down

0 comments on commit e1cceb7

Please sign in to comment.