object-introspection/test/test_add_padding.cpp
Alastair Robertson bd919ae4e4 TypeGraph: Add core code
This code mostly works, but is obviously not complete. This commit just
adds the code and tests, but does not enable it in OID or OIL.
2023-05-30 13:27:23 +01:00

87 lines
2.6 KiB
C++

#include <gtest/gtest.h>
#include "oi/type_graph/AddPadding.h"
#include "oi/type_graph/PassManager.h"
#include "oi/type_graph/Printer.h"
#include "oi/type_graph/TypeGraph.h"
#include "oi/type_graph/Types.h"
using namespace type_graph;
void test(Pass pass,
std::vector<std::reference_wrapper<Type>> types,
std::string_view expected) {
TypeGraph typeGraph;
for (const auto& type : types) {
typeGraph.addRoot(type);
}
pass.run(typeGraph);
std::stringstream out;
Printer printer(out);
for (const auto& type : types) {
printer.print(type);
}
expected.remove_prefix(1); // Remove initial '\n'
EXPECT_EQ(expected, out.str());
}
TEST(AddPaddingTest, BetweenMembers) {
auto myclass = std::make_unique<Class>(Class::Kind::Class, "MyClass", 16);
auto myint8 = std::make_unique<Primitive>(Primitive::Kind::Int8);
auto myint64 = std::make_unique<Primitive>(Primitive::Kind::Int64);
myclass->members.push_back(Member(myint8.get(), "n1", 0));
myclass->members.push_back(Member(myint64.get(), "n2", 8));
test(AddPadding::createPass(), {*myclass}, R"(
[0] Class: MyClass (size: 16)
Member: n1 (offset: 0)
Primitive: int8_t
Member: __oid_padding (offset: 1)
[1] Array: (length: 7)
Primitive: int8_t
Member: n2 (offset: 8)
Primitive: int64_t
)");
}
TEST(AddPaddingTest, AtEnd) {
auto myclass = std::make_unique<Class>(Class::Kind::Struct, "MyStruct", 16);
auto myint8 = std::make_unique<Primitive>(Primitive::Kind::Int8);
auto myint64 = std::make_unique<Primitive>(Primitive::Kind::Int64);
myclass->members.push_back(Member(myint64.get(), "n1", 0));
myclass->members.push_back(Member(myint8.get(), "n2", 8));
test(AddPadding::createPass(), {*myclass}, R"(
[0] Struct: MyStruct (size: 16)
Member: n1 (offset: 0)
Primitive: int64_t
Member: n2 (offset: 8)
Primitive: int8_t
Member: __oid_padding (offset: 9)
[1] Array: (length: 7)
Primitive: int8_t
)");
}
TEST(AddPaddingTest, UnionNotPadded) {
auto myclass = std::make_unique<Class>(Class::Kind::Union, "MyUnion", 8);
auto myint8 = std::make_unique<Primitive>(Primitive::Kind::Int8);
auto myint64 = std::make_unique<Primitive>(Primitive::Kind::Int64);
myclass->members.push_back(Member(myint64.get(), "n1", 0));
myclass->members.push_back(Member(myint8.get(), "n2", 0));
test(AddPadding::createPass(), {*myclass}, R"(
[0] Union: MyUnion (size: 8)
Member: n1 (offset: 0)
Primitive: int64_t
Member: n2 (offset: 0)
Primitive: int8_t
)");
}
// TODO test we follow class members, templates, children