Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions jlm/tooling/Makefile.sub
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,24 @@ libtooling_HEADERS = \
jlm/tooling/CommandGraphGenerator.hpp \
jlm/tooling/CommandLine.hpp \

libtooling_TESTS = \
tests/jlm/tooling/TestJlcCommandGraphGenerator \
tests/jlm/tooling/TestJlcCommandLineParser \
tests/jlm/tooling/TestJlmOptCommand \
tests/jlm/tooling/TestJlmOptCommandLineParser \
$(eval $(call common_library,libtooling))

tests/jlm/tooling/run-libtooling-tests_SOURCES = \
tests/jlm/tooling/TestJlcCommandGraphGenerator.cpp \
tests/jlm/tooling/TestJlcCommandLineParser.cpp \
tests/jlm/tooling/TestJlmOptCommand.cpp \
tests/jlm/tooling/TestJlmOptCommandLineParser.cpp

libtooling_TEST_LIBS = \
tests/jlm/tooling/run-libtooling-tests_LIBS = \
libtooling \
$(if $(ENABLE_HLS),libhls) \
$(if $(ENABLE_MLIR),libmlir) \
libllvm \
librvsdg \
libutil \
libjlmtest \
libutil

libtooling_TEST_EXTRA_LDFLAGS = \
tests/jlm/tooling/run-libtooling-tests_EXTRA_LDFLAGS = \
$(MLIR_LDFLAGS) \
$(shell $(LLVMCONFIG) --ldflags --libs --system-libs) \
$(shell $(LLVMCONFIG) --ldflags --libs --system-libs)

$(eval $(call common_library,libtooling))
$(eval $(call common_test,tests/jlm/tooling/run-libtooling-tests))
46 changes: 15 additions & 31 deletions tests/jlm/tooling/TestJlcCommandGraphGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
* See COPYING for terms of redistribution.
*/

#include "test-registry.hpp"
#include <gtest/gtest.h>

#include <jlm/tooling/Command.hpp>
#include <jlm/tooling/CommandGraphGenerator.hpp>
#include <jlm/tooling/CommandLine.hpp>

#include <cassert>

static void
TestJlcCompiling()
TEST(JlcCommandGraphGeneratorTests, TestJlcCompiling)
{
using namespace jlm::tooling;
using namespace jlm::util;
Expand All @@ -32,18 +31,14 @@ TestJlcCompiling()
auto commandGraph = JlcCommandGraphGenerator::Generate(commandLineOptions);

// Assert
assert(commandGraph->NumNodes() == 5);
EXPECT_EQ(commandGraph->NumNodes(), 5);
auto & commandNode = commandGraph->GetExitNode().IncomingEdges().begin()->GetSource();
auto command = dynamic_cast<const LlcCommand *>(&commandNode.GetCommand());
assert(command && command->OutputFile() == "foo.o");
EXPECT_NE(command, nullptr);
EXPECT_EQ(command->OutputFile(), "foo.o");
}

JLM_UNIT_TEST_REGISTER(
"jlm/tooling/TestJlcCommandGraphGenerator-TestJlcCompiling",
TestJlcCompiling);

static void
TestJlcLinking()
TEST(JlcCommandGraphGeneratorTests, TestJlcLinking)
{
using namespace jlm::tooling;
using namespace jlm::util;
Expand All @@ -58,16 +53,14 @@ TestJlcLinking()
auto commandGraph = JlcCommandGraphGenerator::Generate(commandLineOptions);

// Assert
assert(commandGraph->NumNodes() == 3);
EXPECT_EQ(commandGraph->NumNodes(), 3);
auto & commandNode = commandGraph->GetExitNode().IncomingEdges().begin()->GetSource();
auto command = dynamic_cast<const ClangCommand *>(&commandNode.GetCommand());
assert(command->InputFiles()[0] == "foo.o" && command->OutputFile() == "foobar");
EXPECT_EQ(command->InputFiles()[0], "foo.o");
EXPECT_EQ(command->OutputFile(), "foobar");
}

JLM_UNIT_TEST_REGISTER("jlm/tooling/TestJlcCommandGraphGenerator-TestJlcLinking", TestJlcLinking);

static void
TestJlmOptOptimizations()
TEST(JlcCommandGraphGeneratorTests, TestJlmOptOptimizations)
{
using namespace jlm::tooling;
using namespace jlm::util;
Expand All @@ -91,17 +84,12 @@ TestJlmOptOptimizations()
auto & jlmOptCommand = *dynamic_cast<const JlmOptCommand *>(&jlmOptCommandNode.GetCommand());
auto & optimizations = jlmOptCommand.GetCommandLineOptions().GetOptimizationIds();

assert(optimizations.size() == 2);
assert(optimizations[0] == JlmOptCommandLineOptions::OptimizationId::CommonNodeElimination);
assert(optimizations[1] == JlmOptCommandLineOptions::OptimizationId::DeadNodeElimination);
EXPECT_EQ(optimizations.size(), 2);
EXPECT_EQ(optimizations[0], JlmOptCommandLineOptions::OptimizationId::CommonNodeElimination);
EXPECT_EQ(optimizations[1], JlmOptCommandLineOptions::OptimizationId::DeadNodeElimination);
}

JLM_UNIT_TEST_REGISTER(
"jlm/tooling/TestJlcCommandGraphGenerator-TestJlmOptOptimizations",
TestJlmOptOptimizations);

static void
TestJlmOptStatistics()
TEST(JlcCommandGraphGeneratorTests, TestJlmOptStatistics)
{
using namespace jlm::util;

Expand All @@ -126,9 +114,5 @@ TestJlmOptStatistics()
auto & statisticsCollectorSettings =
jlmOptCommand.GetCommandLineOptions().GetStatisticsCollectorSettings();

assert(statisticsCollectorSettings.GetDemandedStatistics() == expectedStatistics);
EXPECT_EQ(statisticsCollectorSettings.GetDemandedStatistics(), expectedStatistics);
}

JLM_UNIT_TEST_REGISTER(
"jlm/tooling/TestJlcCommandGraphGenerator-TestJlmOptStatistics",
TestJlmOptStatistics);
142 changes: 43 additions & 99 deletions tests/jlm/tooling/TestJlcCommandLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* See COPYING for terms of redistribution.
*/

#include "test-registry.hpp"
#include <gtest/gtest.h>

#include <jlm/tooling/CommandLine.hpp>

#include <cassert>
#include <cstring>

static const jlm::tooling::JlcCommandLineOptions &
Expand All @@ -25,101 +24,72 @@ ParseCommandLineArguments(const std::vector<std::string> & commandLineArguments)
cStrings.data());
}

static void
Test1()
TEST(JlcCommandLineParserTests, Test1)
{
/*
* Arrange
*/
// Arrange
std::vector<std::string> commandLineArguments({ "jlc", "-c", "-o", "foo.o", "foo.c" });

/*
* Act
*/
// Act
auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);

/*
* Assert
*/
assert(commandLineOptions.Compilations_.size() == 1);
// Assert
EXPECT_EQ(commandLineOptions.Compilations_.size(), 1);
auto & compilation = commandLineOptions.Compilations_[0];

assert(compilation.RequiresLinking() == false);
assert(compilation.OutputFile() == "foo.o");
EXPECT_EQ(compilation.RequiresLinking(), false);
EXPECT_EQ(compilation.OutputFile(), "foo.o");
}

static void
Test2()
TEST(JlcCommandLineParserTests, Test2)
{
/*
* Arrange
*/
// Arrange
std::vector<std::string> commandLineArguments({ "jlc", "-o", "foobar", "/tmp/f1.o" });

/*
* Act
*/
// Act
auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);

/*
* Assert
*/
assert(commandLineOptions.Compilations_.size() == 1);
assert(commandLineOptions.OutputFile_ == "foobar");
// Assert
EXPECT_EQ(commandLineOptions.Compilations_.size(), 1);
EXPECT_EQ(commandLineOptions.OutputFile_, "foobar");

auto & compilation = commandLineOptions.Compilations_[0];
assert(compilation.RequiresParsing() == false);
assert(compilation.RequiresOptimization() == false);
assert(compilation.RequiresAssembly() == false);
assert(compilation.RequiresLinking() == true);
EXPECT_FALSE(compilation.RequiresParsing());
EXPECT_FALSE(compilation.RequiresOptimization());
EXPECT_FALSE(compilation.RequiresAssembly());
EXPECT_TRUE(compilation.RequiresLinking());
}

static void
Test3()
TEST(JlcCommandLineParserTests, Test3)
{
using namespace jlm::tooling;

/*
* Arrange
*/
// Arrange
std::vector<std::string> commandLineArguments({ "jlc", "-O", "foobar.c" });

/*
* Act
*/
// Act
auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);

/*
* Assert
*/
assert(commandLineOptions.OptimizationLevel_ == JlcCommandLineOptions::OptimizationLevel::O0);
// Assert
EXPECT_EQ(commandLineOptions.OptimizationLevel_, JlcCommandLineOptions::OptimizationLevel::O0);
}

static void
Test4()
TEST(JlcCommandLineParserTests, Test4)
{
/*
* Arrange
*/
// Arrange
std::vector<std::string> commandLineArguments({ "jlc", "foobar.c", "-c" });

/*
* Act
*/
// Act
auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);

/*
* Assert
*/
assert(commandLineOptions.Compilations_.size() == 1);
// Assert
EXPECT_EQ(commandLineOptions.Compilations_.size(), 1);

auto & compilation = commandLineOptions.Compilations_[0];
assert(compilation.RequiresLinking() == false);
assert(compilation.OutputFile() == "foobar.o");
EXPECT_FALSE(compilation.RequiresLinking());
EXPECT_EQ(compilation.OutputFile(), "foobar.o");
}

static void
TestJlmOptOptimizations()
TEST(JlcCommandLineParserTests, TestJlmOptOptimizations)
{
using namespace jlm::tooling;

Expand All @@ -131,39 +101,27 @@ TestJlmOptOptimizations()
auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);

// Assert
assert(commandLineOptions.JlmOptOptimizations_.size() == 2);
assert(
commandLineOptions.JlmOptOptimizations_[0]
== JlmOptCommandLineOptions::OptimizationId::CommonNodeElimination);
assert(
commandLineOptions.JlmOptOptimizations_[1]
== JlmOptCommandLineOptions::OptimizationId::DeadNodeElimination);
EXPECT_EQ(commandLineOptions.JlmOptOptimizations_.size(), 2);
EXPECT_EQ(
commandLineOptions.JlmOptOptimizations_[0],
JlmOptCommandLineOptions::OptimizationId::CommonNodeElimination);
EXPECT_EQ(
commandLineOptions.JlmOptOptimizations_[1],
JlmOptCommandLineOptions::OptimizationId::DeadNodeElimination);
}

static void
TestFalseJlmOptOptimization()
TEST(JlcCommandLineParserTests, TestFalseJlmOptOptimization)
{
using namespace jlm::tooling;

// Arrange
std::vector<std::string> commandLineArguments({ "jlc", "-JFoobar", "foobar.c" });

// Act & Assert
bool exceptionThrown = false;
try
{
ParseCommandLineArguments(commandLineArguments);
}
catch (CommandLineParser::Exception &)
{
exceptionThrown = true;
}

assert(exceptionThrown);
EXPECT_THROW(ParseCommandLineArguments(commandLineArguments), CommandLineParser::Exception);
}

static void
TestJlmOptPassStatistics()
TEST(JlcCommandLineParserTests, TestJlmOptPassStatistics)
{
using namespace jlm::tooling;

Expand All @@ -173,26 +131,12 @@ TestJlmOptPassStatistics()
"--JlmOptPassStatistics=print-andersen-analysis",
"foobar.c" });

jlm::util::HashSet<jlm::util::Statistics::Id> expectedStatistics(
jlm::util::HashSet expectedStatistics(
{ jlm::util::Statistics::Id::Aggregation, jlm::util::Statistics::Id::AndersenAnalysis });

// Act
auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);

// Assert
assert(commandLineOptions.JlmOptPassStatistics_ == expectedStatistics);
EXPECT_EQ(commandLineOptions.JlmOptPassStatistics_, expectedStatistics);
}

static void
Test()
{
Test1();
Test2();
Test3();
Test4();
TestJlmOptOptimizations();
TestFalseJlmOptOptimization();
TestJlmOptPassStatistics();
}

JLM_UNIT_TEST_REGISTER("jlm/tooling/TestJlcCommandLineParser", Test)
Loading
Loading