1 | /* -*- C++ -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2006 |
---|
6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
8 | * |
---|
9 | * Permission to use, modify and distribute this software is granted |
---|
10 | * provided that this copyright notice appears in all copies. For |
---|
11 | * precise terms see the accompanying LICENSE file. |
---|
12 | * |
---|
13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
14 | * express or implied, and with no claim as to its suitability for any |
---|
15 | * purpose. |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | #include <iostream> |
---|
20 | #include <vector> |
---|
21 | |
---|
22 | #include <lemon/graph_utils.h> |
---|
23 | |
---|
24 | #include <lemon/list_graph.h> |
---|
25 | #include <lemon/smart_graph.h> |
---|
26 | #include <lemon/full_graph.h> |
---|
27 | |
---|
28 | #include "test_tools.h" |
---|
29 | #include "graph_utils_test.h" |
---|
30 | |
---|
31 | |
---|
32 | using namespace lemon; |
---|
33 | |
---|
34 | template<class Graph> |
---|
35 | void checkSnapDeg() |
---|
36 | { |
---|
37 | Graph g; |
---|
38 | typename Graph::Node n1=g.addNode(); |
---|
39 | typename Graph::Node n2=g.addNode(); |
---|
40 | |
---|
41 | InDegMap<Graph> ind(g); |
---|
42 | |
---|
43 | g.addEdge(n1,n2); |
---|
44 | |
---|
45 | typename Graph::Snapshot snap(g); |
---|
46 | |
---|
47 | OutDegMap<Graph> outd(g); |
---|
48 | |
---|
49 | check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value."); |
---|
50 | check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value."); |
---|
51 | |
---|
52 | g.addEdge(n1,n2); |
---|
53 | g.addEdge(n2,n1); |
---|
54 | |
---|
55 | check(ind[n1]==1 && ind[n2]==2, "Wrong InDegMap value."); |
---|
56 | check(outd[n1]==2 && outd[n2]==1, "Wrong OutDegMap value."); |
---|
57 | |
---|
58 | snap.restore(); |
---|
59 | |
---|
60 | check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value."); |
---|
61 | check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value."); |
---|
62 | |
---|
63 | } |
---|
64 | |
---|
65 | int main() { |
---|
66 | ///\file |
---|
67 | { // checking list graph |
---|
68 | checkGraphCounters<ListGraph>(); |
---|
69 | checkFindEdge<ListGraph>(); |
---|
70 | } |
---|
71 | { // checking smart graph |
---|
72 | checkGraphCounters<SmartGraph>(); |
---|
73 | checkFindEdge<SmartGraph>(); |
---|
74 | } |
---|
75 | { |
---|
76 | int num = 5; |
---|
77 | FullGraph fg(num); |
---|
78 | check(countNodes(fg) == num, "FullGraph: wrong node number."); |
---|
79 | check(countEdges(fg) == num*num, "FullGraph: wrong edge number."); |
---|
80 | for (FullGraph::NodeIt src(fg); src != INVALID; ++src) { |
---|
81 | for (FullGraph::NodeIt trg(fg); trg != INVALID; ++trg) { |
---|
82 | ConEdgeIt<FullGraph> con(fg, src, trg); |
---|
83 | check(con != INVALID, "There is no connecting edge."); |
---|
84 | check(fg.source(con) == src, "Wrong source."); |
---|
85 | check(fg.target(con) == trg, "Wrong target."); |
---|
86 | check(++con == INVALID, "There is more connecting edge."); |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | //check In/OutDegMap (and Snapshot feature) |
---|
92 | |
---|
93 | checkSnapDeg<ListGraph>(); |
---|
94 | checkSnapDeg<SmartGraph>(); |
---|
95 | |
---|
96 | { |
---|
97 | const int nodeNum = 10; |
---|
98 | const int edgeNum = 100; |
---|
99 | ListGraph graph; |
---|
100 | InDegMap<ListGraph> inDeg(graph); |
---|
101 | OutDegMap<ListGraph> outDeg(graph); |
---|
102 | std::vector<ListGraph::Node> nodes(nodeNum); |
---|
103 | for (int i = 0; i < nodeNum; ++i) { |
---|
104 | nodes[i] = graph.addNode(); |
---|
105 | } |
---|
106 | std::vector<ListGraph::Edge> edges(edgeNum); |
---|
107 | for (int i = 0; i < edgeNum; ++i) { |
---|
108 | edges[i] = |
---|
109 | graph.addEdge(nodes[urandom(nodeNum)], nodes[urandom(nodeNum)]); |
---|
110 | } |
---|
111 | for (int i = 0; i < nodeNum; ++i) { |
---|
112 | check(inDeg[nodes[i]] == countInEdges(graph, nodes[i]), |
---|
113 | "Wrong in degree map"); |
---|
114 | } |
---|
115 | for (int i = 0; i < nodeNum; ++i) { |
---|
116 | check(outDeg[nodes[i]] == countOutEdges(graph, nodes[i]), |
---|
117 | "Wrong in degree map"); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | ///Everything is OK |
---|
122 | std::cout << __FILE__ ": All tests passed.\n"; |
---|
123 | |
---|
124 | return 0; |
---|
125 | } |
---|