#include "RegExpTest.h"
#include <alib/list>

#include "sax/SaxComposeInterface.h"
#include "sax/SaxParseInterface.h"

#include "regexp/unbounded/UnboundedRegExp.h"
#include <regexp/RegExp.h>

#include "regexp/glushkov/GlushkovFollow.h"
#include "regexp/glushkov/GlushkovIndexate.h"
#include "regexp/glushkov/GlushkovFirst.h"
#include "regexp/glushkov/GlushkovLast.h"

#include <factory/StringDataFactory.hpp>
#include <regexp/string/UnboundedRegExp.h>

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( RegExpTest, "regexp" );
CPPUNIT_TEST_SUITE_REGISTRATION ( RegExpTest );

void RegExpTest::setUp ( ) {
}

void RegExpTest::tearDown ( ) {
}

void RegExpTest::testFirst ( ) {
	{
		std::string input = "#E* #0*";
		regexp::UnboundedRegExp < > regexp = alib::StringDataFactory::fromString ( input );
		regexp::UnboundedRegExp < ext::pair < DefaultSymbolType, int > > indexedRegExp = regexp::GlushkovIndexate::index ( regexp );

		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > first = regexp::GlushkovFirst::first ( indexedRegExp );

		CPPUNIT_ASSERT ( first.size ( ) == 0 );
	}
	{
		std::string input = "#E* a";
		regexp::UnboundedRegExp < > regexp = alib::StringDataFactory::fromString ( input );
		regexp::UnboundedRegExp < ext::pair < DefaultSymbolType, int > > indexedRegExp = regexp::GlushkovIndexate::index ( regexp );

		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > first = regexp::GlushkovFirst::first ( indexedRegExp );

		CPPUNIT_ASSERT ( first.size ( ) == 1 );
	}
}

void RegExpTest::testLast ( ) {
	{
		std::string input = "a+a";
		regexp::UnboundedRegExp < > regexp = alib::StringDataFactory::fromString ( input );
		regexp::UnboundedRegExp < ext::pair < DefaultSymbolType, int > > indexedRegExp = regexp::GlushkovIndexate::index ( regexp );

		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > last = regexp::GlushkovLast::last ( indexedRegExp );

		CPPUNIT_ASSERT ( last.size ( ) == 2 );
	}
	{
		std::string input = "(a+a)b";
		regexp::UnboundedRegExp < > regexp = alib::StringDataFactory::fromString ( input );
		regexp::UnboundedRegExp < ext::pair < DefaultSymbolType, int > > indexedRegExp = regexp::GlushkovIndexate::index ( regexp );

		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > last = regexp::GlushkovLast::last ( indexedRegExp );

		std::cout << last << std::endl;
		CPPUNIT_ASSERT ( last.size ( ) == 1 );
	}
}

void RegExpTest::testFollow ( ) {
	{
		std::string input = "(a+a)b";
		regexp::UnboundedRegExp < > regexp = alib::StringDataFactory::fromString ( input );
		regexp::UnboundedRegExp < ext::pair < DefaultSymbolType, int > > indexedRegExp = regexp::GlushkovIndexate::index ( regexp );

		auto symbolsIter = indexedRegExp.getAlphabet ( ).begin ( );

		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > follow1 = regexp::GlushkovFollow::follow ( indexedRegExp, regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > ( * symbolsIter ) );

		CPPUNIT_ASSERT ( follow1.size ( ) == 1 );

		symbolsIter++;
		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > follow2 = regexp::GlushkovFollow::follow ( indexedRegExp, regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > ( * symbolsIter ) );

		CPPUNIT_ASSERT ( follow2.size ( ) == 1 );

		symbolsIter++;
		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > follow3 = regexp::GlushkovFollow::follow ( indexedRegExp, regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > ( * symbolsIter ) );

		CPPUNIT_ASSERT ( follow3.size ( ) == 0 );
	}
	{
		std::string input = "a+a* (b+a)* c";
		regexp::UnboundedRegExp < > regexp = alib::StringDataFactory::fromString ( input );
		regexp::UnboundedRegExp < ext::pair < DefaultSymbolType, int > > indexedRegExp = regexp::GlushkovIndexate::index ( regexp );

		auto symbolsIter = indexedRegExp.getAlphabet ( ).begin ( );

		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > follow1 = regexp::GlushkovFollow::follow ( indexedRegExp, regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > ( * symbolsIter ) );

		CPPUNIT_ASSERT ( follow1.size ( ) == 0 );

		symbolsIter++;
		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > follow2 = regexp::GlushkovFollow::follow ( indexedRegExp, regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > ( * symbolsIter ) );

		CPPUNIT_ASSERT ( follow2.size ( ) == 4 );

		symbolsIter++;
		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > follow3 = regexp::GlushkovFollow::follow ( indexedRegExp, regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > ( * symbolsIter ) );

		CPPUNIT_ASSERT ( follow3.size ( ) == 3 );

		symbolsIter++;
		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > follow4 = regexp::GlushkovFollow::follow ( indexedRegExp, regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > ( * symbolsIter ) );

		CPPUNIT_ASSERT ( follow4.size ( ) == 3 );

		symbolsIter++;
		ext::set < regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > > follow5 = regexp::GlushkovFollow::follow ( indexedRegExp, regexp::UnboundedRegExpSymbol < ext::pair < DefaultSymbolType, int > > ( * symbolsIter ) );

		CPPUNIT_ASSERT ( follow5.size ( ) == 0 );
	}
}