object-introspection/test/test_alignment_calc.cpp
Alastair Robertson e1b16a3d7e TypeGraph: Switch from pointers to references
References must always have a value, so are semantically clearer than
pointers for variables which must always be set.

No functional changes.
2023-07-06 17:24:33 +01:00

84 lines
2.8 KiB
C++

#include <gtest/gtest.h>
#include "oi/type_graph/AlignmentCalc.h"
#include "test/type_graph_utils.h"
using namespace type_graph;
TEST(AlignmentCalcTest, PrimitiveMembers) {
auto myclass = Class{0, Class::Kind::Class, "MyClass", 16};
auto myint8 = Primitive{Primitive::Kind::Int8};
auto myint64 = Primitive{Primitive::Kind::Int64};
myclass.members.push_back(Member{myint8, "n", 0});
myclass.members.push_back(Member{myint64, "n", 8 * 8});
test(AlignmentCalc::createPass(), {myclass}, R"(
[0] Class: MyClass (size: 16, align: 8)
Member: n (offset: 0, align: 1)
Primitive: int8_t
Member: n (offset: 8, align: 8)
Primitive: int64_t
)");
}
TEST(AlignmentCalcTest, StructMembers) {
auto mystruct = Class{1, Class::Kind::Struct, "MyStruct", 8};
auto myint32 = Primitive{Primitive::Kind::Int32};
mystruct.members.push_back(Member{myint32, "n1", 0});
mystruct.members.push_back(Member{myint32, "n2", 4 * 8});
auto myclass = Class{0, Class::Kind::Class, "MyClass", 12};
auto myint8 = Primitive{Primitive::Kind::Int8};
myclass.members.push_back(Member{myint8, "n", 0});
myclass.members.push_back(Member{mystruct, "s", 4 * 8});
test(AlignmentCalc::createPass(), {myclass}, R"(
[0] Class: MyClass (size: 12, align: 4)
Member: n (offset: 0, align: 1)
Primitive: int8_t
Member: s (offset: 4, align: 4)
[1] Struct: MyStruct (size: 8, align: 4)
Member: n1 (offset: 0, align: 4)
Primitive: int32_t
Member: n2 (offset: 4, align: 4)
Primitive: int32_t
)");
}
TEST(AlignmentCalcTest, StructInContainer) {
auto myclass = Class{1, Class::Kind::Class, "MyClass", 16};
auto myint8 = Primitive{Primitive::Kind::Int8};
auto myint64 = Primitive{Primitive::Kind::Int64};
myclass.members.push_back(Member{myint8, "n", 0});
myclass.members.push_back(Member{myint64, "n", 8 * 8});
auto mycontainer = Container{0, ContainerInfo{}, 8};
mycontainer.templateParams.push_back(myclass);
test(AlignmentCalc::createPass(), {mycontainer}, R"(
[0] Container: (size: 8)
Param
[1] Class: MyClass (size: 16, align: 8)
Member: n (offset: 0, align: 1)
Primitive: int8_t
Member: n (offset: 8, align: 8)
Primitive: int64_t
)");
}
TEST(AlignmentCalcTest, Packed) {
auto mystruct = Class{0, Class::Kind::Struct, "MyStruct", 9};
auto myint8 = Primitive{Primitive::Kind::Int8};
auto myint64 = Primitive{Primitive::Kind::Int64};
mystruct.members.push_back(Member{myint8, "n1", 0});
mystruct.members.push_back(Member{myint64, "n2", 1 * 8});
test(AlignmentCalc::createPass(), {mystruct}, R"(
[0] Struct: MyStruct (size: 9, align: 8, packed)
Member: n1 (offset: 0, align: 1)
Primitive: int8_t
Member: n2 (offset: 1, align: 8)
Primitive: int64_t
)");
}