~ubuntu-branches/ubuntu/trusty/presage/trusty-proposed

« back to all changes in this revision

Viewing changes to test/lib/core/tokenizer/forwardTokenizerTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matteo Vescovi
  • Date: 2011-08-06 09:26:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110806092615-0wvhajaht9974ncx
Tags: upstream-0.8.6
ImportĀ upstreamĀ versionĀ 0.8.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************
 
3
 *  Presage, an extensible predictive text entry system
 
4
 *  ---------------------------------------------------
 
5
 *
 
6
 *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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.
 
21
                                                                             *
 
22
                                                                **********(*)*/
 
23
 
 
24
 
 
25
#include "forwardTokenizerTest.h"
 
26
 
 
27
#include <iostream>
 
28
#include <assert.h>
 
29
 
 
30
CPPUNIT_TEST_SUITE_REGISTRATION( ForwardTokenizerTest );
 
31
 
 
32
void ForwardTokenizerTest::setUp()
 
33
{
 
34
    blankspaces = " \n\t";
 
35
    separators = ".,!";
 
36
 
 
37
    stringSuite = new TestStringSuite();
 
38
}
 
39
 
 
40
void ForwardTokenizerTest::tearDown()
 
41
{
 
42
    delete stringSuite;
 
43
}
 
44
 
 
45
void ForwardTokenizerTest::testConstructor()
 
46
{
 
47
    std::stringstream ss;
 
48
    ForwardTokenizer tok1(ss, blankspaces, separators);
 
49
    
 
50
    CPPUNIT_ASSERT( tok1.blankspaceChars() == blankspaces );
 
51
    CPPUNIT_ASSERT( tok1.separatorChars() == separators );
 
52
}
 
53
 
 
54
void ForwardTokenizerTest::testNextToken()
 
55
{
 
56
    //std::cerr << "ForwardTokenizerTest::testNextToken()" << std::endl;
 
57
 
 
58
    int i;
 
59
    while (stringSuite->hasMoreTestStrings()) {
 
60
        //std::cerr << "Entered loop!" << std::endl;
 
61
        std::stringstream ss;
 
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),
 
69
                                  tok.nextToken() );
 
70
        }
 
71
        stringSuite->nextTestString();
 
72
    }
 
73
}
 
74
 
 
75
void ForwardTokenizerTest::testCountTokens()
 
76
{
 
77
    //std::cerr << "ForwardTokenizerTest::testCountTokens()" << std::endl;
 
78
 
 
79
    while (stringSuite->hasMoreTestStrings()) {
 
80
        std::stringstream ss;
 
81
        assert(stringSuite->currentTestString() != 0);
 
82
        ss << stringSuite->currentTestString()->getstr();
 
83
        ForwardTokenizer tok(ss, blankspaces, separators);
 
84
        CPPUNIT_ASSERT_EQUAL(stringSuite->currentTestString()->tokencount(),
 
85
                             tok.countTokens());
 
86
        stringSuite->nextTestString();
 
87
    }
 
88
}
 
89
 
 
90
void ForwardTokenizerTest::testLowercaseMode()
 
91
{
 
92
    //std::cerr << "ForwardTokenizerTest::testLowercaseMode()" << std::endl;
 
93
 
 
94
    int i;
 
95
    while (stringSuite->hasMoreTestStrings()) {
 
96
        std::stringstream ss;
 
97
        assert(stringSuite->currentTestString() != 0);
 
98
        ss << stringSuite->currentTestString()->getstr();
 
99
        ForwardTokenizer tok(ss, blankspaces, separators);
 
100
        tok.lowercaseMode(true);
 
101
        std::string str;
 
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();
 
106
                 it != str.end();
 
107
                 it++) {
 
108
                *it = tolower(*it);
 
109
            }
 
110
            CPPUNIT_ASSERT_EQUAL( str,
 
111
                                  tok.nextToken() );
 
112
        }
 
113
        stringSuite->nextTestString();
 
114
    }
 
115
}
 
116
 
 
117
void ForwardTokenizerTest::testProgress()
 
118
{
 
119
    //std::cerr << "ForwardTokenizerTest::testProgress()" << std::endl;
 
120
 
 
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;
 
129
        tok.nextToken();
 
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) );
 
134
    }
 
135
}
 
136
 
 
137
void ForwardTokenizerTest::testRepeatability()
 
138
{
 
139
    //std::cerr << "ForwardTokenizerTest::testRepeatability()" << std::endl;
 
140
 
 
141
    const int REPETITIONS = 3;
 
142
 
 
143
    std::stringstream ss;
 
144
    std::streambuf* sb = ss.rdbuf();
 
145
    int index = 0;
 
146
 
 
147
    std::string str = "foo bar foobar";
 
148
    const int TOKENS = 3;
 
149
    std::string tokens[3];
 
150
    tokens[0] = "foo";
 
151
    tokens[1] = "bar";
 
152
    tokens[2] = "foobar";
 
153
 
 
154
    std::string::const_iterator str_it = str.begin();
 
155
    while (str_it != str.end()) {
 
156
        sb->sputc(*str_it);
 
157
        str_it++;
 
158
    }
 
159
 
 
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;
 
165
        }
 
166
        index++;
 
167
    }
 
168
 
 
169
    std::stringstream actual_ss;
 
170
    for (int i = 0; i < REPETITIONS; i++ ) {
 
171
        index = 0;
 
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 );
 
179
            index++;
 
180
        }
 
181
    }
 
182
 
 
183
    CPPUNIT_ASSERT_EQUAL( expected_ss.str(), actual_ss.str() );
 
184
}