mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
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)))
|
||||
#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
|
||||
* @brief The *NORETURN* keyword tells the compiler to assume that the function
|
||||
|
@ -46,10 +46,20 @@ static void test_kernel_version(void)
|
||||
#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)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
new_TestFixture(test_kernel_version),
|
||||
new_TestFixture(test_index_of),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(kernel_defines_tests, NULL, NULL, fixtures);
|
||||
|
Loading…
Reference in New Issue
Block a user