Commit 4cfec0e6 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Enable mismatch-only logging and report summary

parent 36c854d4
Loading
Loading
Loading
Loading
+37 −14
Original line number Diff line number Diff line
#!/bin/python

## @package pycompare
## @file pycompare
#  Script to perform output consistency tests
#
#  Comparing the numeric output can be rendered hard by the amount of information
@@ -41,6 +41,7 @@ def main():
        print("ERROR COUNT: %d"%errors)
        print("WARNING COUNT: %d"%warnings)
        print("NOISE COUNT: %d"%noisy)
        if (config['log_html']): reformat_log(config, errors, warnings, noisy)
    if (errors > 0):
        print("FAILURE: {0:s} is not consistent with {1:s}".format(
            config['c_file_name'], config['fortran_file_name']
@@ -114,6 +115,7 @@ def compare_lines(f_line, c_line, config, line_num=0, num_len=1, log_file=None):
    f_line = f_line.replace("D-","E-").replace("D+","E+")
    if (f_line == c_line):
        if log_file is not None:
            if (config['full_log']):
                num_format = "    <div><pre><code>{0:0%dd}"%num_len
                log_line = (num_format + ": {1:s}</code></pre></div>\n").format(line_num, c_line[:-1])
                log_file.write(log_line)
@@ -165,7 +167,7 @@ def compare_lines(f_line, c_line, config, line_num=0, num_len=1, log_file=None):
                elif (severities[-1] == 2): warnings += 1
                elif (severities[-1] == 3): errors += 1
            if log_file is not None:
                if (len(severities) > 1):
                if (len(severities) > 0):
                    if (severities[-1] == 0):
                        log_line = (
                            log_line + c_groups[-1] + c_line[c_ends[-1]:len(c_line) - 2]
@@ -189,6 +191,8 @@ def compare_lines(f_line, c_line, config, line_num=0, num_len=1, log_file=None):
            #END INDENT
        else:
            if (log_file is not None):
                num_format = "    <div><pre><code>{0:0%dd}"%num_len
                log_line = (num_format + ": ").format(line_num)
                log_line = (
                    log_line + "</code><span style=\"font-weight: bold; color: rgb(255,0,0)\"><code>"
                    + c_line + "</code></span><code>"
@@ -246,6 +250,7 @@ def parse_arguments():
    config = {
        'fortran_file_name': '',
        'c_file_name': '',
        'full_log': False,
        'log_html': False,
        'html_output': 'pycompare.html',
        'warning_threshold': 0.005,
@@ -258,9 +263,11 @@ def parse_arguments():
            config['fortran_file_name'] = split_arg[1]
        elif (arg.startswith("--cfile")):
            config['c_file_name'] = split_arg[1]
        elif (arg.startswith("--full")):
            config['full_log'] = True
        elif (arg.startswith("--html")):
            config['log_html'] = True
        elif (arg.startswith("--logname")):
            if (len(split_arg) == 2):
                config['html_output'] = split_arg[1]
        elif (arg.startswith("--warn")):
            config['warning_threshold'] = float(split_arg[1])
@@ -284,13 +291,29 @@ def print_help():
    print("Valid options are:                          ")
    print("--ffile=FORTRAN_OUTPUT    File containing the output of the FORTRAN code (mandatory).")
    print("--cfile=C++_OUTPUT        File containing the output of the C++ code (mandatory).")
    print("--full                    Print all lines to log file (default prints only mismatches).")
    print("--help                    Print this help and exit.")
    print("--html                   Enable logging to HTML file.")
    print("--logname                Name of the HTML log file (default is \"pycompare.html\").")
    print("--html[=OPT_OUTPUT_NAME]  Enable logging to HTML file (default logs to \"pycompare.html\").")
    print("--quick                   Stop on first mismatch (default is to perform a full check).")
    print("--warn                    Set a fractional threshold for numeric warning (default=0.005).")
    print("                                            ")

def reformat_log(config, errors, warnings, noisy):
    log_file = open(config['html_output'], 'r')
    log_lines = log_file.readlines()
    log_file.close()
    log_file = open(config['html_output'], 'w')
    for i in range(7): log_file.write(log_lines[i] + "\n")
    str_errors = "error" if errors == 1 else "errors"
    str_warnings = "warning" if warnings == 1 else "warnings"
    str_noisy = "noisy value" if noisy == 1 else "noisy values"
    summary = "    <div>Comparison yielded %d %s"%(errors, str_errors)
    summary = summary + ", %d %s"%(warnings, str_warnings)
    summary = summary + " and %d %s.</div>\n"%(noisy, str_noisy)
    log_file.write(summary)
    for i in range(7, len(log_lines)): log_file.write(log_lines[i] + "\n")
    log_file.close()
    

# ### PROGRAM EXECUTION ###
## \cond