Files
MeanField/tests/test_main.cpp

614 lines
23 KiB
C++

#include <algorithm>
#include <catch2/catch_session.hpp>
#include <catch2/catch_test_case_info.hpp>
#include <catch2/reporters/catch_reporter_registrars.hpp>
#include <catch2/reporters/catch_reporter_streaming_base.hpp>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <mfem.hpp>
#include <regex>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_set>
#include <utility>
#include <vector>
#include <CLI/CLI.hpp>
#include <fourdst/config/config.h>
import mean_field;
import test_helpers;
std::string escapeHtml(const std::string &data) {
std::string buffer;
buffer.reserve(data.size());
for (size_t pos = 0; pos != data.size(); ++pos) {
switch (data[pos]) {
case '&':
buffer.append("&amp;");
break;
case '\"':
buffer.append("&quot;");
break;
case '\'':
buffer.append("&apos;");
break;
case '<':
buffer.append("&lt;");
break;
case '>':
buffer.append("&gt;");
break;
default:
buffer.append(&data[pos], 1);
break;
}
}
return buffer;
}
std::string ansiToHtml(const std::string &text) {
// Convert text to HTML-safe first
std::string htmlEscaped = escapeHtml(text);
std::ostringstream oss;
size_t i = 0;
size_t len = htmlEscaped.length();
int openSpans = 0;
auto closeSpans = [&oss, &openSpans]() {
while (openSpans > 0) {
oss << "</span>";
--openSpans;
}
};
while (i < len) {
// Look for ANSI CSI sequence '\033[' or '\x1b['
if ((htmlEscaped[i] == '\033' || htmlEscaped[i] == '\x1b') && i + 1 < len && htmlEscaped[i + 1] == '[') {
size_t seqStart = i + 2;
size_t seqEnd = htmlEscaped.find('m', seqStart);
if (seqEnd != std::string::npos) {
std::string codeStr = htmlEscaped.substr(seqStart, seqEnd - seqStart);
i = seqEnd + 1;
std::istringstream codeStream(codeStr);
std::string codeVal;
// Defaults if sequence is just \033[m (Reset)
if (codeStr.empty()) {
closeSpans();
continue;
}
while (std::getline(codeStream, codeVal, ';')) {
int code = 0;
try {
code = std::stoi(codeVal);
} catch (...) {
continue;
}
switch (code) {
case 0: // Reset
closeSpans();
break;
case 1: // Bold
oss << "<span style='font-weight:bold;'>";
openSpans++;
break;
case 2: // Dim
oss << "<span style='opacity:0.7;'>";
openSpans++;
break;
// Standard Foreground Colors
case 30:
oss << "<span style='color:#2c3e50;'>";
openSpans++;
break; // Black
case 31:
oss << "<span style='color:#e74c3c;'>";
openSpans++;
break; // Red
case 32:
oss << "<span style='color:#27ae60;'>";
openSpans++;
break; // Green
case 33:
oss << "<span style='color:#f39c12;'>";
openSpans++;
break; // Yellow
case 34:
oss << "<span style='color:#2980b9;'>";
openSpans++;
break; // Blue
case 35:
oss << "<span style='color:#8e44ad;'>";
openSpans++;
break; // Magenta
case 36:
oss << "<span style='color:#16a085;'>";
openSpans++;
break; // Cyan
case 37:
oss << "<span style='color:#bdc3c7;'>";
openSpans++;
break; // Light Gray
// Bright Foreground Colors
case 90:
oss << "<span style='color:#7f8c8d;'>";
openSpans++;
break; // Dark Gray
case 91:
oss << "<span style='color:#ff6b6b;'>";
openSpans++;
break; // Bright Red
case 92:
oss << "<span style='color:#51cf66;'>";
openSpans++;
break; // Bright Green
case 93:
oss << "<span style='color:#fcc419;'>";
openSpans++;
break; // Bright Yellow
case 94:
oss << "<span style='color:#339af0;'>";
openSpans++;
break; // Bright Blue
case 95:
oss << "<span style='color:#cc5de8;'>";
openSpans++;
break; // Bright Magenta
case 96:
oss << "<span style='color:#22b8cf;'>";
openSpans++;
break; // Bright Cyan
case 97:
oss << "<span style='color:#ffffff;'>";
openSpans++;
break; // White
default:
break;
}
}
continue;
}
}
oss << htmlEscaped[i];
++i;
}
closeSpans();
return oss.str();
}
std::vector<std::string> wrapText(
const std::string &text,
size_t width
) {
std::vector<std::string> lines;
std::istringstream words(text);
std::string word, line;
while (words >> word) {
if (line.length() + word.length() + 1 > width) {
if (!line.empty()) {
lines.push_back(line);
line.clear();
}
if (word.length() > width) {
lines.push_back(word.substr(0, width - 3) + "...");
continue;
}
}
if (!line.empty())
line += " ";
line += word;
}
if (!line.empty())
lines.push_back(line);
if (lines.empty())
lines.push_back("");
return lines;
}
class CheckReporter : public Catch::StreamingReporterBase {
struct TestCaseData {
std::string name;
std::string tags;
bool passed;
std::size_t assertionsPassed;
std::size_t assertionsFailed;
double durationSeconds;
std::vector<std::string> failureMessages;
std::vector<std::string> infoMessages;
};
std::vector<std::string> m_currentFailures;
std::vector<std::string> m_currentInfos;
std::unordered_set<unsigned int> m_currentInfoSequences;
std::vector<TestCaseData> m_testRunData;
std::chrono::time_point<std::chrono::steady_clock> m_testStartTime;
void captureInfoMessages(Catch::AssertionStats const &assertionStats) {
for (auto const &message : assertionStats.infoMessages) {
if (m_currentInfoSequences.insert(message.sequence).second) {
m_currentInfos.push_back(message.message);
}
}
}
public:
explicit CheckReporter(Catch::ReporterConfig &&config) : Catch::StreamingReporterBase(std::move(config)) {
// INFO messages are delivered through assertionEnded. Request passing
// assertions as well so HTML logging does not depend on Catch2's -s
// flag.
m_preferences.shouldReportAllAssertions = true;
// This reporter does not use assertionStarting events. Disabling them
// preserves Catch2's successful-assertion fast path where possible.
m_preferences.shouldReportAllAssertionStarts = false;
}
static std::string getDescription() {
return "Console reporter with wrapping, tags, live test progress, and collapsible HTML "
"export with ANSI color rendering.";
}
void testRunStarting(Catch::TestRunInfo const &_testRunInfo) override {
StreamingReporterBase::testRunStarting(_testRunInfo);
std::cout << '\n';
std::cout << std::left << std::setw(85) << "Test Case Name"
<< "Status " << std::right << std::setw(8) << "Passed" << std::setw(8) << "Failed" << std::setw(12)
<< "Time (s)" << '\n';
std::cout << std::string(133, '-') << '\n';
}
void testCaseStarting(Catch::TestCaseInfo const &testInfo) override {
StreamingReporterBase::testCaseStarting(testInfo);
m_testStartTime = std::chrono::steady_clock::now();
std::string name = testInfo.name;
auto wrappedName = wrapText(name, 83);
// Print progress line, \r to overwrite later, \033[K to clear till end of line
std::cout << "\r\033[K" << std::left << std::setw(85) << (wrappedName[0] + " ...") << std::flush;
}
void assertionEnded(Catch::AssertionStats const &assertionStats) override {
StreamingReporterBase::assertionEnded(assertionStats);
// Capture every INFO message encountered by either a passing or failing
// assertion. Message sequence IDs prevent a scoped INFO from being
// repeated once for every assertion that occurs while it remains
// active.
captureInfoMessages(assertionStats);
if (!assertionStats.assertionResult.isOk()) {
auto const &result = assertionStats.assertionResult;
std::ostringstream oss;
oss << " \033[31m-> FAILED:\033[0m " << result.getSourceInfo().file << ":" << result.getSourceInfo().line
<< '\n';
oss << " " << result.getTestMacroName() << "( " << result.getExpression() << " )\n";
if (result.hasExpandedExpression()) {
oss << " with expansion:\n"
<< " " << result.getExpandedExpression() << '\n';
}
for (auto const &msg : assertionStats.infoMessages) {
oss << " \033[36m[INFO]\033[0m " << msg.message << '\n';
}
m_currentFailures.push_back(oss.str());
}
}
void testCaseEnded(Catch::TestCaseStats const &stats) override {
StreamingReporterBase::testCaseEnded(stats);
auto endTime = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed = endTime - m_testStartTime;
double duration_s = elapsed.count();
bool passed = stats.totals.assertions.allPassed();
std::string mark = passed ? "\033[32m✓\033[0m" : "\033[31m✗\033[0m";
std::string name = stats.testInfo->name;
auto wrappedName = wrapText(name, 83);
// Overwrite the loading line with the actual result
std::cout << "\r\033[K" << std::left << std::setw(85) << wrappedName[0] << mark << " " << std::right
<< std::setw(8) << stats.totals.assertions.passed << std::setw(8) << stats.totals.assertions.failed
<< std::setw(11) << std::fixed << std::setprecision(3) << duration_s << "s\n";
for (size_t i = 1; i < wrappedName.size(); ++i) {
std::cout << " \033[90m↳ \033[0m" // Dim indent arrow
<< std::left << std::setw(81) << wrappedName[i] << '\n';
}
std::string tagsStr = stats.testInfo->tagsAsString();
if (!tagsStr.empty()) {
auto wrappedTags = wrapText("Tags: " + tagsStr, 83);
for (const auto &line : wrappedTags) {
std::cout << " \033[36m" << line << "\033[0m\n"; // Cyan
}
}
if (!m_currentFailures.empty()) {
std::cout << '\n';
for (auto const &failure : m_currentFailures) {
std::cout << failure << '\n';
}
std::cout << std::string(133, '-') << '\n';
}
m_testRunData.push_back(
{name, tagsStr, passed, stats.totals.assertions.passed, stats.totals.assertions.failed, duration_s,
m_currentFailures, m_currentInfos}
);
m_currentFailures.clear();
m_currentInfos.clear();
m_currentInfoSequences.clear();
}
void testRunEnded(Catch::TestRunStats const &_testRunStats) override {
StreamingReporterBase::testRunEnded(_testRunStats);
std::cout << std::string(133, '=') << '\n';
auto const &tc = _testRunStats.totals.testCases;
auto const &as = _testRunStats.totals.assertions;
std::string tc_passed_str =
tc.passed > 0 ? "\033[32m" + std::to_string(tc.passed) + " passed\033[0m" : "0 passed";
std::string tc_failed_str =
tc.failed > 0 ? "\033[31m" + std::to_string(tc.failed) + " failed\033[0m" : "0 failed";
std::string as_passed_str =
as.passed > 0 ? "\033[32m" + std::to_string(as.passed) + " passed\033[0m" : "0 passed";
std::string as_failed_str =
as.failed > 0 ? "\033[31m" + std::to_string(as.failed) + " failed\033[0m" : "0 failed";
std::cout << "Test Cases: " << tc_passed_str << ", " << tc_failed_str << ", " << tc.total() << " total\n";
std::cout << "Assertions: " << as_passed_str << ", " << as_failed_str << ", " << as.total() << " total\n\n";
generateHtmlReport(_testRunStats);
}
private:
void generateHtmlReport(Catch::TestRunStats const &stats) {
std::ofstream html("test_summary.html");
if (!html)
return;
html << "<!DOCTYPE html>\n<html lang='en'>\n<head>\n"
<< "<meta charset='UTF-8'>\n"
<< "<meta name='viewport' content='width=device-width, "
"initial-scale=1.0'>\n"
<< "<title>Test Run Summary</title>\n"
<< "<style>\n"
<< "body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe "
"UI', "
"Roboto, Helvetica, Arial, sans-serif; "
"background: #f4f6f8; color: #333; margin: 0; padding: 2rem; }\n"
<< "h1 { color: #2c3e50; border-bottom: 2px solid #e0e0e0; "
"padding-bottom: 0.5rem; }\n"
<< ".summary-cards { display: flex; gap: 1rem; margin-bottom: "
"2rem; }\n"
<< ".card { background: white; padding: 1rem 1.5rem; "
"border-radius: "
"8px; box-shadow: 0 2px 4px "
"rgba(0,0,0,0.05); flex: 1; }\n"
<< ".card h3 { margin-top: 0; font-size: 0.9rem; color: #7f8c8d; "
"text-transform: uppercase; }\n"
<< ".card p { font-size: 1.5rem; font-weight: bold; margin: 0; }\n"
<< ".text-green { color: #27ae60; }\n"
<< ".text-red { color: #e74c3c; }\n"
<< ".test-item { background: white; border-radius: 8px; padding: "
"1rem; "
"margin-bottom: 1rem; box-shadow: 0 2px "
"4px rgba(0,0,0,0.05); border-left: 5px solid #bdc3c7; }\n"
<< ".test-item.passed { border-left-color: #27ae60; }\n"
<< ".test-item.failed { border-left-color: #e74c3c; }\n"
<< ".test-header { display: flex; justify-content: space-between; "
"align-items: flex-start; }\n"
<< ".test-name { font-size: 1.1rem; font-weight: 600; margin: 0 0 "
"0.5rem 0; word-break: break-word; }\n"
<< ".tags { font-size: 0.8rem; color: #2980b9; background: "
"#ebf5fb; "
"padding: 2px 6px; border-radius: 4px; "
"display: inline-block; margin-top: 4px; }\n"
<< ".stats { font-size: 0.9rem; color: #7f8c8d; }\n"
<< "details { margin-top: 0.8rem; background: #f8f9fa; border: 1px "
"solid #e9ecef; border-radius: 6px; "
"padding: 0.5rem 0.8rem; }\n"
<< "summary { cursor: pointer; font-weight: 600; color: #34495e; "
"user-select: none; font-size: 0.9rem; }\n"
<< "summary:hover { color: #2980b9; }\n"
<< "pre { background: #1e293b; color: #f8fafc; padding: 1rem; "
"border-radius: 4px; overflow-x: auto; "
"font-size: 0.85rem; line-height: 1.4; margin-top: 0.5rem; }\n"
<< "pre.info-block { background: #0f172a; border-left: 4px solid "
"#0284c7; }\n"
<< "</style>\n</head>\n<body>\n";
html << "<h1>Test Run Summary</h1>\n";
// Summary Cards
html << "<div class='summary-cards'>\n";
html << "<div class='card'><h3>Total Cases</h3><p>" << stats.totals.testCases.total() << "</p></div>\n";
html << "<div class='card'><h3>Cases Passed</h3><p class='text-green'>" << stats.totals.testCases.passed
<< "</p></div>\n";
html << "<div class='card'><h3>Cases Failed</h3><p class='text-red'>" << stats.totals.testCases.failed
<< "</p></div>\n";
html << "</div>\n";
for (const auto &test : m_testRunData) {
std::string statusClass = test.passed ? "passed" : "failed";
html << "<div class='test-item " << statusClass << "'>\n";
html << " <div class='test-header'>\n";
html << " <div>\n";
html << " <h3 class='test-name'>" << escapeHtml(test.name) << "</h3>\n";
if (!test.tags.empty()) {
html << " <div class='tags'>" << escapeHtml(test.tags) << "</div>\n";
}
html << " </div>\n";
html << " <div class='stats'>\n";
html << " <span class='text-green'>&#10003; " << test.assertionsPassed << "</span> | ";
html << " <span class='text-red'>&#10007; " << test.assertionsFailed << "</span> | ";
html << " <span style='color: #34495e;'>&#8987; " << std::fixed << std::setprecision(3)
<< test.durationSeconds << "s</span>\n";
html << " </div>\n";
html << " </div>\n";
// Collapsible INFO Messages section with ANSI color rendering
if (!test.infoMessages.empty()) {
html << " <details>\n";
html << " <summary>Info Logs (" << test.infoMessages.size() << ")</summary>\n";
html << " <pre class='info-block'>";
for (const auto &info : test.infoMessages) {
html << "[INFO] " << ansiToHtml(info) << "\n";
}
html << "</pre>\n";
html << " </details>\n";
}
// Collapsible Failures section with ANSI color rendering
if (!test.failureMessages.empty()) {
html << " <details open>\n";
html << " <summary class='text-red'>Failure Details (" << test.failureMessages.size()
<< ")</summary>\n";
html << " <pre>";
for (const auto &msg : test.failureMessages) {
html << ansiToHtml(msg) << "\n";
}
html << " </pre>\n";
html << " </details>\n";
}
html << "</div>\n";
}
html << "</body>\n</html>\n";
}
};
CATCH_REGISTER_REPORTER(
"check",
CheckReporter
)
int main(
int argc,
char *argv[]
) {
fourdst::config::Config<mean_field::utils::Args> cfg;
CLI::App app{"Mean Field Tests"};
app.allow_extras();
app.set_help_flag("--config-help", "Show mean-field configuration options");
fourdst::config::register_as_cli(cfg, app);
std::vector<std::string> config_arguments;
std::vector<std::string> forced_catch_arguments;
config_arguments.emplace_back(argv[0]);
bool parsing_catch_arguments = false;
for (int i = 1; i < argc; ++i) {
if (std::string_view(argv[i]) == "--catch2") {
parsing_catch_arguments = true;
continue;
}
if (parsing_catch_arguments) {
forced_catch_arguments.emplace_back(argv[i]);
} else {
config_arguments.emplace_back(argv[i]);
}
}
std::vector<const char *> config_argv;
config_argv.reserve(config_arguments.size());
for (const std::string &argument : config_arguments) {
config_argv.push_back(argument.c_str());
}
try {
app.parse(static_cast<int>(config_argv.size()), config_argv.data());
} catch (const CLI::ParseError &error) {
return app.exit(error);
}
std::vector<std::string> catch_arguments;
catch_arguments.emplace_back(argv[0]);
for (const std::string &argument : app.remaining()) {
catch_arguments.push_back(argument);
}
for (const std::string &argument : forced_catch_arguments) {
catch_arguments.push_back(argument);
}
const auto is_reporter_option = [](const std::string &argument) {
return argument == "-r" || argument == "--reporter" || argument.starts_with("-r=") ||
argument.starts_with("--reporter=");
};
if (const bool has_reporter = std::ranges::any_of(catch_arguments, is_reporter_option); !has_reporter) {
catch_arguments.emplace_back("--reporter");
catch_arguments.emplace_back("check");
}
std::vector<const char *> catch_argv;
catch_argv.reserve(catch_arguments.size());
for (const std::string &argument : catch_arguments) {
catch_argv.push_back(argument.c_str());
}
Catch::Session session;
if (const int catch_parse_result = session.applyCommandLine(static_cast<int>(catch_argv.size()), catch_argv.data());
catch_parse_result != 0) {
return catch_parse_result;
}
mfem::Mpi::Init(argc, argv);
constexpr std::string device_config = "cpu";
mfem::Device device(device_config);
const int hdiv_max_q1d = mfem::DeviceDofQuadLimits::Get().HDIV_MAX_Q1D;
std::cout << "H(div) maximum Q1D = " << hdiv_max_q1d << '\n';
std::cout << "Approximate maximum safe integration order = " << 2 * hdiv_max_q1d - 1 << '\n';
mean_field::utils::Args test_args = cfg.main();
if (app.count("--mesh_file") == 0) {
test_args.mesh_file = "sandbox.smesh";
}
if (app.count("--p.rtol") == 0) {
test_args.p.rtol = 1.0e-12;
}
if (app.count("--p.atol") == 0) {
test_args.p.atol = 1.0e-12;
}
test_utils::set_args(std::move(test_args));
return session.run();
}