2
/******************************************************
3
* Presage, an extensible predictive text entry system
4
* ---------------------------------------------------
6
* Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License along
19
with this program; if not, write to the Free Software Foundation, Inc.,
20
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
#include "forwardTokenizerTest.h"
30
CPPUNIT_TEST_SUITE_REGISTRATION( ForwardTokenizerTest );
32
void ForwardTokenizerTest::setUp()
34
blankspaces = " \n\t";
37
stringSuite = new TestStringSuite();
40
void ForwardTokenizerTest::tearDown()
45
void ForwardTokenizerTest::testConstructor()
48
ForwardTokenizer tok1(ss, blankspaces, separators);
50
CPPUNIT_ASSERT( tok1.blankspaceChars() == blankspaces );
51
CPPUNIT_ASSERT( tok1.separatorChars() == separators );
54
void ForwardTokenizerTest::testNextToken()
56
//std::cerr << "ForwardTokenizerTest::testNextToken()" << std::endl;
59
while (stringSuite->hasMoreTestStrings()) {
60
//std::cerr << "Entered loop!" << std::endl;
62
assert(stringSuite->currentTestString() != 0);
63
//std::cerr << "stringSuite->currentTestString():" << stringSuite->currentTestString() << std::endl;
64
ss << stringSuite->currentTestString()->getstr();
65
//std::cerr << "Tokenizing string: " << ss.str() << std::endl;
66
ForwardTokenizer tok(ss, blankspaces, separators);
67
for (i = 0; tok.hasMoreTokens(); i++) {
68
CPPUNIT_ASSERT_EQUAL( stringSuite->currentTestString()->token(i),
71
stringSuite->nextTestString();
75
void ForwardTokenizerTest::testCountTokens()
77
//std::cerr << "ForwardTokenizerTest::testCountTokens()" << std::endl;
79
while (stringSuite->hasMoreTestStrings()) {
81
assert(stringSuite->currentTestString() != 0);
82
ss << stringSuite->currentTestString()->getstr();
83
ForwardTokenizer tok(ss, blankspaces, separators);
84
CPPUNIT_ASSERT_EQUAL(stringSuite->currentTestString()->tokencount(),
86
stringSuite->nextTestString();
90
void ForwardTokenizerTest::testLowercaseMode()
92
//std::cerr << "ForwardTokenizerTest::testLowercaseMode()" << std::endl;
95
while (stringSuite->hasMoreTestStrings()) {
97
assert(stringSuite->currentTestString() != 0);
98
ss << stringSuite->currentTestString()->getstr();
99
ForwardTokenizer tok(ss, blankspaces, separators);
100
tok.lowercaseMode(true);
102
for (i = 0; tok.hasMoreTokens(); i++) {
103
str = stringSuite->currentTestString()->token(i);
104
// convert string to lowercase
105
for (std::string::iterator it = str.begin();
110
CPPUNIT_ASSERT_EQUAL( str,
113
stringSuite->nextTestString();
117
void ForwardTokenizerTest::testProgress()
119
//std::cerr << "ForwardTokenizerTest::testProgress()" << std::endl;
121
std::stringstream ss;
122
ss << "01 02 03 04 05 06 07 08 09 10";
123
ForwardTokenizer tok(ss, " ", " ");
124
double expectedProgress = 0.0;
125
const double expectedProgressIncrement = 0.1;
126
const double delta = 0.035;
127
while (tok.hasMoreTokens()) {
128
expectedProgress += expectedProgressIncrement;
130
//std::cerr << "[expected] " << expectedProgress
131
// << " [progress] " << tok.progress() << std::endl;
132
CPPUNIT_ASSERT( (expectedProgress - delta) < tok.progress() );
133
CPPUNIT_ASSERT( tok.progress() < (expectedProgress + delta) );
137
void ForwardTokenizerTest::testRepeatability()
139
//std::cerr << "ForwardTokenizerTest::testRepeatability()" << std::endl;
141
const int REPETITIONS = 3;
143
std::stringstream ss;
144
std::streambuf* sb = ss.rdbuf();
147
std::string str = "foo bar foobar";
148
const int TOKENS = 3;
149
std::string tokens[3];
152
tokens[2] = "foobar";
154
std::string::const_iterator str_it = str.begin();
155
while (str_it != str.end()) {
160
// build expected stringstream
161
std::stringstream expected_ss;
162
while (index < REPETITIONS) {
163
for (int i = 0; i < TOKENS; i++) {
164
expected_ss << tokens[i] << std::endl;
169
std::stringstream actual_ss;
170
for (int i = 0; i < REPETITIONS; i++ ) {
172
ForwardTokenizer fT(ss, blankspaces, separators);
173
//std::cerr << "[stream] " << fT.streamToString() << std::endl;
174
while (fT.hasMoreTokens()) {
175
std::string token = fT.nextToken();
176
//std::cerr << "[repeatability] " << token << std::endl;
177
actual_ss << token << std::endl;
178
CPPUNIT_ASSERT_EQUAL( tokens[index], token );
183
CPPUNIT_ASSERT_EQUAL( expected_ss.str(), actual_ss.str() );