2
* Classes handling tag patches
4
* Copyright (C) 2003 Enrico Zini <enrico@debian.org>
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.
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.
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
21
#include <tests/test-utils.h>
22
#include <tagcoll/patch.h>
23
#include <tagcoll/coll/simple.h>
27
template<typename TAG, typename _Traits>
28
basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& out, const std::set<TAG>& tags)
30
for (typename std::set<TAG>::const_iterator i = tags.begin();
32
if (i == tags.begin())
39
template<typename ITEM, typename TAG>
40
ostream& operator<<(ostream& out, const tagcoll::Patch<ITEM, TAG>& p)
42
out << p.item << ": ";
44
for (typename std::set<TAG>::const_iterator i = p.added.begin();
45
i != p.added.end(); ++i)
53
for (typename std::set<TAG>::const_iterator i = p.removed.begin();
54
i != p.removed.end(); ++i)
70
using namespace tagcoll;
71
using namespace tagcoll::tests;
72
using namespace wibble::operators;
74
struct tagcoll_patches_shar {
76
TESTGRP(tagcoll_patches);
83
Patch<string, int> a("foo");
84
ensure_equals(a.item, "foo");
85
ensure(a.added.empty());
86
ensure(a.removed.empty());
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());
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());
103
// Test the different constructors
104
template<> template<>
107
Patch<string, int> a("foo");
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);
117
std::set<int> removed;
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);
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);
139
// Test non-empty patches
140
template<> template<>
143
Patch<string, int> p("foo");
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);
154
// Check removeRedundant()
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);
165
template<> template<>
173
"d: c::D, e::F, f::g\n"
179
"d: c::D, c::d, e::F\n"
181
coll::Simple<string, string> result;
183
PatchList<string, string> patches;
185
std::set<string> added;
186
std::set<string> removed;
189
removed.insert("c"); removed.insert("d");
190
patches.addPatch(Patch<string, string>("a", added, removed));
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));
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));
201
parseCollection(input_coll, patcher(patches, inserter(result)));
203
coll::Simple<string, string> reference;
204
parseCollection(output_coll, inserter(reference));
206
ensure_coll_equals(reference, result);
210
// Check addPatchInverted
211
template<> template<>
214
PatchList<string, string> patches;
215
patches.addPatchInverted(Patch<string, string>(
217
wibble::singleton(string("tomato")),
218
wibble::singleton(string("ketchup"))));
220
ensure_equals(patches.size(), 2u);
222
PatchList<string, string>::const_iterator i = patches.begin();
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");
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");
237
// Test diffing collections
238
template<> template<>
249
coll::Simple<string, string> coll1;
250
coll::Simple<string, string> coll2;
251
parseCollection(in_coll1, inserter(coll1));
252
parseCollection(in_coll2, inserter(coll2));
254
PatchList<string, string> patches;
255
patches.addPatch(coll1, coll2);
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"));
266
// Removing items should work
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"));
274
// Adding items should work
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"));
283
ensure(i == patches.end());
289
#include <tagcoll/TextFormat.tcc>
290
#include <tagcoll/patch.tcc>
292
// vim:set ts=4 sw=4: