File: intervalsettest.cpp

package info (click to toggle)
graphite2 1.3.14-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,588 kB
  • sloc: cpp: 14,738; cs: 1,998; python: 1,737; ansic: 1,673; perl: 184; xml: 123; sh: 104; makefile: 62
file content (252 lines) | stat: -rw-r--r-- 7,629 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*  GRAPHITE2 LICENSING

    Copyright 2010, SIL International
    All rights reserved.

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2.1 of 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
    Lesser General Public License for more details.

    You should also have received a copy of the GNU Lesser General Public
    License along with this library in the file named "LICENSE".
    If not, write to the Free Software Foundation, 51 Franklin Street,
    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    internet at http://www.fsf.org/licenses/lgpl.html.

Alternatively, the contents of this file may be used under the terms of the
Mozilla Public License (http://mozilla.org/MPL) or 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.
*/
#include "inc/Intervals.h"
#include <utility>
#include <vector>
#include <cstdio>
#include <cstddef>
#include <cassert>

namespace gr2 = graphite2;

typedef std::pair<float, float> fpair;
typedef std::vector<fpair> fvector;
typedef fvector::iterator ifvector;

static int testCount = 0;

void printRanges(const char *pref, gr2::IntervalSet &is)
{
    printf ("%s: [ ", pref);
    for (gr2::IntervalSet::ivtpair s = is.begin(), end = is.end(); s != end; ++s)
        printf("(%.1f, %.1f) ", s->first, s->second);
    printf ("]\n");
}

int doTest(const char *pref, gr2::IntervalSet &is, fvector &fv)
{
    bool pass = true;
    gr2::IntervalSet::ivtpair s = is.begin(), e = is.end();
    ifvector bs = fv.begin(), be = fv.end();
    for ( ; s != e && bs != be; ++s, ++bs)
    {
        if (*s != *bs)
        {
            pass = false;
            break;
        }
    }
    if (bs != be || s != e)
        pass = false;
    printf("%d %s) ", ++testCount, pass ? "pass" : "FAIL");
    printRanges(pref, is);
    return pass ? 0 : 1;
}

void printFloat(const char *pref, float val)
{
    printf ("%s: [ %.1f ]\n", pref, val);
}

int doFloatTest(const char *pref, float test, float base)
{
    bool pass = (test == base);
    printf ("%d %s) %s: [ %.1f == %.1f ]\n", ++testCount, (pass ? "pass" : "FAIL"), pref, test, base);
    return (pass ? 0 : 1);
}

int main(int /*argc*/, char ** /*argv*/)
{
    int res = 0;
    fvector base;
    float fres;
    gr2::IntervalSet test, test2, testl;

    test.clear();
    base.clear();
    test.add(fpair(10., 100.));
    base.push_back(fpair(10., 100.));
    res += doTest("Init test", test, base);

    test.remove(fpair(30., 50.));
    base.clear();
    base.push_back(fpair(10., 30.));
    base.push_back(fpair(50., 100.));
    res += doTest("remove(30,50)", test, base);

    test.remove(fpair(45., 60.));
    base.back() = fpair(60., 100.);
    res += doTest("remove(45,60)", test, base);

    test.remove(fpair(80., 90.));
    base.clear();
    base.push_back(fpair(10., 30.));
    base.push_back(fpair(60., 80.));
    base.push_back(fpair(90., 100.));
    res += doTest("remove(80,90)", test, base);

    test2.clear();
    test2.add(fpair(10., 100.));
    test2.remove(fpair(20., 40.));
    test2.remove(fpair(45., 50.));
    test2.remove(fpair(55., 85.));
    base.clear();
    base.push_back(fpair(10., 20.));
    base.push_back(fpair(40., 45.));
    base.push_back(fpair(50., 55.));
    base.push_back(fpair(85., 100.));
    res += doTest("Init test2", test2, base);
    base.clear();
    base.push_back(fpair(20., 30.));
    base.push_back(fpair(60., 80));
    test.remove(test2);
    res += doTest("test - test2", test, base);

    test.clear();
    test.add(fpair(10., 85.));
    test.remove(fpair(30., 40.));
    base.clear();
    base.push_back(fpair(10., 30.));
    base.push_back(fpair(40., 85.));
    res += doTest("Init test3", test, base);
    test2.clear();
    test2.add(fpair(20., 90.));
    test2.remove(fpair(50., 60.));
    test2.remove(fpair(70., 80.));
    base.clear();
    base.push_back(fpair(20., 50.));
    base.push_back(fpair(60., 70.));
    base.push_back(fpair(80., 90.));
    res += doTest("Init test4", test2, base);
    test.remove(test2);
    base.clear();
    base.push_back(fpair(10., 20.));
    base.push_back(fpair(50., 60.));
    base.push_back(fpair(70., 80.));
    res += doTest("test3 - test4", test, base);
    testl = test.locate(fpair(30., 35.));
    base.clear();
    base.push_back(fpair(-20., -15.));
    base.push_back(fpair(20., 25.));
    base.push_back(fpair(40., 45.));
    res += doTest("locate(30,35)", testl, base);
    fres = testl.findClosestCoverage(0.);
    res += doFloatTest("Cover(0)", fres, -15.);
    fres = testl.findClosestCoverage(23.);
    res += doFloatTest("Cover(23)", fres, 23.);

    test.clear();
    test.remove(fpair(10., 20.));
    base.clear();
    res += doTest("Removing from empty", test, base);

    test.clear();
    test.add(fpair(10., 50.));
    test.add(fpair(20., 30.));
    test.add(fpair(40., 60.));
    test.add(fpair(60., 80.));
    test.add(fpair(90., 90.));
    test.add(fpair(80., 100.));
    base.clear();
    base.push_back(fpair(10., 100.));
    res += doTest("Slow add 10-100", test, base);

    test.clear();
    test.add(fpair(-447.149, 121.39));
    testl = test.locate(fpair(-487.753, -106.255));
    printRanges("          locate(-487,-186) in (-447,121)", testl);
    fres = testl.findClosestCoverage(0.);
    printFloat("          bestfit(0)", fres);

    test.clear();
    test.add(fpair(10., 90.));
    test2.clear();
    test2.add(fpair(50., 60.));
    test.remove(test2);
    test2.clear();
    test2.add(fpair(30., 40.));
    test.remove(test2);
    base.clear();
    base.push_back(fpair(10., 30.));
    base.push_back(fpair(40., 50.));
    base.push_back(fpair(60., 90.));
    res += doTest("(10,90)-[(50,60)]-[(30,40)]", test, base);

    test.clear();
    test.add(fpair(10., 90.));
    test2.clear();
    test2.add(fpair(20., 30.));
    test2.add(fpair(25., 40.));
    test.remove(test2);
    base.clear();
    base.push_back(fpair(10., 20.));
    base.push_back(fpair(40., 90.));
    res += doTest("(10,90)-[(20,30),(25,40)]", test, base);

    test.clear();
    test.add(fpair(10., 30.));
    test.add(fpair(40., 50.));
    test.add(fpair(60., 90.));
    test2.clear();
    test2.add(fpair(20., 80.));
    test.remove(test2);
    base.clear();
    base.push_back(fpair(10., 20.));
    base.push_back(fpair(80., 90.));
    res += doTest("Test 4 remove spans", test, base);
    test.clear();
    test.add(fpair(10., 30.));
    test.add(fpair(40., 50.));
    test.add(fpair(60., 90.));
    test.remove(fpair(20., 80.));
    res += doTest("Test 5 remove pair", test, base);

    test.clear();
    test.add(fpair(10., 30.));
    test.add(fpair(40., 50.));
    test.add(fpair(60., 90.));
    test2.clear();
    test2.add(15., 45.);
    test2.add(65., 70.);
    test2.add(75., 80.);
    test.intersect(test2);
    base.clear();
    base.push_back(fpair(15., 30.));
    base.push_back(fpair(40., 45.));
    base.push_back(fpair(65., 70.));
    base.push_back(fpair(75., 80.));
    res += doTest("Test 6 intersection", test, base);

    test.clear();
    test.add(1332, 1433);
    test.remove(1356, 1627);
    base.clear();
    base.push_back(fpair(1332, 1356));
    res += doTest("Test 7 right overlap removal", test, base);

    return res;
}