Skip to content
Snippets Groups Projects
RegExpOptimizeTest.cpp 5.32 KiB
Newer Older
#include <alib/list>
Jan Trávníček's avatar
Jan Trávníček committed
#include "RegExpOptimizeTest.h"

#include "regexp/unbounded/UnboundedRegExp.h"

Jan Trávníček's avatar
Jan Trávníček committed
#include "regexp/simplify/RegExpOptimize.h"
Jan Trávníček's avatar
Jan Trávníček committed

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

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegExpOptimizeTest, "regexp" );
Jan Trávníček's avatar
Jan Trávníček committed
CPPUNIT_TEST_SUITE_REGISTRATION( RegExpOptimizeTest );

void RegExpOptimizeTest::setUp() {
}

void RegExpOptimizeTest::tearDown() {
}

void RegExpOptimizeTest::testOptimize() {
	{
		std::string input = "a+a";
		regexp::UnboundedRegExp < > regexp = alib::StringDataFactory::fromString ( input );
		regexp::UnboundedRegExp < > res = regexp::simplify::RegExpOptimize::optimize ( regexp );

		std::string inputRes = "a";
		regexp::UnboundedRegExp < > regexpRes = alib::StringDataFactory::fromString ( inputRes );

		std::cout << res << std::endl;
		std::cout << regexpRes << std::endl;

		CPPUNIT_ASSERT ( regexpRes == res );
	}
Jan Trávníček's avatar
Jan Trávníček committed
	{
		std::string input = "(a+a)b + (#0 b + (#0 a + (#0 b + a)))";
		regexp::UnboundedRegExp < > regexp = alib::StringDataFactory::fromString ( input );
Jan Trávníček's avatar
Jan Trávníček committed

		regexp::UnboundedRegExp < > res = regexp::simplify::RegExpOptimize::optimize ( regexp );
		std::string inputRes = "a + a b";
		regexp::UnboundedRegExp < > regexpRes = alib::StringDataFactory::fromString ( inputRes );

		std::cout << res << std::endl;
		std::cout << regexpRes << std::endl;

		CPPUNIT_ASSERT ( regexpRes == res );
Jan Trávníček's avatar
Jan Trávníček committed
	}
	{
		std::string input = "a z + a b* b z";
		regexp::UnboundedRegExp < > regexp = alib::StringDataFactory::fromString ( input );
Jan Trávníček's avatar
Jan Trávníček committed

		regexp::UnboundedRegExp < > res = regexp::simplify::RegExpOptimize::optimize ( regexp );

		std::string inputRes = "a b* z";
		regexp::UnboundedRegExp < > regexpRes = alib::StringDataFactory::fromString ( inputRes );

		std::cout << res << std::endl;
		std::cout << regexpRes << std::endl;

		CPPUNIT_ASSERT ( regexpRes == res );
void RegExpOptimizeTest::testOptimizeTemplated() {
	regexp::UnboundedRegExpSymbol < char > a ( 'a' );
	regexp::UnboundedRegExpSymbol < char > b ( 'b' );
	regexp::UnboundedRegExpSymbol < char > z ( 'z' );

	{
		regexp::UnboundedRegExpAlternation < char > alt;
		alt.appendElement ( a );
		alt.appendElement ( a );

		regexp::UnboundedRegExp < char > regexp ( regexp::UnboundedRegExpStructure < char > { alt } );

		regexp::UnboundedRegExp < char > res = regexp::simplify::RegExpOptimize::optimize(regexp);

		regexp::UnboundedRegExp < char > regexpRes( regexp::UnboundedRegExpStructure < char > { a } );

		std::cout << res << std::endl;
		std::cout << regexpRes << std::endl;

		CPPUNIT_ASSERT ( regexpRes == res );
	}
	{
		regexp::UnboundedRegExpAlternation < char > alt1;
		alt1.appendElement ( a );
		alt1.appendElement ( a );
		regexp::UnboundedRegExpConcatenation < char > con1;
		con1.appendElement ( alt1 );
		con1.appendElement ( b );

		regexp::UnboundedRegExpConcatenation < char > con2;
		con2.appendElement ( regexp::UnboundedRegExpEmpty < char > { } );
		con2.appendElement ( b );
		regexp::UnboundedRegExpAlternation < char > alt2;
		alt2.appendElement ( con2 );
		alt2.appendElement ( a );

		regexp::UnboundedRegExpConcatenation < char > con3;
		con3.appendElement ( regexp::UnboundedRegExpEmpty < char > { } );
		con3.appendElement ( a );
		regexp::UnboundedRegExpAlternation < char > alt3;
		alt3.appendElement ( con3 );
		alt3.appendElement ( alt2 );

		regexp::UnboundedRegExpConcatenation < char > con4;
		con4.appendElement ( regexp::UnboundedRegExpEmpty < char > { } );
		con4.appendElement ( b );
		regexp::UnboundedRegExpAlternation < char > alt4;
		alt4.appendElement ( con4 );
		alt4.appendElement ( alt3 );

		regexp::UnboundedRegExpAlternation < char > alt5;
		alt5.appendElement ( con1 );
		alt5.appendElement ( alt4 );

		regexp::UnboundedRegExp < char > regexp ( regexp::UnboundedRegExpStructure < char > { alt5 } );

		regexp::UnboundedRegExp < char > res = regexp::simplify::RegExpOptimize::optimize ( regexp );

		regexp::UnboundedRegExpConcatenation < char > con6;
		con6.appendElement ( a );
		con6.appendElement ( b );
		regexp::UnboundedRegExpAlternation < char > alt6;
		alt6.appendElement ( a );
		alt6.appendElement ( con6 );

		regexp::UnboundedRegExp < char > regexpRes( regexp::UnboundedRegExpStructure < char > { alt6 } );

		std::cout << regexp << std::endl;
		std::cout << res << std::endl;
		std::cout << regexpRes << std::endl;

		CPPUNIT_ASSERT ( regexpRes == res );
	}
	{
		regexp::UnboundedRegExpConcatenation < char > con1;
		con1.appendElement ( a );
		con1.appendElement ( z );
		regexp::UnboundedRegExpIteration < char > iter { b };
		regexp::UnboundedRegExpConcatenation < char > con2;
		con2.appendElement ( a );
		con2.appendElement ( iter );
		con2.appendElement ( b );
		con2.appendElement ( z );
		regexp::UnboundedRegExpAlternation < char > alt1;
		alt1.appendElement ( con1 );
		alt1.appendElement ( con2);

		regexp::UnboundedRegExp < char > regexp( regexp::UnboundedRegExpStructure < char > { alt1 } );

		regexp::UnboundedRegExp < char > res = regexp::simplify::RegExpOptimize::optimize(regexp);

		regexp::UnboundedRegExpConcatenation < char > con3;
		con3.appendElement ( a );
		con3.appendElement ( iter );
		con3.appendElement ( z );

		regexp::UnboundedRegExp < char > regexpRes( regexp::UnboundedRegExpStructure < char > { con3 } );

		std::cout << res << std::endl;
		std::cout << regexpRes << std::endl;

		CPPUNIT_ASSERT ( regexpRes == res );