2023-05-26 18:26:32 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-06-20 17:57:43 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
2023-05-26 18:26:32 +01:00
|
|
|
|
2023-06-20 17:57:43 +01:00
|
|
|
#include "mocks.h"
|
|
|
|
#include "oi/CodeGen.h"
|
|
|
|
#include "oi/type_graph/Printer.h"
|
|
|
|
#include "oi/type_graph/TypeGraph.h"
|
|
|
|
#include "oi/type_graph/Types.h"
|
|
|
|
#include "type_graph_utils.h"
|
|
|
|
|
|
|
|
using namespace type_graph;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using ref = std::reference_wrapper<T>;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void testTransform(Type& type,
|
|
|
|
std::string_view expectedBefore,
|
|
|
|
std::string_view expectedAfter) {
|
|
|
|
check({type}, expectedBefore, "before transform");
|
|
|
|
|
|
|
|
type_graph::TypeGraph typeGraph;
|
|
|
|
typeGraph.addRoot(type);
|
|
|
|
|
|
|
|
OICodeGen::Config config;
|
|
|
|
MockSymbolService symbols;
|
|
|
|
CodeGen codegen{config, symbols};
|
|
|
|
codegen.transform(typeGraph);
|
|
|
|
|
|
|
|
check({type}, expectedAfter, "after transform");
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(CodeGenTest, TransformContainerAllocator) {
|
|
|
|
auto myint = Primitive{Primitive::Kind::Int32};
|
|
|
|
|
|
|
|
auto myalloc = Class{Class::Kind::Struct, "MyAlloc", 8};
|
|
|
|
myalloc.functions.push_back(Function{"allocate"});
|
|
|
|
myalloc.functions.push_back(Function{"deallocate"});
|
|
|
|
|
|
|
|
auto container = getVector();
|
|
|
|
container.templateParams.push_back(TemplateParam{&myint});
|
|
|
|
container.templateParams.push_back(TemplateParam{&myalloc});
|
|
|
|
|
|
|
|
testTransform(container, R"(
|
|
|
|
[0] Container: std::vector (size: 24)
|
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Param
|
|
|
|
[1] Struct: MyAlloc (size: 8)
|
|
|
|
Function: allocate
|
|
|
|
Function: deallocate
|
|
|
|
)",
|
|
|
|
R"(
|
|
|
|
[0] Container: std::vector<int32_t, DummyAllocator<int32_t, 8, 0>> (size: 24)
|
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Param
|
|
|
|
DummyAllocator (size: 8)
|
|
|
|
Primitive: int32_t
|
|
|
|
)");
|
2023-05-26 18:26:32 +01:00
|
|
|
}
|