From 21edcf2e8f54fa19dd27776c4bed0c06a1fcb0fc Mon Sep 17 00:00:00 2001 From: Tomas Pecka <peckato1@fit.cvut.cz> Date: Sat, 23 Feb 2019 11:32:49 +0100 Subject: [PATCH] Unit tests: CPPUNIT -> Catch2: algo_experimental --- .../parsing/AbsorbTerminalSymbolTest.cpp | 219 +++-- .../parsing/AbsorbTerminalSymbolTest.h | 22 - .../parsing/CornerSubstitutionTest.cpp | 191 ++-- .../grammar/parsing/CornerSubstitutionTest.h | 20 - .../parsing/ExtractRightContextTest.cpp | 189 ++-- .../grammar/parsing/ExtractRightContextTest.h | 20 - .../test-src/grammar/parsing/FirstTest.cpp | 107 +-- .../test-src/grammar/parsing/FirstTest.h | 22 - .../test-src/grammar/parsing/FollowTest.cpp | 27 +- .../test-src/grammar/parsing/FollowTest.h | 19 - .../parsing/HandleFirstFirstConflictTest.cpp | 271 +++--- .../parsing/HandleFirstFirstConflictTest.h | 20 - .../parsing/HandleFirstFollowConflictTest.cpp | 283 +++--- .../parsing/HandleFirstFollowConflictTest.h | 20 - .../grammar/parsing/LL1ParseTableTest.cpp | 21 +- .../grammar/parsing/LL1ParseTableTest.h | 18 - .../grammar/parsing/LR0ParserTest.cpp | 514 +++++----- .../test-src/grammar/parsing/LR0ParserTest.h | 36 - .../test-src/grammar/parsing/LRParserTest.cpp | 248 ++--- .../test-src/grammar/parsing/LRParserTest.h | 36 - .../grammar/parsing/LeftFactorizeTest.cpp | 226 +++-- .../grammar/parsing/LeftFactorizeTest.h | 20 - .../grammar/parsing/SLR1ParseTableTest.cpp | 883 +++++++++--------- .../grammar/parsing/SLR1ParseTableTest.h | 36 - alib2algo_experimental/test-src/main.cpp | 168 +--- .../query/CompactSuffixAutomatonQueryTest.cpp | 27 +- .../query/CompactSuffixAutomatonQueryTest.h | 18 - 27 files changed, 1556 insertions(+), 2125 deletions(-) delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/AbsorbTerminalSymbolTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/CornerSubstitutionTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/ExtractRightContextTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/FirstTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/FollowTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflictTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/HandleFirstFollowConflictTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/LL1ParseTableTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/LR0ParserTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/LRParserTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/LeftFactorizeTest.h delete mode 100644 alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTableTest.h delete mode 100644 alib2algo_experimental/test-src/stringology/query/CompactSuffixAutomatonQueryTest.h diff --git a/alib2algo_experimental/test-src/grammar/parsing/AbsorbTerminalSymbolTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/AbsorbTerminalSymbolTest.cpp index 3c54b98233..d70dbaefe6 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/AbsorbTerminalSymbolTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/AbsorbTerminalSymbolTest.cpp @@ -1,154 +1,147 @@ -#include "AbsorbTerminalSymbolTest.h" +#include <catch2/catch.hpp> #include "grammar/ContextFree/CFG.h" #include "grammar/parsing/AbsorbTerminalSymbol.h" #include "factory/StringDataFactory.hpp" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( AbsorbTerminalSymbolTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( AbsorbTerminalSymbolTest ); +TEST_CASE ( "Absorb Terminal Symbol", "[unit][grammar]" ) { + SECTION ( "Test 1" ) { + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); -void AbsorbTerminalSymbolTest::setUp ( ) { -} - -void AbsorbTerminalSymbolTest::tearDown ( ) { -} + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); -void AbsorbTerminalSymbolTest::testAbsorbTerminalSymbol ( ) { - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); + DefaultSymbolType Ba = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( B, a ) ) ); - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); + grammar::CFG < > grammar ( A ); - DefaultSymbolType Ba = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( B, a ) ) ); + grammar.setTerminalAlphabet ( { a, b, c } ); + grammar.setNonterminalAlphabet ( { A, B, C } ); + grammar.setInitialSymbol ( A ); - grammar::CFG < > grammar ( A ); + grammar.addRule ( A, { B, a, C } ); + grammar.addRule ( B, { } ); + grammar.addRule ( B, { a, a, C } ); + grammar.addRule ( C, { c } ); + grammar.addRule ( C, { b, C } ); - grammar.setTerminalAlphabet ( { a, b, c } ); - grammar.setNonterminalAlphabet ( { A, B, C } ); - grammar.setInitialSymbol ( A ); + grammar::CFG < > res = grammar; + grammar::parsing::AbsorbTerminalSymbol::absorbTerminalSymbol ( res, a, { B } ); - grammar.addRule ( A, { B, a, C } ); - grammar.addRule ( B, { } ); - grammar.addRule ( B, { a, a, C } ); - grammar.addRule ( C, { c } ); - grammar.addRule ( C, { b, C } ); + grammar::CFG < > comp ( A ); - grammar::CFG < > res = grammar; - grammar::parsing::AbsorbTerminalSymbol::absorbTerminalSymbol ( res, a, { B } ); + comp.setTerminalAlphabet ( { a, b, c } ); + comp.setNonterminalAlphabet ( { A, B, Ba, C } ); + comp.setInitialSymbol ( A ); - grammar::CFG < > comp ( A ); + comp.addRule ( A, { Ba, C } ); + comp.addRule ( B, { } ); + comp.addRule ( B, { a, a, C } ); + comp.addRule ( Ba, { a } ); + comp.addRule ( Ba, { a, a, C, a } ); + comp.addRule ( C, { c } ); + comp.addRule ( C, { b, C } ); - comp.setTerminalAlphabet ( { a, b, c } ); - comp.setNonterminalAlphabet ( { A, B, Ba, C } ); - comp.setInitialSymbol ( A ); + // INFO ( alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << alib::StringDataFactory::toString ( grammar::Grammar ( comp ) ) ); - comp.addRule ( A, { Ba, C } ); - comp.addRule ( B, { } ); - comp.addRule ( B, { a, a, C } ); - comp.addRule ( Ba, { a } ); - comp.addRule ( Ba, { a, a, C, a } ); - comp.addRule ( C, { c } ); - comp.addRule ( C, { b, C } ); + CHECK ( res == comp ); + } - // std::cout << alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << alib::StringDataFactory::toString ( grammar::Grammar ( comp ) ) << std::endl; + SECTION ( "Test 2" ) { + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + DefaultSymbolType X = DefaultSymbolType ( "X" ); - CPPUNIT_ASSERT ( res == comp ); -} + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); -void AbsorbTerminalSymbolTest::testAbsorbTerminalSymbol2 ( ) { - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - DefaultSymbolType X = DefaultSymbolType ( "X" ); + DefaultSymbolType Ba = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( B, a ) ) ); + DefaultSymbolType Xa = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( X, a ) ) ); - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); + grammar::CFG < > grammar ( A ); - DefaultSymbolType Ba = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( B, a ) ) ); - DefaultSymbolType Xa = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( X, a ) ) ); + grammar.setTerminalAlphabet ( { a, b, c } ); + grammar.setNonterminalAlphabet ( { A, B, C, X } ); + grammar.setInitialSymbol ( A ); - grammar::CFG < > grammar ( A ); + grammar.addRule ( A, { X, a, C } ); + grammar.addRule ( X, { c, B } ); + grammar.addRule ( B, { } ); + grammar.addRule ( B, { a, a, C } ); + grammar.addRule ( C, { c } ); + grammar.addRule ( C, { b, C } ); - grammar.setTerminalAlphabet ( { a, b, c } ); - grammar.setNonterminalAlphabet ( { A, B, C, X } ); - grammar.setInitialSymbol ( A ); + grammar::CFG < > res = grammar; + grammar::parsing::AbsorbTerminalSymbol::absorbTerminalSymbol ( res, a, { B, X } ); - grammar.addRule ( A, { X, a, C } ); - grammar.addRule ( X, { c, B } ); - grammar.addRule ( B, { } ); - grammar.addRule ( B, { a, a, C } ); - grammar.addRule ( C, { c } ); - grammar.addRule ( C, { b, C } ); + grammar::CFG < > comp ( A ); - grammar::CFG < > res = grammar; - grammar::parsing::AbsorbTerminalSymbol::absorbTerminalSymbol ( res, a, { B, X } ); + comp.setTerminalAlphabet ( { a, b, c } ); + comp.setNonterminalAlphabet ( { A, B, Ba, C, X, Xa } ); + comp.setInitialSymbol ( A ); - grammar::CFG < > comp ( A ); + comp.addRule ( A, { Xa, C } ); + comp.addRule ( X, { c, B } ); + comp.addRule ( Xa, { c, Ba } ); + comp.addRule ( B, { } ); + comp.addRule ( B, { a, a, C } ); + comp.addRule ( Ba, { a } ); + comp.addRule ( Ba, { a, a, C, a } ); + comp.addRule ( C, { c } ); + comp.addRule ( C, { b, C } ); - comp.setTerminalAlphabet ( { a, b, c } ); - comp.setNonterminalAlphabet ( { A, B, Ba, C, X, Xa } ); - comp.setInitialSymbol ( A ); + // INFO ( "res " << alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << "comp " << alib::StringDataFactory::toString ( grammar::Grammar ( comp ) ) ); - comp.addRule ( A, { Xa, C } ); - comp.addRule ( X, { c, B } ); - comp.addRule ( Xa, { c, Ba } ); - comp.addRule ( B, { } ); - comp.addRule ( B, { a, a, C } ); - comp.addRule ( Ba, { a } ); - comp.addRule ( Ba, { a, a, C, a } ); - comp.addRule ( C, { c } ); - comp.addRule ( C, { b, C } ); - - // std::cout << "res " << alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << "comp " << alib::StringDataFactory::toString ( grammar::Grammar ( comp ) ) << std::endl; - - CPPUNIT_ASSERT ( res == comp ); -} + CHECK ( res == comp ); + } -void AbsorbTerminalSymbolTest::testAbsorbTerminalSymbol3 ( ) { - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); + SECTION ( "Test 3" ) { + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); - DefaultSymbolType Ba = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( B, a ) ) ); + DefaultSymbolType Ba = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( B, a ) ) ); - grammar::CFG < > grammar ( A ); + grammar::CFG < > grammar ( A ); - grammar.setTerminalAlphabet ( { a, b, c } ); - grammar.setNonterminalAlphabet ( { A, B, C } ); - grammar.setInitialSymbol ( A ); + grammar.setTerminalAlphabet ( { a, b, c } ); + grammar.setNonterminalAlphabet ( { A, B, C } ); + grammar.setInitialSymbol ( A ); - grammar.addRule ( A, { B, a, C } ); - grammar.addRule ( B, { } ); - grammar.addRule ( B, { a, a, B } ); - grammar.addRule ( C, { c } ); - grammar.addRule ( C, { b, C } ); + grammar.addRule ( A, { B, a, C } ); + grammar.addRule ( B, { } ); + grammar.addRule ( B, { a, a, B } ); + grammar.addRule ( C, { c } ); + grammar.addRule ( C, { b, C } ); - grammar::CFG < > res = grammar; - grammar::parsing::AbsorbTerminalSymbol::absorbTerminalSymbol ( res, a, { B } ); + grammar::CFG < > res = grammar; + grammar::parsing::AbsorbTerminalSymbol::absorbTerminalSymbol ( res, a, { B } ); - grammar::CFG < > comp ( A ); + grammar::CFG < > comp ( A ); - comp.setTerminalAlphabet ( { a, b, c } ); - comp.setNonterminalAlphabet ( { A, B, Ba, C } ); - comp.setInitialSymbol ( A ); + comp.setTerminalAlphabet ( { a, b, c } ); + comp.setNonterminalAlphabet ( { A, B, Ba, C } ); + comp.setInitialSymbol ( A ); - comp.addRule ( A, { Ba, C } ); - comp.addRule ( B, { } ); - comp.addRule ( B, { a, a, B } ); - comp.addRule ( Ba, { a } ); - comp.addRule ( Ba, { a, a, Ba } ); - comp.addRule ( C, { c } ); - comp.addRule ( C, { b, C } ); + comp.addRule ( A, { Ba, C } ); + comp.addRule ( B, { } ); + comp.addRule ( B, { a, a, B } ); + comp.addRule ( Ba, { a } ); + comp.addRule ( Ba, { a, a, Ba } ); + comp.addRule ( C, { c } ); + comp.addRule ( C, { b, C } ); - // std::cout << alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << alib::StringDataFactory::toString ( grammar::Grammar ( comp ) ) << std::endl; + // INFO ( alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << alib::StringDataFactory::toString ( grammar::Grammar ( comp ) ) ); - CPPUNIT_ASSERT ( res == comp ); + CHECK ( res == comp ); + } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/AbsorbTerminalSymbolTest.h b/alib2algo_experimental/test-src/grammar/parsing/AbsorbTerminalSymbolTest.h deleted file mode 100644 index ed461819d0..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/AbsorbTerminalSymbolTest.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef ABSORB_TERMINAL_SYMBOL_TEST_H_ -#define ABSORB_TERMINAL_SYMBOL_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class AbsorbTerminalSymbolTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( AbsorbTerminalSymbolTest ); - CPPUNIT_TEST ( testAbsorbTerminalSymbol ); - CPPUNIT_TEST ( testAbsorbTerminalSymbol2 ); - CPPUNIT_TEST ( testAbsorbTerminalSymbol3 ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testAbsorbTerminalSymbol ( ); - void testAbsorbTerminalSymbol2 ( ); - void testAbsorbTerminalSymbol3 ( ); -}; - -#endif /* ABSORB_TERMINAL_SYMBOL_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitutionTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitutionTest.cpp index 667b20cfb2..8ecd9b2525 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitutionTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitutionTest.cpp @@ -1,105 +1,96 @@ -#include "CornerSubstitutionTest.h" +#include <catch2/catch.hpp> #include "grammar/ContextFree/CFG.h" #include "grammar/parsing/CornerSubstitution.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( CornerSubstitutionTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( CornerSubstitutionTest ); - -void CornerSubstitutionTest::setUp ( ) { -} - -void CornerSubstitutionTest::tearDown ( ) { -} - -void CornerSubstitutionTest::testCornerSubstitution ( ) { - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - DefaultSymbolType d = DefaultSymbolType ( 'd' ); - - grammar::CFG < > grammar ( A ); - - grammar.setTerminalAlphabet ( { a, b, c, d } ); - grammar.setNonterminalAlphabet ( { A, B, C } ); - grammar.setInitialSymbol ( A ); - - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - - grammar::CFG < > res = grammar; - grammar::parsing::CornerSubstitution::cornerSubstitution ( res, a, A ); - - grammar::CFG < > comp ( A ); - - comp.setTerminalAlphabet ( { a, b, c, d } ); - comp.setNonterminalAlphabet ( { A, B, C } ); - comp.setInitialSymbol ( A ); - - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, B } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - -// std::cout << res << std::endl << comp << std::endl; - - CPPUNIT_ASSERT ( res == comp ); -} - -void CornerSubstitutionTest::testCornerSubstitution2 ( ) { - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - DefaultSymbolType d = DefaultSymbolType ( 'd' ); - - grammar::CFG < > grammar ( A ); - - grammar.setTerminalAlphabet ( { a, b, c, d } ); - grammar.setNonterminalAlphabet ( { A, B, C } ); - grammar.setInitialSymbol ( A ); - - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B, b } ); - - grammar::CFG < > res = grammar; - grammar::parsing::CornerSubstitution::cornerSubstitution ( res, a, A ); - grammar::parsing::CornerSubstitution::cornerSubstitution ( res, a, A ); - - grammar::CFG < > comp ( A ); - - comp.setTerminalAlphabet ( { a, b, c, d } ); - comp.setNonterminalAlphabet ( { A, B, C } ); - comp.setInitialSymbol ( A ); - - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, b, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B, b, B } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B, b } ); - -// std::cout << res << std::endl << comp << std::endl; - - CPPUNIT_ASSERT ( res == comp ); +TEST_CASE ( "Corner Substitution", "[unit][grammar]" ) { + SECTION ( "Test 1" ) { + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); + + grammar::CFG < > grammar ( A ); + + grammar.setTerminalAlphabet ( { a, b, c, d } ); + grammar.setNonterminalAlphabet ( { A, B, C } ); + grammar.setInitialSymbol ( A ); + + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); + + grammar::CFG < > res = grammar; + grammar::parsing::CornerSubstitution::cornerSubstitution ( res, a, A ); + + grammar::CFG < > comp ( A ); + + comp.setTerminalAlphabet ( { a, b, c, d } ); + comp.setNonterminalAlphabet ( { A, B, C } ); + comp.setInitialSymbol ( A ); + + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, B } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); + + INFO ( "Res: " << res << ", " << comp ); + CHECK ( res == comp ); + } + + SECTION ( "Test 2" ) { + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); + + grammar::CFG < > grammar ( A ); + + grammar.setTerminalAlphabet ( { a, b, c, d } ); + grammar.setNonterminalAlphabet ( { A, B, C } ); + grammar.setInitialSymbol ( A ); + + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B, b } ); + + grammar::CFG < > res = grammar; + grammar::parsing::CornerSubstitution::cornerSubstitution ( res, a, A ); + grammar::parsing::CornerSubstitution::cornerSubstitution ( res, a, A ); + + grammar::CFG < > comp ( A ); + + comp.setTerminalAlphabet ( { a, b, c, d } ); + comp.setNonterminalAlphabet ( { A, B, C } ); + comp.setInitialSymbol ( A ); + + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, b, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B, b, B } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B, b } ); + + INFO ( "Res: " << res << ", " << comp ); + CHECK ( res == comp ); + } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitutionTest.h b/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitutionTest.h deleted file mode 100644 index 0efad99c91..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitutionTest.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef CORNER_SUBSTITUTION_TEST_H_ -#define CORNER_SUBSTITUTION_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class CornerSubstitutionTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( CornerSubstitutionTest ); - CPPUNIT_TEST ( testCornerSubstitution ); - CPPUNIT_TEST ( testCornerSubstitution2 ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testCornerSubstitution ( ); - void testCornerSubstitution2 ( ); -}; - -#endif /* CORNER_SUBSTITUTION_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContextTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContextTest.cpp index dcdfe8659c..2e40b222d0 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContextTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContextTest.cpp @@ -1,104 +1,95 @@ -#include "ExtractRightContextTest.h" +#include <catch2/catch.hpp> #include "grammar/ContextFree/CFG.h" #include "grammar/parsing/ExtractRightContext.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( ExtractRightContextTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( ExtractRightContextTest ); - -void ExtractRightContextTest::setUp ( ) { -} - -void ExtractRightContextTest::tearDown ( ) { -} - -void ExtractRightContextTest::testExtractRightContext ( ) { - DefaultSymbolType S = DefaultSymbolType ( "S" ); - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - - grammar::CFG < > grammar ( S ); - - grammar.setTerminalAlphabet ( { a, b, c } ); - grammar.setNonterminalAlphabet ( { S, A, C } ); - grammar.setInitialSymbol ( S ); - - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, A, C } ); - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, b } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, c, A, b } ); - grammar.addRule ( C, { a, b } ); - grammar.addRule ( C, { c, C } ); - - grammar::CFG < > res = grammar; - grammar::parsing::ExtractRightContext::extractRightContext ( res, a, { A } ); - - grammar::CFG < > comp ( S ); - - comp.setTerminalAlphabet ( { a, b, c } ); - comp.setNonterminalAlphabet ( { S, A, C } ); - comp.setInitialSymbol ( S ); - - comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, A, a, b } ); - comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, A, c, C } ); - comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, b } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, c, A, b } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, b } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); - - // std::cout << res << std::endl << comp << std::endl; - - CPPUNIT_ASSERT ( res == comp ); -} - -void ExtractRightContextTest::testExtractRightContext2 ( ) { - DefaultSymbolType S = DefaultSymbolType ( "S" ); - DefaultSymbolType X = DefaultSymbolType ( "X" ); - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - - grammar::CFG < > grammar ( S ); - - grammar.setTerminalAlphabet ( { a, b, c } ); - grammar.setNonterminalAlphabet ( { S, X, A, C } ); - grammar.setInitialSymbol ( S ); - - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, X, C } ); - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, b } ); - grammar.addRule ( X, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, A } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, c, A, b } ); - grammar.addRule ( C, { A, a, b } ); - grammar.addRule ( C, { c, C } ); - - grammar::CFG < > res = grammar; - grammar::parsing::ExtractRightContext::extractRightContext ( res, a, { A, X } ); - - grammar::CFG < > comp ( S ); - - comp.setTerminalAlphabet ( { a, b, c } ); - comp.setNonterminalAlphabet ( { S, X, A, C } ); - comp.setInitialSymbol ( S ); - - comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, X, A, a, b } ); - comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, X, c, C } ); - comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, b } ); - comp.addRule ( X, { b, A } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, c, A, b } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { A, a, b } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); - - // std::cout << res << std::endl << comp << std::endl; - - CPPUNIT_ASSERT ( res == comp ); +TEST_CASE ( "Extract Right Context", "[unit][grammar]" ) { + SECTION ( "Test 1" ) { + DefaultSymbolType S = DefaultSymbolType ( "S" ); + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + + grammar::CFG < > grammar ( S ); + + grammar.setTerminalAlphabet ( { a, b, c } ); + grammar.setNonterminalAlphabet ( { S, A, C } ); + grammar.setInitialSymbol ( S ); + + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, A, C } ); + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, b } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, c, A, b } ); + grammar.addRule ( C, { a, b } ); + grammar.addRule ( C, { c, C } ); + + grammar::CFG < > res = grammar; + grammar::parsing::ExtractRightContext::extractRightContext ( res, a, { A } ); + + grammar::CFG < > comp ( S ); + + comp.setTerminalAlphabet ( { a, b, c } ); + comp.setNonterminalAlphabet ( { S, A, C } ); + comp.setInitialSymbol ( S ); + + comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, A, a, b } ); + comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, A, c, C } ); + comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, b } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, c, A, b } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, b } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); + + INFO ( "Res: " << res << ", " << comp ); + CHECK ( res == comp ); + } + + SECTION ( "Test 2" ) { + DefaultSymbolType S = DefaultSymbolType ( "S" ); + DefaultSymbolType X = DefaultSymbolType ( "X" ); + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + + grammar::CFG < > grammar ( S ); + + grammar.setTerminalAlphabet ( { a, b, c } ); + grammar.setNonterminalAlphabet ( { S, X, A, C } ); + grammar.setInitialSymbol ( S ); + + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, X, C } ); + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, b } ); + grammar.addRule ( X, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, A } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, c, A, b } ); + grammar.addRule ( C, { A, a, b } ); + grammar.addRule ( C, { c, C } ); + + grammar::CFG < > res = grammar; + grammar::parsing::ExtractRightContext::extractRightContext ( res, a, { A, X } ); + + grammar::CFG < > comp ( S ); + + comp.setTerminalAlphabet ( { a, b, c } ); + comp.setNonterminalAlphabet ( { S, X, A, C } ); + comp.setInitialSymbol ( S ); + + comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, X, A, a, b } ); + comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, c, X, c, C } ); + comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, b } ); + comp.addRule ( X, { b, A } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, c, A, b } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { A, a, b } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); + + INFO ( "Res: " << res << ", " << comp ); + CHECK ( res == comp ); + } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContextTest.h b/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContextTest.h deleted file mode 100644 index 6665311744..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContextTest.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef EXTRACT_RIGHT_CONTEXT_TEST_H_ -#define EXTRACT_RIGHT_CONTEXT_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class ExtractRightContextTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( ExtractRightContextTest ); - CPPUNIT_TEST ( testExtractRightContext ); - CPPUNIT_TEST ( testExtractRightContext2 ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testExtractRightContext ( ); - void testExtractRightContext2 ( ); -}; - -#endif /* EXTRACT_RIGHT_CONTEXT_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp index e51164a2f7..3885752c5d 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp @@ -1,20 +1,11 @@ -#include "FirstTest.h" +#include <catch2/catch.hpp> #include "grammar/parsing/First.h" -#include "string/Epsilon.h" #include "grammar/ContextFree/CFG.h" +#include "string/Epsilon.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( FirstTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( FirstTest ); - -void FirstTest::setUp ( ) { -} - -void FirstTest::tearDown ( ) { -} - -void FirstTest::testFirst ( ) { - { +TEST_CASE ( "LL1 First", "[unit][grammar]" ) { + SECTION ( "Test 1" ) { DefaultSymbolType nE = DefaultSymbolType ( 'E' ); DefaultSymbolType nT = DefaultSymbolType ( 'T' ); DefaultSymbolType nF = DefaultSymbolType ( 'F' ); @@ -73,10 +64,10 @@ void FirstTest::testFirst ( ) { for ( const auto & rhs : rule.second ) firstAlgo[rhs] = grammar::parsing::First::first ( grammar, rhs ); - CPPUNIT_ASSERT ( first == firstAlgo ); + CHECK ( first == firstAlgo ); } - { + SECTION ( "Test 2" ) { DefaultSymbolType nS = DefaultSymbolType ( 'S' ); DefaultSymbolType nA = DefaultSymbolType ( 'A' ); DefaultSymbolType nB = DefaultSymbolType ( 'B' ); @@ -178,54 +169,54 @@ void FirstTest::testFirst ( ) { for ( const auto & rhs : rule.second ) firstAlgo[rhs] = grammar::parsing::First::first ( grammar, rhs ); - CPPUNIT_ASSERT ( first == firstAlgo ); + CHECK ( first == firstAlgo ); } -} -void FirstTest::testFirst2 ( ) { - DefaultSymbolType A = DefaultSymbolType ( 'A' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - DefaultSymbolType d = DefaultSymbolType ( 'd' ); + SECTION ( "Test 3" ) { + DefaultSymbolType A = DefaultSymbolType ( 'A' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); - grammar::CFG < > grammar ( A ); + grammar::CFG < > grammar ( A ); - grammar.setTerminalAlphabet ( { c, d } ); - grammar.setNonterminalAlphabet ( { A } ); - grammar.setInitialSymbol ( A ); + grammar.setTerminalAlphabet ( { c, d } ); + grammar.setNonterminalAlphabet ( { A } ); + grammar.setInitialSymbol ( A ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { A, c } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { A, c } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); - ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = { { { d }, { d } }, { { A, c }, { d } } }; - CPPUNIT_ASSERT ( res == grammar::parsing::First::first ( grammar ) ); -} + ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = { { { d }, { d } }, { { A, c }, { d } } }; + CHECK ( res == grammar::parsing::First::first ( grammar ) ); + } -void FirstTest::testFirst3 ( ) { - DefaultSymbolType S = DefaultSymbolType ( 'S' ); - DefaultSymbolType A = DefaultSymbolType ( 'A' ); - DefaultSymbolType B = DefaultSymbolType ( 'B' ); - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - DefaultSymbolType d = DefaultSymbolType ( 'd' ); - DefaultSymbolType f = DefaultSymbolType ( 'f' ); - - grammar::CFG < > grammar ( S ); - - grammar.setTerminalAlphabet ( { a, b, c, d, f } ); - grammar.setNonterminalAlphabet ( { S, A, B } ); - grammar.setInitialSymbol ( S ); - - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { A, a } ); - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, S } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, A, d } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { f, S } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - - ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = - { - { { A, a }, { c, f, a } }, { { b, S }, { b } }, { { c, A, d }, { c } }, { { B }, { f, string::Epsilon < >::EPSILON } }, { { f, S }, { f } }, { { }, { string::Epsilon < >::EPSILON } } - }; - CPPUNIT_ASSERT ( res == grammar::parsing::First::first ( grammar ) ); + SECTION ( "Test 4" ) { + DefaultSymbolType S = DefaultSymbolType ( 'S' ); + DefaultSymbolType A = DefaultSymbolType ( 'A' ); + DefaultSymbolType B = DefaultSymbolType ( 'B' ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); + DefaultSymbolType f = DefaultSymbolType ( 'f' ); + + grammar::CFG < > grammar ( S ); + + grammar.setTerminalAlphabet ( { a, b, c, d, f } ); + grammar.setNonterminalAlphabet ( { S, A, B } ); + grammar.setInitialSymbol ( S ); + + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { A, a } ); + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, S } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, A, d } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { f, S } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + + ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = + { + { { A, a }, { c, f, a } }, { { b, S }, { b } }, { { c, A, d }, { c } }, { { B }, { f, string::Epsilon < >::EPSILON } }, { { f, S }, { f } }, { { }, { string::Epsilon < >::EPSILON } } + }; + CHECK ( res == grammar::parsing::First::first ( grammar ) ); + } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/FirstTest.h b/alib2algo_experimental/test-src/grammar/parsing/FirstTest.h deleted file mode 100644 index f0bd2252da..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/FirstTest.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef FIRST_TEST_H_ -#define FIRST_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class FirstTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( FirstTest ); - CPPUNIT_TEST ( testFirst ); - CPPUNIT_TEST ( testFirst2 ); - CPPUNIT_TEST ( testFirst3 ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testFirst ( ); - void testFirst2 ( ); - void testFirst3 ( ); -}; - -#endif /* FIRST_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/FollowTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/FollowTest.cpp index b0eb04c7a2..dece54940d 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/FollowTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/FollowTest.cpp @@ -1,20 +1,11 @@ -#include "FollowTest.h" +#include <catch2/catch.hpp> #include "grammar/parsing/Follow.h" -#include "string/Epsilon.h" #include "grammar/ContextFree/CFG.h" +#include "string/Epsilon.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( FollowTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( FollowTest ); - -void FollowTest::setUp ( ) { -} - -void FollowTest::tearDown ( ) { -} - -void FollowTest::testFollow ( ) { - { +TEST_CASE ( "LL1 Follow", "[unit][grammar]" ) { + SECTION ( "Test 1" ) { DefaultSymbolType nE = DefaultSymbolType ( 'E' ); DefaultSymbolType nT = DefaultSymbolType ( 'T' ); DefaultSymbolType nF = DefaultSymbolType ( 'F' ); @@ -63,12 +54,10 @@ void FollowTest::testFollow ( ) { for ( const auto & nt : grammar.getNonterminalAlphabet ( ) ) followAlgo[nt] = grammar::parsing::Follow::follow ( grammar, nt ); - // std::cout << follow << std::endl; - // std::cout << followAlgo << std::endl; - CPPUNIT_ASSERT ( follow == followAlgo ); + CHECK ( follow == followAlgo ); } - { + SECTION ( "Test 2" ) { DefaultSymbolType nS = DefaultSymbolType ( 'S' ); DefaultSymbolType nA = DefaultSymbolType ( 'A' ); DefaultSymbolType nB = DefaultSymbolType ( 'B' ); @@ -149,8 +138,6 @@ void FollowTest::testFollow ( ) { for ( const auto & nt : grammar.getNonterminalAlphabet ( ) ) followAlgo[nt] = grammar::parsing::Follow::follow ( grammar, nt ); - // std::cout << follow << std::endl; - // std::cout << followAlgo << std::endl; - CPPUNIT_ASSERT ( follow == followAlgo ); + CHECK ( follow == followAlgo ); } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/FollowTest.h b/alib2algo_experimental/test-src/grammar/parsing/FollowTest.h deleted file mode 100644 index aa992d4afa..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/FollowTest.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef FOLLOW_TEST_H_ -#define FOLLOW_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class FollowTest : public CppUnit::TestFixture -{ - CPPUNIT_TEST_SUITE( FollowTest ); - CPPUNIT_TEST( testFollow ); - CPPUNIT_TEST_SUITE_END(); - -public: - void setUp(); - void tearDown(); - - void testFollow(); -}; - -#endif /* FOLLOW_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflictTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflictTest.cpp index cc9f352d85..6412330b15 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflictTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflictTest.cpp @@ -1,150 +1,135 @@ -#include "HandleFirstFirstConflictTest.h" +#include <catch2/catch.hpp> #include "grammar/ContextFree/CFG.h" #include "grammar/parsing/HandleFirstFirstConflict.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( HandleFirstFirstConflictTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( HandleFirstFirstConflictTest ); +TEST_CASE ( "Handle First First Conflict", "[unit][grammar]" ) { + SECTION ( "Test 1" ) { + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType Ap = DefaultSymbolType ( object::AnyObject < std::string > ( "A", 1 ) ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); + + grammar::CFG < > grammar ( A ); -void HandleFirstFirstConflictTest::setUp ( ) { -} - -void HandleFirstFirstConflictTest::tearDown ( ) { -} - -void HandleFirstFirstConflictTest::testHandleFirstFirstConflict ( ) { - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType Ap = DefaultSymbolType ( object::AnyObject < std::string > ( "A", 1 ) ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - DefaultSymbolType d = DefaultSymbolType ( 'd' ); - - grammar::CFG < > grammar ( A ); - - grammar.setTerminalAlphabet ( { a, b, c, d } ); - grammar.setNonterminalAlphabet ( { A, B, C } ); - grammar.setInitialSymbol ( A ); - - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - - grammar::CFG < > res = grammar; - grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B }, {C ,B} } ); - - grammar::CFG < > comp ( A ); - - comp.setTerminalAlphabet ( { a, b, c, d } ); - comp.setNonterminalAlphabet ( { A, B, C } ); - comp.setInitialSymbol ( A ); - - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, B } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - -// std::cout << res << std::endl << comp << std::endl; - - CPPUNIT_ASSERT ( res == comp ); - - grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B }, {a, C, B} } ); - - grammar::CFG < > comp2 ( A ); - - comp2.setTerminalAlphabet ( { a, b, c, d } ); - comp2.setNonterminalAlphabet ( { A, Ap, B, C } ); - comp2.setInitialSymbol ( A ); - - comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ap } ); - comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, B } ); - comp2.addRule ( Ap, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); - comp2.addRule ( Ap, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); - comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); - comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); - comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - -// std::cout << res << std::endl << comp2 << std::endl; - - CPPUNIT_ASSERT ( res == comp2 ); -} - -void HandleFirstFirstConflictTest::testHandleFirstFirstConflict2 ( ) { - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType Ap = DefaultSymbolType ( object::AnyObject < std::string > ( "A", 1 ) ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - DefaultSymbolType d = DefaultSymbolType ( 'd' ); - - grammar::CFG < > grammar ( A ); - - grammar.setTerminalAlphabet ( { a, b, c, d } ); - grammar.setNonterminalAlphabet ( { A, B, C } ); - grammar.setInitialSymbol ( A ); - - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B, b } ); - - grammar::CFG < > res = grammar; - grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B}, {C, B}}); - grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B}, {a, C, B}, {B, b, B}}); - - grammar::CFG < > comp ( A ); - - comp.setTerminalAlphabet ( { a, b, c, d } ); - comp.setNonterminalAlphabet ( { A, B, C } ); - comp.setInitialSymbol ( A ); - - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, b, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B, b, B } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B, b } ); - -// std::cout << res << std::endl << comp << std::endl; - - CPPUNIT_ASSERT ( res == comp ); - - grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B}, {a, C, B}, {a, b, B}}); - - grammar::CFG < > comp2 ( A ); - - comp2.setTerminalAlphabet ( { a, b, c, d } ); - comp2.setNonterminalAlphabet ( { A, Ap, B, C } ); - comp2.setInitialSymbol ( A ); - - comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ap } ); - comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B, b, B } ); - comp2.addRule ( Ap, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); - comp2.addRule ( Ap, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); - comp2.addRule ( Ap, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); - comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); - comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B, b } ); - -// std::cout << res << std::endl << comp2 << std::endl; + grammar.setTerminalAlphabet ( { a, b, c, d } ); + grammar.setNonterminalAlphabet ( { A, B, C } ); + grammar.setInitialSymbol ( A ); - CPPUNIT_ASSERT ( res == comp2 ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); + + grammar::CFG < > res = grammar; + grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B }, {C ,B} } ); + + grammar::CFG < > comp ( A ); + + comp.setTerminalAlphabet ( { a, b, c, d } ); + comp.setNonterminalAlphabet ( { A, B, C } ); + comp.setInitialSymbol ( A ); + + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, B } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); + + CHECK ( res == comp ); + + grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B }, {a, C, B} } ); + + grammar::CFG < > comp2 ( A ); + + comp2.setTerminalAlphabet ( { a, b, c, d } ); + comp2.setNonterminalAlphabet ( { A, Ap, B, C } ); + comp2.setInitialSymbol ( A ); + + comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ap } ); + comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, B } ); + comp2.addRule ( Ap, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); + comp2.addRule ( Ap, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); + comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); + comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); + comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); + + CHECK ( res == comp2 ); + } + + SECTION ( "Test 2" ) { + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType Ap = DefaultSymbolType ( object::AnyObject < std::string > ( "A", 1 ) ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); + + grammar::CFG < > grammar ( A ); + + grammar.setTerminalAlphabet ( { a, b, c, d } ); + grammar.setNonterminalAlphabet ( { A, B, C } ); + grammar.setInitialSymbol ( A ); + + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B, b } ); + + grammar::CFG < > res = grammar; + grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B}, {C, B}}); + grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B}, {a, C, B}, {B, b, B}}); + + grammar::CFG < > comp ( A ); + + comp.setTerminalAlphabet ( { a, b, c, d } ); + comp.setNonterminalAlphabet ( { A, B, C } ); + comp.setInitialSymbol ( A ); + + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, b, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B, b, B } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B, b } ); + + CHECK ( res == comp ); + + grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B}, {a, C, B}, {a, b, B}}); + + grammar::CFG < > comp2 ( A ); + + comp2.setTerminalAlphabet ( { a, b, c, d } ); + comp2.setNonterminalAlphabet ( { A, Ap, B, C } ); + comp2.setInitialSymbol ( A ); + + comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ap } ); + comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B, b, B } ); + comp2.addRule ( Ap, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); + comp2.addRule ( Ap, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C, B } ); + comp2.addRule ( Ap, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); + comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, B } ); + comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); + comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B, b } ); + + CHECK ( res == comp2 ); + } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflictTest.h b/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflictTest.h deleted file mode 100644 index 9ef067cdd9..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflictTest.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef HANDLE_FIRST_FIRST_CONFLICT_TEST_H_ -#define HANDLE_FIRST_FIRST_CONFLICT_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class HandleFirstFirstConflictTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( HandleFirstFirstConflictTest ); - CPPUNIT_TEST ( testHandleFirstFirstConflict ); - CPPUNIT_TEST ( testHandleFirstFirstConflict2 ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testHandleFirstFirstConflict ( ); - void testHandleFirstFirstConflict2 ( ); -}; - -#endif /* HANDLE_FIRST_FIRST_CONFLICT_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFollowConflictTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFollowConflictTest.cpp index 2f39266241..c136b35de7 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFollowConflictTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFollowConflictTest.cpp @@ -1,158 +1,143 @@ -#include "HandleFirstFollowConflictTest.h" +#include <catch2/catch.hpp> #include "grammar/ContextFree/CFG.h" #include "grammar/parsing/HandleFirstFollowConflict.h" #include "factory/StringDataFactory.hpp" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( HandleFirstFollowConflictTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( HandleFirstFollowConflictTest ); +TEST_CASE ( "Handle First Follow Conflict", "[unit][grammar]" ) { + SECTION ( "Test 1" ) { + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); -void HandleFirstFollowConflictTest::setUp ( ) { -} - -void HandleFirstFollowConflictTest::tearDown ( ) { -} - -void HandleFirstFollowConflictTest::testHandleFirstFollowConflict ( ) { - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - DefaultSymbolType d = DefaultSymbolType ( 'd' ); - - DefaultSymbolType Ba = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( B, a ) ) ); - - grammar::CFG < > grammar ( A ); - - grammar.setTerminalAlphabet ( { a, b, c, d } ); - grammar.setNonterminalAlphabet ( { A, B, C } ); - grammar.setInitialSymbol ( A ); - - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, C } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, a, d } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - - grammar::CFG < > res = grammar; - grammar::parsing::HandleFirstFollowConflict::handleFirstFollowConflict ( res, a, B, { { a, B, C }, { } } ); - - grammar::CFG < > comp ( A ); - - comp.setTerminalAlphabet ( { a, b, c, d } ); - comp.setNonterminalAlphabet ( { A, B, C } ); - comp.setInitialSymbol ( A ); - - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, a, C } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, b, B } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, a, d } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - -// std::cout << "res " << alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << "comp " << alib::StringDataFactory::toString ( grammar::Grammar ( comp ) ) << std::endl; - - CPPUNIT_ASSERT ( res == comp ); - - grammar::parsing::HandleFirstFollowConflict::handleFirstFollowConflict ( res, a, B, { { a, B, C }, { } } ); - - grammar::CFG < > comp2 ( A ); - - comp2.setTerminalAlphabet ( { a, b, c, d } ); - comp2.setNonterminalAlphabet ( { A, B, Ba, C } ); - comp2.setInitialSymbol ( A ); - - comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ba, C } ); - comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, b, B } ); - comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); - comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ba, d } ); - comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - comp2.addRule ( Ba, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ba, d, a } ); - comp2.addRule ( Ba, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); - comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - -// std::cout << "res2 " << alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << "comp2 " << alib::StringDataFactory::toString ( grammar::Grammar ( comp2 ) ) << std::endl; - - CPPUNIT_ASSERT ( res == comp2 ); -} - -void HandleFirstFollowConflictTest::testHandleFirstFollowConflict2 ( ) { - DefaultSymbolType A = DefaultSymbolType ( "A" ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - DefaultSymbolType X = DefaultSymbolType ( "X" ); - - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - DefaultSymbolType d = DefaultSymbolType ( 'd' ); - - DefaultSymbolType Ba = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( B, a ) ) ); - DefaultSymbolType Xa = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( X, a ) ) ); - - grammar::CFG < > grammar ( A ); - - grammar.setTerminalAlphabet ( { a, b, c, d } ); - grammar.setNonterminalAlphabet ( { A, B, C, X } ); - grammar.setInitialSymbol ( A ); - - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, X, C, a } ); - grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); - grammar.addRule ( X, { c, B }); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, a, d } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, c } ); - - grammar::CFG < > res = grammar; - grammar::parsing::HandleFirstFollowConflict::handleFirstFollowConflict ( res, a, B, { { a, B, C }, { } } ); - - grammar::CFG < > comp ( A ); - - comp.setTerminalAlphabet ( { a, b, c, d } ); - comp.setNonterminalAlphabet ( { A, B, C, X } ); - comp.setInitialSymbol ( A ); - - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, X, a, C, a } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, X, b, B, c, a } ); - comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); - comp.addRule ( X, { c, B } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, a, d } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, c } ); - -// std::cout << "res " << alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << "comp " << alib::StringDataFactory::toString ( grammar::Grammar ( comp ) ) << std::endl; - - CPPUNIT_ASSERT ( res == comp ); - - grammar::parsing::HandleFirstFollowConflict::handleFirstFollowConflict ( res, a, B, { { a, B, C }, { } } ); - - grammar::CFG < > comp2 ( A ); - - comp2.setTerminalAlphabet ( { a, b, c, d } ); - comp2.setNonterminalAlphabet ( { A, B, Ba, C, X, Xa } ); - comp2.setInitialSymbol ( A ); - - comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Xa, C, a } ); - comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, X, b, B, c, a } ); - comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); - comp2.addRule ( X, { c, B } ); - comp2.addRule ( Xa, { c, Ba } ); - comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ba, d } ); - comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - comp2.addRule ( Ba, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ba, d, a } ); - comp2.addRule ( Ba, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); - comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, c } ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); + + DefaultSymbolType Ba = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( B, a ) ) ); + + grammar::CFG < > grammar ( A ); + + grammar.setTerminalAlphabet ( { a, b, c, d } ); + grammar.setNonterminalAlphabet ( { A, B, C } ); + grammar.setInitialSymbol ( A ); -// std::cout << "res2 " << alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << "comp2 " << alib::StringDataFactory::toString ( grammar::Grammar ( comp2 ) ) << std::endl; + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, C } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, a, d } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - CPPUNIT_ASSERT ( res == comp2 ); + grammar::CFG < > res = grammar; + grammar::parsing::HandleFirstFollowConflict::handleFirstFollowConflict ( res, a, B, { { a, B, C }, { } } ); + + grammar::CFG < > comp ( A ); + + comp.setTerminalAlphabet ( { a, b, c, d } ); + comp.setNonterminalAlphabet ( { A, B, C } ); + comp.setInitialSymbol ( A ); + + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, a, C } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, b, B } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, a, d } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); + + CHECK ( res == comp ); + + grammar::parsing::HandleFirstFollowConflict::handleFirstFollowConflict ( res, a, B, { { a, B, C }, { } } ); + + grammar::CFG < > comp2 ( A ); + + comp2.setTerminalAlphabet ( { a, b, c, d } ); + comp2.setNonterminalAlphabet ( { A, B, Ba, C } ); + comp2.setInitialSymbol ( A ); + + comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ba, C } ); + comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, b, B } ); + comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); + comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ba, d } ); + comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + comp2.addRule ( Ba, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ba, d, a } ); + comp2.addRule ( Ba, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); + comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); + + CHECK ( res == comp2 ); + } + + SECTION ( "Test 2" ) { + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + DefaultSymbolType X = DefaultSymbolType ( "X" ); + + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); + + DefaultSymbolType Ba = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( B, a ) ) ); + DefaultSymbolType Xa = DefaultSymbolType ( object::AnyObject < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( ext::make_pair ( X, a ) ) ); + + grammar::CFG < > grammar ( A ); + + grammar.setTerminalAlphabet ( { a, b, c, d } ); + grammar.setNonterminalAlphabet ( { A, B, C, X } ); + grammar.setInitialSymbol ( A ); + + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, X, C, a } ); + grammar.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); + grammar.addRule ( X, { c, B }); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, a, d } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, c } ); + + grammar::CFG < > res = grammar; + grammar::parsing::HandleFirstFollowConflict::handleFirstFollowConflict ( res, a, B, { { a, B, C }, { } } ); + + grammar::CFG < > comp ( A ); + + comp.setTerminalAlphabet ( { a, b, c, d } ); + comp.setNonterminalAlphabet ( { A, B, C, X } ); + comp.setInitialSymbol ( A ); + + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, X, a, C, a } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, X, b, B, c, a } ); + comp.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); + comp.addRule ( X, { c, B } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B, a, d } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, c } ); + + CHECK ( res == comp ); + + grammar::parsing::HandleFirstFollowConflict::handleFirstFollowConflict ( res, a, B, { { a, B, C }, { } } ); + + grammar::CFG < > comp2 ( A ); + + comp2.setTerminalAlphabet ( { a, b, c, d } ); + comp2.setNonterminalAlphabet ( { A, B, Ba, C, X, Xa } ); + comp2.setInitialSymbol ( A ); + + comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Xa, C, a } ); + comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, X, b, B, c, a } ); + comp2.addRule ( A, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); + comp2.addRule ( X, { c, B } ); + comp2.addRule ( Xa, { c, Ba } ); + comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ba, d } ); + comp2.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + comp2.addRule ( Ba, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Ba, d, a } ); + comp2.addRule ( Ba, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a } ); + comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + comp2.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B, c } ); + + CHECK ( res == comp2 ); + } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFollowConflictTest.h b/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFollowConflictTest.h deleted file mode 100644 index f8c0ff0700..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFollowConflictTest.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef HANDLE_FIRST_FOLLOW_CONFLICT_TEST_H_ -#define HANDLE_FIRST_FOLLOW_CONFLICT_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class HandleFirstFollowConflictTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( HandleFirstFollowConflictTest ); - CPPUNIT_TEST ( testHandleFirstFollowConflict ); - CPPUNIT_TEST ( testHandleFirstFollowConflict2 ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testHandleFirstFollowConflict ( ); - void testHandleFirstFollowConflict2 ( ); -}; - -#endif /* HANDLE_FIRST_FOLLOW_CONFLICT_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTableTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTableTest.cpp index cbd9078349..fda66736dd 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTableTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTableTest.cpp @@ -1,20 +1,11 @@ -#include "LL1ParseTableTest.h" +#include <catch2/catch.hpp> #include "grammar/parsing/LL1ParseTable.h" -#include "string/Epsilon.h" #include "grammar/ContextFree/CFG.h" +#include "string/Epsilon.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( LL1ParseTableTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( LL1ParseTableTest ); - -void LL1ParseTableTest::setUp ( ) { -} - -void LL1ParseTableTest::tearDown ( ) { -} - -void LL1ParseTableTest::testLL1Table ( ) { - { +TEST_CASE ( "LL1 Parse Table", "[unit][grammar]" ) { + SECTION ( "Test 1" ) { DefaultSymbolType nE = DefaultSymbolType ( 'E' ); DefaultSymbolType nEp = DefaultSymbolType ( "E'" ); DefaultSymbolType nT = DefaultSymbolType ( 'T' ); @@ -74,8 +65,6 @@ void LL1ParseTableTest::testLL1Table ( ) { ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > parseTableAlgo = grammar::parsing::LL1ParseTable::parseTable ( grammar ); - std::cout << parseTable << std::endl; - std::cout << parseTableAlgo << std::endl; - CPPUNIT_ASSERT ( parseTable == parseTableAlgo ); + CHECK ( parseTable == parseTableAlgo ); } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTableTest.h b/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTableTest.h deleted file mode 100644 index 86581f6c7c..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTableTest.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef LL_1_PARSER_TABLE_H_ -#define LL_1_PARSER_TABLE_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class LL1ParseTableTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( LL1ParseTableTest ); - CPPUNIT_TEST ( testLL1Table ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testLL1Table ( ); -}; - -#endif /* LL_1_PARSER_TABLE_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/LR0ParserTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/LR0ParserTest.cpp index 4eed584927..8f481da374 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LR0ParserTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/LR0ParserTest.cpp @@ -1,346 +1,350 @@ -#include "LR0ParserTest.h" - -#include "grammar/parsing/LRParser.h" -#include "grammar/parsing/LR0Parser.h" +#include <catch2/catch.hpp> #include <alib/vector> -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( LR0ParserTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( LR0ParserTest ); +#include "grammar/ContextFree/CFG.h" +#include "grammar/parsing/LRParser.h" +#include "grammar/parsing/LR0Parser.h" -void LR0ParserTest::setUp ( ) { -} +static DefaultSymbolType E = DefaultSymbolType ( 'E' ); +static DefaultSymbolType T = DefaultSymbolType ( 'T' ); +static DefaultSymbolType F = DefaultSymbolType ( 'F' ); -void LR0ParserTest::tearDown ( ) { -} +static DefaultSymbolType plus = DefaultSymbolType ( '+' ); +static DefaultSymbolType times = DefaultSymbolType ( '*' ); +static DefaultSymbolType leftParenthesis = DefaultSymbolType ( '(' ); +static DefaultSymbolType rightParenthesis = DefaultSymbolType ( ')' ); +static DefaultSymbolType identifier = DefaultSymbolType ( "id" ); -grammar::CFG < > LR0ParserTest::getExpressionGrammar() { +static grammar::CFG < > getExpressionGrammar ( ) { grammar::CFG < > expressionGrammar( { E, T, F }, { plus, times, leftParenthesis, rightParenthesis, identifier }, E ); - + expressionGrammar.addRule ( E, { E, plus, T } ); expressionGrammar.addRule ( E, { T } ); expressionGrammar.addRule ( T, { T, times, F } ); expressionGrammar.addRule ( T, { F } ); expressionGrammar.addRule ( F, { leftParenthesis, E, rightParenthesis } ); expressionGrammar.addRule ( F, { identifier } ); - + return expressionGrammar; } -void LR0ParserTest::testClosure ( ) { - grammar::parsing::LR0Items kernelItems { - { - E, +TEST_CASE ( "LR0 Parser", "[unit][grammar]" ) { + SECTION ( "Test Closure" ) { + grammar::parsing::LR0Items kernelItems { { - { 2 , { E, plus, T } } - } - }, - }; + E, + { + { 2 , { E, plus, T } } + } + }, + }; + + grammar::parsing::LR0Items closure = grammar::parsing::LR0Parser::getClosure( kernelItems, getExpressionGrammar ( ) ); - grammar::parsing::LR0Items closure = grammar::parsing::LR0Parser::getClosure( kernelItems, getExpressionGrammar ( ) ); - - grammar::parsing::LR0Items correctClosure { - { - E, + grammar::parsing::LR0Items correctClosure { { - { 2 , { E, plus, T } } - } - }, - { - T, + E, + { + { 2 , { E, plus, T } } + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; + }; - CPPUNIT_ASSERT ( closure == correctClosure ); -} + CHECK ( closure == correctClosure ); + } -void LR0ParserTest::testNextStateItems ( ) { - grammar::parsing::LR0Items currentStateItems { - { - E, + SECTION ( "Test Next State Items" ) { + grammar::parsing::LR0Items currentStateItems { { - { 1 , { E, plus, T } } - } - }, - }; - - grammar::parsing::LR0Items nextStateItems = grammar::parsing::LR0Parser::getNextStateItems( currentStateItems, plus, getExpressionGrammar ( ) ); - - grammar::parsing::LR0Items correctNextStateItems { - { - E, - { - { 2 , { E, plus, T } } - } - }, - { - T, + E, + { + { 1 , { E, plus, T } } + } + }, + }; + + grammar::parsing::LR0Items nextStateItems = grammar::parsing::LR0Parser::getNextStateItems( currentStateItems, plus, getExpressionGrammar ( ) ); + + grammar::parsing::LR0Items correctNextStateItems { { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + E, + { + { 2 , { E, plus, T } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, + { + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; - - CPPUNIT_ASSERT ( nextStateItems == correctNextStateItems ); -} + }; -void LR0ParserTest::testAutomaton ( ) { - ext::vector < grammar::parsing::LR0Items > items(12); + CHECK ( nextStateItems == correctNextStateItems ); + } - grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); + SECTION ( "Test Automaton" ) { + ext::vector < grammar::parsing::LR0Items > items(12); - items[0] = { - { - augmentedExpressionGrammar.getInitialSymbol(), - { - { 0 , { E } } - } - }, - { - E, + grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); + + items[0] = { { - { 0 , { E, plus, T } }, - { 0 , { T } } - } - }, - { - T, + augmentedExpressionGrammar.getInitialSymbol(), + { + { 0 , { E } } + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + E, + { + { 0 , { E, plus, T } }, + { 0 , { T } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, + { + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; + }; - items[1] = { - { - augmentedExpressionGrammar.getInitialSymbol(), + items[1] = { { - { 1 , { E } } + augmentedExpressionGrammar.getInitialSymbol(), + { + { 1 , { E } } + } + }, + { + E, + { + { 1 , { E, plus, T } } + } } - }, - { - E, - { - { 1 , { E, plus, T } } - } - } - }; + }; - items[2] = { - { - E, + items[2] = { { - { 1 , { T } } - } - }, - { - T, + E, + { + { 1 , { T } } + } + }, { - { 1 , { T, times, F } }, + T, + { + { 1 , { T, times, F } }, + } } - } - }; + }; - items[3] = { - { - T, + items[3] = { { - { 1 , { F } } + T, + { + { 1 , { F } } + } } - } - }; + }; - items[4] = { - { - E, + items[4] = { { - { 0 , { E, plus, T } }, - { 0 , { T } } - } - }, - { - T, + E, + { + { 0 , { E, plus, T } }, + { 0 , { T } } + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 1 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 1 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; + }; - items[5] = { - { - F, + items[5] = { { - { 1 , { identifier } } + F, + { + { 1 , { identifier } } + } } - } - }; - - items[6] = { - { - E, + }; + + items[6] = { { - { 2 , { E, plus, T } }, - } - }, - { - T, + E, + { + { 2 , { E, plus, T } }, + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; + }; - items[7] = { - { - T, + items[7] = { { - { 2 , { T, times, F } } - } - }, - { - F, + T, + { + { 2 , { T, times, F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; + }; - items[8] = { - { - E, + items[8] = { { - { 1 , { E, plus, T } } - } - }, - { - F, + E, + { + { 1 , { E, plus, T } } + } + }, { - { 2 , { leftParenthesis, E, rightParenthesis } }, + F, + { + { 2 , { leftParenthesis, E, rightParenthesis } }, + } } - } - }; + }; - items[9] = { - { - E, + items[9] = { { - { 3 , { E, plus, T } } - } - }, - { - T, + E, + { + { 3 , { E, plus, T } } + } + }, { - { 1 , { T, times, F } } + T, + { + { 1 , { T, times, F } } + } } - } - }; + }; - items[10] = { - { - T, + items[10] = { { - { 3 , { T, times, F } } + T, + { + { 3 , { T, times, F } } + } } - } - }; + }; - items[11] = { - { - F, + items[11] = { { - { 3 , { leftParenthesis, E, rightParenthesis } } + F, + { + { 3 , { leftParenthesis, E, rightParenthesis } } + } } - } - }; + }; - automaton::DFA < ext::variant < DefaultSymbolType, DefaultSymbolType >, grammar::parsing::LR0Items > parsingAutomaton ( items[0] ); - for ( const DefaultSymbolType & nonterminal : augmentedExpressionGrammar.getNonterminalAlphabet ( ) ) - parsingAutomaton.addInputSymbol ( nonterminal ); - for ( const DefaultSymbolType & terminal : augmentedExpressionGrammar.getTerminalAlphabet ( ) ) - parsingAutomaton.addInputSymbol ( terminal ); + automaton::DFA < ext::variant < DefaultSymbolType, DefaultSymbolType >, grammar::parsing::LR0Items > parsingAutomaton ( items[0] ); + for ( const DefaultSymbolType & nonterminal : augmentedExpressionGrammar.getNonterminalAlphabet ( ) ) + parsingAutomaton.addInputSymbol ( nonterminal ); + for ( const DefaultSymbolType & terminal : augmentedExpressionGrammar.getTerminalAlphabet ( ) ) + parsingAutomaton.addInputSymbol ( terminal ); - for (const grammar::parsing::LR0Items & state : items) { - parsingAutomaton.addState(state); - } + for (const grammar::parsing::LR0Items & state : items) { + parsingAutomaton.addState(state); + } - parsingAutomaton.addTransition(items[0], E, items[1]); - parsingAutomaton.addTransition(items[0], T, items[2]); - parsingAutomaton.addTransition(items[0], identifier, items[5]); - parsingAutomaton.addTransition(items[0], leftParenthesis, items[4]); - parsingAutomaton.addTransition(items[0], F, items[3]); + parsingAutomaton.addTransition(items[0], E, items[1]); + parsingAutomaton.addTransition(items[0], T, items[2]); + parsingAutomaton.addTransition(items[0], identifier, items[5]); + parsingAutomaton.addTransition(items[0], leftParenthesis, items[4]); + parsingAutomaton.addTransition(items[0], F, items[3]); - parsingAutomaton.addTransition(items[1], plus, items[6]); + parsingAutomaton.addTransition(items[1], plus, items[6]); - parsingAutomaton.addTransition(items[2], times, items[7]); + parsingAutomaton.addTransition(items[2], times, items[7]); - parsingAutomaton.addTransition(items[4], E, items[8]); - parsingAutomaton.addTransition(items[4], F, items[3]); - parsingAutomaton.addTransition(items[4], T, items[2]); - parsingAutomaton.addTransition(items[4], leftParenthesis, items[4]); - parsingAutomaton.addTransition(items[4], identifier, items[5]); + parsingAutomaton.addTransition(items[4], E, items[8]); + parsingAutomaton.addTransition(items[4], F, items[3]); + parsingAutomaton.addTransition(items[4], T, items[2]); + parsingAutomaton.addTransition(items[4], leftParenthesis, items[4]); + parsingAutomaton.addTransition(items[4], identifier, items[5]); - parsingAutomaton.addTransition(items[6], T, items[9]); - parsingAutomaton.addTransition(items[6], F, items[3]); - parsingAutomaton.addTransition(items[6], leftParenthesis, items[4]); - parsingAutomaton.addTransition(items[6], identifier, items[5]); + parsingAutomaton.addTransition(items[6], T, items[9]); + parsingAutomaton.addTransition(items[6], F, items[3]); + parsingAutomaton.addTransition(items[6], leftParenthesis, items[4]); + parsingAutomaton.addTransition(items[6], identifier, items[5]); - parsingAutomaton.addTransition(items[7], F, items[10]); - parsingAutomaton.addTransition(items[7], leftParenthesis, items[4]); - parsingAutomaton.addTransition(items[7], identifier, items[5]); + parsingAutomaton.addTransition(items[7], F, items[10]); + parsingAutomaton.addTransition(items[7], leftParenthesis, items[4]); + parsingAutomaton.addTransition(items[7], identifier, items[5]); - parsingAutomaton.addTransition(items[8], rightParenthesis, items[11]); - parsingAutomaton.addTransition(items[8], plus, items[6]); + parsingAutomaton.addTransition(items[8], rightParenthesis, items[11]); + parsingAutomaton.addTransition(items[8], plus, items[6]); - parsingAutomaton.addTransition(items[9], times, items[7]); + parsingAutomaton.addTransition(items[9], times, items[7]); - CPPUNIT_ASSERT ( grammar::parsing::LR0Parser::getAutomaton(getExpressionGrammar()) == parsingAutomaton ); + CHECK ( grammar::parsing::LR0Parser::getAutomaton(getExpressionGrammar()) == parsingAutomaton ); + } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/LR0ParserTest.h b/alib2algo_experimental/test-src/grammar/parsing/LR0ParserTest.h deleted file mode 100644 index 7fb008e29a..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/LR0ParserTest.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef LR0_PARSER_TEST_H_ -#define LR0_PARSER_TEST_H_ - -#include "grammar/ContextFree/CFG.h" - -#include <cppunit/extensions/HelperMacros.h> - -class LR0ParserTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( LR0ParserTest ); - CPPUNIT_TEST ( testClosure ); - CPPUNIT_TEST ( testNextStateItems ); - CPPUNIT_TEST ( testAutomaton ); - CPPUNIT_TEST_SUITE_END ( ); - - DefaultSymbolType E = DefaultSymbolType ( 'E' ); - DefaultSymbolType T = DefaultSymbolType ( 'T' ); - DefaultSymbolType F = DefaultSymbolType ( 'F' ); - - DefaultSymbolType plus = DefaultSymbolType ( '+' ); - DefaultSymbolType times = DefaultSymbolType ( '*' ); - DefaultSymbolType leftParenthesis = DefaultSymbolType ( '(' ); - DefaultSymbolType rightParenthesis = DefaultSymbolType ( ')' ); - DefaultSymbolType identifier = DefaultSymbolType ( "id" ); - - grammar::CFG < > getExpressionGrammar ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testClosure ( ); - void testNextStateItems ( ); - void testAutomaton ( ); -}; - -#endif /* LR0_PARSER_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/LRParserTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/LRParserTest.cpp index 1be63b3013..393ccf81da 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LRParserTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/LRParserTest.cpp @@ -1,159 +1,163 @@ -#include "LRParserTest.h" - -#include "grammar/parsing/LRParser.h" -#include "grammar/parsing/SLR1ParseTable.h" +#include <catch2/catch.hpp> #include <alib/vector> -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( LRParserTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( LRParserTest ); +#include "grammar/parsing/LRParser.h" +#include "grammar/parsing/SLR1ParseTable.h" +#include "grammar/ContextFree/CFG.h" -void LRParserTest::setUp ( ) { -} +static DefaultSymbolType E = DefaultSymbolType ( 'E' ); +static DefaultSymbolType T = DefaultSymbolType ( 'T' ); +static DefaultSymbolType F = DefaultSymbolType ( 'F' ); -void LRParserTest::tearDown ( ) { -} +static DefaultSymbolType plus = DefaultSymbolType ( '+' ); +static DefaultSymbolType times = DefaultSymbolType ( '*' ); +static DefaultSymbolType leftParenthesis = DefaultSymbolType ( '(' ); +static DefaultSymbolType rightParenthesis = DefaultSymbolType ( ')' ); +static DefaultSymbolType identifier = DefaultSymbolType ( "id" ); -grammar::CFG < > LRParserTest::getExpressionGrammar() { - grammar::CFG < > expressionGrammar( +static grammar::CFG < > getExpressionGrammar ( ) { + grammar::CFG < > expressionGrammar ( { E, T, F }, { plus, times, leftParenthesis, rightParenthesis, identifier }, E ); - + expressionGrammar.addRule ( E, { E, plus, T } ); expressionGrammar.addRule ( E, { T } ); expressionGrammar.addRule ( T, { T, times, F } ); expressionGrammar.addRule ( T, { F } ); expressionGrammar.addRule ( F, { leftParenthesis, E, rightParenthesis } ); expressionGrammar.addRule ( F, { identifier } ); - + return expressionGrammar; } -void LRParserTest::testEndOfInputSymbol ( ) { - grammar::CFG < > expressionGrammar = getExpressionGrammar ( ); - DefaultSymbolType endOfInput = grammar::parsing::LRParser::getEndOfInputSymbol ( expressionGrammar ); +TEST_CASE ( "LR Parser", "[unit][grammar]" ) { + SECTION ( "Test End of Input Symbol" ) { + grammar::CFG < > expressionGrammar = getExpressionGrammar ( ); + DefaultSymbolType endOfInput = grammar::parsing::LRParser::getEndOfInputSymbol ( expressionGrammar ); - bool correct = true; - if ( expressionGrammar.getTerminalAlphabet ( ) . find ( endOfInput ) != expressionGrammar.getTerminalAlphabet ( ) . end ( ) ) { - correct = false; - } + bool correct = true; + if ( expressionGrammar.getTerminalAlphabet ( ) . find ( endOfInput ) != expressionGrammar.getTerminalAlphabet ( ) . end ( ) ) { + correct = false; + } - if ( expressionGrammar.getNonterminalAlphabet ( ) . find ( endOfInput ) != expressionGrammar.getNonterminalAlphabet ( ) . end ( ) ) { - correct = false; + if ( expressionGrammar.getNonterminalAlphabet ( ) . find ( endOfInput ) != expressionGrammar.getNonterminalAlphabet ( ) . end ( ) ) { + correct = false; + } + + CHECK ( correct ); } - CPPUNIT_ASSERT ( correct ); -} + SECTION ( "Test Parse Correct Input" ) { + grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); -void LRParserTest::testParseCorrectInput ( ) { - grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); - - grammar::parsing::LR0Items initialState { - { - augmentedExpressionGrammar.getInitialSymbol(), + grammar::parsing::LR0Items initialState { { - { 0 , { E } } - } - }, - { - E, + augmentedExpressionGrammar.getInitialSymbol(), + { + { 0 , { E } } + } + }, { - { 0 , { E, plus, T } }, - { 0 , { T } } - } - }, - { - T, + E, + { + { 0 , { E, plus, T } }, + { 0 , { T } } + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; - - ext::vector < DefaultSymbolType > correctInput { - leftParenthesis, - identifier, - plus, - leftParenthesis, - identifier, - times, - identifier , - rightParenthesis, - rightParenthesis, - grammar::parsing::LRParser::getEndOfInputSymbol ( grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ) ) - }; - - bool parsingResult = grammar::parsing::LRParser::parse( - grammar::parsing::SLR1ParseTable::getActionTable ( getExpressionGrammar ( ) ), - grammar::parsing::SLR1ParseTable::getGotoTable ( getExpressionGrammar ( ) ), - initialState, - correctInput - ); - - CPPUNIT_ASSERT ( parsingResult == true ); -} + }; + + ext::vector < DefaultSymbolType > correctInput { + leftParenthesis, + identifier, + plus, + leftParenthesis, + identifier, + times, + identifier , + rightParenthesis, + rightParenthesis, + grammar::parsing::LRParser::getEndOfInputSymbol ( grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ) ) + }; + + bool parsingResult = grammar::parsing::LRParser::parse ( + grammar::parsing::SLR1ParseTable::getActionTable ( getExpressionGrammar ( ) ), + grammar::parsing::SLR1ParseTable::getGotoTable ( getExpressionGrammar ( ) ), + initialState, + correctInput + ); + + CHECK ( parsingResult == true ); + } -void LRParserTest::testParseIncorrectInput ( ) { - grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); + SECTION ( "Test Parse Incorrect Input" ) { + grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); - grammar::parsing::LR0Items initialState { - { - augmentedExpressionGrammar.getInitialSymbol(), + grammar::parsing::LR0Items initialState { { - { 0 , { E } } - } - }, - { - E, + augmentedExpressionGrammar.getInitialSymbol(), + { + { 0 , { E } } + } + }, { - { 0 , { E, plus, T } }, - { 0 , { T } } - } - }, - { - T, + E, + { + { 0 , { E, plus, T } }, + { 0 , { T } } + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; - - ext::vector < DefaultSymbolType > incorrectInput { - leftParenthesis, - identifier, - plus, - leftParenthesis, - identifier, - times, - identifier , - rightParenthesis, - grammar::parsing::LRParser::getEndOfInputSymbol ( grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ) ) - }; - - bool parsingResult = grammar::parsing::LRParser::parse( - grammar::parsing::SLR1ParseTable::getActionTable ( getExpressionGrammar ( ) ), - grammar::parsing::SLR1ParseTable::getGotoTable ( getExpressionGrammar ( ) ), - initialState, - incorrectInput - ); - - CPPUNIT_ASSERT ( parsingResult == false ); + }; + + ext::vector < DefaultSymbolType > incorrectInput { + leftParenthesis, + identifier, + plus, + leftParenthesis, + identifier, + times, + identifier , + rightParenthesis, + grammar::parsing::LRParser::getEndOfInputSymbol ( grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ) ) + }; + + bool parsingResult = grammar::parsing::LRParser::parse( + grammar::parsing::SLR1ParseTable::getActionTable ( getExpressionGrammar ( ) ), + grammar::parsing::SLR1ParseTable::getGotoTable ( getExpressionGrammar ( ) ), + initialState, + incorrectInput + ); + + CHECK ( parsingResult == false ); + } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/LRParserTest.h b/alib2algo_experimental/test-src/grammar/parsing/LRParserTest.h deleted file mode 100644 index 0f89940b1e..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/LRParserTest.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef LR_PARSER_TEST_H_ -#define LR_PARSER_TEST_H_ - -#include "grammar/ContextFree/CFG.h" - -#include <cppunit/extensions/HelperMacros.h> - -class LRParserTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( LRParserTest ); - CPPUNIT_TEST ( testEndOfInputSymbol ); - CPPUNIT_TEST ( testParseCorrectInput ); - CPPUNIT_TEST ( testParseIncorrectInput ); - CPPUNIT_TEST_SUITE_END ( ); - - DefaultSymbolType E = DefaultSymbolType ( 'E' ); - DefaultSymbolType T = DefaultSymbolType ( 'T' ); - DefaultSymbolType F = DefaultSymbolType ( 'F' ); - - DefaultSymbolType plus = DefaultSymbolType ( '+' ); - DefaultSymbolType times = DefaultSymbolType ( '*' ); - DefaultSymbolType leftParenthesis = DefaultSymbolType ( '(' ); - DefaultSymbolType rightParenthesis = DefaultSymbolType ( ')' ); - DefaultSymbolType identifier = DefaultSymbolType ( "id" ); - - grammar::CFG < > getExpressionGrammar ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testEndOfInputSymbol ( ); - void testParseCorrectInput ( ); - void testParseIncorrectInput ( ); -}; - -#endif /* LR_PARSER_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/LeftFactorizeTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/LeftFactorizeTest.cpp index d5daecc13c..920e67a5a4 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LeftFactorizeTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/LeftFactorizeTest.cpp @@ -1,124 +1,112 @@ -#include "LeftFactorizeTest.h" +#include <catch2/catch.hpp> #include "grammar/ContextFree/CFG.h" #include "grammar/parsing/LeftFactorize.h" -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( LeftFactorizeTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( LeftFactorizeTest ); - -void LeftFactorizeTest::setUp ( ) { -} - -void LeftFactorizeTest::tearDown ( ) { -} - -void LeftFactorizeTest::testLeftFactorize ( ) { - DefaultSymbolType S = DefaultSymbolType ( "S" ); - DefaultSymbolType Sp = DefaultSymbolType ( object::AnyObject < std::string > ( "S", 1 ) ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType Bp = DefaultSymbolType ( object::AnyObject < std::string > ( "B", 1 ) ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - DefaultSymbolType Cp = DefaultSymbolType ( object::AnyObject < std::string > ( "C", 1 ) ); - - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - - grammar::CFG < > grammar ( S ); - - grammar.setTerminalAlphabet ( { a, b, c } ); - grammar.setNonterminalAlphabet ( { S, B, C } ); - grammar.setInitialSymbol ( S ); - - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c } ); - - 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 } ); - comp.setNonterminalAlphabet ( { S, Sp, B, Bp, C, Cp } ); - comp.setInitialSymbol ( S ); - - comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Sp } ); - comp.addRule ( Sp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); - comp.addRule ( Sp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, Bp } ); - comp.addRule ( Bp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); - comp.addRule ( Bp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, Cp } ); - comp.addRule ( Cp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C } ); - comp.addRule ( Cp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - -// std::cout << res << std::endl << comp << std::endl; - - CPPUNIT_ASSERT ( res == comp ); -} - -void LeftFactorizeTest::testLeftFactorize2 ( ) { - DefaultSymbolType S = DefaultSymbolType ( "S" ); - DefaultSymbolType Sp = DefaultSymbolType ( object::AnyObject < std::string > ( "S", 1 ) ); - DefaultSymbolType B = DefaultSymbolType ( "B" ); - DefaultSymbolType Bp = DefaultSymbolType ( object::AnyObject < std::string > ( "B", 1 ) ); - DefaultSymbolType C = DefaultSymbolType ( "C" ); - DefaultSymbolType Cp = DefaultSymbolType ( object::AnyObject < std::string > ( "C", 1 ) ); - DefaultSymbolType D = DefaultSymbolType ( "D" ); - - DefaultSymbolType a = DefaultSymbolType ( 'a' ); - DefaultSymbolType b = DefaultSymbolType ( 'b' ); - DefaultSymbolType c = DefaultSymbolType ( 'c' ); - DefaultSymbolType d = DefaultSymbolType ( 'd' ); - - grammar::CFG < > grammar ( S ); - - grammar.setTerminalAlphabet ( { a, b, c, d } ); - grammar.setNonterminalAlphabet ( { S, B, C, D } ); - grammar.setInitialSymbol ( S ); - - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); - grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { D } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); - grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c } ); - grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b } ); - grammar.addRule ( D, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { 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, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Sp } ); - comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { D } ); - comp.addRule ( Sp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); - comp.addRule ( Sp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C } ); - comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, Bp } ); - comp.addRule ( Bp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); - comp.addRule ( Bp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, Cp } ); - comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b } ); - comp.addRule ( Cp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C } ); - comp.addRule ( Cp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); - comp.addRule ( D, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); - -// std::cout << res << std::endl << comp << std::endl; - - CPPUNIT_ASSERT ( res == comp ); +TEST_CASE ( "Left Factorize", "[unit][grammar]" ) { + SECTION ( "Test 1" ) { + DefaultSymbolType S = DefaultSymbolType ( "S" ); + DefaultSymbolType Sp = DefaultSymbolType ( object::AnyObject < std::string > ( "S", 1 ) ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType Bp = DefaultSymbolType ( object::AnyObject < std::string > ( "B", 1 ) ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + DefaultSymbolType Cp = DefaultSymbolType ( object::AnyObject < std::string > ( "C", 1 ) ); + + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + + grammar::CFG < > grammar ( S ); + + grammar.setTerminalAlphabet ( { a, b, c } ); + grammar.setNonterminalAlphabet ( { S, B, C } ); + grammar.setInitialSymbol ( S ); + + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c } ); + + 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 } ); + comp.setNonterminalAlphabet ( { S, Sp, B, Bp, C, Cp } ); + comp.setInitialSymbol ( S ); + + comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Sp } ); + comp.addRule ( Sp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); + comp.addRule ( Sp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, Bp } ); + comp.addRule ( Bp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); + comp.addRule ( Bp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, Cp } ); + comp.addRule ( Cp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C } ); + comp.addRule ( Cp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + + CHECK ( res == comp ); + } + + SECTION ( "Test 2" ) { + DefaultSymbolType S = DefaultSymbolType ( "S" ); + DefaultSymbolType Sp = DefaultSymbolType ( object::AnyObject < std::string > ( "S", 1 ) ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType Bp = DefaultSymbolType ( object::AnyObject < std::string > ( "B", 1 ) ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + DefaultSymbolType Cp = DefaultSymbolType ( object::AnyObject < std::string > ( "C", 1 ) ); + DefaultSymbolType D = DefaultSymbolType ( "D" ); + + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); + + grammar::CFG < > grammar ( S ); + + grammar.setTerminalAlphabet ( { a, b, c, d } ); + grammar.setNonterminalAlphabet ( { S, B, C, D } ); + grammar.setInitialSymbol ( S ); + + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, B } ); + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, C } ); + grammar.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { D } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, B } ); + grammar.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, C } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c } ); + grammar.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b } ); + grammar.addRule ( D, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { 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, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { a, Sp } ); + comp.addRule ( S, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { D } ); + comp.addRule ( Sp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); + comp.addRule ( Sp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C } ); + comp.addRule ( B, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b, Bp } ); + comp.addRule ( Bp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { B } ); + comp.addRule ( Bp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { c, Cp } ); + comp.addRule ( C, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { b } ); + comp.addRule ( Cp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { C } ); + comp.addRule ( Cp, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { } ); + comp.addRule ( D, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { d } ); + + CHECK ( res == comp ); + } } - diff --git a/alib2algo_experimental/test-src/grammar/parsing/LeftFactorizeTest.h b/alib2algo_experimental/test-src/grammar/parsing/LeftFactorizeTest.h deleted file mode 100644 index 0673e6138d..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/LeftFactorizeTest.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef LEFT_FACTORIZE_TEST_H_ -#define LEFT_FACTORIZE_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class LeftFactorizeTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( LeftFactorizeTest ); - CPPUNIT_TEST ( testLeftFactorize ); - CPPUNIT_TEST ( testLeftFactorize2 ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testLeftFactorize ( ); - void testLeftFactorize2 ( ); -}; - -#endif /* LEFT_FACTORIZE_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTableTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTableTest.cpp index d516f48672..efa6b6d88d 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTableTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTableTest.cpp @@ -1,558 +1,561 @@ -#include "SLR1ParseTableTest.h" - -#include "grammar/parsing/LRParser.h" -#include "grammar/parsing/SLR1ParseTable.h" +#include <catch2/catch.hpp> #include <alib/variant> #include <alib/vector> -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( SLR1ParseTableTest, "grammar" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( SLR1ParseTableTest ); +#include "grammar/parsing/LRParser.h" +#include "grammar/parsing/SLR1ParseTable.h" -void SLR1ParseTableTest::setUp ( ) { -} +static DefaultSymbolType E = DefaultSymbolType ( 'E' ); +static DefaultSymbolType T = DefaultSymbolType ( 'T' ); +static DefaultSymbolType F = DefaultSymbolType ( 'F' ); -void SLR1ParseTableTest::tearDown ( ) { -} +static DefaultSymbolType plus = DefaultSymbolType ( '+' ); +static DefaultSymbolType times = DefaultSymbolType ( '*' ); +static DefaultSymbolType leftParenthesis = DefaultSymbolType ( '(' ); +static DefaultSymbolType rightParenthesis = DefaultSymbolType ( ')' ); +static DefaultSymbolType identifier = DefaultSymbolType ( "id" ); -grammar::CFG < > SLR1ParseTableTest::getExpressionGrammar() { +static grammar::CFG < > getExpressionGrammar ( ) { grammar::CFG < > expressionGrammar( { E, T, F }, { plus, times, leftParenthesis, rightParenthesis, identifier }, E ); - + expressionGrammar.addRule ( E, { E, plus, T } ); expressionGrammar.addRule ( E, { T } ); expressionGrammar.addRule ( T, { T, times, F } ); expressionGrammar.addRule ( T, { F } ); expressionGrammar.addRule ( F, { leftParenthesis, E, rightParenthesis } ); expressionGrammar.addRule ( F, { identifier } ); - + return expressionGrammar; } -void SLR1ParseTableTest::testActionTable ( ) { - ext::vector < grammar::parsing::LR0Items > items(12); - - grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); - - items[0] = { - { - augmentedExpressionGrammar.getInitialSymbol(), +TEST_CASE ( "SLR1 Parse Table", "[unit][grammar]" ) { + SECTION ( "Test Action Table" ) { + ext::vector < grammar::parsing::LR0Items > items(12); + + grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); + + items[0] = { { - { 0 , { E } } - } - }, - { - E, + augmentedExpressionGrammar.getInitialSymbol(), + { + { 0 , { E } } + } + }, { - { 0 , { E, plus, T } }, - { 0 , { T } } - } - }, - { - T, + E, + { + { 0 , { E, plus, T } }, + { 0 , { T } } + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; - - items[1] = { - { - augmentedExpressionGrammar.getInitialSymbol(), + }; + + items[1] = { { - { 1 , { E } } - } - }, - { - E, + augmentedExpressionGrammar.getInitialSymbol(), + { + { 1 , { E } } + } + }, { - { 1 , { E, plus, T } } + E, + { + { 1 , { E, plus, T } } + } } - } - }; + }; - items[2] = { - { - E, + items[2] = { { - { 1 , { T } } - } - }, - { - T, + E, + { + { 1 , { T } } + } + }, { - { 1 , { T, times, F } }, + T, + { + { 1 , { T, times, F } }, + } } - } - }; + }; - items[3] = { - { - T, + items[3] = { { - { 1 , { F } } + T, + { + { 1 , { F } } + } } - } - }; + }; - items[4] = { - { - E, + items[4] = { { - { 0 , { E, plus, T } }, - { 0 , { T } } - } - }, - { - T, + E, + { + { 0 , { E, plus, T } }, + { 0 , { T } } + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 1 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 1 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; + }; - items[5] = { - { - F, + items[5] = { { - { 1 , { identifier } } + F, + { + { 1 , { identifier } } + } } - } - }; - - items[6] = { - { - E, + }; + + items[6] = { { - { 2 , { E, plus, T } }, - } - }, - { - T, + E, + { + { 2 , { E, plus, T } }, + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; - - items[7] = { - { - T, + }; + + items[7] = { { - { 2 , { T, times, F } } - } - }, - { - F, + T, + { + { 2 , { T, times, F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; - - items[8] = { - { - E, + }; + + items[8] = { { - { 1 , { E, plus, T } } - } - }, - { - F, + E, + { + { 1 , { E, plus, T } } + } + }, { - { 2 , { leftParenthesis, E, rightParenthesis } }, + F, + { + { 2 , { leftParenthesis, E, rightParenthesis } }, + } } - } - }; - - items[9] = { - { - E, + }; + + items[9] = { { - { 3 , { E, plus, T } } - } - }, - { - T, + E, + { + { 3 , { E, plus, T } } + } + }, { - { 1 , { T, times, F } } + T, + { + { 1 , { T, times, F } } + } } - } - }; - - items[10] = { - { - T, + }; + + items[10] = { { - { 3 , { T, times, F } } + T, + { + { 3 , { T, times, F } } + } } - } - }; - - items[11] = { - { - F, + }; + + items[11] = { { - { 3 , { leftParenthesis, E, rightParenthesis } } + F, + { + { 3 , { leftParenthesis, E, rightParenthesis } } + } } - } - }; - - DefaultSymbolType endOfInput = grammar::parsing::LRParser::getEndOfInputSymbol ( getExpressionGrammar ( ) ); - grammar::parsing::LRActionTable correctActionTable { - { { items[0], identifier }, { grammar::parsing::LRAction::Shift, items[5] } }, - { { items[0], leftParenthesis }, { grammar::parsing::LRAction::Shift, items[4] } }, - { { items[1], plus }, { grammar::parsing::LRAction::Shift, items[6] } }, - { { items[1], endOfInput }, { grammar::parsing::LRAction::Accept, items[0] } }, - { { items[2], times }, { grammar::parsing::LRAction::Shift, items[7] } }, - { { items[4], identifier }, { grammar::parsing::LRAction::Shift, items[5] } }, - { { items[4], leftParenthesis }, { grammar::parsing::LRAction::Shift, items[4] } }, - { { items[6], identifier }, { grammar::parsing::LRAction::Shift, items[5] } }, - { { items[6], leftParenthesis }, { grammar::parsing::LRAction::Shift, items[4] } }, - { { items[7], identifier }, { grammar::parsing::LRAction::Shift, items[5] } }, - { { items[7], leftParenthesis }, { grammar::parsing::LRAction::Shift, items[4] } }, - { { items[8], plus }, { grammar::parsing::LRAction::Shift, items[6] } }, - { { items[8], rightParenthesis }, { grammar::parsing::LRAction::Shift, items[11] } }, - { { items[9], times }, { grammar::parsing::LRAction::Shift, items[7] } } - }; - - ext::pair < DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > reduceBy = ext::make_pair ( E, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { T } ); - correctActionTable.insert ( { { items[2], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[2], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[2], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - - reduceBy = ext::make_pair ( T, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { F } ); - correctActionTable.insert ( { { items[3], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[3], times }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[3], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[3], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - - reduceBy = ext::make_pair ( F, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { identifier } ); - correctActionTable.insert ( { { items[5], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[5], times }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[5], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[5], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - - reduceBy = ext::make_pair ( E, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { E, plus, T } ); - correctActionTable.insert ( { { items[9], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[9], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[9], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - - reduceBy = ext::make_pair ( T, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { T, times, F } ); - correctActionTable.insert ( { { items[10], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[10], times }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[10], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[10], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - - reduceBy = ext::make_pair ( F, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { leftParenthesis, E, rightParenthesis } ); - correctActionTable.insert ( { { items[11], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[11], times }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[11], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - correctActionTable.insert ( { { items[11], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); - - grammar::parsing::LRActionTable actionTable = grammar::parsing::SLR1ParseTable::getActionTable ( getExpressionGrammar ( ) ); - - bool correct = true; - for ( const grammar::parsing::LRActionTable::value_type & actionTablePair : correctActionTable ) { - grammar::parsing::LRActionTable::iterator value = actionTable.find ( actionTablePair.first ); - if ( value == actionTable.end ( ) ) { - correct = false; - break; - } + }; - if ( actionTablePair.second.first != value->second.first ) { - correct = false; - break; - } - - bool correctContent = true; - switch ( actionTablePair.second.first ) { - case grammar::parsing::LRAction::Shift: - if ( actionTablePair.second.second.get < grammar::parsing::LR0Items > ( ) != value->second.second.get < grammar::parsing::LR0Items > ( ) ) { - correctContent = false; - } + DefaultSymbolType endOfInput = grammar::parsing::LRParser::getEndOfInputSymbol ( getExpressionGrammar ( ) ); + grammar::parsing::LRActionTable correctActionTable { + { { items[0], identifier }, { grammar::parsing::LRAction::Shift, items[5] } }, + { { items[0], leftParenthesis }, { grammar::parsing::LRAction::Shift, items[4] } }, + { { items[1], plus }, { grammar::parsing::LRAction::Shift, items[6] } }, + { { items[1], endOfInput }, { grammar::parsing::LRAction::Accept, items[0] } }, + { { items[2], times }, { grammar::parsing::LRAction::Shift, items[7] } }, + { { items[4], identifier }, { grammar::parsing::LRAction::Shift, items[5] } }, + { { items[4], leftParenthesis }, { grammar::parsing::LRAction::Shift, items[4] } }, + { { items[6], identifier }, { grammar::parsing::LRAction::Shift, items[5] } }, + { { items[6], leftParenthesis }, { grammar::parsing::LRAction::Shift, items[4] } }, + { { items[7], identifier }, { grammar::parsing::LRAction::Shift, items[5] } }, + { { items[7], leftParenthesis }, { grammar::parsing::LRAction::Shift, items[4] } }, + { { items[8], plus }, { grammar::parsing::LRAction::Shift, items[6] } }, + { { items[8], rightParenthesis }, { grammar::parsing::LRAction::Shift, items[11] } }, + { { items[9], times }, { grammar::parsing::LRAction::Shift, items[7] } } + }; + + ext::pair < DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > reduceBy = ext::make_pair ( E, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { T } ); + correctActionTable.insert ( { { items[2], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[2], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[2], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + + reduceBy = ext::make_pair ( T, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { F } ); + correctActionTable.insert ( { { items[3], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[3], times }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[3], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[3], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + + reduceBy = ext::make_pair ( F, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { identifier } ); + correctActionTable.insert ( { { items[5], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[5], times }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[5], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[5], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + + reduceBy = ext::make_pair ( E, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { E, plus, T } ); + correctActionTable.insert ( { { items[9], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[9], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[9], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + + reduceBy = ext::make_pair ( T, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { T, times, F } ); + correctActionTable.insert ( { { items[10], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[10], times }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[10], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[10], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + + reduceBy = ext::make_pair ( F, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > { leftParenthesis, E, rightParenthesis } ); + correctActionTable.insert ( { { items[11], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[11], times }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[11], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + correctActionTable.insert ( { { items[11], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); + + grammar::parsing::LRActionTable actionTable = grammar::parsing::SLR1ParseTable::getActionTable ( getExpressionGrammar ( ) ); + + bool correct = true; + for ( const grammar::parsing::LRActionTable::value_type & actionTablePair : correctActionTable ) { + grammar::parsing::LRActionTable::iterator value = actionTable.find ( actionTablePair.first ); + if ( value == actionTable.end ( ) ) { + correct = false; break; + } - case grammar::parsing::LRAction::Reduce: - if ( actionTablePair.second.second.get < ext::pair < DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > ( ) != value->second.second.get < ext::pair < DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > ( ) ) { - correctContent = false; - } + if ( actionTablePair.second.first != value->second.first ) { + correct = false; break; + } - case grammar::parsing::LRAction::Accept: + bool correctContent = true; + switch ( actionTablePair.second.first ) { + case grammar::parsing::LRAction::Shift: + if ( actionTablePair.second.second.get < grammar::parsing::LR0Items > ( ) != value->second.second.get < grammar::parsing::LR0Items > ( ) ) { + correctContent = false; + } + break; + + case grammar::parsing::LRAction::Reduce: + if ( actionTablePair.second.second.get < ext::pair < DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > ( ) != value->second.second.get < ext::pair < DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > ( ) ) { + correctContent = false; + } + break; + + case grammar::parsing::LRAction::Accept: + break; + } + + if ( !correctContent ) { + correct = false; break; + } } - - if ( !correctContent ) { + + if (actionTable.size() != correctActionTable.size()) { correct = false; - break; } + + CHECK ( correct ); } - - if (actionTable.size() != correctActionTable.size()) { - correct = false; - } - - CPPUNIT_ASSERT ( correct ); -} -void SLR1ParseTableTest::testGotoTable ( ) { - ext::vector < grammar::parsing::LR0Items > items(12); - - grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); - - items[0] = { - { - augmentedExpressionGrammar.getInitialSymbol(), + SECTION ( "Test Goto table" ) { + ext::vector < grammar::parsing::LR0Items > items(12); + + grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); + + items[0] = { { - { 0 , { E } } - } - }, - { - E, + augmentedExpressionGrammar.getInitialSymbol(), + { + { 0 , { E } } + } + }, { - { 0 , { E, plus, T } }, - { 0 , { T } } - } - }, - { - T, + E, + { + { 0 , { E, plus, T } }, + { 0 , { T } } + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; - - items[1] = { - { - augmentedExpressionGrammar.getInitialSymbol(), + }; + + items[1] = { { - { 1 , { E } } - } - }, - { - E, + augmentedExpressionGrammar.getInitialSymbol(), + { + { 1 , { E } } + } + }, { - { 1 , { E, plus, T } } + E, + { + { 1 , { E, plus, T } } + } } - } - }; + }; - items[2] = { - { - E, + items[2] = { { - { 1 , { T } } - } - }, - { - T, + E, + { + { 1 , { T } } + } + }, { - { 1 , { T, times, F } }, + T, + { + { 1 , { T, times, F } }, + } } - } - }; + }; - items[3] = { - { - T, + items[3] = { { - { 1 , { F } } + T, + { + { 1 , { F } } + } } - } - }; + }; - items[4] = { - { - E, + items[4] = { { - { 0 , { E, plus, T } }, - { 0 , { T } } - } - }, - { - T, + E, + { + { 0 , { E, plus, T } }, + { 0 , { T } } + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 1 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 1 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; + }; - items[5] = { - { - F, + items[5] = { { - { 1 , { identifier } } + F, + { + { 1 , { identifier } } + } } - } - }; - - items[6] = { - { - E, + }; + + items[6] = { { - { 2 , { E, plus, T } }, - } - }, - { - T, + E, + { + { 2 , { E, plus, T } }, + } + }, { - { 0 , { T, times, F } }, - { 0 , { F } } - } - }, - { - F, + T, + { + { 0 , { T, times, F } }, + { 0 , { F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; - - items[7] = { - { - T, + }; + + items[7] = { { - { 2 , { T, times, F } } - } - }, - { - F, + T, + { + { 2 , { T, times, F } } + } + }, { - { 0 , { leftParenthesis, E, rightParenthesis } }, - { 0 , { identifier } } + F, + { + { 0 , { leftParenthesis, E, rightParenthesis } }, + { 0 , { identifier } } + } } - } - }; - - items[8] = { - { - E, + }; + + items[8] = { { - { 1 , { E, plus, T } } - } - }, - { - F, + E, + { + { 1 , { E, plus, T } } + } + }, { - { 2 , { leftParenthesis, E, rightParenthesis } }, + F, + { + { 2 , { leftParenthesis, E, rightParenthesis } }, + } } - } - }; - - items[9] = { - { - E, + }; + + items[9] = { { - { 3 , { E, plus, T } } - } - }, - { - T, + E, + { + { 3 , { E, plus, T } } + } + }, { - { 1 , { T, times, F } } + T, + { + { 1 , { T, times, F } } + } } - } - }; - - items[10] = { - { - T, + }; + + items[10] = { { - { 3 , { T, times, F } } + T, + { + { 3 , { T, times, F } } + } } - } - }; - - items[11] = { - { - F, + }; + + items[11] = { { - { 3 , { leftParenthesis, E, rightParenthesis } } + F, + { + { 3 , { leftParenthesis, E, rightParenthesis } } + } } - } - }; - - grammar::parsing::LRGotoTable gotoTable { - { { items[0], E }, items[1] }, - { { items[0], T }, items[2] }, - { { items[0], F }, items[3] }, - { { items[4], E }, items[8] }, - { { items[4], T }, items[2] }, - { { items[4], F }, items[3] }, - { { items[6], T }, items[9] }, - { { items[6], F }, items[3] }, - { { items[7], F }, items[10] } - }; - - CPPUNIT_ASSERT ( grammar::parsing::SLR1ParseTable::getGotoTable ( getExpressionGrammar ( ) ) == gotoTable ); -} + }; -void SLR1ParseTableTest::testUnambiguousNotSLR1Grammar ( ) { - DefaultSymbolType S = DefaultSymbolType ( 'S' ); - DefaultSymbolType L = DefaultSymbolType ( 'L' ); - DefaultSymbolType R = DefaultSymbolType ( 'R' ); + grammar::parsing::LRGotoTable gotoTable { + { { items[0], E }, items[1] }, + { { items[0], T }, items[2] }, + { { items[0], F }, items[3] }, + { { items[4], E }, items[8] }, + { { items[4], T }, items[2] }, + { { items[4], F }, items[3] }, + { { items[6], T }, items[9] }, + { { items[6], F }, items[3] }, + { { items[7], F }, items[10] } + }; - DefaultSymbolType assignment = DefaultSymbolType ( '=' ); + CHECK ( grammar::parsing::SLR1ParseTable::getGotoTable ( getExpressionGrammar ( ) ) == gotoTable ); + } - grammar::CFG < > unambiguousNotSLR1Grammar( - { S, L, R }, - { assignment, times, identifier }, - S - ); + SECTION ( "Test Unambiguous Not SLR1 Grammar" ) { + DefaultSymbolType S = DefaultSymbolType ( 'S' ); + DefaultSymbolType L = DefaultSymbolType ( 'L' ); + DefaultSymbolType R = DefaultSymbolType ( 'R' ); + + DefaultSymbolType assignment = DefaultSymbolType ( '=' ); - unambiguousNotSLR1Grammar.addRule ( S, { L, assignment, R } ); - unambiguousNotSLR1Grammar.addRule ( S, { R } ); - unambiguousNotSLR1Grammar.addRule ( L, { times, R } ); - unambiguousNotSLR1Grammar.addRule ( L, { identifier } ); - unambiguousNotSLR1Grammar.addRule ( R, { L } ); + grammar::CFG < > unambiguousNotSLR1Grammar( + { S, L, R }, + { assignment, times, identifier }, + S + ); - CPPUNIT_ASSERT_THROW ( grammar::parsing::SLR1ParseTable::getActionTable ( unambiguousNotSLR1Grammar ), exception::CommonException ); + unambiguousNotSLR1Grammar.addRule ( S, { L, assignment, R } ); + unambiguousNotSLR1Grammar.addRule ( S, { R } ); + unambiguousNotSLR1Grammar.addRule ( L, { times, R } ); + unambiguousNotSLR1Grammar.addRule ( L, { identifier } ); + unambiguousNotSLR1Grammar.addRule ( R, { L } ); + + CHECK_THROWS_AS ( grammar::parsing::SLR1ParseTable::getActionTable ( unambiguousNotSLR1Grammar ), exception::CommonException ); + } } diff --git a/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTableTest.h b/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTableTest.h deleted file mode 100644 index 2f3dee17eb..0000000000 --- a/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTableTest.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef SLR1_PARSE_TABLE_TEST_H_ -#define SLR1_PARSE_TABLE_TEST_H_ - -#include "grammar/ContextFree/CFG.h" - -#include <cppunit/extensions/HelperMacros.h> - -class SLR1ParseTableTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( SLR1ParseTableTest ); - CPPUNIT_TEST ( testActionTable ); - CPPUNIT_TEST ( testGotoTable ); - CPPUNIT_TEST ( testUnambiguousNotSLR1Grammar ); - CPPUNIT_TEST_SUITE_END ( ); - - DefaultSymbolType E = DefaultSymbolType ( 'E' ); - DefaultSymbolType T = DefaultSymbolType ( 'T' ); - DefaultSymbolType F = DefaultSymbolType ( 'F' ); - - DefaultSymbolType plus = DefaultSymbolType ( '+' ); - DefaultSymbolType times = DefaultSymbolType ( '*' ); - DefaultSymbolType leftParenthesis = DefaultSymbolType ( '(' ); - DefaultSymbolType rightParenthesis = DefaultSymbolType ( ')' ); - DefaultSymbolType identifier = DefaultSymbolType ( "id" ); - - grammar::CFG < > getExpressionGrammar ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testActionTable ( ); - void testGotoTable ( ); - void testUnambiguousNotSLR1Grammar ( ); -}; - -#endif /* SLR1_PARSE_TABLE_TEST_H_ */ diff --git a/alib2algo_experimental/test-src/main.cpp b/alib2algo_experimental/test-src/main.cpp index adb58324a1..4ed06df1f7 100644 --- a/alib2algo_experimental/test-src/main.cpp +++ b/alib2algo_experimental/test-src/main.cpp @@ -1,166 +1,2 @@ -#include <version.hpp> - -#include <tclap/CmdLine.h> - -#include <cppunit/CompilerOutputter.h> -#include <cppunit/extensions/TestFactoryRegistry.h> -#include <cppunit/ui/text/TestRunner.h> -#include <cppunit/TestResultCollector.h> -#include <cppunit/TestResult.h> -#include <cppunit/XmlOutputter.h> - -#include <cppunit/Test.h> -#include <cppunit/TestFailure.h> -#include <cppunit/portability/Stream.h> -#include <cppunit/TestListener.h> -#include <cppunit/SourceLine.h> -#include <cppunit/Exception.h> - -#include <exception/CommonException.h> - -CPPUNIT_NS_BEGIN - -class CPPUNIT_API TestProgressListener : public TestListener -{ -public: - TestProgressListener(); - - virtual ~TestProgressListener(); - - void startTest( Test *test ); - - void addFailure( const TestFailure &failure ); - - void endTest( Test *test ); - - int getResult() const; - - void printResults() const; - -private: - TestProgressListener( const TestProgressListener © ); - - void operator =( const TestProgressListener © ); - -private: - int m_Failures; - int m_Tests; - int m_Assertions; - bool m_lastTestFailed; -}; - -TestProgressListener::TestProgressListener() : m_Failures( 0 ), m_Tests(0), m_Assertions(0), m_lastTestFailed( false ) -{ -} - -TestProgressListener::~TestProgressListener() -{ -} - -void TestProgressListener::startTest( Test * test ) -{ - stdCOut() << test->getName() << ":" << "\n"; - stdCOut().flush(); - - m_lastTestFailed = false; - m_Tests++; -} - -void TestProgressListener::addFailure( const TestFailure &failure ) -{ - stdCOut() << (failure.isError() ? "error" : "assertion") << " : " << failure.failedTestName() << " : " << failure.sourceLine().lineNumber() << "\n"; - stdCOut() << "Exception " << failure.thrownException()->message().details(); - - m_lastTestFailed = true; - if(failure.isError()) m_Failures++; else m_Assertions++; -} - -void TestProgressListener::endTest( Test * test) -{ - stdCOut() << "Result (" << test->getName() << ")"; - stdCOut().flush(); - - if ( !m_lastTestFailed ) - stdCOut() << " : OK"; - else - stdCOut() << " : Fail"; - stdCOut() << "\n\n"; -} - -int TestProgressListener::getResult() const { - return m_Failures + m_Assertions; -} - -void TestProgressListener::printResults() const { - stdCOut() << "Overal result: Tests: " << m_Tests << " Assertions: " << m_Assertions << " Failures: " << m_Failures << "\n"; -} - -CPPUNIT_NS_END - -int main(int argc, char* argv[]) { - try { - TCLAP::CmdLine cmd("Main test binary.", ' ', ALIB_VERSION_INFO); - - TCLAP::MultiArg<std::string> testPathSegments("p", "path", "test path", false, "string" ); - cmd.add( testPathSegments ); - - cmd.parse(argc, argv); - - CppUnit::TestResult controller; - - CppUnit::TestResultCollector result; - controller.addListener( &result ); - - CppUnit::TestProgressListener progressListener; - controller.addListener( &progressListener ); - - CppUnit::Test *suite = NULL; - std::string testPath = ""; - if(testPathSegments.getValue().size() == 0) { - // Get the top level suite from the registry - suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); - } else if(testPathSegments.getValue().size() == 1) { - suite = CppUnit::TestFactoryRegistry::getRegistry(testPathSegments.getValue()[0]).makeTest(); - } else { - suite = CppUnit::TestFactoryRegistry::getRegistry(testPathSegments.getValue()[0]).makeTest(); - bool first = true; - for(const std::string& path : testPathSegments.getValue()) { - if(first) { - first = false; - continue; - } - testPath += path + "/"; - } - testPath.pop_back(); - } - - // Adds the test to the list of test to run - CppUnit::TextUi::TestRunner runner; - runner.addTest( suite ); - - // Change the default outputter to a compiler error format outputter - runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), std::cerr ) ); - // Run the tests. - runner.run( controller, testPath ); - - progressListener.printResults(); - - std::ofstream xmlFileResults("CppUnitTestResults.xml"); - CppUnit::XmlOutputter xmlOut(&result, xmlFileResults); - xmlOut.write(); - - return progressListener.getResult(); - } catch(const exception::CommonException& exception) { - std::cerr << exception.getCause() << std::endl; - return 1; - } catch(const TCLAP::ArgException& exception) { - std::cerr << exception.error() << std::endl; - return 2; - } catch (const std::exception& exception) { - std::cerr << "Exception caught: " << exception.what() << std::endl; - return 3; - } catch(...) { - std::cerr << "Unknown exception caught." << std::endl; - return 127; - } -} +#define CATCH_CONFIG_MAIN +#include <catch2/catch.hpp> diff --git a/alib2algo_experimental/test-src/stringology/query/CompactSuffixAutomatonQueryTest.cpp b/alib2algo_experimental/test-src/stringology/query/CompactSuffixAutomatonQueryTest.cpp index c4378aa609..8fdd55119d 100644 --- a/alib2algo_experimental/test-src/stringology/query/CompactSuffixAutomatonQueryTest.cpp +++ b/alib2algo_experimental/test-src/stringology/query/CompactSuffixAutomatonQueryTest.cpp @@ -1,4 +1,4 @@ -#include "CompactSuffixAutomatonQueryTest.h" +#include <catch2/catch.hpp> #include <string/String.h> #include <stringology/indexing/ExperimentalCompactSuffixAutomatonConstruct.h> @@ -8,17 +8,7 @@ #include <string/generate/RandomStringFactory.h> #include <string/generate/RandomSubstringFactory.h> -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( CompactSuffixAutomatonQueryTest, "stringology" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( CompactSuffixAutomatonQueryTest ); - -void CompactSuffixAutomatonQueryTest::setUp ( ) { -} - -void CompactSuffixAutomatonQueryTest::tearDown ( ) { -} - -void CompactSuffixAutomatonQueryTest::testCDAWG ( ) { - +TEST_CASE ( "CDAWG", "[unit][stringology]" ) { ext::vector<std::string> subjects; ext::vector<std::string> patterns; @@ -41,18 +31,19 @@ void CompactSuffixAutomatonQueryTest::testCDAWG ( ) { ext::set < unsigned > res = stringology::query::CompactSuffixAutomatonFactors::query ( index, pattern ); ext::set < unsigned > ref = stringology::exact::ExactFactorMatch::match ( subject, pattern ); - CPPUNIT_ASSERT ( res == ref ); - std::cout << subjects[i] << ' ' << patterns[i] << ' ' << res << std::endl; + CAPTURE ( subjects [ i ], patterns [ i ], res, ref ); + CHECK ( res == ref ); } - auto longSubject = string::generate::RandomStringFactory::generateLinearString (4000, 26, false, true); - auto longPattern = string::generate::RandomSubstringFactory::generateSubstring(2, longSubject ); + auto longSubject = string::generate::RandomStringFactory::generateLinearString ( 4000, 26, false, true ); + auto longPattern = string::generate::RandomSubstringFactory::generateSubstring ( 2, longSubject ); indexes::stringology::CompactSuffixAutomatonTerminatingSymbol < std::string > index = stringology::indexing::ExperimentalCompactSuffixAutomatonConstruct::construct ( longSubject ); ext::set < unsigned > res = stringology::query::CompactSuffixAutomatonFactors::query ( index, longPattern ); ext::set < unsigned > ref = stringology::exact::ExactFactorMatch::match ( longSubject, longPattern ); - std::cout << "long: " << res << std::endl; - CPPUNIT_ASSERT ( res == ref); + + CAPTURE ( res, ref ); + CHECK ( res == ref ) ; } diff --git a/alib2algo_experimental/test-src/stringology/query/CompactSuffixAutomatonQueryTest.h b/alib2algo_experimental/test-src/stringology/query/CompactSuffixAutomatonQueryTest.h deleted file mode 100644 index 67286b0117..0000000000 --- a/alib2algo_experimental/test-src/stringology/query/CompactSuffixAutomatonQueryTest.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef COMPACT_SUFFIX_AUTOMATON_QUERY_TEST_H_ -#define COMPACT_SUFFIX_AUTOMATON_QUERY_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class CompactSuffixAutomatonQueryTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( CompactSuffixAutomatonQueryTest ); - CPPUNIT_TEST ( testCDAWG ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testCDAWG ( ); -}; - -#endif // COMPACT_SUFFIX_AUTOMATON_QUERY_TEST_H_ -- GitLab