Skip to content
Permalink
Browse files

log_msg test: rework test_log_std_msg() to better cope with 64-bit bu…

…ilds

Remove #ifdef's and use a loop to avoid duplicating the code.

Suggested by Krzysztof Chruscinski.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
  • Loading branch information...
Nicolas Pitre authored and aescolar committed Jun 10, 2019
1 parent 6987937 commit 1fd379da44b61d4f248958631a26eecce4bd38d1
Showing with 35 additions and 126 deletions.
  1. +35 −126 tests/subsys/logging/log_msg/src/log_msg_test.c
@@ -20,137 +20,46 @@ extern struct k_mem_slab log_msg_pool;
static const char my_string[] = "test_string";
void test_log_std_msg(void)
{
#ifdef CONFIG_64BIT
zassert_true(LOG_MSG_NARGS_SINGLE_CHUNK == 4,
"test assumes following setting");
#else
zassert_true(LOG_MSG_NARGS_SINGLE_CHUNK == 3,
"test assumes following setting");
#endif
zassert_equal(LOG_MSG_NARGS_SINGLE_CHUNK,
IS_ENABLED(CONFIG_64BIT) ? 4 : 3,
"test assumes following setting");

u32_t used_slabs = k_mem_slab_num_used_get(&log_msg_pool);
log_arg_t args[] = {1, 2, 3, 4, 5, 6};
struct log_msg *msg;

/* allocation of 0 argument fits in single buffer */
msg = log_msg_create_0(my_string);

zassert_equal((used_slabs + 1),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs++;

log_msg_put(msg);

zassert_equal((used_slabs - 1),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs--;

/* allocation of 1 argument fits in single buffer */
msg = log_msg_create_1(my_string, 1);
zassert_equal((used_slabs + 1),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs++;

log_msg_put(msg);

zassert_equal((used_slabs - 1),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs--;

/* allocation of 2 argument fits in single buffer */
msg = log_msg_create_2(my_string, 1, 2);
zassert_equal((used_slabs + 1),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs++;

log_msg_put(msg);

zassert_equal((used_slabs - 1),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs--;

/* allocation of 3 argument fits in single buffer */
msg = log_msg_create_3(my_string, 1, 2, 3);

zassert_equal((used_slabs + 1),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs++;

log_msg_put(msg);

zassert_equal((used_slabs - 1),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs--;

#ifdef CONFIG_64BIT
/* allocation of 4 argument fits in single buffer */
msg = log_msg_create_n(my_string, args, 4);

zassert_equal((used_slabs + 1),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs++;

log_msg_put(msg);

zassert_equal((used_slabs - 1),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs--;
#else
/* allocation of 4 argument fits in 2 buffers */
msg = log_msg_create_n(my_string, args, 4);

zassert_equal((used_slabs + 2U),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs += 2U;

log_msg_put(msg);

zassert_equal((used_slabs - 2U),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs -= 2U;
#endif

/* allocation of 5 argument fits in 2 buffers */
msg = log_msg_create_n(my_string, args, 5);

zassert_equal((used_slabs + 2U),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs += 2U;

log_msg_put(msg);

zassert_equal((used_slabs - 2U),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs -= 2U;

/* allocation of 5 argument fits in 2 buffers */
msg = log_msg_create_n(my_string, args, 6);

zassert_equal((used_slabs + 2U),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs += 2U;

log_msg_put(msg);

zassert_equal((used_slabs - 2U),
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
used_slabs -= 2U;
/* Test for expected buffer usage based on number of arguments */
for (int i = 0; i <= 6; i++) {
switch (i) {
case 0:
msg = log_msg_create_0(my_string);
break;
case 1:
msg = log_msg_create_1(my_string, 1);
break;
case 2:
msg = log_msg_create_2(my_string, 1, 2);
break;
case 3:
msg = log_msg_create_3(my_string, 1, 2, 3);
break;
default:
msg = log_msg_create_n(my_string, args, i);
break;
}

used_slabs += (i > LOG_MSG_NARGS_SINGLE_CHUNK) ? 2 : 1;
zassert_equal(used_slabs,
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");

log_msg_put(msg);

used_slabs -= (i > LOG_MSG_NARGS_SINGLE_CHUNK) ? 2 : 1;
zassert_equal(used_slabs,
k_mem_slab_num_used_get(&log_msg_pool),
"Expected mem slab allocation.");
}
}

void test_log_hexdump_msg(void)

0 comments on commit 1fd379d

Please sign in to comment.
You can’t perform that action at this time.