gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Extend maps_test.cc (#302)
0 1 0
default
1 file changed with 85 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 48 line context
... ...
@@ -3,86 +3,94 @@
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <deque>
20 20
#include <set>
21 21

	
22 22
#include <lemon/concept_check.h>
23 23
#include <lemon/concepts/maps.h>
24 24
#include <lemon/maps.h>
25 25
#include <lemon/list_graph.h>
26 26
#include <lemon/smart_graph.h>
27
#include <lemon/adaptors.h>
28
#include <lemon/dfs.h>
27 29

	
28 30
#include "test_tools.h"
29 31

	
30 32
using namespace lemon;
31 33
using namespace lemon::concepts;
32 34

	
33 35
struct A {};
34 36
inline bool operator<(A, A) { return true; }
35 37
struct B {};
36 38

	
37 39
class C {
38 40
  int x;
39 41
public:
40 42
  C(int _x) : x(_x) {}
41 43
};
42 44

	
43 45
class F {
44 46
public:
45 47
  typedef A argument_type;
46 48
  typedef B result_type;
47 49

	
48 50
  B operator()(const A&) const { return B(); }
49 51
private:
50 52
  F& operator=(const F&);
51 53
};
52 54

	
53 55
int func(A) { return 3; }
54 56

	
55 57
int binc(int a, B) { return a+1; }
56 58

	
57 59
typedef ReadMap<A, double> DoubleMap;
58 60
typedef ReadWriteMap<A, double> DoubleWriteMap;
59 61
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
60 62

	
61 63
typedef ReadMap<A, bool> BoolMap;
62 64
typedef ReadWriteMap<A, bool> BoolWriteMap;
63 65
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
64 66

	
67
template<typename Map1, typename Map2, typename ItemIt>
68
void compareMap(const Map1& map1, const Map2& map2, ItemIt it) {
69
  for (; it != INVALID; ++it)
70
    check(map1[it] == map2[it], "The maps are not equal");
71
}
72

	
65 73
int main()
66 74
{
67 75
  // Map concepts
68 76
  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
69 77
  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
70 78
  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
71 79
  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
72 80
  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
73 81
  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
74 82
  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
75 83
  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
76 84

	
77 85
  // NullMap
78 86
  {
79 87
    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
80 88
    NullMap<A,B> map1;
81 89
    NullMap<A,B> map2 = map1;
82 90
    map1 = nullMap<A,B>();
83 91
  }
84 92

	
85 93
  // ConstMap
86 94
  {
87 95
    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
88 96
    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
... ...
@@ -332,48 +340,78 @@
332 340
    typedef std::vector<int> vec;
333 341
    checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
334 342
    checkConcept<WriteMap<int, bool>,
335 343
                 LoggerBoolMap<std::back_insert_iterator<vec> > >();
336 344

	
337 345
    vec v1;
338 346
    vec v2(10);
339 347
    LoggerBoolMap<std::back_insert_iterator<vec> >
340 348
      map1(std::back_inserter(v1));
341 349
    LoggerBoolMap<vec::iterator> map2(v2.begin());
342 350
    map1.set(10, false);
343 351
    map1.set(20, true);   map2.set(20, true);
344 352
    map1.set(30, false);  map2.set(40, false);
345 353
    map1.set(50, true);   map2.set(50, true);
346 354
    map1.set(60, true);   map2.set(60, true);
347 355
    check(v1.size() == 3 && v2.size() == 10 &&
348 356
          v1[0]==20 && v1[1]==50 && v1[2]==60 &&
349 357
          v2[0]==20 && v2[1]==50 && v2[2]==60,
350 358
          "Something is wrong with LoggerBoolMap");
351 359

	
352 360
    int i = 0;
353 361
    for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
354 362
          it != map2.end(); ++it )
355 363
      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
364
    
365
    typedef ListDigraph Graph;
366
    DIGRAPH_TYPEDEFS(Graph);
367
    Graph gr;
368

	
369
    Node n0 = gr.addNode();
370
    Node n1 = gr.addNode();
371
    Node n2 = gr.addNode();
372
    Node n3 = gr.addNode();
373
    
374
    gr.addArc(n3, n0);
375
    gr.addArc(n3, n2);
376
    gr.addArc(n0, n2);
377
    gr.addArc(n2, n1);
378
    gr.addArc(n0, n1);
379
    
380
    {
381
      std::vector<Node> v;
382
      dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run();
383

	
384
      check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
385
            "Something is wrong with LoggerBoolMap");
386
    }
387
    {
388
      std::vector<Node> v(countNodes(gr));
389
      dfs(gr).processedMap(loggerBoolMap(v.begin())).run();
390
      
391
      check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3,
392
            "Something is wrong with LoggerBoolMap");
393
    }
356 394
  }
357 395
  
358 396
  // IdMap, RangeIdMap
359 397
  {
360 398
    typedef ListDigraph Graph;
361 399
    DIGRAPH_TYPEDEFS(Graph);
362 400

	
363 401
    checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
364 402
    checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
365 403
    checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
366 404
    checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
367 405
    
368 406
    Graph gr;
369 407
    IdMap<Graph, Node> nmap(gr);
370 408
    IdMap<Graph, Arc> amap(gr);
371 409
    RangeIdMap<Graph, Node> nrmap(gr);
372 410
    RangeIdMap<Graph, Arc> armap(gr);
373 411
    
374 412
    Node n0 = gr.addNode();
375 413
    Node n1 = gr.addNode();
376 414
    Node n2 = gr.addNode();
377 415
    
378 416
    Arc a0 = gr.addArc(n0, n1);
379 417
    Arc a1 = gr.addArc(n0, n2);
... ...
@@ -406,48 +444,95 @@
406 444

	
407 445
    check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
408 446
    check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
409 447
    
410 448
    gr.erase(n1);
411 449
    
412 450
    if (nrmap[n0] == 1) nrmap.swap(n0, n2);
413 451
    nrmap.swap(n2, n0);
414 452
    if (armap[a1] == 1) armap.swap(a1, a3);
415 453
    armap.swap(a3, a1);
416 454
    
417 455
    check(nrmap.size() == 2 && armap.size() == 2,
418 456
          "Wrong RangeIdMap::size()");
419 457

	
420 458
    check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
421 459
    check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
422 460
    
423 461
    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
424 462
    check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
425 463

	
426 464
    check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
427 465
    check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
428 466
  }
429 467
  
468
  // SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap
469
  {
470
    typedef ListGraph Graph;
471
    GRAPH_TYPEDEFS(Graph);
472
    
473
    checkConcept<ReadMap<Arc, Node>, SourceMap<Graph> >();
474
    checkConcept<ReadMap<Arc, Node>, TargetMap<Graph> >();
475
    checkConcept<ReadMap<Edge, Arc>, ForwardMap<Graph> >();
476
    checkConcept<ReadMap<Edge, Arc>, BackwardMap<Graph> >();
477
    checkConcept<ReadMap<Node, int>, InDegMap<Graph> >();
478
    checkConcept<ReadMap<Node, int>, OutDegMap<Graph> >();
479

	
480
    Graph gr;
481
    Node n0 = gr.addNode();
482
    Node n1 = gr.addNode();
483
    Node n2 = gr.addNode();
484
    
485
    gr.addEdge(n0,n1);
486
    gr.addEdge(n1,n2);
487
    gr.addEdge(n0,n2);
488
    gr.addEdge(n2,n1);
489
    gr.addEdge(n1,n2);
490
    gr.addEdge(n0,n1);
491
    
492
    for (EdgeIt e(gr); e != INVALID; ++e) {
493
      check(forwardMap(gr)[e] == gr.direct(e, true), "Wrong ForwardMap");
494
      check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap");
495
    }
496
    
497
    compareMap(sourceMap(orienter(gr, constMap<Edge, bool>(true))),
498
               targetMap(orienter(gr, constMap<Edge, bool>(false))),
499
               EdgeIt(gr));
500

	
501
    typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph;
502
    Digraph dgr(gr, constMap<Edge, bool>(true));
503
    OutDegMap<Digraph> odm(dgr);
504
    InDegMap<Digraph> idm(dgr);
505
    
506
    check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 1, "Wrong OutDegMap");
507
    check(idm[n0] == 0 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
508
   
509
    gr.addEdge(n2, n0);
510

	
511
    check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 2, "Wrong OutDegMap");
512
    check(idm[n0] == 1 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap");
513
  }
514
  
430 515
  // CrossRefMap
431 516
  {
432 517
    typedef ListDigraph Graph;
433 518
    DIGRAPH_TYPEDEFS(Graph);
434 519

	
435 520
    checkConcept<ReadWriteMap<Node, int>,
436 521
                 CrossRefMap<Graph, Node, int> >();
437 522
    checkConcept<ReadWriteMap<Node, bool>,
438 523
                 CrossRefMap<Graph, Node, bool> >();
439 524
    checkConcept<ReadWriteMap<Node, double>,
440 525
                 CrossRefMap<Graph, Node, double> >();
441 526
    
442 527
    Graph gr;
443 528
    typedef CrossRefMap<Graph, Node, char> CRMap;
444 529
    typedef CRMap::ValueIterator ValueIt;
445 530
    CRMap map(gr);
446 531
    
447 532
    Node n0 = gr.addNode();
448 533
    Node n1 = gr.addNode();
449 534
    Node n2 = gr.addNode();
450 535
    
451 536
    map.set(n0, 'A');
452 537
    map.set(n1, 'B');
453 538
    map.set(n2, 'C');
0 comments (0 inline)