Blame view

morfeusz/tests/TestMorfeusz.cpp 5.83 KB
Michał Lenart authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * File:   TestMorfeusz.cpp
 * Author: lennyn
 *
 * Created on Jun 27, 2014, 1:03:19 PM
 */

#include "TestMorfeusz.hpp"

#include <cstdio>
#include <vector>
#include <fstream>

CPPUNIT_TEST_SUITE_REGISTRATION(TestMorfeusz);

using namespace std;
using namespace morfeusz;

TestMorfeusz::TestMorfeusz() {
}

TestMorfeusz::~TestMorfeusz() {
}

void TestMorfeusz::setUp() {
    morfeusz = Morfeusz::createInstance();
}

void TestMorfeusz::tearDown() {
    delete morfeusz;
}

void TestMorfeusz::testAnalyzeIterate1() {
Michał Lenart authored
34
    cerr << "testAnalyzeIterate1" << endl;
Michał Lenart authored
35
36
37
38
39
40
41
42
43
44
    ResultsIterator* it = morfeusz->analyze("AAAAbbbbCCCC");
    CPPUNIT_ASSERT(it->hasNext());
    CPPUNIT_ASSERT_EQUAL(string("AAAAbbbbCCCC"), it->peek().getOrth());
    CPPUNIT_ASSERT_EQUAL(string("AAAAbbbbCCCC"), it->next().getOrth());
    CPPUNIT_ASSERT(!it->hasNext());
    CPPUNIT_ASSERT_THROW(it->peek(), MorfeuszException);
    CPPUNIT_ASSERT_THROW(it->next(), MorfeuszException);
    delete it;
}
Michał Lenart authored
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
void TestMorfeusz::testAnalyzeIterateWithWhitespaceHandlingKEEP() {
    cerr << "testAnalyzeIterateWithWhitespaceHandlingKEEP" << endl;
    morfeusz->setWhitespaceHandling(KEEP);
    ResultsIterator* it = morfeusz->analyze(" AAAAbbbbCCCC  DDDDeeee.\t");

    CPPUNIT_ASSERT(it->hasNext());
    CPPUNIT_ASSERT_EQUAL(string(" "), it->next().getOrth());

    CPPUNIT_ASSERT(it->hasNext());
    CPPUNIT_ASSERT_EQUAL(string("AAAAbbbbCCCC"), it->next().getOrth());

    CPPUNIT_ASSERT(it->hasNext());
    CPPUNIT_ASSERT_EQUAL(string("  "), it->next().getOrth());

    CPPUNIT_ASSERT(it->hasNext());
    CPPUNIT_ASSERT_EQUAL(string("DDDDeeee"), it->next().getOrth());

    CPPUNIT_ASSERT(it->hasNext());
    CPPUNIT_ASSERT_EQUAL(string("."), it->next().getOrth());

    CPPUNIT_ASSERT(it->hasNext());
    CPPUNIT_ASSERT_EQUAL(string("\t"), it->next().getOrth());

    CPPUNIT_ASSERT(!it->hasNext());
    CPPUNIT_ASSERT_THROW(it->peek(), MorfeuszException);
    CPPUNIT_ASSERT_THROW(it->next(), MorfeuszException);
    delete it;
}

void TestMorfeusz::testAnalyzeIterateWithWhitespaceHandlingAPPEND() {
    cerr << "testAnalyzeIterateWithWhitespaceHandlingAPPEND" << endl;
    morfeusz->setWhitespaceHandling(APPEND);
    ResultsIterator* it = morfeusz->analyze(" AAAAbbbbCCCC  DDDDeeee.\t");

    CPPUNIT_ASSERT(it->hasNext());
    CPPUNIT_ASSERT_EQUAL(string(" AAAAbbbbCCCC  "), it->next().getOrth());

    CPPUNIT_ASSERT(it->hasNext());
    CPPUNIT_ASSERT_EQUAL(string("DDDDeeee"), it->next().getOrth());

    CPPUNIT_ASSERT(it->hasNext());
    CPPUNIT_ASSERT_EQUAL(string(".\t"), it->next().getOrth());

    CPPUNIT_ASSERT(!it->hasNext());
    CPPUNIT_ASSERT_THROW(it->peek(), MorfeuszException);
    CPPUNIT_ASSERT_THROW(it->next(), MorfeuszException);
    delete it;
}
Michał Lenart authored
94
void TestMorfeusz::testAnalyzeVector1() {
Michał Lenart authored
95
    cerr << "testAnalyzeVector1" << endl;
Michał Lenart authored
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    vector<MorphInterpretation> res;
    morfeusz->analyze("AAAAbbbbCCCC", res);
    CPPUNIT_ASSERT_EQUAL((size_t) 1, res.size());
    CPPUNIT_ASSERT_EQUAL(string("AAAAbbbbCCCC"), res[0].getOrth());
    CPPUNIT_ASSERT_EQUAL(string("AAAAbbbbCCCC"), res[0].getLemma());
}

static inline string prepareErrorneusTmpFile() {
    char* filename = tmpnam(NULL);
    ofstream out;
    out.open(filename);
    out << "asfasdfa" << endl;
    out.close();
    return string(filename);
}

void TestMorfeusz::testOpenInvalidFile() {
Michał Lenart authored
113
    cerr << "testOpenInvalidFile" << endl;
Michał Lenart authored
114
115
116
117
118
    string filename(prepareErrorneusTmpFile());
    CPPUNIT_ASSERT_THROW(morfeusz->setAnalyzerDictionary(filename), FileFormatException);
}

void TestMorfeusz::testOpenNonExistentFile() {
Michał Lenart authored
119
    cerr << "testOpenNonExistentFile" << endl;
Michał Lenart authored
120
121
122
123
124
    string filename(tmpnam(NULL));
    CPPUNIT_ASSERT_THROW(morfeusz->setAnalyzerDictionary(filename), std::ios_base::failure);
}

void TestMorfeusz::testSetInvalidAgglOption() {
Michał Lenart authored
125
    cerr << "testSetInvalidAgglOption" << endl;
Michał Lenart authored
126
127
128
129
    CPPUNIT_ASSERT_THROW(morfeusz->setAggl("asdfasdfa"), MorfeuszException);
}

void TestMorfeusz::testSetInvalidPraetOption() {
Michał Lenart authored
130
    cerr << "testSetInvalidPraetOption" << endl;
Michał Lenart authored
131
132
133
134
    CPPUNIT_ASSERT_THROW(morfeusz->setPraet("asdfasdfa"), MorfeuszException);
}

void TestMorfeusz::testWhitespaceHandlingKEEP() {
Michał Lenart authored
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    cerr << "testWhitespaceHandlingKEEP" << endl;
    vector<MorphInterpretation> res;
    morfeusz->setWhitespaceHandling(KEEP);
    morfeusz->analyze("  AAAAbbbbCCCC DDDDeeee\t", res);
    CPPUNIT_ASSERT_EQUAL((size_t) 5, res.size());
    CPPUNIT_ASSERT_EQUAL(string("  "), res[0].getOrth());
    CPPUNIT_ASSERT_EQUAL(string("  "), res[0].getLemma());
    CPPUNIT_ASSERT_EQUAL(1, res[0].getTagnum());
    CPPUNIT_ASSERT_EQUAL(string("AAAAbbbbCCCC"), res[1].getOrth());
    CPPUNIT_ASSERT_EQUAL(string("AAAAbbbbCCCC"), res[1].getLemma());
    CPPUNIT_ASSERT_EQUAL(0, res[1].getTagnum());
    CPPUNIT_ASSERT_EQUAL(string(" "), res[2].getOrth());
    CPPUNIT_ASSERT_EQUAL(string(" "), res[2].getLemma());
    CPPUNIT_ASSERT_EQUAL(1, res[2].getTagnum());
    CPPUNIT_ASSERT_EQUAL(string("DDDDeeee"), res[3].getOrth());
    CPPUNIT_ASSERT_EQUAL(string("DDDDeeee"), res[3].getLemma());
    CPPUNIT_ASSERT_EQUAL(0, res[3].getTagnum());
    CPPUNIT_ASSERT_EQUAL(string("\t"), res[4].getOrth());
    CPPUNIT_ASSERT_EQUAL(string("\t"), res[4].getLemma());
    CPPUNIT_ASSERT_EQUAL(1, res[4].getTagnum());
Michał Lenart authored
155
156
157
}

void TestMorfeusz::testWhitespaceHandlingAPPEND() {
Michał Lenart authored
158
159
160
161
162
163
164
165
166
167
168
    cerr << "testWhitespaceHandlingAPPEND" << endl;
    vector<MorphInterpretation> res;
    morfeusz->setWhitespaceHandling(APPEND);
    morfeusz->analyze("  AAAAbbbbCCCC DDDDeeee\t", res);
    CPPUNIT_ASSERT_EQUAL((size_t) 2, res.size());
    CPPUNIT_ASSERT_EQUAL(string("  AAAAbbbbCCCC "), res[0].getOrth());
    CPPUNIT_ASSERT_EQUAL(string("AAAAbbbbCCCC"), res[0].getLemma());
    CPPUNIT_ASSERT_EQUAL(0, res[0].getTagnum());
    CPPUNIT_ASSERT_EQUAL(string("DDDDeeee\t"), res[1].getOrth());
    CPPUNIT_ASSERT_EQUAL(string("DDDDeeee"), res[1].getLemma());
    CPPUNIT_ASSERT_EQUAL(0, res[1].getTagnum());
Michał Lenart authored
169
}