File: contextChangeDetectorTest.cpp

package info (click to toggle)
presage 0.9.1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 11,572 kB
  • ctags: 11,922
  • sloc: cpp: 86,282; sh: 11,775; ansic: 4,043; python: 1,218; makefile: 1,043; cs: 1,009; xml: 57
file content (120 lines) | stat: -rw-r--r-- 4,875 bytes parent folder | download | duplicates (4)
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
34
35
36
37
38
39
40
41
42
43
44
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

/******************************************************
 *  Presage, an extensible predictive text entry system
 *  ---------------------------------------------------
 *
 *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
                                                                             *
                                                                **********(*)*/


#include "contextChangeDetectorTest.h"

#include "core/charsets.h"

CPPUNIT_TEST_SUITE_REGISTRATION( ContextChangeDetectorTest );

void ContextChangeDetectorTest::setUp()
{
    detector = new ContextChangeDetector(reinterpret_cast<const char *>(DEFAULT_WORD_CHARS),
					 DEFAULT_SEPARATOR_CHARS,
					 DEFAULT_BLANKSPACE_CHARS,
					 DEFAULT_CONTROL_CHARS,
					 false);
}

void ContextChangeDetectorTest::tearDown()
{
    delete detector;
}

void ContextChangeDetectorTest::testCharChanges()
{
    detector->set_sliding_window_size("10");

    const std::string line   = "foo bar foobar, foo   bar! Foobar foo bar... foobar. ";
    const std::string change = "10010001000000100001000001000000010001000100000000010";

    for (size_t i = 0; i < line.size(); i++) {

	bool expected = (change[i] == '0' ? false : true);
	
	std::string stream = line.substr(0, i+1);

	std::stringstream ss;
	ss << "Error detected at: " << stream << '|';
	CPPUNIT_ASSERT_EQUAL_MESSAGE(ss.str().c_str(), expected, detector->context_change(stream));
	detector->update_sliding_window(stream);
    }
}

void ContextChangeDetectorTest::testBlockChanges()
{
    std::string str = "The quick brown fox jumped over the lazy dog";

    detector->set_sliding_window_size("10");

    // sliding window is now empty, so new string will trigger a context change
    CPPUNIT_ASSERT_EQUAL(true,  detector->context_change("The quick"));

    // update detector
    detector->update_sliding_window("The quick");

    // there was no change since last time, should be false
    CPPUNIT_ASSERT_EQUAL(false, detector->context_change("The quick"));
    // no changes, should be false
    CPPUNIT_ASSERT_EQUAL(false, detector->context_change("The quick"));
    // still no changes, should still be false
    CPPUNIT_ASSERT_EQUAL(false, detector->context_change("The quick"));
}

void ContextChangeDetectorTest::testGetChange()
{
    std::string str = "The quick brown fox jumped over the lazy dog";

    detector->set_sliding_window_size("10");

    // when sliding window is empty, change is equal to stream
    CPPUNIT_ASSERT_EQUAL(detector->change(str), str);

    // when sliding window contains part of stream, change is equal to text that is in stream and not in sliding window
    detector->update_sliding_window ("The quick bro");
    CPPUNIT_ASSERT_EQUAL(detector->get_sliding_window(), std::string(" quick bro"));
    CPPUNIT_ASSERT_EQUAL(detector->change(str), std::string("wn fox jumped over the lazy dog"));

    detector->update_sliding_window ("The quick brow");
    CPPUNIT_ASSERT_EQUAL(detector->get_sliding_window(), std::string("quick brow"));
    CPPUNIT_ASSERT_EQUAL(detector->change(str), std::string("n fox jumped over the lazy dog"));

    detector->update_sliding_window ("The quick brown");
    CPPUNIT_ASSERT_EQUAL(detector->get_sliding_window(), std::string("uick brown"));
    CPPUNIT_ASSERT_EQUAL(detector->change(str), std::string(" fox jumped over the lazy dog"));

    detector->update_sliding_window ("The quick brown ");
    CPPUNIT_ASSERT_EQUAL(detector->get_sliding_window(), std::string("ick brown "));
    CPPUNIT_ASSERT_EQUAL(detector->change(str), std::string("fox jumped over the lazy dog"));

    // remove last char from stream
    detector->update_sliding_window ("The quick brown");
    CPPUNIT_ASSERT_EQUAL(detector->get_sliding_window(), std::string("uick brown"));
    CPPUNIT_ASSERT_EQUAL(detector->change(str), std::string(" fox jumped over the lazy dog"));

    // remove last char from stream again
    detector->update_sliding_window ("The quick brow");
    CPPUNIT_ASSERT_EQUAL(detector->get_sliding_window(), std::string("quick brow"));
    CPPUNIT_ASSERT_EQUAL(detector->change(str), std::string("n fox jumped over the lazy dog"));
}