1 /*
2  * Copyright (c) 2023 Codecoup
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef MOCKS_UTIL_H_
8 #define MOCKS_UTIL_H_
9 #include <stddef.h>
10 #include <stdint.h>
11 
12 #include <stdint.h>
13 
14 #include <zephyr/sys/util_internal.h>
15 #include <zephyr/types.h>
16 #include <zephyr/sys/util_macro.h>
17 #include <zephyr/sys/util_internal.h>
18 #include <zephyr/ztest.h>
19 #include <zephyr/ztest_assert.h>
20 
21 #define CHECK_EMPTY(_x) UTIL_BOOL(IS_EMPTY(_x))
22 #define COND_CODE_EMPTY(_x, _if_any_code, _else_code)                                              \
23 	COND_CODE_1(CHECK_EMPTY(_x), _if_any_code, _else_code)
24 #define IF_EMPTY(_a, _code) COND_CODE_EMPTY(_a, _code, ())
25 #define IF_NOT_EMPTY(_a, _code) COND_CODE_EMPTY(_a, (), _code)
26 
27 #define expect_data(_func_name, _arg_name, _exp_data, _data, _len)                                 \
28 	IF_NOT_EMPTY(_exp_data, (expect_data_equal(_func_name, _arg_name, _exp_data, _data, _len);))
29 
30 #define zexpect_call_count(_func_name, _expected, _actual)                                         \
31 	zexpect_equal(_expected, _actual, "'%s()' was called %u times, expected %u times",         \
32 		      _func_name, _actual, _expected)
33 
expect_data_equal(const char * func_name,const char * arg_name,const uint8_t * expect,const uint8_t * data,size_t len)34 static inline void expect_data_equal(const char *func_name, const char *arg_name,
35 				     const uint8_t *expect, const uint8_t *data, size_t len)
36 {
37 	for (size_t i = 0U; i < len; i++) {
38 		zexpect_equal(expect[i], data[i],
39 			      "'%s()' was called with incorrect %s[%zu]=0x%02x != 0x%02x value",
40 			      func_name, arg_name, i, data[i], expect[i]);
41 	}
42 }
43 
44 #endif /* MOCKS_UTIL_H_ */
45