Source

gpalign-cpp / src / tests / testGenerator.hpp

//--------------------------------------------------------------------------//
// testGenerator.cpp
// Lars Yencken <lars.yencken@gmail.com>
// vim: ts=4 sw=4 sts=4 expandtab:
// Mon Oct  8 11:09:27 EST 2007
//--------------------------------------------------------------------------//

#ifndef TESTGENERATOR_HPP
#define TESTGENERATOR_HPP

#include <cppunit/TestFixture.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestAssert.h>
#include <cppunit/TestSuite.h>
#include "../generator.hpp"
#include "../scripts.hpp"

//--------------------------------------------------------------------------//

class GeneratorTest : public CppUnit::TestFixture
{
private:
    Segment* m_unique;
    Segment* m_basic;
    Segment* m_smallKana;
    Segment* m_nKana;
    Segment* m_longVowel;
    Segment* m_mixed;

public:
    void setUp()
    {
        m_unique = new Segment(L"私", L"わたし");
        m_basic = new Segment(L"漢字", L"たんだん"); 
        m_smallKana = new Segment(L"発起", L"ほっき");
        m_nKana = new Segment(L"発起人", L"ほきにん");
        m_longVowel = new Segment(L"ビール", L"ビール");
        m_mixed = new Segment(L"此の節", L"このせつ");
    }

    void testUnique()
    {
        CPPUNIT_ASSERT(m_unique->g.size() == 1);
        CPPUNIT_ASSERT(m_unique->p.size() == 3);
        vector<Alignment> alignments;
        potentialAlignments(*m_unique, alignments);
        CPPUNIT_ASSERT(alignments.size() == 1);
        Alignment& alignment = alignments[0];
        CPPUNIT_ASSERT(alignment.size() == 1);
        CPPUNIT_ASSERT(alignment[0].g == m_unique->g);
        CPPUNIT_ASSERT(alignment[0].p == m_unique->p);
    }

    void testBasic()
    {
        vector<Alignment> alignments;
        potentialAlignments(*m_basic, alignments);
        CPPUNIT_ASSERT(alignments.size() == 2);
        {
            Alignment& alignment = alignments[0];
            CPPUNIT_ASSERT(alignment.size() == 2);
            CPPUNIT_ASSERT(alignment[0] == Segment(L"漢", L"たん"));
            CPPUNIT_ASSERT(alignment[1] == Segment(L"字", L"だん"));
        }
        {
            Alignment& alignment = alignments[1];
            CPPUNIT_ASSERT(alignment.size() == 1);
            CPPUNIT_ASSERT(alignment[0] == Segment(L"漢字", L"たんだん"));
        }
    }

    void testSmallKana()
    {
        vector<Alignment> alignments;
        potentialAlignments(*m_smallKana, alignments);
        CPPUNIT_ASSERT(alignments.size() == 2);
        for (int i = 0; i < 2; i++) {
            Alignment& a = alignments[i];
            for (Alignment::iterator iter = a.begin(); iter != a.end();
                    iter++) {
                CPPUNIT_ASSERT(!isSmallKana(iter->p[0]));
            }
        }
    }
    
    void testNKana()
    {
        vector<Alignment> alignments;
        potentialAlignments(*m_nKana, alignments);
        CPPUNIT_ASSERT(alignments.size() == 4);
        for (unsigned int i = 0; i < alignments.size(); i++) {
            Alignment& a = alignments[i];
            for (Alignment::iterator iter = a.begin(); iter != a.end();
                    iter++) {
                CPPUNIT_ASSERT(iter->g.size() == bigKanaLen(iter->p));
            }
        }
    }

    void testLongVowel()
    {
        CPPUNIT_ASSERT(m_longVowel->g.size() == 3);
        CPPUNIT_ASSERT(m_longVowel->p.size() == 3);
        vector<Alignment> alignments;
        potentialAlignments(*m_longVowel, alignments);
        CPPUNIT_ASSERT(alignments.size() == 1);
        Alignment& alignment = alignments[0];
        CPPUNIT_ASSERT(alignment.size() == 1);
        CPPUNIT_ASSERT(alignment[0].g == m_longVowel->g);
        CPPUNIT_ASSERT(alignment[0].p == m_longVowel->p);
    }

    void testMixedScript()
    {
        m_mixed = new Segment(L"此の節", L"このせつ");
        CPPUNIT_ASSERT(m_mixed->g.size() == 3);
        CPPUNIT_ASSERT(m_mixed->p.size() == 4);
        vector<Alignment> alignments;
        potentialAlignments(*m_mixed, alignments);
        CPPUNIT_ASSERT(alignments.size() == 1);
        Alignment& alignment = alignments[0];
        CPPUNIT_ASSERT(alignment.size() == 3);
        CPPUNIT_ASSERT(alignment[0].g == wstring(L"此"));
        CPPUNIT_ASSERT(alignment[0].p == wstring(L"こ"));
        CPPUNIT_ASSERT(alignment[1].g == wstring(L"の"));
        CPPUNIT_ASSERT(alignment[1].p == wstring(L"の"));
        CPPUNIT_ASSERT(alignment[2].g == wstring(L"節"));
        CPPUNIT_ASSERT(alignment[2].p == wstring(L"せつ"));
    }

    void tearDown()
    {
        delete m_unique;
        delete m_basic;
        delete m_smallKana;
        delete m_longVowel;
        delete m_mixed;
    }

    static CppUnit::Test* suite()
    {
        CppUnit::TestSuite* s = new CppUnit::TestSuite("GeneratorTest");
        s->addTest(new CppUnit::TestCaller<GeneratorTest>("testUnique",
                    &GeneratorTest::testUnique));
        s->addTest(new CppUnit::TestCaller<GeneratorTest>("testBasic",
                    &GeneratorTest::testBasic));
        s->addTest(new CppUnit::TestCaller<GeneratorTest>("testSmallKana",
                    &GeneratorTest::testSmallKana));
        s->addTest(new CppUnit::TestCaller<GeneratorTest>("testNKana",
                    &GeneratorTest::testNKana));
        s->addTest(new CppUnit::TestCaller<GeneratorTest>("testLongVowel",
                    &GeneratorTest::testLongVowel));
        s->addTest(new CppUnit::TestCaller<GeneratorTest>("testMixedScript",
                    &GeneratorTest::testMixedScript));

        return s;
    }
};

#endif