2023-05-31 14:26:46 +01:00
|
|
|
#include "test/type_graph_utils.h"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "oi/ContainerInfo.h"
|
2023-08-15 15:27:20 +01:00
|
|
|
#include "oi/type_graph/NodeTracker.h"
|
2023-05-31 14:26:46 +01:00
|
|
|
#include "oi/type_graph/PassManager.h"
|
|
|
|
#include "oi/type_graph/Printer.h"
|
|
|
|
#include "oi/type_graph/TypeGraph.h"
|
2023-07-11 17:01:13 +01:00
|
|
|
#include "test/TypeGraphParser.h"
|
2023-05-31 14:26:46 +01:00
|
|
|
|
|
|
|
using type_graph::Container;
|
2023-07-05 09:14:05 +01:00
|
|
|
using type_graph::NodeId;
|
2023-05-31 14:26:46 +01:00
|
|
|
using type_graph::Pass;
|
|
|
|
using type_graph::Type;
|
|
|
|
using type_graph::TypeGraph;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using ref = std::reference_wrapper<T>;
|
|
|
|
|
2023-08-02 11:11:52 +01:00
|
|
|
void check(const TypeGraph& typeGraph,
|
2023-05-31 14:26:46 +01:00
|
|
|
std::string_view expected,
|
|
|
|
std::string_view comment) {
|
|
|
|
std::stringstream out;
|
2023-08-15 15:27:20 +01:00
|
|
|
NodeTracker tracker;
|
|
|
|
type_graph::Printer printer(out, tracker, typeGraph.size());
|
2023-05-31 14:26:46 +01:00
|
|
|
|
2023-08-02 11:11:52 +01:00
|
|
|
for (const auto& type : typeGraph.rootTypes()) {
|
2023-05-31 14:26:46 +01:00
|
|
|
printer.print(type);
|
|
|
|
}
|
|
|
|
|
2023-07-11 17:01:13 +01:00
|
|
|
if (expected[0] == '\n')
|
|
|
|
expected.remove_prefix(1); // Remove initial '\n'
|
2023-06-20 17:57:43 +01:00
|
|
|
ASSERT_EQ(expected, out.str()) << "Test failure " << comment;
|
2023-05-31 14:26:46 +01:00
|
|
|
}
|
|
|
|
|
2023-07-11 17:01:13 +01:00
|
|
|
void test(type_graph::Pass pass,
|
|
|
|
std::string_view input,
|
|
|
|
std::string_view expectedAfter) {
|
|
|
|
input.remove_prefix(1); // Remove initial '\n'
|
|
|
|
TypeGraph typeGraph;
|
|
|
|
TypeGraphParser parser{typeGraph};
|
2023-07-18 12:14:41 +01:00
|
|
|
try {
|
|
|
|
parser.parse(input);
|
|
|
|
} catch (const TypeGraphParserError& err) {
|
|
|
|
FAIL() << "Error parsing input graph: " << err.what();
|
|
|
|
}
|
2023-07-11 17:01:13 +01:00
|
|
|
|
|
|
|
// Validate input formatting
|
2023-08-02 11:11:52 +01:00
|
|
|
check(typeGraph, input, "parsing input graph");
|
2023-05-31 14:26:46 +01:00
|
|
|
|
2023-08-15 15:27:20 +01:00
|
|
|
NodeTracker tracker;
|
|
|
|
pass.run(typeGraph, tracker);
|
2023-05-31 14:26:46 +01:00
|
|
|
|
2023-08-02 11:11:52 +01:00
|
|
|
check(typeGraph, expectedAfter, "after running pass");
|
2023-05-31 14:26:46 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 11:11:52 +01:00
|
|
|
void testNoChange(type_graph::Pass pass, std::string_view input) {
|
|
|
|
test(pass, input, input);
|
2023-05-31 14:26:46 +01:00
|
|
|
}
|
|
|
|
|
2023-11-02 13:05:35 +00:00
|
|
|
std::vector<std::unique_ptr<ContainerInfo>> getContainerInfos() {
|
|
|
|
auto std_vector =
|
|
|
|
std::make_unique<ContainerInfo>("std::vector", SEQ_TYPE, "vector");
|
|
|
|
std_vector->stubTemplateParams = {1};
|
|
|
|
|
|
|
|
auto std_map = std::make_unique<ContainerInfo>("std::map", SEQ_TYPE, "map");
|
|
|
|
std_map->stubTemplateParams = {2, 3};
|
|
|
|
|
|
|
|
auto std_list =
|
|
|
|
std::make_unique<ContainerInfo>("std::list", SEQ_TYPE, "list");
|
|
|
|
std_list->stubTemplateParams = {1};
|
|
|
|
|
|
|
|
auto std_pair =
|
|
|
|
std::make_unique<ContainerInfo>("std::pair", SEQ_TYPE, "list");
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<ContainerInfo>> containers;
|
|
|
|
containers.emplace_back(std::move(std_vector));
|
|
|
|
containers.emplace_back(std::move(std_map));
|
|
|
|
containers.emplace_back(std::move(std_list));
|
|
|
|
containers.emplace_back(std::move(std_pair));
|
|
|
|
|
|
|
|
return containers;
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:14:05 +01:00
|
|
|
Container getVector(NodeId id) {
|
2023-05-31 14:26:46 +01:00
|
|
|
static ContainerInfo info{"std::vector", SEQ_TYPE, "vector"};
|
|
|
|
info.stubTemplateParams = {1};
|
2023-12-14 16:15:43 +00:00
|
|
|
return Container{id, info, 24, nullptr};
|
2023-05-31 14:26:46 +01:00
|
|
|
}
|
2023-06-26 21:36:24 +01:00
|
|
|
|
2023-07-05 09:14:05 +01:00
|
|
|
Container getMap(NodeId id) {
|
2023-06-26 21:36:24 +01:00
|
|
|
static ContainerInfo info{"std::map", STD_MAP_TYPE, "map"};
|
|
|
|
info.stubTemplateParams = {2, 3};
|
2023-12-14 16:15:43 +00:00
|
|
|
return Container{id, info, 48, nullptr};
|
2023-06-26 21:36:24 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 09:14:05 +01:00
|
|
|
Container getList(NodeId id) {
|
2023-06-26 21:36:24 +01:00
|
|
|
static ContainerInfo info{"std::list", LIST_TYPE, "list"};
|
|
|
|
info.stubTemplateParams = {1};
|
2023-12-14 16:15:43 +00:00
|
|
|
return Container{id, info, 24, nullptr};
|
2023-06-26 21:36:24 +01:00
|
|
|
}
|
2023-07-06 15:29:06 +01:00
|
|
|
|
|
|
|
Container getPair(NodeId id) {
|
|
|
|
static ContainerInfo info{"std::pair", PAIR_TYPE, "utility"};
|
2023-12-14 16:15:43 +00:00
|
|
|
return Container{
|
|
|
|
id, info, 8, nullptr}; // Nonsense size, shouldn't matter for tests
|
2023-07-06 15:29:06 +01:00
|
|
|
}
|