object-introspection/test/test_name_gen.cpp
Alastair Robertson 00b46377a1 Unit tests: Remove unnecessary make_unique calls
This was a purely mechanical change made with some find-and-replace
macros.
2023-07-04 17:18:43 +01:00

322 lines
10 KiB
C++

#include <gtest/gtest.h>
#include "oi/type_graph/NameGen.h"
#include "oi/type_graph/Types.h"
#include "test/type_graph_utils.h"
using namespace type_graph;
TEST(NameGenTest, ClassParams) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13};
auto myclass = Class{Class::Kind::Struct, "MyClass<MyParam, MyParam>", 13};
myclass.templateParams.push_back(&myparam1);
myclass.templateParams.push_back(&myparam2);
NameGen nameGen;
nameGen.generateNames({myclass});
EXPECT_EQ(myclass.name(), "MyClass_0");
EXPECT_EQ(myparam1.name(), "MyParam_1");
EXPECT_EQ(myparam2.name(), "MyParam_2");
}
TEST(NameGenTest, ClassContainerParam) {
auto myint = Primitive{Primitive::Kind::Int32};
auto myparam = getVector();
myparam.templateParams.push_back(&myint);
auto myclass = Class{Class::Kind::Struct, "MyClass", 13};
myclass.templateParams.push_back(&myparam);
NameGen nameGen;
nameGen.generateNames({myclass});
EXPECT_EQ(myclass.name(), "MyClass_0");
EXPECT_EQ(myparam.name(), "std::vector<int32_t>");
}
TEST(NameGenTest, ClassParents) {
auto myparent1 = Class{Class::Kind::Struct, "MyParent", 13};
auto myparent2 = Class{Class::Kind::Struct, "MyParent", 13};
auto myclass = Class{Class::Kind::Struct, "MyClass", 13};
myclass.parents.push_back(Parent{&myparent1, 0});
myclass.parents.push_back(Parent{&myparent2, 0});
NameGen nameGen;
nameGen.generateNames({myclass});
EXPECT_EQ(myclass.name(), "MyClass_0");
EXPECT_EQ(myparent1.name(), "MyParent_1");
EXPECT_EQ(myparent2.name(), "MyParent_2");
}
TEST(NameGenTest, ClassMembers) {
auto mymember1 = Class{Class::Kind::Struct, "MyMember", 13};
auto mymember2 = Class{Class::Kind::Struct, "MyMember", 13};
auto myclass = Class{Class::Kind::Struct, "MyClass", 13};
// A class may end up with members sharing a name after flattening
myclass.members.push_back(Member{&mymember1, "mem", 0});
myclass.members.push_back(Member{&mymember2, "mem", 0});
NameGen nameGen;
nameGen.generateNames({myclass});
EXPECT_EQ(myclass.name(), "MyClass_0");
EXPECT_EQ(myclass.members[0].name, "mem_0");
EXPECT_EQ(myclass.members[1].name, "mem_1");
EXPECT_EQ(mymember1.name(), "MyMember_1");
EXPECT_EQ(mymember2.name(), "MyMember_2");
}
TEST(NameGenTest, ClassChildren) {
auto mychild1 = Class{Class::Kind::Struct, "MyChild", 13};
auto mychild2 = Class{Class::Kind::Struct, "MyChild", 13};
auto myclass = Class{Class::Kind::Struct, "MyClass", 13};
myclass.children.push_back(mychild1);
myclass.children.push_back(mychild2);
NameGen nameGen;
nameGen.generateNames({myclass});
EXPECT_EQ(myclass.name(), "MyClass_0");
EXPECT_EQ(mychild1.name(), "MyChild_1");
EXPECT_EQ(mychild2.name(), "MyChild_2");
}
TEST(NameGenTest, ContainerParams) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam1);
mycontainer.templateParams.push_back(&myparam2);
NameGen nameGen;
nameGen.generateNames({mycontainer});
EXPECT_EQ(myparam1.name(), "MyParam_0");
EXPECT_EQ(myparam2.name(), "MyParam_1");
EXPECT_EQ(mycontainer.name(), "std::vector<MyParam_0, MyParam_1>");
}
TEST(NameGenTest, ContainerParamsDuplicates) {
auto myparam = Class{Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam);
mycontainer.templateParams.push_back(&myparam);
NameGen nameGen;
nameGen.generateNames({mycontainer});
EXPECT_EQ(myparam.name(), "MyParam_0");
EXPECT_EQ(mycontainer.name(), "std::vector<MyParam_0, MyParam_0>");
}
TEST(NameGenTest, ContainerParamsDuplicatesDeep) {
auto myparam = Class{Class::Kind::Struct, "MyParam", 13};
auto mycontainer1 = getVector();
mycontainer1.templateParams.push_back(&myparam);
auto mycontainer2 = getVector();
mycontainer2.templateParams.push_back(&myparam);
mycontainer2.templateParams.push_back(&mycontainer1);
NameGen nameGen;
nameGen.generateNames({mycontainer2});
EXPECT_EQ(myparam.name(), "MyParam_0");
EXPECT_EQ(mycontainer1.name(), "std::vector<MyParam_0>");
EXPECT_EQ(mycontainer2.name(),
"std::vector<MyParam_0, std::vector<MyParam_0>>");
}
TEST(NameGenTest, ContainerParamsDuplicatesAcrossContainers) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13};
auto myparam3 = Class{Class::Kind::Struct, "MyParam", 13};
auto mycontainer1 = getVector();
mycontainer1.templateParams.push_back(&myparam1);
mycontainer1.templateParams.push_back(&myparam2);
auto mycontainer2 = getVector();
mycontainer2.templateParams.push_back(&myparam2);
mycontainer2.templateParams.push_back(&myparam3);
NameGen nameGen;
nameGen.generateNames({mycontainer1, mycontainer2});
EXPECT_EQ(myparam1.name(), "MyParam_0");
EXPECT_EQ(myparam2.name(), "MyParam_1");
EXPECT_EQ(myparam3.name(), "MyParam_2");
EXPECT_EQ(mycontainer1.name(), "std::vector<MyParam_0, MyParam_1>");
EXPECT_EQ(mycontainer2.name(), "std::vector<MyParam_1, MyParam_2>");
}
TEST(NameGenTest, ContainerParamsConst) {
auto myparam1 = Class{Class::Kind::Struct, "MyConstParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector();
mycontainer.templateParams.push_back(
TemplateParam{&myparam1, {Qualifier::Const}});
mycontainer.templateParams.push_back(TemplateParam{&myparam2});
NameGen nameGen;
nameGen.generateNames({mycontainer});
EXPECT_EQ(myparam1.name(), "MyConstParam_0");
EXPECT_EQ(myparam2.name(), "MyParam_1");
EXPECT_EQ(mycontainer.name(), "std::vector<const MyConstParam_0, MyParam_1>");
}
TEST(NameGenTest, ContainerNoParams) {
auto mycontainer = getVector();
NameGen nameGen;
nameGen.generateNames({mycontainer});
EXPECT_EQ(mycontainer.name(), "std::vector");
}
TEST(NameGenTest, ContainerParamsValue) {
auto myint = Primitive{Primitive::Kind::Int32};
auto myenum = Enum{"MyEnum", 4};
auto mycontainer = getVector();
mycontainer.templateParams.push_back(TemplateParam{"123"});
mycontainer.templateParams.push_back(TemplateParam{"MyEnum::OptionC"});
NameGen nameGen;
nameGen.generateNames({mycontainer});
EXPECT_EQ(myint.name(), "int32_t");
EXPECT_EQ(myenum.name(), "MyEnum");
EXPECT_EQ(mycontainer.name(), "std::vector<123, MyEnum::OptionC>");
}
TEST(NameGenTest, Array) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam1);
mycontainer.templateParams.push_back(&myparam2);
auto myarray = Array{&mycontainer, 5};
NameGen nameGen;
nameGen.generateNames({myarray});
EXPECT_EQ(myparam1.name(), "MyParam_0");
EXPECT_EQ(myparam2.name(), "MyParam_1");
EXPECT_EQ(mycontainer.name(), "std::vector<MyParam_0, MyParam_1>");
EXPECT_EQ(myarray.name(), "OIArray<std::vector<MyParam_0, MyParam_1>, 5>");
}
TEST(NameGenTest, Typedef) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam1);
mycontainer.templateParams.push_back(&myparam2);
auto mytypedef = Typedef{"MyTypedef", &mycontainer};
NameGen nameGen;
nameGen.generateNames({mytypedef});
EXPECT_EQ(myparam1.name(), "MyParam_1");
EXPECT_EQ(myparam2.name(), "MyParam_2");
EXPECT_EQ(mycontainer.name(), "std::vector<MyParam_1, MyParam_2>");
EXPECT_EQ(mytypedef.name(), "MyTypedef_0");
}
TEST(NameGenTest, TypedefAliasTemplate) {
auto myint = Primitive{Primitive::Kind::Int32};
auto mytypedef = Typedef{"MyTypedef<ParamA, ParamB>", &myint};
NameGen nameGen;
nameGen.generateNames({mytypedef});
EXPECT_EQ(mytypedef.name(), "MyTypedef_0");
}
TEST(NameGenTest, Pointer) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam1);
mycontainer.templateParams.push_back(&myparam2);
auto mypointer = Pointer{&mycontainer};
NameGen nameGen;
nameGen.generateNames({mypointer});
EXPECT_EQ(myparam1.name(), "MyParam_0");
EXPECT_EQ(myparam2.name(), "MyParam_1");
EXPECT_EQ(mycontainer.name(), "std::vector<MyParam_0, MyParam_1>");
EXPECT_EQ(mypointer.name(), "std::vector<MyParam_0, MyParam_1>*");
}
TEST(NameGenTest, Dummy) {
auto dummy = Dummy{12, 34};
NameGen nameGen;
nameGen.generateNames({dummy});
EXPECT_EQ(dummy.name(), "DummySizedOperator<12, 34>");
}
TEST(NameGenTest, DummyAllocator) {
auto myparam1 = Class{Class::Kind::Struct, "MyParam", 13};
auto myparam2 = Class{Class::Kind::Struct, "MyParam", 13};
auto mycontainer = getVector();
mycontainer.templateParams.push_back(&myparam1);
mycontainer.templateParams.push_back(&myparam2);
auto myalloc = DummyAllocator{mycontainer, 12, 34};
NameGen nameGen;
nameGen.generateNames({myalloc});
EXPECT_EQ(myparam1.name(), "MyParam_0");
EXPECT_EQ(myparam2.name(), "MyParam_1");
EXPECT_EQ(mycontainer.name(), "std::vector<MyParam_0, MyParam_1>");
EXPECT_EQ(myalloc.name(),
"DummyAllocator<std::vector<MyParam_0, MyParam_1>, 12, 34>");
}
TEST(NameGenTest, Cycle) {
auto classA = Class{Class::Kind::Class, "ClassA", 69};
auto classB = Class{Class::Kind::Class, "ClassB", 69};
auto ptrA = Pointer{&classA};
classA.members.push_back(Member(&classB, "b", 0));
classB.members.push_back(Member(&ptrA, "a", 0));
NameGen nameGen;
nameGen.generateNames({classA});
EXPECT_EQ(classA.name(), "ClassA_0");
EXPECT_EQ(classB.name(), "ClassB_1");
}
TEST(NameGenTest, ContainerCycle) {
auto container = getVector();
auto myclass = Class{Class::Kind::Class, "MyClass", 69};
myclass.members.push_back(Member(&container, "c", 0));
container.templateParams.push_back(TemplateParam(&myclass));
NameGen nameGen;
nameGen.generateNames({myclass});
EXPECT_EQ(myclass.name(), "MyClass_0");
EXPECT_EQ(container.name(), "std::vector<MyClass_0>");
}