From a6ba027f36067914dfb03c6742346fb72877acbb Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sat, 7 Nov 2015 09:14:06 +0100 Subject: [PATCH] another leftFactorize test --- .../grammar/parsing/LeftFactorize.cpp | 66 +++++++++++++++++-- .../test-src/grammar/parsing/LeftFactorize.h | 2 - 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp b/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp index c7a37a2e80..d743388334 100644 --- a/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp +++ b/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp @@ -13,12 +13,6 @@ void LeftFactorize::tearDown ( ) { } void LeftFactorize::testLeftFactorize ( ) { -} - -void LeftFactorize::testLeftFactorize2 ( ) { -} - -void LeftFactorize::testLeftFactorize3 ( ) { alphabet::Symbol S = alphabet::symbolFrom ( "S" ); alphabet::Symbol Sp = alphabet::symbolFrom ( "S'" ); alphabet::Symbol B = alphabet::symbolFrom ( "B" ); @@ -68,3 +62,63 @@ void LeftFactorize::testLeftFactorize3 ( ) { CPPUNIT_ASSERT ( res == comp ); } + +void LeftFactorize::testLeftFactorize2 ( ) { + alphabet::Symbol S = alphabet::symbolFrom ( "S" ); + alphabet::Symbol Sp = alphabet::symbolFrom ( "S'" ); + alphabet::Symbol B = alphabet::symbolFrom ( "B" ); + alphabet::Symbol Bp = alphabet::symbolFrom ( "B'" ); + alphabet::Symbol C = alphabet::symbolFrom ( "C" ); + alphabet::Symbol Cp = alphabet::symbolFrom ( "C'" ); + alphabet::Symbol D = alphabet::symbolFrom ( "D" ); + + alphabet::Symbol a = alphabet::symbolFrom ( 'a' ); + alphabet::Symbol b = alphabet::symbolFrom ( 'b' ); + alphabet::Symbol c = alphabet::symbolFrom ( 'c' ); + alphabet::Symbol d = alphabet::symbolFrom ( 'd' ); + + grammar::CFG grammar ( S ); + + grammar.setTerminalAlphabet ( { a, b, c, d } ); + grammar.setNonterminalAlphabet ( { { S, B, C, D } } ); + grammar.setInitialSymbol ( S ); + + grammar.addRule ( S, std::vector < alphabet::Symbol > { a, B } ); + grammar.addRule ( S, std::vector < alphabet::Symbol > { a, C } ); + grammar.addRule ( S, std::vector < alphabet::Symbol > { D } ); + grammar.addRule ( B, std::vector < alphabet::Symbol > { b, B } ); + grammar.addRule ( B, std::vector < alphabet::Symbol > { b } ); + grammar.addRule ( C, std::vector < alphabet::Symbol > { c, C } ); + grammar.addRule ( C, std::vector < alphabet::Symbol > { c } ); + grammar.addRule ( C, std::vector < alphabet::Symbol > { b } ); + grammar.addRule ( D, std::vector < alphabet::Symbol > { d } ); + + grammar::CFG res = grammar; + grammar::parsing::LeftFactorize::leftFactorize ( res, a, S); + grammar::parsing::LeftFactorize::leftFactorize ( res, b, B); + grammar::parsing::LeftFactorize::leftFactorize ( res, c, C); + + grammar::CFG comp ( S ); + + comp.setTerminalAlphabet ( { a, b, c, d } ); + comp.setNonterminalAlphabet ( { { S, Sp, B, Bp, C, Cp, D} } ); + comp.setInitialSymbol ( S ); + + comp.addRule ( S, std::vector < alphabet::Symbol > { a, Sp } ); + comp.addRule ( S, std::vector < alphabet::Symbol > { D } ); + comp.addRule ( Sp, std::vector < alphabet::Symbol > { B } ); + comp.addRule ( Sp, std::vector < alphabet::Symbol > { C } ); + comp.addRule ( B, std::vector < alphabet::Symbol > { b, Bp } ); + comp.addRule ( Bp, std::vector < alphabet::Symbol > { B } ); + comp.addRule ( Bp, std::vector < alphabet::Symbol > { } ); + comp.addRule ( C, std::vector < alphabet::Symbol > { c, Cp } ); + comp.addRule ( C, std::vector < alphabet::Symbol > { b } ); + comp.addRule ( Cp, std::vector < alphabet::Symbol > { C } ); + comp.addRule ( Cp, std::vector < alphabet::Symbol > { } ); + comp.addRule ( D, std::vector < alphabet::Symbol > { d } ); + + std::cout << res << std::endl << comp << std::endl; + + CPPUNIT_ASSERT ( res == comp ); +} + diff --git a/alib2algo/test-src/grammar/parsing/LeftFactorize.h b/alib2algo/test-src/grammar/parsing/LeftFactorize.h index 88f457070d..6503af75b2 100644 --- a/alib2algo/test-src/grammar/parsing/LeftFactorize.h +++ b/alib2algo/test-src/grammar/parsing/LeftFactorize.h @@ -7,7 +7,6 @@ class LeftFactorize : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE ( LeftFactorize ); CPPUNIT_TEST ( testLeftFactorize ); CPPUNIT_TEST ( testLeftFactorize2 ); - CPPUNIT_TEST ( testLeftFactorize3 ); CPPUNIT_TEST_SUITE_END ( ); public: @@ -16,7 +15,6 @@ public: void testLeftFactorize ( ); void testLeftFactorize2 ( ); - void testLeftFactorize3 ( ); }; #endif /* LEFT_FACTORIZE_TEST_H_ */ -- GitLab