From e1bc5c7b5e188968e2064d49986d2854a43e8aae Mon Sep 17 00:00:00 2001 From: Alastair Robertson Date: Wed, 31 May 2023 06:26:46 -0700 Subject: [PATCH] Tests: Split common code out into type_graph_utils --- test/CMakeLists.txt | 2 ++ test/main.cpp | 19 +++++++++++ test/test_add_padding.cpp | 22 +------------ test/test_name_gen.cpp | 8 +---- test/type_graph_utils.cpp | 68 +++++++++++++++++++++++++++++++++++++++ test/type_graph_utils.h | 22 +++++++++++++ 6 files changed, 113 insertions(+), 28 deletions(-) create mode 100644 test/main.cpp create mode 100644 test/type_graph_utils.cpp create mode 100644 test/type_graph_utils.h diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4f34818..a873778 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 diff --git a/test/main.cpp b/test/main.cpp new file mode 100644 index 0000000..c3686ba --- /dev/null +++ b/test/main.cpp @@ -0,0 +1,19 @@ +#include + +/* + * 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(); +} diff --git a/test/test_add_padding.cpp b/test/test_add_padding.cpp index c8f16b0..d02a873 100644 --- a/test/test_add_padding.cpp +++ b/test/test_add_padding.cpp @@ -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> 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::Kind::Class, "MyClass", 16); auto myint8 = std::make_unique(Primitive::Kind::Int8); diff --git a/test/test_name_gen.cpp b/test/test_name_gen.cpp index ae8044e..44ba065 100644 --- a/test/test_name_gen.cpp +++ b/test/test_name_gen.cpp @@ -1,17 +1,11 @@ #include -#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::Kind::Struct, "MyParam", 13); auto myparam2 = std::make_unique(Class::Kind::Struct, "MyParam", 13); diff --git a/test/type_graph_utils.cpp b/test/type_graph_utils.cpp new file mode 100644 index 0000000..49ed491 --- /dev/null +++ b/test/type_graph_utils.cpp @@ -0,0 +1,68 @@ +#include "test/type_graph_utils.h" + +#include + +#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 +using ref = std::reference_wrapper; + +namespace { +void check(const std::vector>& 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> 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> 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}; +} diff --git a/test/type_graph_utils.h b/test/type_graph_utils.h new file mode 100644 index 0000000..c339557 --- /dev/null +++ b/test/type_graph_utils.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include + +namespace type_graph { +class Container; +class Pass; +class Type; +} // namespace type_graph + +void test(type_graph::Pass pass, + std::vector> rootTypes, + std::string_view expectedBefore, + std::string_view expectedAfter); + +void test(type_graph::Pass pass, + std::vector> rootTypes, + std::string_view expectedAfter); + +type_graph::Container getVector();