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 
10 #include <zephyr/types.h>
11 #include <zephyr/sys/util_macro.h>
12 #include <zephyr/ztest.h>
13 
14 #define CHECK_EMPTY(_x) UTIL_BOOL(IS_EMPTY(_x))
15 #define COND_CODE_EMPTY(_x, _if_any_code, _else_code)                                              \
16 	COND_CODE_1(CHECK_EMPTY(_x), _if_any_code, _else_code)
17 #define IF_EMPTY(_a, _code) COND_CODE_EMPTY(_a, _code, ())
18 #define IF_NOT_EMPTY(_a, _code) COND_CODE_EMPTY(_a, (), _code)
19 
20 #define expect_data(_func_name, _arg_name, _exp_data, _data, _len)                                 \
21 	IF_NOT_EMPTY(_exp_data, (expect_data_equal(_func_name, _arg_name, _exp_data, _data, _len);))
22 
23 #define zexpect_call_count(_func_name, _expected, _actual)                                         \
24 	zexpect_equal(_expected, _actual, "'%s()' was called %u times, expected %u times",         \
25 		      _func_name, _actual, _expected)
26 
expect_data_equal(const char * func_name,const char * arg_name,const uint8_t * expect,const uint8_t * data,size_t len)27 static inline void expect_data_equal(const char *func_name, const char *arg_name,
28 				     const uint8_t *expect, const uint8_t *data, size_t len)
29 {
30 	for (size_t i = 0U; i < len; i++) {
31 		zexpect_equal(expect[i], data[i],
32 			      "'%s()' was called with incorrect %s[%zu]=0x%02x != 0x%02x value",
33 			      func_name, arg_name, i, data[i], expect[i]);
34 	}
35 }
36 
37 #endif /* MOCKS_UTIL_H_ */
38