#!/usr/bin/perl -w
# Copyright (c) 2015, University of Kaiserslautern
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# Authors:
#    Matthias Jung
#    Eder F. Zulian
#

# Test error:
# This test runs the simulation with error model enabled.
# The main configuration file for this test is DRAMSys/tests/error/sim-batch.xml.
#
use warnings;
use strict;
use Term::ANSIColor;

my $trace_file = "test_error.stl";

# Create the input file (first make sure the script is executable)
chdir("../../DRAMSys/tests/error");
if (! -e $trace_file) {
	`chmod u+x ./generateErrorTest.pl`;
	`./generateErrorTest.pl > $trace_file`;
}

# Run Simulation:
chdir("../../../build/simulator/");
`./DRAMSys ../../DRAMSys/tests/error/sim-batch.xml > ../../DRAMSys/tests/error/output.txt`;

chdir("../../DRAMSys/tests/error");

my @channels_retention_errors = ();
@channels_retention_errors = split(/\n/,`cat output.txt | grep 'Number of Retention Error Events' | cut -d= -f2 | sed 's/ //g'`);

if (!@channels_retention_errors) {
    print color("red"), "\tError: ", color("green"), "error model output information not found.\n";
    print color("yellow"), "\tCheck if error model is enabled.\n";
    exit -1
}

my $errors_found = 0;
foreach (@channels_retention_errors) {
    print "$_\n";
    $errors_found += $_;
}

print color("green"), "Number of Retention Errors Events (all banks): $errors_found\n";

if ($errors_found == 0) {
    # Fail, errors expected but not found.
    exit -1;
}

# Success, at least one error was found.
exit 0;

