Tests: Split common code out into type_graph_utils

This commit is contained in:
Alastair Robertson 2023-05-31 06:26:46 -07:00 committed by Alastair Robertson
parent 3a7a647a73
commit e1bc5c7b5e
6 changed files with 113 additions and 28 deletions

View File

@ -48,6 +48,7 @@ target_link_libraries(integration_sleepy folly_headers)
# Unit tests
add_executable(test_type_graph
main.cpp
test_add_padding.cpp
test_alignment_calc.cpp
test_drgn_parser.cpp
@ -56,6 +57,7 @@ add_executable(test_type_graph
test_remove_top_level_pointer.cpp
test_topo_sorter.cpp
test_type_identifier.cpp
type_graph_utils.cpp
)
add_dependencies(test_type_graph integration_test_target)
target_compile_definitions(test_type_graph PRIVATE

19
test/main.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <gtest/gtest.h>
/*
* This listener ensures that test failures (ASSERTs) in helper functions
* propagate all the way up and stop the test from continuing.
*/
class ThrowListener : public testing::EmptyTestEventListener {
void OnTestPartResult(const testing::TestPartResult& result) override {
if (result.type() == testing::TestPartResult::kFatalFailure) {
throw testing::AssertionException(result);
}
}
};
int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
::testing::UnitTest::GetInstance()->listeners().Append(new ThrowListener);
return RUN_ALL_TESTS();
}

View File

@ -5,30 +5,10 @@
#include "oi/type_graph/Printer.h"
#include "oi/type_graph/TypeGraph.h"
#include "oi/type_graph/Types.h"
#include "test/type_graph_utils.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);

View File

@ -1,17 +1,11 @@
#include <gtest/gtest.h>
#include "oi/ContainerInfo.h"
#include "oi/type_graph/NameGen.h"
#include "oi/type_graph/Types.h"
#include "test/type_graph_utils.h"
using namespace type_graph;
Container getVector() {
ContainerInfo info{"std::vector", SEQ_TYPE, "vector"};
return Container{info, 24};
}
TEST(NameGenTest, ClassParams) {
auto myparam1 = std::make_unique<Class>(Class::Kind::Struct, "MyParam", 13);
auto myparam2 = std::make_unique<Class>(Class::Kind::Struct, "MyParam", 13);

68
test/type_graph_utils.cpp Normal file
View File

@ -0,0 +1,68 @@
#include "test/type_graph_utils.h"
#include <gtest/gtest.h>
#include "oi/ContainerInfo.h"
#include "oi/type_graph/PassManager.h"
#include "oi/type_graph/Printer.h"
#include "oi/type_graph/TypeGraph.h"
using type_graph::Container;
using type_graph::Pass;
using type_graph::Type;
using type_graph::TypeGraph;
template <typename T>
using ref = std::reference_wrapper<T>;
namespace {
void check(const std::vector<ref<Type>>& types,
std::string_view expected,
std::string_view comment) {
std::stringstream out;
type_graph::Printer printer(out);
for (const auto& type : types) {
printer.print(type);
}
expected.remove_prefix(1); // Remove initial '\n'
ASSERT_EQ(expected, out.str())
<< "Test failure " << comment << " running pass";
}
} // namespace
void test(type_graph::Pass pass,
std::vector<ref<Type>> rootTypes,
std::string_view expectedBefore,
std::string_view expectedAfter) {
check(rootTypes, expectedBefore, "before");
TypeGraph typeGraph;
for (const auto& type : rootTypes) {
typeGraph.addRoot(type);
}
pass.run(typeGraph);
check(rootTypes, expectedAfter, "after");
}
void test(type_graph::Pass pass,
std::vector<ref<Type>> rootTypes,
std::string_view expectedAfter) {
TypeGraph typeGraph;
for (const auto& type : rootTypes) {
typeGraph.addRoot(type);
}
pass.run(typeGraph);
check(rootTypes, expectedAfter, "after");
}
Container getVector() {
static ContainerInfo info{"std::vector", SEQ_TYPE, "vector"};
info.stubTemplateParams = {1};
return Container{info, 24};
}

22
test/type_graph_utils.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <functional>
#include <string_view>
#include <vector>
namespace type_graph {
class Container;
class Pass;
class Type;
} // namespace type_graph
void test(type_graph::Pass pass,
std::vector<std::reference_wrapper<type_graph::Type>> rootTypes,
std::string_view expectedBefore,
std::string_view expectedAfter);
void test(type_graph::Pass pass,
std::vector<std::reference_wrapper<type_graph::Type>> rootTypes,
std::string_view expectedAfter);
type_graph::Container getVector();