Welcome to mirror list, hosted at ThFree Co, Russian Federation.

path_test.cc « test « lemon-1.3.1 « 3rd « quadriflow « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f2d960381356b9722d810afe38ca8234d1e73ea3 (plain)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* -*- mode: C++; indent-tabs-mode: nil; -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library.
 *
 * Copyright (C) 2003-2013
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided "AS IS" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */

#include <string>
#include <iostream>

#include <lemon/concepts/path.h>
#include <lemon/concepts/digraph.h>
#include <lemon/concept_check.h>

#include <lemon/path.h>
#include <lemon/list_graph.h>

#include "test_tools.h"

using namespace std;
using namespace lemon;

template <typename GR>
void checkConcepts() {
  checkConcept<concepts::Path<GR>, concepts::Path<GR> >();
  checkConcept<concepts::Path<GR>, Path<GR> >();
  checkConcept<concepts::Path<GR>, SimplePath<GR> >();
  checkConcept<concepts::Path<GR>, StaticPath<GR> >();
  checkConcept<concepts::Path<GR>, ListPath<GR> >();
}

// Conecpt checking for path structures
void checkPathConcepts() {
  checkConcepts<concepts::Digraph>();
  checkConcepts<ListDigraph>();
}

// Check if proper copy consructor is called (use valgrind for testing)
template <typename GR, typename P1, typename P2>
void checkCopy(typename GR::Arc a) {
  P1 p;
  p.addBack(a);
  P1 q;
  q = p;
  P1 r(p);
  P2 q2;
  q2 = p;
  P2 r2(p);
}

// Tests for copy constructors and assignment operators of paths
void checkPathCopy() {
  ListDigraph g;
  ListDigraph::Arc a = g.addArc(g.addNode(), g.addNode());

  typedef Path<ListDigraph> Path1;
  typedef SimplePath<ListDigraph> Path2;
  typedef ListPath<ListDigraph> Path3;
  typedef StaticPath<ListDigraph> Path4;
  checkCopy<ListDigraph, Path1, Path2>(a);
  checkCopy<ListDigraph, Path1, Path3>(a);
  checkCopy<ListDigraph, Path1, Path4>(a);
  checkCopy<ListDigraph, Path2, Path1>(a);
  checkCopy<ListDigraph, Path2, Path3>(a);
  checkCopy<ListDigraph, Path2, Path4>(a);
  checkCopy<ListDigraph, Path3, Path1>(a);
  checkCopy<ListDigraph, Path3, Path2>(a);
  checkCopy<ListDigraph, Path3, Path4>(a);
}

// Class for testing path functions
class CheckPathFunctions {
  typedef ListDigraph GR;
  DIGRAPH_TYPEDEFS(GR);
  GR gr;
  const GR& cgr;
  Node n1, n2, n3, n4;
  Node tmp_n;
  Arc a1, a2, a3, a4;
  Arc tmp_a;

public:

  CheckPathFunctions() : cgr(gr) {
    n1 = gr.addNode();
    n2 = gr.addNode();
    n3 = gr.addNode();
    n4 = gr.addNode();
    a1 = gr.addArc(n1, n2);
    a2 = gr.addArc(n2, n3);
    a3 = gr.addArc(n3, n4);
    a4 = gr.addArc(n4, n1);
  }

  void run() {
    checkBackAndFrontInsertablePath<Path<GR> >();
    checkBackAndFrontInsertablePath<ListPath<GR> >();
    checkBackInsertablePath<SimplePath<GR> >();

    checkListPathSplitAndSplice();
  }

private:

  template <typename P>
  void checkBackInsertablePath() {

    // Create and check empty path
    P p;
    const P& cp = p;
    check(cp.empty(), "The path is not empty");
    check(cp.length() == 0, "The path is not empty");
//    check(cp.front() == INVALID, "Wrong front()");
//    check(cp.back() == INVALID, "Wrong back()");
    typename P::ArcIt ai(cp);
    check(ai == INVALID, "Wrong ArcIt");
    check(pathSource(cgr, cp) == INVALID, "Wrong pathSource()");
    check(pathTarget(cgr, cp) == INVALID, "Wrong pathTarget()");
    check(checkPath(cgr, cp), "Wrong checkPath()");
    PathNodeIt<P> ni(cgr, cp);
    check(ni == INVALID, "Wrong PathNodeIt");

    // Check single-arc path
    p.addBack(a1);
    check(!cp.empty(), "Wrong empty()");
    check(cp.length() == 1, "Wrong length");
    check(cp.front() == a1, "Wrong front()");
    check(cp.back() == a1, "Wrong back()");
    check(cp.nth(0) == a1, "Wrong nth()");
    ai = cp.nthIt(0);
    check((tmp_a = ai) == a1, "Wrong nthIt()");
    check(++ai == INVALID, "Wrong nthIt()");
    typename P::ArcIt ai2(cp);
    check((tmp_a = ai2) == a1, "Wrong ArcIt");
    check(++ai2 == INVALID, "Wrong ArcIt");
    check(pathSource(cgr, cp) == n1, "Wrong pathSource()");
    check(pathTarget(cgr, cp) == n2, "Wrong pathTarget()");
    check(checkPath(cgr, cp), "Wrong checkPath()");
    PathNodeIt<P> ni2(cgr, cp);
    check((tmp_n = ni2) == n1, "Wrong PathNodeIt");
    check((tmp_n = ++ni2) == n2, "Wrong PathNodeIt");
    check(++ni2 == INVALID, "Wrong PathNodeIt");

    // Check adding more arcs
    p.addBack(a2);
    p.addBack(a3);
    check(!cp.empty(), "Wrong empty()");
    check(cp.length() == 3, "Wrong length");
    check(cp.front() == a1, "Wrong front()");
    check(cp.back() == a3, "Wrong back()");
    check(cp.nth(0) == a1, "Wrong nth()");
    check(cp.nth(1) == a2, "Wrong nth()");
    check(cp.nth(2) == a3, "Wrong nth()");
    typename P::ArcIt ai3(cp);
    check((tmp_a = ai3) == a1, "Wrong ArcIt");
    check((tmp_a = ++ai3) == a2, "Wrong nthIt()");
    check((tmp_a = ++ai3) == a3, "Wrong nthIt()");
    check(++ai3 == INVALID, "Wrong nthIt()");
    ai = cp.nthIt(0);
    check((tmp_a = ai) == a1, "Wrong nthIt()");
    check((tmp_a = ++ai) == a2, "Wrong nthIt()");
    ai = cp.nthIt(2);
    check((tmp_a = ai) == a3, "Wrong nthIt()");
    check(++ai == INVALID, "Wrong nthIt()");
    check(pathSource(cgr, cp) == n1, "Wrong pathSource()");
    check(pathTarget(cgr, cp) == n4, "Wrong pathTarget()");
    check(checkPath(cgr, cp), "Wrong checkPath()");
    PathNodeIt<P> ni3(cgr, cp);
    check((tmp_n = ni3) == n1, "Wrong PathNodeIt");
    check((tmp_n = ++ni3) == n2, "Wrong PathNodeIt");
    check((tmp_n = ++ni3) == n3, "Wrong PathNodeIt");
    check((tmp_n = ++ni3) == n4, "Wrong PathNodeIt");
    check(++ni3 == INVALID, "Wrong PathNodeIt");

    // Check arc removal and addition
    p.eraseBack();
    p.eraseBack();
    p.addBack(a2);
    check(!cp.empty(), "Wrong empty()");
    check(cp.length() == 2, "Wrong length");
    check(cp.front() == a1, "Wrong front()");
    check(cp.back() == a2, "Wrong back()");
    check(pathSource(cgr, cp) == n1, "Wrong pathSource()");
    check(pathTarget(cgr, cp) == n3, "Wrong pathTarget()");
    check(checkPath(cgr, cp), "Wrong checkPath()");

    // Check clear()
    p.clear();
    check(cp.empty(), "The path is not empty");
    check(cp.length() == 0, "The path is not empty");

    // Check inconsistent path
    p.addBack(a4);
    p.addBack(a2);
    p.addBack(a1);
    check(!cp.empty(), "Wrong empty()");
    check(cp.length() == 3, "Wrong length");
    check(cp.front() == a4, "Wrong front()");
    check(cp.back() == a1, "Wrong back()");
    check(pathSource(cgr, cp) == n4, "Wrong pathSource()");
    check(pathTarget(cgr, cp) == n2, "Wrong pathTarget()");
    check(!checkPath(cgr, cp), "Wrong checkPath()");
  }

  template <typename P>
  void checkBackAndFrontInsertablePath() {

    // Include back insertable test cases
    checkBackInsertablePath<P>();

    // Check front and back insertion
    P p;
    const P& cp = p;
    p.addFront(a4);
    p.addBack(a1);
    p.addFront(a3);
    check(!cp.empty(), "Wrong empty()");
    check(cp.length() == 3, "Wrong length");
    check(cp.front() == a3, "Wrong front()");
    check(cp.back() == a1, "Wrong back()");
    check(cp.nth(0) == a3, "Wrong nth()");
    check(cp.nth(1) == a4, "Wrong nth()");
    check(cp.nth(2) == a1, "Wrong nth()");
    typename P::ArcIt ai(cp);
    check((tmp_a = ai) == a3, "Wrong ArcIt");
    check((tmp_a = ++ai) == a4, "Wrong nthIt()");
    check((tmp_a = ++ai) == a1, "Wrong nthIt()");
    check(++ai == INVALID, "Wrong nthIt()");
    ai = cp.nthIt(0);
    check((tmp_a = ai) == a3, "Wrong nthIt()");
    check((tmp_a = ++ai) == a4, "Wrong nthIt()");
    ai = cp.nthIt(2);
    check((tmp_a = ai) == a1, "Wrong nthIt()");
    check(++ai == INVALID, "Wrong nthIt()");
    check(pathSource(cgr, cp) == n3, "Wrong pathSource()");
    check(pathTarget(cgr, cp) == n2, "Wrong pathTarget()");
    check(checkPath(cgr, cp), "Wrong checkPath()");

    // Check eraseFront()
    p.eraseFront();
    p.addBack(a2);
    check(!cp.empty(), "Wrong empty()");
    check(cp.length() == 3, "Wrong length");
    check(cp.front() == a4, "Wrong front()");
    check(cp.back() == a2, "Wrong back()");
    check(cp.nth(0) == a4, "Wrong nth()");
    check(cp.nth(1) == a1, "Wrong nth()");
    check(cp.nth(2) == a2, "Wrong nth()");
    typename P::ArcIt ai2(cp);
    check((tmp_a = ai2) == a4, "Wrong ArcIt");
    check((tmp_a = ++ai2) == a1, "Wrong nthIt()");
    check((tmp_a = ++ai2) == a2, "Wrong nthIt()");
    check(++ai2 == INVALID, "Wrong nthIt()");
    ai = cp.nthIt(0);
    check((tmp_a = ai) == a4, "Wrong nthIt()");
    check((tmp_a = ++ai) == a1, "Wrong nthIt()");
    ai = cp.nthIt(2);
    check((tmp_a = ai) == a2, "Wrong nthIt()");
    check(++ai == INVALID, "Wrong nthIt()");
    check(pathSource(cgr, cp) == n4, "Wrong pathSource()");
    check(pathTarget(cgr, cp) == n3, "Wrong pathTarget()");
    check(checkPath(cgr, cp), "Wrong checkPath()");
  }

  void checkListPathSplitAndSplice() {

    // Build a path with spliceFront() and spliceBack()
    ListPath<GR> p1, p2, p3, p4;
    p1.addBack(a3);
    p1.addFront(a2);
    p2.addBack(a1);
    p1.spliceFront(p2);
    p3.addFront(a4);
    p1.spliceBack(p3);
    check(p1.length() == 4, "Wrong length");
    check(p1.front() == a1, "Wrong front()");
    check(p1.back() == a4, "Wrong back()");
    ListPath<GR>::ArcIt ai(p1);
    check((tmp_a = ai) == a1, "Wrong ArcIt");
    check((tmp_a = ++ai) == a2, "Wrong nthIt()");
    check((tmp_a = ++ai) == a3, "Wrong nthIt()");
    check((tmp_a = ++ai) == a4, "Wrong nthIt()");
    check(++ai == INVALID, "Wrong nthIt()");
    check(checkPath(cgr, p1), "Wrong checkPath()");

    // Check split()
    p1.split(p1.nthIt(2), p2);
    check(p1.length() == 2, "Wrong length");
    ai = p1.nthIt(0);
    check((tmp_a = ai) == a1, "Wrong ArcIt");
    check((tmp_a = ++ai) == a2, "Wrong nthIt()");
    check(++ai == INVALID, "Wrong nthIt()");
    check(checkPath(cgr, p1), "Wrong checkPath()");
    check(p2.length() == 2, "Wrong length");
    ai = p2.nthIt(0);
    check((tmp_a = ai) == a3, "Wrong ArcIt");
    check((tmp_a = ++ai) == a4, "Wrong nthIt()");
    check(++ai == INVALID, "Wrong nthIt()");
    check(checkPath(cgr, p2), "Wrong checkPath()");

    // Check split() and splice()
    p1.spliceFront(p2);
    p1.split(p1.nthIt(2), p2);
    p2.split(p2.nthIt(1), p3);
    p2.spliceBack(p1);
    p2.splice(p2.nthIt(1), p3);
    check(p2.length() == 4, "Wrong length");
    check(p2.front() == a1, "Wrong front()");
    check(p2.back() == a4, "Wrong back()");
    ai = p2.nthIt(0);
    check((tmp_a = ai) == a1, "Wrong ArcIt");
    check((tmp_a = ++ai) == a2, "Wrong nthIt()");
    check((tmp_a = ++ai) == a3, "Wrong nthIt()");
    check((tmp_a = ++ai) == a4, "Wrong nthIt()");
    check(++ai == INVALID, "Wrong nthIt()");
    check(checkPath(cgr, p2), "Wrong checkPath()");
  }

};

int main() {
  checkPathConcepts();
  checkPathCopy();
  CheckPathFunctions cpf;
  cpf.run();

  return 0;
}