Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | /*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
#include <zephyr/types.h>
#include <string.h>
#include <stdio.h>
struct parameter {
struct parameter *next;
const char *fn;
const char *name;
uintptr_t value;
};
#ifndef KERNEL
#include <stdlib.h>
#include <stdarg.h>
static void free_parameter(struct parameter *param)
{
free(param);
}
static struct parameter *alloc_parameter(void)
{
struct parameter *param;
param = calloc(1, sizeof(struct parameter));
if (!param) {
PRINT("Failed to allocate mock parameter\n");
ztest_test_fail();
}
return param;
}
void z_init_mock(void)
{
}
void printk(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
void vprintk(const char *fmt, va_list ap)
{
vprintf(fmt, ap);
}
#else
/*
* FIXME: move to sys_io.h once the argument signature for bitmap has
* been fixed to void* or similar GH-2825
*/
#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 inline int sys_bitfield_find_first_clear(const unsigned long *bitmap,
const unsigned int bits)
{
const size_t words = (bits + BITS_PER_UL - 1) / BITS_PER_UL;
size_t cnt;
unsigned int long neg_bitmap;
/*
* By bitwise negating the bitmap, we are actually implementing
* 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. Try next word. */
continue;
} else if (neg_bitmap == ~0UL) {
/* First bit is free */
return cnt * BITS_PER_UL;
} else {
const unsigned int bit = (cnt * BITS_PER_UL) +
__builtin_ffsl(neg_bitmap) - 1;
/* Ensure first free bit is within total bits count */
if (bit < bits) {
return bit;
} else {
return -1;
}
}
}
return -1;
}
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;
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;
(void)memset(param, 0, sizeof(*param));
return param;
}
void z_init_mock(void)
{
}
#endif
static struct parameter *find_and_delete_value(struct parameter *param,
const char *fn, const char *name)
{
struct parameter *value;
if (!param->next) {
return NULL;
}
if (strcmp(param->next->name, name) || strcmp(param->next->fn, fn)) {
return find_and_delete_value(param->next, fn, name);
}
value = param->next;
param->next = param->next->next;
value->next = NULL;
return value;
}
static void insert_value(struct parameter *param, const char *fn,
const char *name, uintptr_t val)
{
struct parameter *value;
value = alloc_parameter();
value->fn = fn;
value->name = name;
value->value = val;
/* Seek to end of linked list to ensure correct discovery order in find_and_delete_value */
while (param->next) {
param = param->next;
}
/* Append to end of linked list */
value->next = param->next;
param->next = value;
}
static struct parameter parameter_list = { NULL, "", "", 0 };
static struct parameter return_value_list = { NULL, "", "", 0 };
void z_ztest_expect_value(const char *fn, const char *name, uintptr_t val)
{
insert_value(¶meter_list, fn, name, val);
}
void z_ztest_check_expected_value(const char *fn, const char *name,
uintptr_t val)
{
struct parameter *param;
uintptr_t expected;
param = find_and_delete_value(¶meter_list, fn, name);
if (!param) {
PRINT("Failed to find parameter %s for %s\n", name, fn);
ztest_test_fail();
}
expected = param->value;
free_parameter(param);
if (expected != val) {
/* We need to cast these values since the toolchain doesn't
* provide inttypes.h
*/
PRINT("%s:%s received wrong value: Got %lu, expected %lu\n", fn,
name, (unsigned long)val, (unsigned long)expected);
ztest_test_fail();
}
}
void z_ztest_expect_data(const char *fn, const char *name, void *val)
{
insert_value(¶meter_list, fn, name, (uintptr_t)val);
}
void z_ztest_check_expected_data(const char *fn, const char *name, void *data,
uint32_t length)
{
struct parameter *param;
void *expected;
param = find_and_delete_value(¶meter_list, fn, name);
if (!param) {
PRINT("Failed to find parameter %s for %s\n", name, fn);
/* No return from this function but for coverity reasons
* put a return after to avoid the warning of a null
* dereference of param below.
*/
ztest_test_fail();
return;
}
expected = (void *)param->value;
free_parameter(param);
if (expected == NULL && data != NULL) {
PRINT("%s:%s received null pointer\n", fn, name);
ztest_test_fail();
} else if (data == NULL && expected != NULL) {
PRINT("%s:%s received data while expected null pointer\n", fn,
name);
ztest_test_fail();
} else if (data != NULL) {
if (memcmp(data, expected, length) != 0) {
PRINT("%s:%s data provided don't match\n", fn, name);
ztest_test_fail();
}
}
}
void z_ztest_returns_value(const char *fn, uintptr_t value)
{
insert_value(&return_value_list, fn, "", value);
}
uintptr_t z_ztest_get_return_value(const char *fn)
{
uintptr_t value;
struct parameter *param =
find_and_delete_value(&return_value_list, fn, "");
if (!param) {
PRINT("Failed to find return value for function %s\n", fn);
ztest_test_fail();
}
value = param->value;
free_parameter(param);
return value;
}
static void free_param_list(struct parameter *param)
{
struct parameter *next;
while (param) {
next = param->next;
free_parameter(param);
param = next;
}
}
int z_cleanup_mock(void)
{
int fail = 0;
if (parameter_list.next) {
PRINT("Parameter not used by mock: %s:%s\n",
parameter_list.next->fn,
parameter_list.next->name);
fail = 1;
}
if (return_value_list.next) {
PRINT("Return value no used by mock: %s\n",
return_value_list.next->fn);
fail = 2;
}
free_param_list(parameter_list.next);
free_param_list(return_value_list.next);
parameter_list.next = NULL;
return_value_list.next = NULL;
return fail;
}
|