# Copyright (c) 2023, Technische Universität 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: 
#    Derek Christ

###############################################
###             tests_regression            ###
###############################################

cmake_minimum_required(VERSION 3.1.0)

project(tests_regression)

find_program(Bash bash)
find_program(SqlDiff sqldiff)

if(NOT Bash OR NOT SqlDiff)
    message(WARNING "Regression tests require bash and sqldiff to be installed")
    return()
endif()

set(TABLES_TO_COMPARE
    Phases
    Transactions
    Power
)

function(test_standard standard base_config resource_dir output_filename)
    # Put all the generated files into a subdirectory
    file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${standard})

    configure_file(compare.sh ${standard}/compare.sh)

    # Test to create database
    add_test(
        NAME Regression${standard}.CreateDatabase
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${standard}
        COMMAND $<TARGET_FILE:DRAMSys> ${base_config} ${resource_dir}
    )
    set_tests_properties(Regression${standard}.CreateDatabase PROPERTIES FIXTURES_SETUP Regression${standard}.CreateDatabase)

    # Test to diff the whole database. This test should not fail.
    # The purpose of this test is solely to output the differences of the two databases
    # so that they can be inspected easily.
    add_test(
        NAME Regression${standard}.SqlDiff
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${standard}
        COMMAND compare.sh
    )
    set_tests_properties(Regression${standard}.SqlDiff PROPERTIES FIXTURES_REQUIRED Regression${standard}.CreateDatabase)

    # Tests to diff individual tables
    foreach(table IN LISTS TABLES_TO_COMPARE)
        configure_file(compare_table.sh ${standard}/compare_table-${table}.sh)

        add_test(
            NAME Regression${standard}.SqlDiff.${table}
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${standard}
            COMMAND compare_table-${table}.sh
        )
        set_tests_properties(Regression${standard}.SqlDiff.${table} PROPERTIES FIXTURES_REQUIRED Regression${standard}.CreateDatabase)
    endforeach()
endfunction()

test_standard(DDR3 ${CMAKE_CURRENT_SOURCE_DIR}/DDR3/ddr3-example.json ${CMAKE_CURRENT_SOURCE_DIR}/DDR3 DRAMSys_ddr3-dual-rank_ddr3_ch0.tdb)
test_standard(DDR4 ${CMAKE_CURRENT_SOURCE_DIR}/DDR4/ddr4-example.json ${CMAKE_CURRENT_SOURCE_DIR}/DDR4 DRAMSys_ddr4-bankgrp_ddr4_ch0.tdb)
test_standard(LPDDR4 ${CMAKE_CURRENT_SOURCE_DIR}/LPDDR4/lpddr4-example.json ${CMAKE_CURRENT_SOURCE_DIR}/LPDDR4 DRAMSys_lpddr4-example_lpddr4_ch0.tdb)
test_standard(HBM2.Ch0 ${CMAKE_CURRENT_SOURCE_DIR}/HBM2/hbm2-example.json ${CMAKE_CURRENT_SOURCE_DIR}/HBM2 DRAMSys_hbm2-example_hbm2_ch0.tdb)
test_standard(HBM2.Ch1 ${CMAKE_CURRENT_SOURCE_DIR}/HBM2/hbm2-example.json ${CMAKE_CURRENT_SOURCE_DIR}/HBM2 DRAMSys_hbm2-example_hbm2_ch1.tdb)
