alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@57: * alpar@209: * This file is a part of LEMON, a generic C++ optimization library. deba@57: * alpar@107: * Copyright (C) 2003-2008 deba@57: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@57: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@57: * deba@57: * Permission to use, modify and distribute this software is granted deba@57: * provided that this copyright notice appears in all copies. For deba@57: * precise terms see the accompanying LICENSE file. deba@57: * deba@57: * This software is provided "AS IS" with no warranty of any kind, deba@57: * express or implied, and with no claim as to its suitability for any deba@57: * purpose. deba@57: * deba@57: */ deba@57: deba@57: #include deba@57: #include kpeter@171: #include deba@57: //#include deba@57: //#include deba@57: deba@57: #include "test_tools.h" kpeter@171: #include "graph_test.h" deba@57: deba@57: using namespace lemon; deba@57: using namespace lemon::concepts; deba@57: deba@228: template kpeter@338: void checkDigraphBuild() { deba@228: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); deba@228: Digraph G; deba@228: deba@228: checkGraphNodeList(G, 0); deba@228: checkGraphArcList(G, 0); deba@228: deba@228: Node deba@228: n1 = G.addNode(), deba@228: n2 = G.addNode(), deba@228: n3 = G.addNode(); deba@228: checkGraphNodeList(G, 3); deba@228: checkGraphArcList(G, 0); deba@228: deba@228: Arc a1 = G.addArc(n1, n2); deba@228: check(G.source(a1) == n1 && G.target(a1) == n2, "Wrong arc"); deba@228: checkGraphNodeList(G, 3); deba@228: checkGraphArcList(G, 1); deba@228: deba@228: checkGraphOutArcList(G, n1, 1); deba@228: checkGraphOutArcList(G, n2, 0); deba@228: checkGraphOutArcList(G, n3, 0); deba@228: deba@228: checkGraphInArcList(G, n1, 0); deba@228: checkGraphInArcList(G, n2, 1); deba@228: checkGraphInArcList(G, n3, 0); deba@228: deba@228: checkGraphConArcList(G, 1); deba@228: kpeter@338: Arc a2 = G.addArc(n2, n1), kpeter@338: a3 = G.addArc(n2, n3), kpeter@338: a4 = G.addArc(n2, n3); kpeter@338: kpeter@338: checkGraphNodeList(G, 3); kpeter@338: checkGraphArcList(G, 4); kpeter@338: kpeter@338: checkGraphOutArcList(G, n1, 1); kpeter@338: checkGraphOutArcList(G, n2, 3); kpeter@338: checkGraphOutArcList(G, n3, 0); kpeter@338: kpeter@338: checkGraphInArcList(G, n1, 1); kpeter@338: checkGraphInArcList(G, n2, 1); kpeter@338: checkGraphInArcList(G, n3, 2); kpeter@338: kpeter@338: checkGraphConArcList(G, 4); kpeter@338: kpeter@338: checkNodeIds(G); kpeter@338: checkArcIds(G); kpeter@338: checkGraphNodeMap(G); kpeter@338: checkGraphArcMap(G); kpeter@338: } kpeter@338: kpeter@338: template kpeter@338: void checkDigraphSplit() { kpeter@338: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@338: kpeter@338: Digraph G; kpeter@338: Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode(); kpeter@338: Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1), kpeter@338: a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3); kpeter@338: kpeter@338: Node n4 = G.split(n2); kpeter@338: kpeter@338: check(G.target(OutArcIt(G, n2)) == n4 && kpeter@338: G.source(InArcIt(G, n4)) == n2, kpeter@338: "Wrong split."); kpeter@338: kpeter@338: checkGraphNodeList(G, 4); kpeter@338: checkGraphArcList(G, 5); kpeter@338: kpeter@338: checkGraphOutArcList(G, n1, 1); kpeter@338: checkGraphOutArcList(G, n2, 1); kpeter@338: checkGraphOutArcList(G, n3, 0); kpeter@338: checkGraphOutArcList(G, n4, 3); kpeter@338: kpeter@338: checkGraphInArcList(G, n1, 1); kpeter@338: checkGraphInArcList(G, n2, 1); kpeter@338: checkGraphInArcList(G, n3, 2); kpeter@338: checkGraphInArcList(G, n4, 1); kpeter@338: kpeter@338: checkGraphConArcList(G, 5); kpeter@338: } kpeter@338: kpeter@338: template kpeter@338: void checkDigraphAlter() { kpeter@338: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@338: kpeter@338: Digraph G; kpeter@338: Node n1 = G.addNode(), n2 = G.addNode(), kpeter@338: n3 = G.addNode(), n4 = G.addNode(); kpeter@338: Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1), kpeter@338: a3 = G.addArc(n4, n3), a4 = G.addArc(n4, n3), kpeter@338: a5 = G.addArc(n2, n4); kpeter@338: kpeter@338: checkGraphNodeList(G, 4); kpeter@338: checkGraphArcList(G, 5); kpeter@338: kpeter@338: // Check changeSource() and changeTarget() kpeter@338: G.changeTarget(a4, n1); kpeter@338: kpeter@338: checkGraphNodeList(G, 4); kpeter@338: checkGraphArcList(G, 5); kpeter@338: kpeter@338: checkGraphOutArcList(G, n1, 1); kpeter@338: checkGraphOutArcList(G, n2, 1); kpeter@338: checkGraphOutArcList(G, n3, 0); kpeter@338: checkGraphOutArcList(G, n4, 3); kpeter@338: kpeter@338: checkGraphInArcList(G, n1, 2); kpeter@338: checkGraphInArcList(G, n2, 1); kpeter@338: checkGraphInArcList(G, n3, 1); kpeter@338: checkGraphInArcList(G, n4, 1); kpeter@338: kpeter@338: checkGraphConArcList(G, 5); kpeter@338: kpeter@338: G.changeSource(a4, n3); kpeter@338: kpeter@338: checkGraphNodeList(G, 4); kpeter@338: checkGraphArcList(G, 5); kpeter@338: kpeter@338: checkGraphOutArcList(G, n1, 1); kpeter@338: checkGraphOutArcList(G, n2, 1); kpeter@338: checkGraphOutArcList(G, n3, 1); kpeter@338: checkGraphOutArcList(G, n4, 2); kpeter@338: kpeter@338: checkGraphInArcList(G, n1, 2); kpeter@338: checkGraphInArcList(G, n2, 1); kpeter@338: checkGraphInArcList(G, n3, 1); kpeter@338: checkGraphInArcList(G, n4, 1); kpeter@338: kpeter@338: checkGraphConArcList(G, 5); kpeter@338: kpeter@338: // Check contract() kpeter@338: G.contract(n2, n4, false); kpeter@338: kpeter@338: checkGraphNodeList(G, 3); kpeter@338: checkGraphArcList(G, 5); kpeter@338: kpeter@338: checkGraphOutArcList(G, n1, 1); kpeter@338: checkGraphOutArcList(G, n2, 3); kpeter@338: checkGraphOutArcList(G, n3, 1); kpeter@338: kpeter@338: checkGraphInArcList(G, n1, 2); kpeter@338: checkGraphInArcList(G, n2, 2); kpeter@338: checkGraphInArcList(G, n3, 1); kpeter@338: kpeter@338: checkGraphConArcList(G, 5); kpeter@338: kpeter@338: G.contract(n2, n1); kpeter@338: kpeter@338: checkGraphNodeList(G, 2); kpeter@338: checkGraphArcList(G, 3); kpeter@338: kpeter@338: checkGraphOutArcList(G, n2, 2); kpeter@338: checkGraphOutArcList(G, n3, 1); kpeter@338: kpeter@338: checkGraphInArcList(G, n2, 2); kpeter@338: checkGraphInArcList(G, n3, 1); kpeter@338: kpeter@338: checkGraphConArcList(G, 3); kpeter@338: } kpeter@338: kpeter@338: template kpeter@338: void checkDigraphErase() { kpeter@338: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@338: kpeter@338: Digraph G; kpeter@338: Node n1 = G.addNode(), n2 = G.addNode(), kpeter@338: n3 = G.addNode(), n4 = G.addNode(); kpeter@338: Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1), kpeter@338: a3 = G.addArc(n4, n3), a4 = G.addArc(n3, n1), kpeter@338: a5 = G.addArc(n2, n4); kpeter@338: kpeter@338: // Check arc deletion kpeter@338: G.erase(a1); kpeter@338: kpeter@338: checkGraphNodeList(G, 4); kpeter@338: checkGraphArcList(G, 4); kpeter@338: kpeter@338: checkGraphOutArcList(G, n1, 0); kpeter@338: checkGraphOutArcList(G, n2, 1); kpeter@338: checkGraphOutArcList(G, n3, 1); kpeter@338: checkGraphOutArcList(G, n4, 2); kpeter@338: kpeter@338: checkGraphInArcList(G, n1, 2); kpeter@338: checkGraphInArcList(G, n2, 0); kpeter@338: checkGraphInArcList(G, n3, 1); kpeter@338: checkGraphInArcList(G, n4, 1); kpeter@338: kpeter@338: checkGraphConArcList(G, 4); kpeter@338: kpeter@338: // Check node deletion kpeter@338: G.erase(n4); kpeter@338: kpeter@338: checkGraphNodeList(G, 3); kpeter@338: checkGraphArcList(G, 1); kpeter@338: kpeter@338: checkGraphOutArcList(G, n1, 0); kpeter@338: checkGraphOutArcList(G, n2, 0); kpeter@338: checkGraphOutArcList(G, n3, 1); kpeter@338: checkGraphOutArcList(G, n4, 0); kpeter@338: kpeter@338: checkGraphInArcList(G, n1, 1); kpeter@338: checkGraphInArcList(G, n2, 0); kpeter@338: checkGraphInArcList(G, n3, 0); kpeter@338: checkGraphInArcList(G, n4, 0); kpeter@338: kpeter@338: checkGraphConArcList(G, 1); kpeter@338: } kpeter@338: kpeter@338: kpeter@338: template kpeter@338: void checkDigraphSnapshot() { kpeter@338: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@338: kpeter@338: Digraph G; kpeter@338: Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode(); kpeter@338: Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1), kpeter@338: a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3); kpeter@338: kpeter@338: typename Digraph::Snapshot snapshot(G); kpeter@338: kpeter@338: Node n = G.addNode(); kpeter@338: G.addArc(n3, n); kpeter@338: G.addArc(n, n3); kpeter@338: kpeter@338: checkGraphNodeList(G, 4); kpeter@338: checkGraphArcList(G, 6); kpeter@338: kpeter@338: snapshot.restore(); kpeter@338: deba@228: checkGraphNodeList(G, 3); deba@228: checkGraphArcList(G, 4); deba@228: deba@228: checkGraphOutArcList(G, n1, 1); deba@228: checkGraphOutArcList(G, n2, 3); deba@228: checkGraphOutArcList(G, n3, 0); deba@228: deba@228: checkGraphInArcList(G, n1, 1); deba@228: checkGraphInArcList(G, n2, 1); deba@228: checkGraphInArcList(G, n3, 2); deba@228: deba@228: checkGraphConArcList(G, 4); deba@228: deba@228: checkNodeIds(G); deba@228: checkArcIds(G); deba@228: checkGraphNodeMap(G); deba@228: checkGraphArcMap(G); deba@228: kpeter@338: G.addNode(); kpeter@338: snapshot.save(G); kpeter@338: kpeter@338: G.addArc(G.addNode(), G.addNode()); kpeter@338: kpeter@338: snapshot.restore(); kpeter@338: kpeter@338: checkGraphNodeList(G, 4); kpeter@338: checkGraphArcList(G, 4); deba@228: } deba@228: deba@228: void checkConcepts() { kpeter@171: { // Checking digraph components deba@57: checkConcept(); deba@57: alpar@209: checkConcept, deba@57: IDableDigraphComponent<> >(); deba@57: alpar@209: checkConcept, deba@57: IterableDigraphComponent<> >(); deba@57: alpar@209: checkConcept, deba@57: MappableDigraphComponent<> >(); deba@57: } kpeter@171: { // Checking skeleton digraph deba@57: checkConcept(); deba@57: } kpeter@171: { // Checking ListDigraph kpeter@171: checkConcept(); deba@57: checkConcept, ListDigraph>(); deba@57: checkConcept, ListDigraph>(); deba@57: checkConcept, ListDigraph>(); deba@57: checkConcept, ListDigraph>(); kpeter@171: } kpeter@171: { // Checking SmartDigraph kpeter@171: checkConcept(); kpeter@171: checkConcept, SmartDigraph>(); kpeter@171: checkConcept, SmartDigraph>(); kpeter@171: checkConcept, SmartDigraph>(); kpeter@171: } kpeter@171: // { // Checking FullDigraph kpeter@171: // checkConcept(); kpeter@171: // } kpeter@171: // { // Checking HyperCubeDigraph kpeter@171: // checkConcept(); kpeter@171: // } kpeter@171: } deba@57: kpeter@171: template deba@228: void checkDigraphValidity() { kpeter@171: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@171: Digraph g; kpeter@171: kpeter@171: Node kpeter@171: n1 = g.addNode(), kpeter@171: n2 = g.addNode(), kpeter@171: n3 = g.addNode(); kpeter@171: kpeter@171: Arc kpeter@171: e1 = g.addArc(n1, n2), kpeter@171: e2 = g.addArc(n2, n3); kpeter@171: kpeter@171: check(g.valid(n1), "Wrong validity check"); kpeter@171: check(g.valid(e1), "Wrong validity check"); kpeter@171: kpeter@171: check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); kpeter@171: check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); kpeter@171: } kpeter@171: kpeter@171: template deba@228: void checkDigraphValidityErase() { kpeter@171: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@171: Digraph g; kpeter@171: kpeter@171: Node kpeter@171: n1 = g.addNode(), kpeter@171: n2 = g.addNode(), kpeter@171: n3 = g.addNode(); kpeter@171: kpeter@171: Arc kpeter@171: e1 = g.addArc(n1, n2), kpeter@171: e2 = g.addArc(n2, n3); kpeter@171: kpeter@171: check(g.valid(n1), "Wrong validity check"); kpeter@171: check(g.valid(e1), "Wrong validity check"); kpeter@171: kpeter@171: g.erase(n1); kpeter@171: kpeter@171: check(!g.valid(n1), "Wrong validity check"); kpeter@171: check(g.valid(n2), "Wrong validity check"); kpeter@171: check(g.valid(n3), "Wrong validity check"); kpeter@171: check(!g.valid(e1), "Wrong validity check"); kpeter@171: check(g.valid(e2), "Wrong validity check"); kpeter@171: kpeter@171: check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); kpeter@171: check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); kpeter@171: } kpeter@171: deba@228: void checkDigraphs() { kpeter@171: { // Checking ListDigraph kpeter@338: checkDigraphBuild(); kpeter@338: checkDigraphSplit(); kpeter@338: checkDigraphAlter(); kpeter@338: checkDigraphErase(); kpeter@338: checkDigraphSnapshot(); deba@228: checkDigraphValidityErase(); deba@57: } kpeter@171: { // Checking SmartDigraph kpeter@338: checkDigraphBuild(); kpeter@338: checkDigraphSplit(); kpeter@338: checkDigraphSnapshot(); deba@228: checkDigraphValidity(); kpeter@171: } kpeter@171: } deba@57: kpeter@171: int main() { deba@228: checkDigraphs(); deba@228: checkConcepts(); deba@57: return 0; deba@57: }