Merge pull request #17142 from benpicco/index_of
core/include/kernel_defines.h: add index_of() macro
This commit is contained in:
commit
321918ce02
@ -63,6 +63,16 @@ extern "C" {
|
|||||||
((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER)))
|
((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER)))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @def index_of(ARRAY, ELEMENT)
|
||||||
|
* @brief Returns the index of a pointer to an array element.
|
||||||
|
|
||||||
|
* @param[in] ARRAY an array
|
||||||
|
* @param[in] ELEMENT pointer to an array element
|
||||||
|
* @return Index of the element in the array
|
||||||
|
*/
|
||||||
|
#define index_of(ARRAY, ELEMENT) (((uintptr_t)(ELEMENT) - (uintptr_t)(ARRAY)) / sizeof(*(ELEMENT)))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @def NORETURN
|
* @def NORETURN
|
||||||
* @brief The *NORETURN* keyword tells the compiler to assume that the function
|
* @brief The *NORETURN* keyword tells the compiler to assume that the function
|
||||||
|
|||||||
@ -46,10 +46,20 @@ static void test_kernel_version(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_index_of(void)
|
||||||
|
{
|
||||||
|
unsigned foo[8];
|
||||||
|
uint8_t bar[32];
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(5, index_of(foo, &foo[5]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(17, index_of(bar, &bar[17]));
|
||||||
|
}
|
||||||
|
|
||||||
Test *tests_kernel_defines_tests(void)
|
Test *tests_kernel_defines_tests(void)
|
||||||
{
|
{
|
||||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||||
new_TestFixture(test_kernel_version),
|
new_TestFixture(test_kernel_version),
|
||||||
|
new_TestFixture(test_index_of),
|
||||||
};
|
};
|
||||||
|
|
||||||
EMB_UNIT_TESTCALLER(kernel_defines_tests, NULL, NULL, fixtures);
|
EMB_UNIT_TESTCALLER(kernel_defines_tests, NULL, NULL, fixtures);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user