~ubuntu-branches/ubuntu/trusty/tagcoll2/trusty-proposed

« back to all changes in this revision

Viewing changes to tagcoll/patch-tut.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Fontaine
  • Date: 2006-11-18 12:15:11 UTC
  • Revision ID: james.westby@ubuntu.com-20061118121511-nic9no49s64crb7i
Tags: upstream-2.0.4
ImportĀ upstreamĀ versionĀ 2.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Classes handling tag patches
 
3
 *
 
4
 * Copyright (C) 2003  Enrico Zini <enrico@debian.org>
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
19
 */
 
20
 
 
21
#include <tests/test-utils.h>
 
22
#include <tagcoll/patch.h>
 
23
#include <tagcoll/coll/simple.h>
 
24
 
 
25
namespace std {
 
26
 
 
27
template<typename TAG, typename _Traits>
 
28
basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& out, const std::set<TAG>& tags)
 
29
{
 
30
        for (typename std::set<TAG>::const_iterator i = tags.begin();
 
31
                        i != tags.end(); ++i)
 
32
                if (i == tags.begin())
 
33
                        out << *i;
 
34
                else
 
35
                        out << ", " << *i;
 
36
        return out;
 
37
}
 
38
 
 
39
template<typename ITEM, typename TAG>
 
40
ostream& operator<<(ostream& out, const tagcoll::Patch<ITEM, TAG>& p)
 
41
{
 
42
        out << p.item << ": ";
 
43
        bool first = true;
 
44
        for (typename std::set<TAG>::const_iterator i = p.added.begin();
 
45
                        i != p.added.end(); ++i)
 
46
        {
 
47
                if (first)
 
48
                        first = false;
 
49
                else
 
50
                        out << ", ";
 
51
                out << "+" << *i;
 
52
        }
 
53
        for (typename std::set<TAG>::const_iterator i = p.removed.begin();
 
54
                        i != p.removed.end(); ++i)
 
55
        {
 
56
                if (first)
 
57
                        first = false;
 
58
                else
 
59
                        out << ", ";
 
60
                out << "-" << *i;
 
61
        }
 
62
        return out;
 
63
 
 
64
}
 
65
 
 
66
}
 
67
 
 
68
namespace tut {
 
69
using namespace std;
 
70
using namespace tagcoll;
 
71
using namespace tagcoll::tests;
 
72
using namespace wibble::operators;
 
73
 
 
74
struct tagcoll_patches_shar {
 
75
};
 
76
TESTGRP(tagcoll_patches);
 
77
 
 
78
 
 
79
// Test empty patches
 
80
template<> template<>
 
81
void to::test<1>()
 
82
{
 
83
        Patch<string, int> a("foo");
 
84
        ensure_equals(a.item, "foo");
 
85
        ensure(a.added.empty());
 
86
        ensure(a.removed.empty());
 
87
 
 
88
        Patch<string, int> b("foo", std::set<int>(), std::set<int>());
 
89
        ensure_equals(b.item, "foo");
 
90
        ensure(b.added.empty());
 
91
        ensure(b.removed.empty());
 
92
 
 
93
        Patch<string, int> c("foo", wibble::Empty<int>(), wibble::Empty<int>());
 
94
        ensure_equals(c.item, "foo");
 
95
        ensure(c.added.empty());
 
96
        ensure(c.removed.empty());
 
97
 
 
98
        ensure_equals(a, b);
 
99
        ensure_equals(a, c);
 
100
        ensure_equals(b, c);
 
101
}
 
102
 
 
103
// Test the different constructors
 
104
template<> template<>
 
105
void to::test<2>()
 
106
{
 
107
        Patch<string, int> a("foo");
 
108
        a.add(1);
 
109
        a.remove(2);
 
110
        ensure_equals(a.item, "foo");
 
111
        ensure_equals(a.added.size(), 1u);
 
112
        ensure_equals(a.removed.size(), 1u);
 
113
        ensure_equals(*a.added.begin(), 1);
 
114
        ensure_equals(*a.removed.begin(), 2);
 
115
 
 
116
        std::set<int> added;
 
117
        std::set<int> removed;
 
118
        added.insert(1);
 
119
        removed.insert(2);
 
120
        Patch<string, int> b("foo", added, removed);
 
121
        ensure_equals(b.item, "foo");
 
122
        ensure_equals(b.added.size(), 1u);
 
123
        ensure_equals(b.removed.size(), 1u);
 
124
        ensure_equals(*b.added.begin(), 1);
 
125
        ensure_equals(*b.removed.begin(), 2);
 
126
 
 
127
        Patch<string, int> c("foo", wibble::singleton(1), wibble::singleton(2));
 
128
        ensure_equals(c.item, "foo");
 
129
        ensure_equals(c.added.size(), 1u);
 
130
        ensure_equals(c.removed.size(), 1u);
 
131
        ensure_equals(*c.added.begin(), 1);
 
132
        ensure_equals(*c.removed.begin(), 2);
 
133
 
 
134
        ensure_equals(a, b);
 
135
        ensure_equals(a, c);
 
136
        ensure_equals(b, c);
 
137
}
 
138
 
 
139
// Test non-empty patches
 
140
template<> template<>
 
141
void to::test<3>()
 
142
{
 
143
        Patch<string, int> p("foo");
 
144
        p.add(1);
 
145
        p.add(2);
 
146
        p.remove(3);
 
147
        p.remove(4);
 
148
 
 
149
        // Check that getReverse() actually returns the reverse patch
 
150
        Patch<string, int> rp = p.getReverse();
 
151
        ensure_equals(p.added, rp.removed);
 
152
        ensure_equals(p.removed, rp.added);
 
153
 
 
154
        // Check removeRedundant()
 
155
        std::set<int> ts;
 
156
        ts.insert(1);
 
157
        ts.insert(3);
 
158
        p.removeRedundant(ts);
 
159
        ensure_not_contains(p.added, 1);
 
160
        ensure_contains(p.added, 2);
 
161
        ensure_contains(p.removed, 3);
 
162
        ensure_not_contains(p.removed, 4);
 
163
}
 
164
 
 
165
template<> template<>
 
166
void to::test<4>()
 
167
{
 
168
#if 0
 
169
        string input_coll(
 
170
                        "a: b, c\n"
 
171
                        "b:\n"
 
172
                        "c: \n"
 
173
                        "d:  c::D, e::F,    f::g\n"
 
174
                        );
 
175
        string output_coll(
 
176
                        "a: b\n"
 
177
                        "b: b, c\n"
 
178
                        "c: \n"
 
179
                        "d: c::D, c::d, e::F\n"
 
180
                        `);
 
181
        coll::Simple<string, string> result;
 
182
#endif
 
183
        PatchList<string, string> patches;
 
184
 
 
185
        std::set<string> added;
 
186
        std::set<string> removed;
 
187
 
 
188
        added.insert("b");
 
189
        removed.insert("c"); removed.insert("d");
 
190
        patches.addPatch(Patch<string, string>("a", added, removed));
 
191
 
 
192
        added.clear(); added.insert("b"), added.insert("c"), added.insert("b");
 
193
        removed.clear(); removed.insert("a");
 
194
        patches.addPatch(Patch<string, string>("b", added, removed));
 
195
 
 
196
        added.clear(); added.insert("c::D"), added.insert("c::d");
 
197
        removed.clear(); removed.insert("f::g");
 
198
        patches.addPatch(Patch<string, string>("d", added, removed));
 
199
 
 
200
#if 0
 
201
        parseCollection(input_coll, patcher(patches, inserter(result)));
 
202
 
 
203
        coll::Simple<string, string> reference;
 
204
        parseCollection(output_coll, inserter(reference));
 
205
 
 
206
        ensure_coll_equals(reference, result);
 
207
#endif
 
208
}
 
209
 
 
210
// Check addPatchInverted
 
211
template<> template<>
 
212
void to::test<5>()
 
213
{
 
214
        PatchList<string, string> patches;
 
215
        patches.addPatchInverted(Patch<string, string>(
 
216
                                "pizza",
 
217
                                wibble::singleton(string("tomato")),
 
218
                                wibble::singleton(string("ketchup"))));
 
219
 
 
220
        ensure_equals(patches.size(), 2u);
 
221
 
 
222
        PatchList<string, string>::const_iterator i = patches.begin();
 
223
 
 
224
        ensure_equals(i->first, "ketchup");
 
225
        ensure_equals(i->second.added.size(), 0u);
 
226
        ensure_equals(i->second.removed.size(), 1u);
 
227
        ensure_equals(*i->second.removed.begin(), "pizza");
 
228
 
 
229
        ++i;
 
230
 
 
231
        ensure_equals(i->first, "tomato");
 
232
        ensure_equals(i->second.added.size(), 1u);
 
233
        ensure_equals(i->second.removed.size(), 0u);
 
234
        ensure_equals(*i->second.added.begin(), "pizza");
 
235
}
 
236
 
 
237
// Test diffing collections
 
238
template<> template<>
 
239
void to::test<6>()
 
240
{
 
241
        string in_coll1(
 
242
                        "a: b, c\n"
 
243
                        "b: a\n"
 
244
                        );
 
245
        string in_coll2(
 
246
                        "a: d, c\n"
 
247
                        "c: a\n"
 
248
                        );
 
249
        coll::Simple<string, string> coll1;
 
250
        coll::Simple<string, string> coll2;
 
251
        parseCollection(in_coll1, inserter(coll1));
 
252
        parseCollection(in_coll2, inserter(coll2));
 
253
 
 
254
        PatchList<string, string> patches;
 
255
        patches.addPatch(coll1, coll2);
 
256
 
 
257
        // Replacing items should work
 
258
        PatchList<string, string>::const_iterator i = patches.begin();
 
259
        ensure(i != patches.end());
 
260
        ensure_equals(i->first, string("a"));
 
261
        ensure_equals(i->second.added.size(), 1u);
 
262
        ensure_equals(*i->second.added.begin(), string("d"));
 
263
        ensure_equals(i->second.removed.size(), 1u);
 
264
        ensure_equals(*i->second.removed.begin(), string("b"));
 
265
 
 
266
        // Removing items should work
 
267
        ++i;
 
268
        ensure(i != patches.end());
 
269
        ensure_equals(i->first, string("b"));
 
270
        ensure(i->second.added.empty());
 
271
        ensure_equals(i->second.removed.size(), 1u);
 
272
        ensure_equals(*i->second.removed.begin(), string("a"));
 
273
 
 
274
        // Adding items should work
 
275
        ++i;
 
276
        ensure(i != patches.end());
 
277
        ensure_equals(i->first, string("c"));
 
278
        ensure(i->second.removed.empty());
 
279
        ensure_equals(i->second.added.size(), 1u);
 
280
        ensure_equals(*i->second.added.begin(), string("a"));
 
281
 
 
282
        ++i;
 
283
        ensure(i == patches.end());
 
284
}
 
285
 
 
286
 
 
287
}
 
288
 
 
289
#include <tagcoll/TextFormat.tcc>
 
290
#include <tagcoll/patch.tcc>
 
291
 
 
292
// vim:set ts=4 sw=4: