Files
nearby/cpp/platform/container_of_test.cc
T
Alexey Polyudov 204f76077d nearby: snapshot as of cl/296436629
Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: I2cf5bf225b76f4c1541954651f3a7544a14e0cec
2020-04-04 12:52:31 -07:00

48 lines
1.1 KiB
C++

#include "platform/container_of.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace location::nearby {
TEST(OffsetOf, OffsetOfTest) {
struct [[gnu::packed]] S {
char x;
double y;
};
EXPECT_EQ(OffsetOf(&S::x), 0U);
EXPECT_EQ(OffsetOf(&S::y), sizeof(S::x));
}
TEST(OffsetOf, ExplicitOffsetOfTest) {
struct [[gnu::packed]] S1 { int x; };
struct [[gnu::packed]] S2 { double y; };
struct [[gnu::packed]] S : public S1, S2 { char t; };
EXPECT_EQ((OffsetOf<decltype(std::declval<S>().x), S>(&S::x)), 0U);
EXPECT_EQ((OffsetOf<decltype(std::declval<S>().y), S>(&S::y)), sizeof(S::x));
}
TEST(ContainerOf, ContainerOfTest) {
struct [[gnu::packed]] S {
char x;
double y;
} s;
char* p = &s.x;
double* q = &s.y;
EXPECT_EQ(ContainerOf(p, &S::x), &s);
EXPECT_EQ(ContainerOf(q, &S::y), &s);
}
TEST(ContainerOf, ContainerOfTestConst) {
struct [[gnu::packed]] S {
char x;
double y;
} s;
const char* p = &s.x;
const double* q = &s.y;
EXPECT_EQ(ContainerOf(p, &S::x), &s);
EXPECT_EQ(ContainerOf(q, &S::y), &s);
}
} // namespace location::nearby