{#

################################################################################
#
# Copyright 2020 OpenHW Group
#
# Licensed under the Solderpad Hardware Licence, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://solderpad.org/licenses/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier:Apache-2.0 WITH SHL-2.0
#
################################################################################

#}
{% import 'regress_macros.j2' as regress_macros -%}

#!/bin/bash

# --------------------------------------------------------------------------------------
# Variables
# --------------------------------------------------------------------------------------
pass_count=0
fail_count=0
failed=0

# --------------------------------------------------------------------------------------
# Functions
# --------------------------------------------------------------------------------------
check_log () {
    log=$1
    simulation_passed="$2"
    test_name=$3

    if grep -q "${simulation_passed}" ${log}; then
        echo "{{session}}: Test PASSED: ${test_name} Log: ${log}"
    else
        echo "{{session}}: Test FAILED: ${test_name} Log: ${log}"
        failed=1
    fi
}

incr_test_counts () {
    if [[ ${failed} == "0" ]]; then
        ((pass_count+=1))
    else
        ((fail_count+=1))
    fi
}

# --------------------------------------------------------------------------------------
# Builds
# --------------------------------------------------------------------------------------

{% for b in unique_builds.values() %}
# Build:{{b.name}} {{b.description}}
{% set cmd = b.cmd + ' CV_CORE=' + project + ' CFG=' + b.cfg + ' SIMULATOR=' + b.simulator + ' COV=' + regress_macros.yesorno(b.cov) + ' ' + regress_macros.cv_results(results) + ' ' + makeargs %}
echo "{{session}}: Running build: [cd {{b.abs_dir}} && {{cmd}}]"
pushd {{b.abs_dir}} > /dev/null
{{cmd}}
popd > /dev/null

{% endfor -%}

# --------------------------------------------------------------------------------------
# Tests
# --------------------------------------------------------------------------------------

{% for r in regressions %}
{% for t in r.tests.values()|sort(attribute='name') %}
{% for build in t.builds %}
# --> Test: {{t.name}} : Build: {{build}} : {{t.description}}
{% if t.precmd %}
#  Test (Precommand): {{t.name}} {{t.description}}
{% set cmd = t.precmd + ' CV_CORE=' + project + 'CFG=' + r.builds[build].cfg + ' ' + toolchain|upper + '=1' + ' SIMULATOR=' + t.simulator + ' SEED=random GEN_NUM_TESTS=' + t.num + ' ' + regress_macros.cv_results(results) + ' ' + makeargs + ' ' + t.makearg %}
echo "{{session}}: Running precmd: [cd {{t.abs_dir}} && {{cmd}}]
pushd {{t.abs_dir}} > /dev/null
{{cmd}} >& /dev/null;
popd > /dev/null

{% endif %}
{% for run_index in range(t.num|int) %}
{% set test_cmd = t.cmd %}
{% set test_log = t.log %}
# --> Test (Index: {{run_index}}): {{t.cmd}} : {{t.description}}
{% set cmd = test_cmd + ' CV_CORE=' + project + ' CFG=' + r.builds[build].cfg + ' ' + toolchain|upper + '=1' + ' SIMULATOR=' + t.simulator + ' COMP=0 USE_ISS=' + regress_macros.yesorno(t.iss) + ' COV=' + regress_macros.yesorno(t.cov) + ' SEED=random GEN_START_INDEX=' + run_index|string + ' RUN_INDEX=' + run_index|string + ' ' + regress_macros.cv_results(results) + ' ' + makeargs %}
echo "{{session}}: Running test [cd {{t.abs_dir}} && {{cmd}}]"
pushd {{t.abs_dir}} > /dev/null
{{cmd}} >& /dev/null;
popd > /dev/null

{# Determine results directory #}
{% if results %}
{% set results_dir = results + '/' + t.simulator + '_results' %}
{% else %}
{% set results_dir = t.simulator + '_results' %}
{% endif %}

{# Determine log location #}
{% if t.results %}
log={{t.abs_dir}}/{{results_dir}}/{{r.builds[build].cfg}}/{{t.results}}/{{run_index}}/{{t.simulator}}-{{test_log}}.log
{% else %}
log={{t.abs_dir}}/{{results_dir}}/{{r.builds[build].cfg}}/{{test_log}}/{{run_index}}/{{t.simulator}}-{{test_log}}.log
{% endif %}

{# Compliance signature check #}
{% if 'compliance' in t.cmd %}
{% if t.results %}
compliance_diff_log={{t.abs_dir}}/{{results_dir}}/{{cfg}}/{{t.results}}/{{run_index}}/diff_signatures.log
{% else %}
compliance_diff_log={{t.abs_dir}}/{{results_dir}}/{{cfg}}/{{test_log}}_{{run_index}}/diff_signatures.log
{% endif %}
{% endif %}

failed=0
check_log ${log} "{{t.simulation_passed}}" {{test_log}}
{% if 'compliance' in t.cmd %}
check_log ${compliance_diff_log} "All signatures passed" {{test_log}}
{% endif %}
incr_test_counts
{% endfor %}

{% endfor %}
{% endfor %}
{% endfor %}

echo "{{session}}: Passing tests: ${pass_count}"
echo "{{session}}: Failing tests: ${fail_count}"

if [ ${fail_count} -ne 0 ]; then
    exit 1
fi
exit 0
