Skip to content

Commit

Permalink
test: increase coverage (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks authored Mar 21, 2024
1 parent 9635bf6 commit e3a59bc
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/iguana/algorithms/Algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ namespace iguana {
/// @param creator the creator function
/// @param new_banks if this algorithm creates *new* banks, list them here
/// @returns true if the algorithm has not yet been registered
static bool Register(const std::string& name, algo_creator_t creator, const std::vector<std::string> new_banks={}) noexcept;
static bool Register(const std::string& name, algo_creator_t creator, const std::vector<std::string> new_banks = {}) noexcept;

/// Create an algorithm. Throws an exception if the algorithm cannot be created
/// @param name the name of the algorithm, which was used as an argument in the `AlgorithmFactory::Register` call
Expand Down
13 changes: 10 additions & 3 deletions src/iguana/services/Logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace iguana {
{
for(auto& [lev_i, lev_n] : m_level_names) {
if(lev == lev_n) {
SetLevel(lev_i);
m_level = lev_i;
Debug("log level set to '{}'", lev);
return;
}
}
Expand All @@ -30,8 +31,14 @@ namespace iguana {

void Logger::SetLevel(const Level lev)
{
m_level = lev;
Debug("log level set to '{}'", m_level_names.at(m_level));
try {
auto level_name = m_level_names.at(lev);
m_level = lev;
Debug("log level set to '{}'", level_name);
}
catch(const std::out_of_range& ex) {
Error("Log level '{}' is not a known log level; the log level will remain at '{}'", static_cast<int>(lev), m_level_names.at(m_level));
}
}

Logger::Level Logger::GetLevel()
Expand Down
23 changes: 14 additions & 9 deletions src/iguana/tests/iguana-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "TestAlgorithm.h"
#include "TestConfig.h"
#include "TestLogger.h"
#include "TestValidator.h"

int main(int argc, char** argv)
Expand All @@ -17,14 +18,16 @@ int main(int argc, char** argv)
std::vector<std::string> bank_names;

// get the command
auto exe = std::string(argv[0]);
auto UsageCommands = [&](int exit_code)
{
fmt::print(stderr, "\nUSAGE: {} [COMMAND] [OPTIONS]...\n", argv[0]);
fmt::print(stderr, "\nUSAGE: {} [COMMAND] [OPTIONS]...\n", exe);
fmt::print("\n COMMANDS:\n\n");
fmt::print(" {:<20} {}\n", "algorithm", "call `Run` on an algorithm");
fmt::print(" {:<20} {}\n", "validator", "run an algorithm's validator");
fmt::print(" {:<20} {}\n", "unit", "call `Test` on an algorithm, for unit tests");
fmt::print(" {:<20} {}\n", "config", "test config file parsing");
fmt::print(" {:<20} {}\n", "logger", "test Logger");
fmt::print("\n OPTIONS:\n\n");
fmt::print(" Each command has its own set of OPTIONS; either provide no OPTIONS\n");
fmt::print(" or use the --help option for more usage information about a specific command\n");
Expand Down Expand Up @@ -92,20 +95,23 @@ int main(int argc, char** argv)
else if(command == "config") {
available_options = {"t"};
}
else if(command == "logger") {
available_options = {};
}
else {
fmt::print(stderr, "ERROR: unknown command '{}'\n", command);
return 1;
}
available_options.push_back("v");
fmt::print(stderr, "\nUSAGE: {} {} [OPTIONS]...\n", argv[0], command);
fmt::print(stderr, "\nUSAGE: {} {} [OPTIONS]...\n", exe, command);
fmt::print("\n OPTIONS:\n\n");
for(auto available_opt : available_options) {
print_option.at(available_opt)();
fmt::print("\n");
}
return exit_code;
};
if(argc <= 2)
if(argc <= 2 && command != "logger")
return UsageOptions(2);
auto first_option = std::string(argv[2]);
if(first_option == "--help" || first_option == "-h")
Expand Down Expand Up @@ -165,15 +171,14 @@ int main(int argc, char** argv)
fmt::print("\n");

// run test
if(command == "algorithm" || command == "unit") {
if(command == "algorithm" || command == "unit")
return TestAlgorithm(command, algo_name, bank_names, data_file, num_events, verbose);
}
else if(command == "validator") {
else if(command == "validator")
return TestValidator(algo_name, bank_names, data_file, num_events, output_dir, verbose);
}
else if(command == "config") {
else if(command == "config")
return TestConfig(test_num, verbose);
}
else if(command == "logger")
return TestLogger();
else {
fmt::print(stderr, "ERROR: unknown command '{}'\n", command);
return 1;
Expand Down
2 changes: 2 additions & 0 deletions src/iguana/tests/include/TestAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ inline int TestAlgorithm(
// define the algorithm
iguana::AlgorithmSequence seq;
seq.Add(algo_name);
seq.SetName("TEST");
seq.PrintSequence();
seq.SetOption(algo_name, "log", verbose ? "trace" : "info");

// start the algorithm
Expand Down
12 changes: 12 additions & 0 deletions src/iguana/tests/include/TestConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ inline int TestConfig(int test_num, bool verbose)
fmt::print(stderr, "ERROR: need a test number\n");
return 1;
}
// first, some sanity checks of `ConfigFileReader`
iguana::ConfigFileReader bad_config("bad_config");
bad_config.AddDirectory("non_existent_directory");
try {
bad_config.AddFile("non_existent_file.yaml");
}
catch(const std::exception& ex) {
fmt::print("excpected exception thrown when trying to add non-existent file\n");
}
bad_config.PrintDirectories();

// then test configuring an algorithm
auto algo = iguana::AlgorithmFactory::Create("example::ExampleAlgorithm");
algo->SetOption("log", verbose ? "debug" : "info");
algo->SetConfigDirectory("src/iguana/tests"); // must be relative to build directory
Expand Down
45 changes: 45 additions & 0 deletions src/iguana/tests/include/TestLogger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// test configuration

#include <iguana/services/Logger.h>

inline int TestLogger()
{
// this test just runs the `Logger` methods to catch any runtime errors
std::vector<iguana::Logger> logs;
logs.push_back({"styled_logger", iguana::Logger::Level::trace});
logs.push_back({"unstyled_logger", iguana::Logger::Level::trace});

// set styles
logs.at(0).EnableStyle();
logs.at(1).DisableStyle();

// set non-existent level; should print errors
auto non_existent_level = static_cast<iguana::Logger::Level>(1000);
logs.at(0).SetLevel(non_existent_level);
logs.at(0).SetLevel("non_existent_level");

for(auto& log : logs) {
// test all log levels
log.Trace("trace is level {}", static_cast<int>(iguana::Logger::Level::trace));
log.Debug("debug is level {}", static_cast<int>(iguana::Logger::Level::debug));
log.Info("info is level {}", static_cast<int>(iguana::Logger::Level::info));
log.Warn("warn is level {}", static_cast<int>(iguana::Logger::Level::warn));
log.Error("error is level {}", static_cast<int>(iguana::Logger::Level::error));
// test non-existent level
log.Print(non_existent_level, "print to non-existent log level {}", static_cast<int>(non_existent_level));
// test silence
log.SetLevel("silent");
log.Error("if this prints, 'silent' level failed");
log.SetLevel("trace");
// test run-time errors from `fmt`
log.Info("too many arguments: {}", 1, 2); // noexcept
try {
log.Info("too few arguments: {} {}", 1);
}
catch(const std::exception& ex) {
log.Info("too few arguments test threw expected exception");
}
}

return 0;
}
19 changes: 19 additions & 0 deletions src/iguana/tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ else
message('Test sample file NOT ' + stat_file + '; you may run algorithm tests manually with `' + get_option('prefix') / get_option('bindir') / test_exe_name + '`')
endif

# test usage guide for each iguana-test command
foreach command : ['', 'unknown', 'algorithm', 'validator', 'unit', 'config']
test(
'iguana-test-usage-guide' + (command=='' ? '' : '-' + command),
test_exe,
args: command=='' ? [] : [command],
env: project_test_env,
should_fail: true,
)
endforeach

# test config files
foreach test_num : [ '1', '2', '3' ]
config_file_name = 'test_' + test_num + '.yaml'
Expand All @@ -88,3 +99,11 @@ foreach test_num : [ '1', '2', '3' ]
env: project_test_env,
)
endforeach

# test Logger
test(
'logger',
test_exe,
args: [ 'logger' ],
env: project_test_env,
)

0 comments on commit e3a59bc

Please sign in to comment.