2023-05-26 13:52:09 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
#include "oi/type_graph/TopoSorter.h"
|
|
|
|
#include "oi/type_graph/Types.h"
|
2023-07-04 16:56:27 +01:00
|
|
|
#include "test/type_graph_utils.h"
|
2023-05-26 13:52:09 +01:00
|
|
|
|
|
|
|
using namespace type_graph;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using ref = std::reference_wrapper<T>;
|
|
|
|
|
|
|
|
void test(const std::vector<ref<Type>> input, std::string expected) {
|
|
|
|
TopoSorter topo;
|
|
|
|
topo.sort(input);
|
|
|
|
|
|
|
|
std::string output;
|
|
|
|
for (const Type& type : topo.sortedTypes()) {
|
|
|
|
output += type.name();
|
|
|
|
output.push_back('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::trim(expected);
|
|
|
|
boost::trim(output);
|
|
|
|
|
|
|
|
EXPECT_EQ(expected, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, SingleType) {
|
2023-07-04 16:55:10 +01:00
|
|
|
auto myenum = Enum{"MyEnum", 4};
|
|
|
|
test({myenum}, "MyEnum");
|
2023-05-26 13:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, UnrelatedTypes) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto mystruct = Class{0, Class::Kind::Struct, "MyStruct", 13};
|
2023-07-04 16:55:10 +01:00
|
|
|
auto myenum = Enum{"MyEnum", 4};
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myclass = Class{1, Class::Kind::Class, "MyClass", 69};
|
2023-05-26 13:52:09 +01:00
|
|
|
|
|
|
|
// Try the same input in a few different orders and ensure they output order
|
|
|
|
// matches the input order
|
2023-07-04 16:55:10 +01:00
|
|
|
test({mystruct, myenum, myclass}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
MyStruct
|
|
|
|
MyEnum
|
|
|
|
MyClass
|
|
|
|
)");
|
2023-07-04 16:55:10 +01:00
|
|
|
test({myenum, mystruct, myclass}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
MyEnum
|
|
|
|
MyStruct
|
|
|
|
MyClass
|
|
|
|
)");
|
2023-07-04 16:55:10 +01:00
|
|
|
test({myclass, myenum, mystruct}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
MyClass
|
|
|
|
MyEnum
|
|
|
|
MyStruct
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, ClassMembers) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto mystruct = Class{0, Class::Kind::Struct, "MyStruct", 13};
|
2023-07-04 16:55:10 +01:00
|
|
|
auto myenum = Enum{"MyEnum", 4};
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myclass = Class{1, Class::Kind::Class, "MyClass", 69};
|
2023-07-05 10:33:44 +01:00
|
|
|
myclass.members.push_back(Member{mystruct, "n", 0});
|
|
|
|
myclass.members.push_back(Member{myenum, "e", 4});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-04 16:55:10 +01:00
|
|
|
test({myclass}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
MyStruct
|
|
|
|
MyEnum
|
|
|
|
MyClass
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, Parents) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myparent = Class{0, Class::Kind::Struct, "MyParent", 13};
|
|
|
|
auto myclass = Class{1, Class::Kind::Class, "MyClass", 69};
|
2023-07-05 10:33:44 +01:00
|
|
|
myclass.parents.push_back(Parent{myparent, 0});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-04 16:55:10 +01:00
|
|
|
test({myclass}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
MyParent
|
|
|
|
MyClass
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, TemplateParams) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myparam = Class{0, Class::Kind::Struct, "MyParam", 13};
|
|
|
|
auto myclass = Class{1, Class::Kind::Class, "MyClass", 69};
|
2023-07-05 10:33:44 +01:00
|
|
|
myclass.templateParams.push_back(TemplateParam{myparam});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-04 16:55:10 +01:00
|
|
|
test({myclass}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
MyParam
|
|
|
|
MyClass
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2023-07-26 12:51:33 +01:00
|
|
|
TEST(TopoSorterTest, TemplateParamValue) {
|
|
|
|
auto myclass = Class{1, Class::Kind::Class, "MyClass", 69};
|
2023-08-16 20:44:58 +01:00
|
|
|
auto myint = Primitive{Primitive::Kind::Int32};
|
|
|
|
myclass.templateParams.push_back(TemplateParam{myint, "123"});
|
2023-07-26 12:51:33 +01:00
|
|
|
|
|
|
|
test({myclass}, R"(
|
2023-08-24 13:42:12 +01:00
|
|
|
int32_t
|
2023-07-26 12:51:33 +01:00
|
|
|
MyClass
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2023-05-26 13:52:09 +01:00
|
|
|
TEST(TopoSorterTest, Children) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto mymember = Class{0, Class::Kind::Struct, "MyMember", 13};
|
|
|
|
auto mychild = Class{1, Class::Kind::Struct, "MyChild", 13};
|
2023-07-05 10:33:44 +01:00
|
|
|
mychild.members.push_back(Member{mymember, "mymember", 0});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myclass = Class{2, Class::Kind::Class, "MyClass", 69};
|
2023-07-05 10:33:44 +01:00
|
|
|
mychild.parents.push_back(Parent{myclass, 0});
|
2023-07-04 16:55:10 +01:00
|
|
|
myclass.children.push_back(mychild);
|
2023-05-26 13:52:09 +01:00
|
|
|
|
|
|
|
std::vector<std::vector<ref<Type>>> inputs = {
|
2023-07-04 16:55:10 +01:00
|
|
|
{myclass},
|
|
|
|
{mychild},
|
2023-05-26 13:52:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Same as for pointer cycles, outputs must be in the same order no matter
|
|
|
|
// which type we start the sort on.
|
|
|
|
for (const auto& input : inputs) {
|
|
|
|
test(input, R"(
|
|
|
|
MyClass
|
|
|
|
MyMember
|
|
|
|
MyChild
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, ChildrenCycle) {
|
|
|
|
// class MyParent {};
|
|
|
|
// class ClassA {
|
|
|
|
// MyParent p;
|
|
|
|
// };
|
|
|
|
// class MyChild : MyParent {
|
|
|
|
// A a;
|
|
|
|
// };
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myparent = Class{0, Class::Kind::Class, "MyParent", 69};
|
|
|
|
auto classA = Class{1, Class::Kind::Struct, "ClassA", 5};
|
|
|
|
auto mychild = Class{2, Class::Kind::Struct, "MyChild", 13};
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-05 10:33:44 +01:00
|
|
|
mychild.parents.push_back(Parent{myparent, 0});
|
2023-07-04 16:55:10 +01:00
|
|
|
myparent.children.push_back(mychild);
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-05 10:33:44 +01:00
|
|
|
mychild.members.push_back(Member{classA, "a", 0});
|
|
|
|
classA.members.push_back(Member{myparent, "p", 0});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
|
|
|
std::vector<std::vector<ref<Type>>> inputs = {
|
2023-07-04 16:55:10 +01:00
|
|
|
{myparent},
|
|
|
|
{classA},
|
|
|
|
{mychild},
|
2023-05-26 13:52:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Same as for pointer cycles, outputs must be in the same order no matter
|
|
|
|
// which type we start the sort on.
|
|
|
|
for (const auto& input : inputs) {
|
|
|
|
test(input, R"(
|
|
|
|
MyParent
|
|
|
|
ClassA
|
|
|
|
MyChild
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, Containers) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myparam1 = Class{1, Class::Kind::Struct, "MyParam1", 13};
|
|
|
|
auto myparam2 = Class{2, Class::Kind::Struct, "MyParam2", 13};
|
2023-06-26 21:36:24 +01:00
|
|
|
auto mycontainer = getMap();
|
2023-07-05 10:33:44 +01:00
|
|
|
mycontainer.templateParams.push_back((myparam1));
|
|
|
|
mycontainer.templateParams.push_back((myparam2));
|
2023-06-26 21:36:24 +01:00
|
|
|
|
|
|
|
test({mycontainer}, R"(
|
|
|
|
MyParam1
|
|
|
|
MyParam2
|
|
|
|
std::map
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, ContainersVector) {
|
|
|
|
// std::vector allows forward declared template parameters
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myparam = Class{1, Class::Kind::Struct, "MyParam", 13};
|
2023-05-26 13:52:09 +01:00
|
|
|
auto mycontainer = getVector();
|
2023-07-05 10:33:44 +01:00
|
|
|
mycontainer.templateParams.push_back((myparam));
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-06-26 21:36:24 +01:00
|
|
|
test({mycontainer}, R"(
|
|
|
|
std::vector
|
|
|
|
MyParam
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, ContainersList) {
|
|
|
|
// std::list allows forward declared template parameters
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myparam = Class{1, Class::Kind::Struct, "MyParam", 13};
|
2023-06-26 21:36:24 +01:00
|
|
|
auto mycontainer = getList();
|
2023-07-05 10:33:44 +01:00
|
|
|
mycontainer.templateParams.push_back((myparam));
|
2023-06-26 21:36:24 +01:00
|
|
|
|
|
|
|
test({mycontainer}, R"(
|
|
|
|
std::list
|
|
|
|
MyParam
|
|
|
|
)");
|
2023-05-26 13:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, Arrays) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myclass = Class{0, Class::Kind::Class, "MyClass", 69};
|
2023-07-05 10:33:44 +01:00
|
|
|
auto myarray = Array{1, myclass, 10};
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-04 16:55:10 +01:00
|
|
|
test({myarray}, "MyClass\n");
|
2023-05-26 13:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, Typedef) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto classA = Class{0, Class::Kind::Class, "ClassA", 8};
|
2023-07-05 10:33:44 +01:00
|
|
|
auto aliasA = Typedef{1, "aliasA", classA};
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-04 16:55:10 +01:00
|
|
|
test({aliasA}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
ClassA
|
|
|
|
aliasA
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, Pointers) {
|
|
|
|
// Pointers do not require pointee types to be defined first
|
2023-07-24 13:14:31 +01:00
|
|
|
auto classA = Class{0, Class::Kind::Class, "ClassA", 69};
|
|
|
|
auto mypointer = Pointer{1, classA};
|
|
|
|
|
|
|
|
auto myclass = Class{2, Class::Kind::Class, "MyClass", 69};
|
|
|
|
myclass.members.push_back(Member{mypointer, "ptr", 0});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-24 13:14:31 +01:00
|
|
|
test({myclass}, R"(
|
|
|
|
MyClass
|
|
|
|
ClassA
|
|
|
|
)");
|
2023-05-26 13:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, PointerCycle) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto classA = Class{0, Class::Kind::Class, "ClassA", 69};
|
|
|
|
auto classB = Class{1, Class::Kind::Class, "ClassB", 69};
|
2023-07-05 10:33:44 +01:00
|
|
|
auto ptrA = Pointer{2, classA};
|
|
|
|
classA.members.push_back(Member{classB, "b", 0});
|
|
|
|
classB.members.push_back(Member{ptrA, "a", 0});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
|
|
|
std::vector<std::vector<ref<Type>>> inputs = {
|
2023-07-04 16:55:10 +01:00
|
|
|
{classA},
|
|
|
|
{classB},
|
|
|
|
{ptrA},
|
2023-05-26 13:52:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// No matter which node we start the topological sort with, we must always get
|
|
|
|
// the same sorted order for ClassA and ClassB.
|
|
|
|
for (const auto& input : inputs) {
|
|
|
|
test(input, R"(
|
|
|
|
ClassB
|
|
|
|
ClassA
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 13:14:31 +01:00
|
|
|
TEST(TopoSorterTest, PointerToTypedef) {
|
|
|
|
auto classA = Class{0, Class::Kind::Class, "ClassA", 8};
|
|
|
|
auto aliasA = Typedef{1, "aliasA", classA};
|
|
|
|
|
|
|
|
auto mypointer = Pointer{1, aliasA};
|
|
|
|
|
|
|
|
auto myclass = Class{2, Class::Kind::Class, "MyClass", 69};
|
|
|
|
myclass.members.push_back(Member{mypointer, "ptrToTypedef", 0});
|
|
|
|
|
|
|
|
test({myclass}, R"(
|
|
|
|
ClassA
|
|
|
|
aliasA
|
|
|
|
MyClass
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2023-05-26 13:52:09 +01:00
|
|
|
TEST(TopoSorterTest, TwoDeep) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myunion = Class{0, Class::Kind::Union, "MyUnion", 7};
|
|
|
|
auto mystruct = Class{1, Class::Kind::Struct, "MyStruct", 13};
|
|
|
|
auto myclass = Class{2, Class::Kind::Class, "MyClass", 69};
|
2023-07-05 10:33:44 +01:00
|
|
|
myclass.members.push_back(Member{mystruct, "mystruct", 0});
|
|
|
|
mystruct.members.push_back(Member{myunion, "myunion", 0});
|
2023-05-26 13:52:09 +01:00
|
|
|
|
2023-07-04 16:55:10 +01:00
|
|
|
test({myclass}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
MyUnion
|
|
|
|
MyStruct
|
|
|
|
MyClass
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TopoSorterTest, MultiplePaths) {
|
2023-07-05 09:14:05 +01:00
|
|
|
auto myunion = Class{0, Class::Kind::Union, "MyUnion", 7};
|
|
|
|
auto mystruct = Class{1, Class::Kind::Struct, "MyStruct", 13};
|
|
|
|
auto myclass = Class{2, Class::Kind::Class, "MyClass", 69};
|
2023-07-05 10:33:44 +01:00
|
|
|
myclass.members.push_back(Member{mystruct, "mystruct", 0});
|
|
|
|
myclass.members.push_back(Member{myunion, "myunion1", 0});
|
|
|
|
mystruct.members.push_back(Member{myunion, "myunion2", 0});
|
2023-07-04 16:55:10 +01:00
|
|
|
|
|
|
|
test({myclass}, R"(
|
2023-05-26 13:52:09 +01:00
|
|
|
MyUnion
|
|
|
|
MyStruct
|
|
|
|
MyClass
|
|
|
|
)");
|
|
|
|
}
|
2023-09-22 14:32:56 +01:00
|
|
|
|
|
|
|
TEST(TopoSorterTest, CaptureKeys) {
|
|
|
|
auto myparam1 = Class{1, Class::Kind::Struct, "MyParam1", 13};
|
|
|
|
auto myparam2 = Class{2, Class::Kind::Struct, "MyParam2", 13};
|
|
|
|
auto mycontainer = getMap();
|
|
|
|
mycontainer.templateParams.push_back((myparam1));
|
|
|
|
mycontainer.templateParams.push_back((myparam2));
|
|
|
|
|
|
|
|
auto captureKeys = CaptureKeys{mycontainer, mycontainer.containerInfo_};
|
|
|
|
|
|
|
|
test({captureKeys}, R"(
|
|
|
|
MyParam1
|
|
|
|
MyParam2
|
|
|
|
std::map
|
|
|
|
OICaptureKeys<std::map>
|
|
|
|
)");
|
|
|
|
}
|