Skip to content

Commit 09f4f54

Browse files
inaky-intcnashif
authored andcommitted
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: I5d43f804d6bec3a464124accbe3be238f9cade82 Signed-off-by: Inaky Perez-Gonzalez <[email protected]> Signed-off-by: Anas Nashif <[email protected]>
1 parent c2fe55b commit 09f4f54

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

tests/ztest/src/ztest_mock.c

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,37 +55,75 @@ void _init_mock(void)
5555

5656
#else
5757

58-
static struct parameter params[CONFIG_ZTEST_PARAMETER_COUNT];
59-
static struct k_fifo *fifo;
58+
/*
59+
* FIXME: move to sys_io.h once the argument signature for bitmap has
60+
* been fixed to void* or similar ZEP-1347
61+
*/
62+
#define BITS_PER_UL (8 * sizeof(unsigned long int))
63+
#define DEFINE_BITFIELD(name, bits) \
64+
unsigned long int (name)[((bits) + BITS_PER_UL - 1) / BITS_PER_UL]
6065

61-
static void free_parameter(struct parameter *param)
66+
static inline
67+
int sys_bitfield_find_first_clear(const unsigned long *bitmap,
68+
unsigned int bits)
6269
{
63-
if (param) {
64-
k_fifo_put(fifo, param);
70+
unsigned int words = (bits + BITS_PER_UL - 1) / BITS_PER_UL;
71+
unsigned int cnt;
72+
unsigned int long neg_bitmap;
73+
74+
/*
75+
* By bitwise negating the bitmap, we are actually implemeting
76+
* ffc (find first clear) using ffs (find first set).
77+
*/
78+
for (cnt = 0; cnt < words; cnt++) {
79+
neg_bitmap = ~bitmap[cnt];
80+
if (neg_bitmap == 0) /* all full */
81+
continue;
82+
else if (neg_bitmap == ~0UL) /* first bit */
83+
return cnt * BITS_PER_UL;
84+
else
85+
return cnt * BITS_PER_UL + __builtin_ffsl(neg_bitmap);
6586
}
87+
return -1;
6688
}
67-
static struct parameter *alloc_parameter(void)
89+
90+
static DEFINE_BITFIELD(params_allocation, CONFIG_ZTEST_PARAMETER_COUNT);
91+
static struct parameter params[CONFIG_ZTEST_PARAMETER_COUNT];
92+
93+
static
94+
void free_parameter(struct parameter *param)
6895
{
96+
unsigned int allocation_index = param - params;
97+
98+
if (param == NULL)
99+
return;
100+
__ASSERT(allocation_index < CONFIG_ZTEST_PARAMETER_COUNT,
101+
"param %p given to free is not in the static buffer %p:%u",
102+
param, params, CONFIG_ZTEST_PARAMETER_COUNT);
103+
sys_bitfield_clear_bit((mem_addr_t) params_allocation,
104+
allocation_index);
105+
}
106+
107+
static
108+
struct parameter *alloc_parameter(void)
109+
{
110+
int allocation_index;
69111
struct parameter *param;
70112

71-
param = k_fifo_get(fifo, K_NO_WAIT);
72-
if (!param) {
73-
PRINT("Failed to allocate mock parameter\n");
113+
allocation_index = sys_bitfield_find_first_clear(
114+
params_allocation, CONFIG_ZTEST_PARAMETER_COUNT);
115+
if (allocation_index == -1) {
116+
printk("No more mock parameters available for allocation\n");
74117
ztest_test_fail();
75118
}
76-
119+
sys_bitfield_set_bit((mem_addr_t) params_allocation, allocation_index);
120+
param = params + allocation_index;
121+
memset(param, 0, sizeof(*param));
77122
return param;
78123
}
79124

80125
void _init_mock(void)
81126
{
82-
int i;
83-
84-
k_fifo_init(fifo);
85-
for (i = 0; i < CONFIG_ZTEST_PARAMETER_COUNT; i++) {
86-
87-
k_fifo_put(fifo, &params[i]);
88-
}
89127
}
90128

91129
#endif
@@ -157,8 +195,7 @@ void _ztest_check_expected_value(const char *fn, const char *name,
157195
* provide inttypes.h
158196
*/
159197
PRINT("%s received wrong value: Got %lu, expected %lu\n",
160-
fn, (unsigned long)val,
161-
(unsigned long)expected);
198+
fn, (unsigned long)val, (unsigned long)expected);
162199
ztest_test_fail();
163200
}
164201
}

tests/ztest/test/mock/testcase.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[test]
22
tags = test_framework
3-
arch_whitelist = x86 arc
3+
# sys_bitfield_*() still not implemented for ARM, ZEP-82
4+
arch_exclude = arm
45

56
[test_unit]
67
type = unit

0 commit comments

Comments
 (0)