Files
bacnet_stack/test/ztest/include/zephyr/sys/util.h
T
Greg Shue c74a8cf3a9 Feature/bacnet bacerror unit tests (#407)
* Add <zephyr/fff.h> from zephyr v3.1.0

Zephyr is deprecating the zmock library in favor of using
the FFF faking library for defining and controlling fake
implementations of depended-upon APIs called by the code-under-test.

Signed-off-by: Gregory Shue <gregory.shue@legrand.com>

* Fix bacerror_decode_service_request return value; add unit tests

bacerror_decode_service_request() return value now includes
the apdu bytes parsed for invoke_id and service.

Also added a unit test for functions in bacerror.c, using `fff`
for faking the depended-upon functions.

Verified by:

1. (Pass) cd $bacnet-stack/test && make test

2. (Pass) west build -p always -b unit_testing \
              bacnet-stack/zephyr/tests/unit/bacnet/bacerror/ && \
              ./build/testbinary

3. (Pass) ./zephyr/scripts/twister -p unit_testing  \
              -T bacnet-stack/zephyr/tests/unit/bacnet/bacerror/

Signed-off-by: Gregory Shue <gregory.shue@legrand.com>

---------

Signed-off-by: Gregory Shue <gregory.shue@legrand.com>
Co-authored-by: Gregory Shue <gregory.shue@legrand.com>
2023-03-07 17:01:33 -06:00

105 lines
2.7 KiB
C

/*
* Copyright (c) 2011-2014, Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*
* Content copied out of <zephyr/sys/util.h> from Zephyr v3.2.0.
* Copyright and licensing retained.
*/
#ifndef BACNETSTACK_TEST_ZTEST_INCLUDE_ZEPHYR_SYS_UTIL_H_
#define BACNETSTACK_TEST_ZTEST_INCLUDE_ZEPHYR_SYS_UTIL_H_
/** @brief 0 if @p cond is true-ish; causes a compile error otherwise. */
#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
#if defined(__cplusplus)
/* The built-in function used below for type checking in C is not
* supported by GNU C++.
*/
#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
#else /* __cplusplus */
/**
* @brief Zero if @p array has an array type, a compile error otherwise
*
* This macro is available only from C, not C++.
*/
#define IS_ARRAY(array) \
ZERO_OR_COMPILE_ERROR( \
!__builtin_types_compatible_p(__typeof__(array), \
__typeof__(&(array)[0])))
/**
* @brief Number of elements in the given @p array
*
* In C++, due to language limitations, this will accept as @p array
* any type that implements <tt>operator[]</tt>. The results may not be
* particularly meaningful in this case.
*
* In C, passing a pointer as @p array causes a compile error.
*/
#define ARRAY_SIZE(array) \
((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
#endif /* __cplusplus */
/**
* @brief Get a pointer to a structure containing the element
*
* Example:
*
* struct foo {
* int bar;
* };
*
* struct foo my_foo;
* int *ptr = &my_foo.bar;
*
* struct foo *container = CONTAINER_OF(ptr, struct foo, bar);
*
* Above, @p container points at @p my_foo.
*
* @param ptr pointer to a structure element
* @param type name of the type that @p ptr is an element of
* @param field the name of the field within the struct @p ptr points to
* @return a pointer to the structure that contains @p ptr
*/
#define CONTAINER_OF(ptr, type, field) \
((type *)(((char *)(ptr)) - offsetof(type, field)))
#ifndef MAX
/**
* @brief Obtain the maximum of two values.
*
* @note Arguments are evaluated twice. Use Z_MAX for a GCC-only, single
* evaluation version
*
* @param a First value.
* @param b Second value.
*
* @returns Maximum value of @p a and @p b.
*/
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef MIN
/**
* @brief Obtain the minimum of two values.
*
* @note Arguments are evaluated twice. Use Z_MIN for a GCC-only, single
* evaluation version
*
* @param a First value.
* @param b Second value.
*
* @returns Minimum value of @p a and @p b.
*/
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#endif /* BACNETSTACK_TEST_ZTEST_INCLUDE_ZEPHYR_SYS_UTIL_H_ */