diff --git a/core/pta/tests/misc.c b/core/pta/tests/misc.c index 63dbea48b70..c0f1f7fae75 100644 --- a/core/pta/tests/misc.c +++ b/core/pta/tests/misc.c @@ -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; @@ -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; }