From 3da7666adb37d6d45f70a8cad6c013a446805c9b Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Fri, 6 Nov 2015 22:26:21 +0100 Subject: [PATCH] left factorize tests --- .../grammar/parsing/LeftFactorize.cpp | 70 +++++++++++++++++++ .../test-src/grammar/parsing/LeftFactorize.h | 22 ++++++ 2 files changed, 92 insertions(+) create mode 100644 alib2algo/test-src/grammar/parsing/LeftFactorize.cpp create mode 100644 alib2algo/test-src/grammar/parsing/LeftFactorize.h diff --git a/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp b/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp new file mode 100644 index 0000000000..c7a37a2e80 --- /dev/null +++ b/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp @@ -0,0 +1,70 @@ +#include "LeftFactorize.h" + +#include "grammar/ContextFree/CFG.h" +#include "grammar/parsing/LeftFactorize.h" + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( LeftFactorize, "grammar" ); +CPPUNIT_TEST_SUITE_REGISTRATION ( LeftFactorize ); + +void LeftFactorize::setUp ( ) { +} + +void LeftFactorize::tearDown ( ) { +} + +void LeftFactorize::testLeftFactorize ( ) { +} + +void LeftFactorize::testLeftFactorize2 ( ) { +} + +void LeftFactorize::testLeftFactorize3 ( ) { + alphabet::Symbol S = alphabet::symbolFrom ( "S" ); + alphabet::Symbol Sp = alphabet::symbolFrom ( "S'" ); + alphabet::Symbol B = alphabet::symbolFrom ( "B" ); + alphabet::Symbol Bp = alphabet::symbolFrom ( "B'" ); + alphabet::Symbol C = alphabet::symbolFrom ( "C" ); + alphabet::Symbol Cp = alphabet::symbolFrom ( "C'" ); + + alphabet::Symbol a = alphabet::symbolFrom ( 'a' ); + alphabet::Symbol b = alphabet::symbolFrom ( 'b' ); + alphabet::Symbol c = alphabet::symbolFrom ( 'c' ); + + grammar::CFG grammar ( S ); + + grammar.setTerminalAlphabet ( { a, b, c } ); + grammar.setNonterminalAlphabet ( { { S, B, C } } ); + grammar.setInitialSymbol ( S ); + + grammar.addRule ( S, std::vector < alphabet::Symbol > { a, B } ); + grammar.addRule ( S, std::vector < alphabet::Symbol > { a, C } ); + grammar.addRule ( B, std::vector < alphabet::Symbol > { b, B } ); + grammar.addRule ( B, std::vector < alphabet::Symbol > { b } ); + grammar.addRule ( C, std::vector < alphabet::Symbol > { c, C } ); + grammar.addRule ( C, std::vector < alphabet::Symbol > { c } ); + + grammar::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, std::vector < alphabet::Symbol > { a, Sp } ); + comp.addRule ( Sp, std::vector < alphabet::Symbol > { B } ); + comp.addRule ( Sp, std::vector < alphabet::Symbol > { C } ); + comp.addRule ( B, std::vector < alphabet::Symbol > { b, Bp } ); + comp.addRule ( Bp, std::vector < alphabet::Symbol > { B } ); + comp.addRule ( Bp, std::vector < alphabet::Symbol > { } ); + comp.addRule ( C, std::vector < alphabet::Symbol > { c, Cp } ); + comp.addRule ( Cp, std::vector < alphabet::Symbol > { C } ); + comp.addRule ( Cp, std::vector < alphabet::Symbol > { } ); + + std::cout << res << std::endl << comp << std::endl; + + CPPUNIT_ASSERT ( res == comp ); +} diff --git a/alib2algo/test-src/grammar/parsing/LeftFactorize.h b/alib2algo/test-src/grammar/parsing/LeftFactorize.h new file mode 100644 index 0000000000..88f457070d --- /dev/null +++ b/alib2algo/test-src/grammar/parsing/LeftFactorize.h @@ -0,0 +1,22 @@ +#ifndef LEFT_FACTORIZE_TEST_H_ +#define LEFT_FACTORIZE_TEST_H_ + +#include <cppunit/extensions/HelperMacros.h> + +class LeftFactorize : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE ( LeftFactorize ); + CPPUNIT_TEST ( testLeftFactorize ); + CPPUNIT_TEST ( testLeftFactorize2 ); + CPPUNIT_TEST ( testLeftFactorize3 ); + CPPUNIT_TEST_SUITE_END ( ); + +public: + void setUp ( ); + void tearDown ( ); + + void testLeftFactorize ( ); + void testLeftFactorize2 ( ); + void testLeftFactorize3 ( ); +}; + +#endif /* LEFT_FACTORIZE_TEST_H_ */ -- GitLab