1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

adds container_of

This commit is contained in:
Falk Werner 2019-04-03 22:49:12 +02:00
parent 87735c3ee2
commit 7069478408
3 changed files with 46 additions and 0 deletions

View File

@ -272,6 +272,7 @@ pkg_check_modules(GMOCK gmock)
add_executable(alltests
test/msleep.cc
test/mock_authenticator.cc
test/test_container_of.cc
test/test_response_parser.cc
test/test_server.cc
test/test_timepoint.cc

View File

@ -0,0 +1,16 @@
#ifndef WF_CONTAINER_OF_H
#define WF_CONTAINER_OF_H
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
#endif
#define WF_CONTAINER_OF(pointer, type, member) \
({ \
const typeof( ((type *)0)->member ) *__mptr = (pointer); \
(type *)( (char *)__mptr - offsetof(type, member) ); \
})
#endif

29
test/test_container_of.cc Normal file
View File

@ -0,0 +1,29 @@
#include <gtest/gtest.h>
#include "webfuse/core/container_of.h"
namespace
{
struct MyStruct
{
int first;
int second;
};
}
TEST(ContainerOf, FirstMember)
{
MyStruct my_struct = {23, 42};
int * first = &my_struct.first;
ASSERT_EQ(&my_struct, WF_CONTAINER_OF(first, MyStruct, first));
}
TEST(ContainerOf, SecondMember)
{
MyStruct my_struct = {23, 42};
int * second = &my_struct.second;
ASSERT_EQ(&my_struct, WF_CONTAINER_OF(second, MyStruct, second));
}