6#ifndef CHEBYSHEV_OUTPUT_H
7#define CHEBYSHEV_OUTPUT_H
30 struct field_options {
32 using FieldInterpreter = std::function<std::string(
const std::string&)>;
56 using OutputFormat_t = std::function<
58 const std::vector<std::vector<std::string>>&,
59 const std::vector<std::string>&,
132 const std::vector<std::vector<std::string>>& table,
133 const std::vector<std::string>& fields,
139 std::stringstream result;
141 for (
size_t i = 0; i < table.size(); ++i) {
143 if(table[i].size() != fields.size()) {
145 "Number of columns and fields argument must have "
146 "the same size in output::format::barebone");
149 for (
size_t j = 0; j < table[i].size(); ++j) {
151 auto it = settings.fieldOptions.find(fields[j]);
153 if(it != settings.fieldOptions.end() && i)
154 result << std::setw(it->second.columnWidth)
155 << std::left << it->second.fieldInterpreter(table[i][j]);
156 else if(it != settings.fieldOptions.end())
157 result << std::setw(it->second.columnWidth)
158 << std::left << table[i][j];
160 result << std::setw(settings.defaultColumnWidth)
161 << std::left << table[i][j];
180 const std::vector<std::vector<std::string>>& table,
181 const std::vector<std::string>& fields,
187 std::stringstream result;
188 std::stringstream header_str;
192 for (
size_t i = 0; i < table[0].size(); ++i) {
194 auto it = settings.fieldOptions.find(fields[i]);
196 if(it != settings.fieldOptions.end())
197 header_str << std::setw(it->second.columnWidth) << table[0][i] <<
" | ";
199 header_str << std::setw(settings.defaultColumnWidth) << table[0][i] <<
" | ";
202 std::string header = header_str.str();
203 std::string decoration =
" +";
204 for (
size_t i = 4; i < header.size(); ++i)
206 decoration +=
"+ \n";
208 for (
size_t i = 1; i < table.size(); ++i) {
210 if(table[i].size() != fields.size()) {
212 "Number of columns and <fields> argument must have "
213 "the same size in output::format::simple");
218 for (
size_t j = 0; j < table[i].size(); ++j) {
220 auto it = settings.fieldOptions.find(fields[j]);
222 if(it != settings.fieldOptions.end())
223 result << std::setw(it->second.columnWidth)
224 << it->second.fieldInterpreter(table[i][j]) <<
" | ";
226 result << std::setw(settings.defaultColumnWidth)
227 << table[i][j] <<
" | ";
250 return [adaptiveWidth](
251 const std::vector<std::vector<std::string>>& table,
252 const std::vector<std::string>& fields,
260 size_t eff_length = 0;
261 std::stringstream header_str;
267 auto proc_table = table;
270 for (
size_t i = 0; i < proc_table[0].size(); ++i) {
272 auto it = settings.fieldOptions.find(fields[i]);
274 if (it == settings.fieldOptions.end())
277 for (
size_t j = 1; j < proc_table.size(); ++j)
278 proc_table[j][i] = it->second.fieldInterpreter(table[j][i]);
281 std::vector<size_t> computedWidth;
286 computedWidth.resize(proc_table[0].size());
288 for (
size_t i = 0; i < proc_table[0].size(); ++i) {
290 computedWidth[i] = settings.defaultColumnWidth;
292 for (
size_t j = 0; j < proc_table.size(); ++j)
293 if (proc_table[j][i].size() > computedWidth[i])
294 computedWidth[i] = proc_table[j][i].size();
299 for (
size_t i = 0; i < table[0].size(); ++i) {
301 auto it = settings.fieldOptions.find(fields[i]);
303 unsigned int width = 0;
305 if(it != settings.fieldOptions.end() && !adaptiveWidth)
306 width = it->second.columnWidth;
307 else if (adaptiveWidth)
308 width = computedWidth[i];
310 width = settings.defaultColumnWidth;
312 header_str << std::setw(width) << table[0][i] <<
" │ ";
313 eff_length += width + 3;
316 std::string header =
" ┌";
319 for (
size_t i = 4; i < eff_length; ++i)
323 header += header_str.str() +
"\n";
327 for (
size_t i = 4; i < eff_length; ++i)
331 std::stringstream result;
333 for (
size_t i = 1; i < proc_table.size(); ++i) {
335 if(proc_table[i].size() != fields.size()) {
337 "Number of columns and <fields> argument must have "
338 "the same size in output::format::fancy");
343 for (
size_t j = 0; j < proc_table[i].size(); ++j) {
345 auto it = settings.fieldOptions.find(fields[j]);
347 if(it != settings.fieldOptions.end() && !adaptiveWidth)
348 result << std::setw(it->second.columnWidth)
349 << proc_table[i][j] <<
" │ ";
350 else if (adaptiveWidth) {
351 result << std::setw(computedWidth[j])
352 << proc_table[i][j] <<
" │ ";
354 result << std::setw(settings.defaultColumnWidth)
355 << proc_table[i][j] <<
" │ ";
361 std::string underline =
" └";
362 for (
size_t i = 4; i < eff_length; ++i) {
367 return header + result.str() + underline;
380 const std::vector<std::vector<std::string>>& table,
381 const std::vector<std::string>& fields,
386 for (
size_t i = 0; i < table.size(); ++i) {
388 if(table[i].size() != fields.size()) {
389 throw std::runtime_error(
390 "Number of columns and <fields> argument must have "
391 "the same size in output::format::csv");
395 for (
size_t j = 0; j < table[i].size(); ++j) {
397 auto it = settings.fieldOptions.find(fields[j]);
399 if(it != settings.fieldOptions.end() && i)
400 s <<
"\"" << it->second.fieldInterpreter(table[i][j]) <<
"\"";
402 s <<
"\"" << table[i][j] <<
"\"";
404 if(j != table[i].size() - 1)
421 const std::vector<std::vector<std::string>>& table,
422 const std::vector<std::string>& fields,
428 std::stringstream result;
429 std::stringstream header_str;
433 for (
size_t i = 0; i < table[0].size(); ++i) {
435 auto it = settings.fieldOptions.find(fields[i]);
437 if(it != settings.fieldOptions.end())
438 header_str << std::setw(it->second.columnWidth)
439 << std::left << table[0][i];
441 header_str << std::setw(settings.defaultColumnWidth)
442 << std::left << table[0][i];
447 std::string header = header_str.str();
448 std::string decoration =
"|";
449 for (
size_t i = 1; i < header.size() - 1; ++i)
450 decoration += (header[i] ==
'|') ?
"|" :
"-";
453 for (
size_t i = 1; i < table.size(); ++i) {
455 if(table[i].size() != fields.size()) {
457 "Number of columns and <fields> argument must have "
458 "the same size in output::format::markdown");
463 for (
size_t j = 0; j < table[i].size(); ++j) {
465 auto it = settings.fieldOptions.find(fields[j]);
467 if(it != settings.fieldOptions.end())
468 result << std::setw(it->second.columnWidth)
469 << std::left << it->second.fieldInterpreter(table[i][j]);
471 result << std::setw(settings.defaultColumnWidth)
472 << std::left << table[i][j];
480 return header +
"\n" + decoration + result.str();
490 const std::vector<std::vector<std::string>>& table,
491 const std::vector<std::string>& fields,
497 std::stringstream result;
498 result <<
"\\begin{tabular}{";
503 for (
unsigned int i = 0; i < fields.size(); ++i)
506 result <<
"}\n\\hline\n";
508 for (
size_t i = 0; i < table[0].size(); ++i) {
510 result << table[0][i];
512 if(i != table[0].size() - 1)
515 result <<
" \\\\\n\\hline\n";
517 for (
size_t i = 1; i < table.size(); ++i) {
519 if(table[i].size() != fields.size()) {
521 "Number of columns and <fields> argument must have "
522 "the same size in output::format::latex");
525 for (
size_t j = 0; j < table[i].size(); ++j) {
527 auto it = settings.fieldOptions.find(fields[j]);
529 if(it != settings.fieldOptions.end())
530 result << it->second.fieldInterpreter(table[i][j]);
532 result << table[i][j];
534 if(j != table[i].size() - 1)
541 result <<
"\\hline\n\\end{tabular}\n";
566 settings.fieldNames[
"name"] =
"Function";
567 settings.fieldNames[
"maxErr"] =
"Max Err.";
568 settings.fieldNames[
"meanErr"] =
"Mean Err.";
569 settings.fieldNames[
"rmsErr"] =
"RMS Err.";
570 settings.fieldNames[
"relErr"] =
"Rel. Err.";
571 settings.fieldNames[
"absErr"] =
"Abs. Err.";
572 settings.fieldNames[
"tolerance"] =
"Tolerance";
573 settings.fieldNames[
"failed"] =
"Result";
574 settings.fieldNames[
"iterations"] =
"Iterations";
577 settings.fieldNames[
"difference"] =
"Difference";
578 settings.fieldNames[
"evaluated"] =
"Evaluated";
579 settings.fieldNames[
"expected"] =
"Expected";
582 settings.fieldNames[
"totalRuntime"] =
"Tot. Time (ms)";
583 settings.fieldNames[
"averageRuntime"] =
"Avg. Time (ms)";
584 settings.fieldNames[
"stdevRuntime"] =
"Stdev. Time (ms)";
585 settings.fieldNames[
"runsPerSecond"] =
"Runs per Sec.";
586 settings.fieldNames[
"runs"] =
"Runs";
587 settings.fieldNames[
"seed"] =
"Seed";
590 settings.fieldNames[
"correctType"] =
"Correct Type";
591 settings.fieldNames[
"description"] =
"Description";
592 settings.fieldNames[
"expectedFlags"] =
"Exp. Flags";
593 settings.fieldNames[
"thrown"] =
"Has Thrown";
596 settings.fieldOptions[
"name"].columnWidth = 20;
597 settings.fieldOptions[
"averageRuntime"].columnWidth = 14;
598 settings.fieldOptions[
"stdevRuntime"].columnWidth = 16;
599 settings.fieldOptions[
"runsPerSecond"].columnWidth = 14;
600 settings.fieldOptions[
"description"].columnWidth = 20;
603 settings.fieldOptions[
"failed"].fieldInterpreter = [](
const std::string& s) {
604 if(s ==
"0")
return "PASS";
605 else if(s ==
"1")
return "FAIL";
606 else return "UNKNOWN";
622 for (
auto& file_pair :
settings.openFiles)
623 if(file_pair.second.is_open())
624 file_pair.second.close();
648 std::stringstream value;
650 if(fieldName ==
"name") {
652 }
else if(fieldName ==
"maxErr") {
653 value << std::scientific
654 << std::setprecision(
settings.outputPrecision)
656 }
else if(fieldName ==
"meanErr") {
657 value << std::scientific
658 << std::setprecision(
settings.outputPrecision)
660 }
else if(fieldName ==
"rmsErr") {
661 value << std::scientific
662 << std::setprecision(
settings.outputPrecision)
664 }
else if(fieldName ==
"relErr") {
665 value << std::scientific
666 << std::setprecision(
settings.outputPrecision)
668 }
else if(fieldName ==
"absErr") {
669 value << std::scientific
670 << std::setprecision(
settings.outputPrecision)
672 }
else if(fieldName ==
"tolerance") {
673 value << std::scientific
674 << std::setprecision(
settings.outputPrecision)
676 }
else if(fieldName ==
"failed") {
698 std::stringstream value;
700 if(fieldName ==
"name") {
702 }
else if(fieldName ==
"evaluated") {
704 }
else if(fieldName ==
"expected") {
706 }
else if(fieldName ==
"difference") {
707 value << std::scientific
708 << std::setprecision(
settings.outputPrecision)
710 }
else if(fieldName ==
"tolerance") {
711 value << std::scientific
712 << std::setprecision(
settings.outputPrecision)
714 }
else if(fieldName ==
"failed") {
733 std::stringstream value;
735 if(fieldName ==
"name") {
737 }
else if(fieldName ==
"runs") {
739 }
else if(fieldName ==
"iterations") {
741 }
else if(fieldName ==
"totalRuntime") {
742 value << std::scientific
743 << std::setprecision(
settings.outputPrecision)
745 }
else if(fieldName ==
"averageRuntime") {
746 value << std::scientific
747 << std::setprecision(
settings.outputPrecision)
749 }
else if(fieldName ==
"stdevRuntime") {
750 value << std::scientific
751 << std::setprecision(
settings.outputPrecision)
753 }
else if(fieldName ==
"runsPerSecond") {
758 }
else if(fieldName ==
"failed") {
760 }
else if(fieldName ==
"seed") {
782 std::stringstream value;
784 if(fieldName ==
"name") {
786 }
else if(fieldName ==
"evaluated") {
788 }
else if(fieldName ==
"description") {
790 }
else if(fieldName ==
"failed") {
808 std::stringstream value;
810 if(fieldName ==
"name") {
812 }
else if(fieldName ==
"evaluated") {
814 }
else if(fieldName ==
"expectedFlags") {
816 int res_flag = 0xFFFFFFFF;
822 }
else if(fieldName ==
"failed") {
840 std::stringstream value;
842 if(fieldName ==
"name") {
844 }
else if(fieldName ==
"thrown") {
846 }
else if(fieldName ==
"correctType") {
848 }
else if(fieldName ==
"failed") {
867 template<
typename ResultType>
869 const std::map<std::string, std::vector<ResultType>>& results,
870 const std::vector<std::string>& fields) {
872 std::vector<std::vector<std::string>> table;
875 std::vector<std::string> header (fields.size());
876 for (
size_t i = 0; i < fields.size(); ++i) {
878 const auto it =
settings.fieldNames.find(fields[i]);
882 header[i] = it->second;
884 header[i] = fields[i];
886 table.emplace_back(header);
889 for (
const auto& p : results) {
890 for (
const auto& result : p.second) {
896 std::vector<std::string> row (fields.size());
898 for (
size_t i = 0; i < fields.size(); ++i)
901 table.emplace_back(row);
917 const auto file_pair =
settings.openFiles.find(filename);
920 if (file_pair ==
settings.openFiles.end() || !file_pair->second.is_open()) {
922 settings.openFiles[filename].open(filename);
924 if (!
settings.openFiles[filename].is_open()) {
939 template<
typename ResultType>
941 const std::map<std::string, std::vector<ResultType>>& results,
942 const std::vector<std::string>& fields,
943 const std::vector<std::string>& filenames) {
950 std::vector<std::vector<std::string>> table =
generate_table(results, fields);
964 for (
const auto& filename : filenames) {
967 error(
"Unable to write to output file: " + filename);
971 auto& file =
settings.openFiles[filename];
974 const auto it =
settings.fileOutputFormat.find(filename);
976 if(it !=
settings.fileOutputFormat.end())
977 file << it->second(table, fields,
settings);
981 info(
"Results have been saved in: " + filename);
985 for (
const auto& filename :
settings.outputFiles) {
988 error(
"Unable to write to output file: " + filename);
992 auto& file =
settings.openFiles[filename];
995 const auto it =
settings.fileOutputFormat.find(filename);
997 if(it !=
settings.fileOutputFormat.end())
998 file << it->second(table, fields,
settings);
1002 info(
"Results have been saved in: " + filename);
1016 if (level == LogLevel::ERROR) {
1017 std::cerr <<
"ERROR: " << message << std::endl;
1027 inline void debug(
const std::string& message) {
1028 log(message, LogLevel::DEBUG);
1034 inline void info(
const std::string& message) {
1035 log(message, LogLevel::INFO);
1041 inline void warn(
const std::string& message) {
1042 log(message, LogLevel::WARNING);
1048 inline void error(
const std::string& message) {
1049 log(message, LogLevel::ERROR);
Structures for the benchmark module.
std::string resolve_field(const std::string &fieldName, benchmark::benchmark_result r)
Resolve the field of a benchmark result by name, returning the value as a string.
Definition output.h:730
void warn(const std::string &message)
Write a warning to standard output according to the set log level.
Definition output.h:1041
auto generate_table(const std::map< std::string, std::vector< ResultType > > &results, const std::vector< std::string > &fields)
Generate a table of results as a string matrix to pass to a specific formatter of OutputFormat type.
Definition output.h:868
void terminate()
Terminate the output module by closing all output files and resetting its settings.
Definition output.h:619
void error(const std::string &message)
Write an error message to standard error according to the set log level.
Definition output.h:1048
void setup()
Setup printing to the output stream with default options.
Definition output.h:559
output_context()
Default constructor for the output module.
Definition output.h:629
bool open_file(const std::string &filename)
Try to open a new output file, returning whether it was correctly opened.
Definition output.h:915
std::string resolve_field(const std::string &fieldName, err::errno_result r)
Resolve the field of an errno checking result by name, returning the value as a string.
Definition output.h:805
std::string resolve_field(const std::string &fieldName, err::exception_result r)
Resolve the field of an exception checking result by name, returning the value as a string.
Definition output.h:837
~output_context()
Destructor which automatically terminates the module.
Definition output.h:635
void info(const std::string &message)
Write runtime information to standard output according to the set log level.
Definition output.h:1034
void log(const std::string &message, LogLevel level)
Log a message to standard output according to the set log level.
Definition output.h:1011
std::string resolve_field(const std::string &fieldName, prec::estimate_result r)
Resolve the field of an estimate result by name, returning the value as a string.
Definition output.h:645
std::string resolve_field(const std::string &fieldName, err::assert_result r)
Resolve the field of an assertion result by name, returning the value as a string.
Definition output.h:779
std::string resolve_field(const std::string &fieldName, prec::equation_result r)
Resolve the field of an equation result by name, returning the value as a string.
Definition output.h:695
void print_results(const std::map< std::string, std::vector< ResultType > > &results, const std::vector< std::string > &fields, const std::vector< std::string > &filenames)
Print the test results to standard output and output files.
Definition output.h:940
void debug(const std::string &message)
Write debug information to standard output according to the set log level.
Definition output.h:1027
output_settings settings
Settings for the output module.
Definition output.h:555
#define CHEBYSHEV_OUTPUT_WIDTH
Default width of output columns.
Definition common.h:32
Structures for the error checking module.
Logging system for the output module.
Functions to manage printing results.
LogLevel
Log levels for the output module.
Definition log.h:16
output_settings::OutputFormat_t OutputFormat
A function which converts the table entries of a row to a string to print (e.g.
Definition output.h:109
std::string loglevel_to_string(LogLevel level)
Convert a log level to a string for printing.
Definition log.h:25
General namespace of the framework.
Definition benchmark.h:22
Structures for precision testing.
Structure holding the results of a benchmark.
Definition benchmark_structures.h:23
prec_t runsPerSecond
Number of runs per second.
Definition benchmark_structures.h:44
prec_t averageRuntime
Estimated average runtime.
Definition benchmark_structures.h:38
bool failed
Whether the benchmark failed because an exception was thrown.
Definition benchmark_structures.h:51
unsigned int runs
Number of runs.
Definition benchmark_structures.h:29
unsigned int iterations
Number of iterations.
Definition benchmark_structures.h:32
std::map< std::string, prec_t > additionalFields
Additional fields in floating point representation.
Definition benchmark_structures.h:57
uint64_t seed
The seed used for randomized input generation.
Definition benchmark_structures.h:47
prec_t stdevRuntime
Sample standard deviation of the runtime.
Definition benchmark_structures.h:41
std::string name
Identifying name of the function or test case.
Definition benchmark_structures.h:26
prec_t totalRuntime
Total runtime over all runs and iterations.
Definition benchmark_structures.h:35
Result of assertion checking of a function.
Definition err_structures.h:21
std::string name
Identifying name of the function or test case.
Definition err_structures.h:24
bool evaluated
Evaluated boolean value.
Definition err_structures.h:27
bool failed
Whether the test failed.
Definition err_structures.h:33
std::string description
Description of the assertion.
Definition err_structures.h:30
Result of errno checking of a function.
Definition err_structures.h:42
int evaluated
Evaluated errno value.
Definition err_structures.h:48
std::string name
Identifying name of the function or test case.
Definition err_structures.h:45
std::vector< int > expectedFlags
Expected errno flags.
Definition err_structures.h:51
bool failed
Whether the test failed.
Definition err_structures.h:54
Result of exception checking of a function.
Definition err_structures.h:63
bool thrown
Whether the function has thrown.
Definition err_structures.h:69
std::string name
Identifying name of the function or test case.
Definition err_structures.h:66
bool correctType
Whether the type of the thrown exception was correct.
Definition err_structures.h:73
bool failed
Whether the test failed.
Definition err_structures.h:76
Custom options for printing a certain field.
Definition output.h:30
std::map< std::string, prec_t > additionalFields
Additional custom options.
Definition output.h:42
unsigned int columnWidth
Width for the column associated with the field.
Definition output.h:35
FieldInterpreter fieldInterpreter
A function which gets as input the value of a field as a string and returns a new string (e....
Definition output.h:39
Global settings of printing results to standard output.
Definition output.h:54
bool quiet
Whether to output to standard output.
Definition output.h:92
std::map< std::string, OutputFormat_t > fileOutputFormat
The output format to use for a specific file, by filename.
Definition output.h:89
std::vector< std::string > outputFiles
A list of output files.
Definition output.h:70
bool outputToFile
Whether to print the results to file.
Definition output.h:95
OutputFormat_t defaultFileOutputFormat
The default output format to use for files, when no format has been set for a file.
Definition output.h:86
std::map< std::string, field_options > fieldOptions
Options for the different fields.
Definition output.h:67
OutputFormat_t outputFormat
The output format to use to print to standard output.
Definition output.h:82
LogLevel logLevel
Log level for standard output and error messages.
Definition output.h:101
unsigned int defaultColumnWidth
Default width for a field.
Definition output.h:76
std::map< std::string, std::string > fieldNames
Map of field name to output string (e.g.
Definition output.h:64
std::map< std::string, std::ofstream > openFiles
A map of open output files, by filename.
Definition output.h:73
unsigned int outputPrecision
The number of digits to show in scientific notation.
Definition output.h:79
bool wasSetup
Whether the output module was setup.
Definition output.h:98
A structure holding the result of an evaluation.
Definition prec_structures.h:167
bool failed
Whether the test failed.
Definition prec_structures.h:189
std::string name
Identifying name of the function or test case.
Definition prec_structures.h:170
prec_t evaluated
Evaluated value.
Definition prec_structures.h:173
prec_t difference
Evaluated difference between expected and evaluated values.
Definition prec_structures.h:179
prec_t expected
Expected value.
Definition prec_structures.h:176
prec_t tolerance
Tolerance on the absolute difference.
Definition prec_structures.h:186
std::map< std::string, prec_t > additionalFields
Additional fields by name, as a floating point value.
Definition prec_structures.h:183
A structure holding the result of precision estimation.
Definition prec_structures.h:25
std::map< std::string, prec_t > additionalFields
Additional fields by name, as a floating point value.
Definition prec_structures.h:52
std::string name
Identifying name of the function or test case.
Definition prec_structures.h:28
prec_t maxErr
Estimated maximum absolute error on interval.
Definition prec_structures.h:37
prec_t meanErr
Estimated mean error on interval.
Definition prec_structures.h:40
prec_t absErr
Estimated absolute error on interval.
Definition prec_structures.h:49
bool failed
Whether the test failed.
Definition prec_structures.h:55
prec_t tolerance
Tolerance on the max absolute error.
Definition prec_structures.h:34
prec_t relErr
Estimated relative error on interval.
Definition prec_structures.h:46
prec_t rmsErr
Estimated RMS error on interval.
Definition prec_structures.h:43