/*
 * BWTFMIndexTest.cpp
 *
 *  Created on: 21. 02. 2021
 *      Author: Martin Prokopic
 */

#include <catch2/catch.hpp>

#include <string/String.h>
#include <stringology/transform/BWTNaive.h>
#include <stringology/indexing/BWTAuxiliaryDataConstruct.h>
#include <stringology/indexing/SimpleFMIndexConstruct.h>
#include <stringology/query/FMIndexCount.h>

using stringology::transform::BWTNaive;
using stringology::indexing::BWTAuxiliaryDataConstruct;
using stringology::indexing::SimpleFMIndexConstruct;
using stringology::query::FMIndexCount;
using string::LinearStringTerminatingSymbolBWT;

TEST_CASE ( "FM Index Count", "[unit][stringology][query][fmindex][bwt]" ) {

    SECTION ( "basic test" ) {
        auto testcase = GENERATE (
                std::make_tuple ( "banana", "na", 2u ),
                std::make_tuple ( "banana", "ana", 2u ),
                std::make_tuple ( "banana", "ba", 1u ),
                std::make_tuple ( "banana", "z", 0u ),
                std::make_tuple ( "banana", "asdf", 0u ),
                std::make_tuple ( "", "asdf", 0u ));

        auto originalSubject = string::stringFrom ( std::get < 0 > ( testcase ));
        auto pattern = string::stringFrom ( std::get < 1 > ( testcase ));

        auto transformedSubject = stringology::transform::BWTNaive::construct ( originalSubject );

        auto data = BWTAuxiliaryDataConstruct::construct ( transformedSubject );
        INFO ( data );

        auto index = SimpleFMIndexConstruct::construct ( transformedSubject, 4, 2, 0 );
        unsigned count = FMIndexCount::count ( index, pattern );
        REQUIRE ( count == std::get < 2 > ( testcase ));
    }

    SECTION ( "throws exception if pattern is empty" ) {
        auto subject = LinearStringTerminatingSymbolBWT < char > ( '$', string::stringFrom ( "subject$" ));
        auto pattern = string::stringFrom ( "" );

        auto index = SimpleFMIndexConstruct::construct ( subject, 4, 2, 0 );
        REQUIRE_THROWS_AS( FMIndexCount::count ( index, pattern ), exception::CommonException );
    }
}