Jlm
JlmOptCommandLineParserTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #include <gtest/gtest.h>
7 
9 
10 #include <cstring>
11 
13 ParseCommandLineArguments(const std::vector<std::string> & commandLineArguments)
14 {
15  std::vector<const char *> cStrings;
16  for (const auto & commandLineArgument : commandLineArguments)
17  {
18  cStrings.push_back(commandLineArgument.c_str());
19  }
20 
21  static jlm::tooling::JlmOptCommandLineParser commandLineParser;
22  return commandLineParser.ParseCommandLineArguments(
23  static_cast<int>(cStrings.size()),
24  cStrings.data());
25 }
26 
27 TEST(JlmOptCommandLinerParserTests, TestOptimizationCommandLineArgumentConversion)
28 {
29  using namespace jlm::tooling;
30 
31  for (size_t n =
33  n != static_cast<std::size_t>(JlmOptCommandLineOptions::OptimizationId::LastEnumValue);
34  n++)
35  {
36  auto expectedOptimizationId = static_cast<JlmOptCommandLineOptions::OptimizationId>(n);
37  auto commandLineArgument =
39  auto receivedOptimizationId =
41 
42  EXPECT_EQ(receivedOptimizationId, expectedOptimizationId);
43  }
44 }
45 
46 TEST(JlmOptCommandLinerParserTests, TestStatisticsCommandLineArgumentConversion)
47 {
48  using namespace jlm::tooling;
49  for (size_t n = static_cast<std::size_t>(jlm::util::Statistics::Id::FirstEnumValue) + 1;
50  n != static_cast<std::size_t>(jlm::util::Statistics::Id::LastEnumValue);
51  n++)
52  {
53  auto expectedStatisticsId = static_cast<jlm::util::Statistics::Id>(n);
54  auto commandLineArgument =
56  auto receivedStatisticsId =
58 
59  EXPECT_EQ(receivedStatisticsId, expectedStatisticsId);
60  }
61 }
62 
63 TEST(JlmOptCommandLinerParserTests, TestOutputFormatToCommandLineArgument)
64 {
65  using namespace jlm::tooling;
66 
67  // Arrange
68  auto start = static_cast<std::size_t>(JlmOptCommandLineOptions::OutputFormat::FirstEnumValue) + 1;
69  auto end = static_cast<std::size_t>(JlmOptCommandLineOptions::OutputFormat::LastEnumValue);
70 
71  // Act & Assert
72  for (size_t n = start; n != end; n++)
73  {
74  auto outputFormat = static_cast<JlmOptCommandLineOptions::OutputFormat>(n);
75 
76  // throws exception / asserts on failure
78  }
79 }
80 
81 TEST(JlmOptCommandLinerParserTests, OutputFormatParsing)
82 {
83  using namespace jlm::tooling;
84 
85  auto testOutputFormatParsing =
86  [](std::string_view outputFormatString, JlmOptCommandLineOptions::OutputFormat outputFormat)
87  {
88  // Arrange
89  std::vector<std::string> commandLineArguments(
90  { "jlm-opt", "--output-format", std::string(outputFormatString), "foo.c" });
91 
92  // Act
93  auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);
94 
95  // Assert
96  EXPECT_EQ(commandLineOptions.GetOutputFormat(), outputFormat);
97  };
98 
99  auto start = static_cast<std::size_t>(JlmOptCommandLineOptions::OutputFormat::FirstEnumValue) + 1;
100  auto end = static_cast<std::size_t>(JlmOptCommandLineOptions::OutputFormat::LastEnumValue);
101 
102  for (size_t n = start; n != end; n++)
103  {
104  auto outputFormat = static_cast<JlmOptCommandLineOptions::OutputFormat>(n);
105 #ifndef ENABLE_MLIR
107  continue;
108 #endif
109 
110  auto outputFormatString = JlmOptCommandLineOptions::ToCommandLineArgument(outputFormat);
111  testOutputFormatParsing(outputFormatString, outputFormat);
112  }
113 }
static const jlm::tooling::JlmOptCommandLineOptions & ParseCommandLineArguments(const std::vector< std::string > &commandLineArguments)
TEST(JlmOptCommandLinerParserTests, TestOptimizationCommandLineArgumentConversion)
static OptimizationId FromCommandLineArgumentToOptimizationId(std::string_view commandLineArgument)
static std::string_view ToCommandLineArgument(OptimizationId optimizationId)
static util::Statistics::Id FromCommandLineArgumentToStatisticsId(std::string_view commandLineArgument)
const JlmOptCommandLineOptions & ParseCommandLineArguments(int argc, const char *const *argv) override