tests/ztest/mock: remove usage of legacy k_fifo_get()

Legacy FIFO operations were failing and thus the TC was failing to run.

Stop using k_fifo_get() for allocation and use a bitmap allocator. A
couple of the bitmap operations should be moved to a common header
once ZEP-1347 is completed.

Passes on all arches and boards, whitelist removed; ARM excluded
though due to missing bitfield implementation as per ZEP-82.

Note there is a false checkpatch positive in the decl of
sys_bitfield_find_first_clear().

Change-Id: I1d3ce8e988bf799573041026e419e3946d153590
Signed-off-by: Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
This commit is contained in:
Inaky Perez-Gonzalez 2016-11-15 17:25:44 -08:00 committed by Anas Nashif
parent b53e6d7774
commit 462c6efca4
2 changed files with 58 additions and 20 deletions

View file

@ -55,37 +55,75 @@ void _init_mock(void)
#else
static struct parameter params[CONFIG_ZTEST_PARAMETER_COUNT];
static struct k_fifo *fifo;
/*
* FIXME: move to sys_io.h once the argument signature for bitmap has
* been fixed to void* or similar ZEP-1347
*/
#define BITS_PER_UL (8 * sizeof(unsigned long int))
#define DEFINE_BITFIELD(name, bits) \
unsigned long int (name)[((bits) + BITS_PER_UL - 1) / BITS_PER_UL]
static void free_parameter(struct parameter *param)
static inline
int sys_bitfield_find_first_clear(const unsigned long *bitmap,
unsigned int bits)
{
if (param) {
k_fifo_put(fifo, param);
unsigned int words = (bits + BITS_PER_UL - 1) / BITS_PER_UL;
unsigned int cnt;
unsigned int long neg_bitmap;
/*
* By bitwise negating the bitmap, we are actually implemeting
* ffc (find first clear) using ffs (find first set).
*/
for (cnt = 0; cnt < words; cnt++) {
neg_bitmap = ~bitmap[cnt];
if (neg_bitmap == 0) /* all full */
continue;
else if (neg_bitmap == ~0UL) /* first bit */
return cnt * BITS_PER_UL;
else
return cnt * BITS_PER_UL + __builtin_ffsl(neg_bitmap);
}
return -1;
}
static struct parameter *alloc_parameter(void)
static DEFINE_BITFIELD(params_allocation, CONFIG_ZTEST_PARAMETER_COUNT);
static struct parameter params[CONFIG_ZTEST_PARAMETER_COUNT];
static
void free_parameter(struct parameter *param)
{
unsigned int allocation_index = param - params;
if (param == NULL)
return;
__ASSERT(allocation_index < CONFIG_ZTEST_PARAMETER_COUNT,
"param %p given to free is not in the static buffer %p:%u",
param, params, CONFIG_ZTEST_PARAMETER_COUNT);
sys_bitfield_clear_bit((mem_addr_t) params_allocation,
allocation_index);
}
static
struct parameter *alloc_parameter(void)
{
int allocation_index;
struct parameter *param;
param = k_fifo_get(fifo, K_NO_WAIT);
if (!param) {
PRINT("Failed to allocate mock parameter\n");
allocation_index = sys_bitfield_find_first_clear(
params_allocation, CONFIG_ZTEST_PARAMETER_COUNT);
if (allocation_index == -1) {
printk("No more mock parameters available for allocation\n");
ztest_test_fail();
}
sys_bitfield_set_bit((mem_addr_t) params_allocation, allocation_index);
param = params + allocation_index;
memset(param, 0, sizeof(*param));
return param;
}
void _init_mock(void)
{
int i;
k_fifo_init(fifo);
for (i = 0; i < CONFIG_ZTEST_PARAMETER_COUNT; i++) {
k_fifo_put(fifo, &params[i]);
}
}
#endif
@ -157,8 +195,7 @@ void _ztest_check_expected_value(const char *fn, const char *name,
* provide inttypes.h
*/
PRINT("%s received wrong value: Got %lu, expected %lu\n",
fn, (unsigned long)val,
(unsigned long)expected);
fn, (unsigned long)val, (unsigned long)expected);
ztest_test_fail();
}
}

View file

@ -1,6 +1,7 @@
[test]
tags = test_framework
arch_whitelist = x86 arc
# sys_bitfield_*() still not implemented for ARM, ZEP-82
arch_exclude = arm
[test_unit]
type = unit