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@877: * Copyright (C) 2003-2010 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 kpeter@774: #include deba@353: #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@374: 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: kpeter@736: G.reserveNode(3); kpeter@736: G.reserveArc(4); kpeter@736: 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@374: Arc a2 = G.addArc(n2, n1), kpeter@374: a3 = G.addArc(n2, n3), kpeter@374: a4 = G.addArc(n2, n3); kpeter@374: kpeter@374: checkGraphNodeList(G, 3); kpeter@374: checkGraphArcList(G, 4); kpeter@374: kpeter@374: checkGraphOutArcList(G, n1, 1); kpeter@374: checkGraphOutArcList(G, n2, 3); kpeter@374: checkGraphOutArcList(G, n3, 0); kpeter@374: kpeter@374: checkGraphInArcList(G, n1, 1); kpeter@374: checkGraphInArcList(G, n2, 1); kpeter@374: checkGraphInArcList(G, n3, 2); kpeter@374: kpeter@374: checkGraphConArcList(G, 4); kpeter@374: kpeter@374: checkNodeIds(G); kpeter@374: checkArcIds(G); kpeter@374: checkGraphNodeMap(G); kpeter@374: checkGraphArcMap(G); kpeter@374: } kpeter@374: kpeter@374: template kpeter@374: void checkDigraphSplit() { kpeter@374: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@374: kpeter@374: Digraph G; kpeter@374: Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode(); kpeter@374: Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1), kpeter@374: a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3); kpeter@374: kpeter@374: Node n4 = G.split(n2); kpeter@374: kpeter@374: check(G.target(OutArcIt(G, n2)) == n4 && kpeter@374: G.source(InArcIt(G, n4)) == n2, kpeter@374: "Wrong split."); kpeter@374: kpeter@374: checkGraphNodeList(G, 4); kpeter@374: checkGraphArcList(G, 5); kpeter@374: kpeter@374: checkGraphOutArcList(G, n1, 1); kpeter@374: checkGraphOutArcList(G, n2, 1); kpeter@374: checkGraphOutArcList(G, n3, 0); kpeter@374: checkGraphOutArcList(G, n4, 3); kpeter@374: kpeter@374: checkGraphInArcList(G, n1, 1); kpeter@374: checkGraphInArcList(G, n2, 1); kpeter@374: checkGraphInArcList(G, n3, 2); kpeter@374: checkGraphInArcList(G, n4, 1); kpeter@374: kpeter@374: checkGraphConArcList(G, 5); kpeter@374: } kpeter@374: kpeter@374: template kpeter@374: void checkDigraphAlter() { kpeter@374: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@374: kpeter@374: Digraph G; kpeter@374: Node n1 = G.addNode(), n2 = G.addNode(), kpeter@374: n3 = G.addNode(), n4 = G.addNode(); kpeter@374: Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1), kpeter@374: a3 = G.addArc(n4, n3), a4 = G.addArc(n4, n3), kpeter@374: a5 = G.addArc(n2, n4); kpeter@374: kpeter@374: checkGraphNodeList(G, 4); kpeter@374: checkGraphArcList(G, 5); kpeter@374: kpeter@374: // Check changeSource() and changeTarget() kpeter@374: G.changeTarget(a4, n1); kpeter@374: kpeter@374: checkGraphNodeList(G, 4); kpeter@374: checkGraphArcList(G, 5); kpeter@374: kpeter@374: checkGraphOutArcList(G, n1, 1); kpeter@374: checkGraphOutArcList(G, n2, 1); kpeter@374: checkGraphOutArcList(G, n3, 0); kpeter@374: checkGraphOutArcList(G, n4, 3); kpeter@374: kpeter@374: checkGraphInArcList(G, n1, 2); kpeter@374: checkGraphInArcList(G, n2, 1); kpeter@374: checkGraphInArcList(G, n3, 1); kpeter@374: checkGraphInArcList(G, n4, 1); kpeter@374: kpeter@374: checkGraphConArcList(G, 5); kpeter@374: kpeter@374: G.changeSource(a4, n3); kpeter@374: kpeter@374: checkGraphNodeList(G, 4); kpeter@374: checkGraphArcList(G, 5); kpeter@374: kpeter@374: checkGraphOutArcList(G, n1, 1); kpeter@374: checkGraphOutArcList(G, n2, 1); kpeter@374: checkGraphOutArcList(G, n3, 1); kpeter@374: checkGraphOutArcList(G, n4, 2); kpeter@374: kpeter@374: checkGraphInArcList(G, n1, 2); kpeter@374: checkGraphInArcList(G, n2, 1); kpeter@374: checkGraphInArcList(G, n3, 1); kpeter@374: checkGraphInArcList(G, n4, 1); kpeter@374: kpeter@374: checkGraphConArcList(G, 5); kpeter@374: kpeter@374: // Check contract() kpeter@374: G.contract(n2, n4, false); kpeter@374: kpeter@374: checkGraphNodeList(G, 3); kpeter@374: checkGraphArcList(G, 5); kpeter@374: kpeter@374: checkGraphOutArcList(G, n1, 1); kpeter@374: checkGraphOutArcList(G, n2, 3); kpeter@374: checkGraphOutArcList(G, n3, 1); kpeter@374: kpeter@374: checkGraphInArcList(G, n1, 2); kpeter@374: checkGraphInArcList(G, n2, 2); kpeter@374: checkGraphInArcList(G, n3, 1); kpeter@374: kpeter@374: checkGraphConArcList(G, 5); kpeter@374: kpeter@374: G.contract(n2, n1); kpeter@374: kpeter@374: checkGraphNodeList(G, 2); kpeter@374: checkGraphArcList(G, 3); kpeter@374: kpeter@374: checkGraphOutArcList(G, n2, 2); kpeter@374: checkGraphOutArcList(G, n3, 1); kpeter@374: kpeter@374: checkGraphInArcList(G, n2, 2); kpeter@374: checkGraphInArcList(G, n3, 1); kpeter@374: kpeter@374: checkGraphConArcList(G, 3); kpeter@374: } kpeter@374: kpeter@374: template kpeter@374: void checkDigraphErase() { kpeter@374: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@374: kpeter@374: Digraph G; kpeter@374: Node n1 = G.addNode(), n2 = G.addNode(), kpeter@374: n3 = G.addNode(), n4 = G.addNode(); kpeter@374: Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1), kpeter@374: a3 = G.addArc(n4, n3), a4 = G.addArc(n3, n1), kpeter@374: a5 = G.addArc(n2, n4); kpeter@374: kpeter@374: // Check arc deletion kpeter@374: G.erase(a1); kpeter@374: kpeter@374: checkGraphNodeList(G, 4); kpeter@374: checkGraphArcList(G, 4); kpeter@374: kpeter@374: checkGraphOutArcList(G, n1, 0); kpeter@374: checkGraphOutArcList(G, n2, 1); kpeter@374: checkGraphOutArcList(G, n3, 1); kpeter@374: checkGraphOutArcList(G, n4, 2); kpeter@374: kpeter@374: checkGraphInArcList(G, n1, 2); kpeter@374: checkGraphInArcList(G, n2, 0); kpeter@374: checkGraphInArcList(G, n3, 1); kpeter@374: checkGraphInArcList(G, n4, 1); kpeter@374: kpeter@374: checkGraphConArcList(G, 4); kpeter@374: kpeter@374: // Check node deletion kpeter@374: G.erase(n4); kpeter@374: kpeter@374: checkGraphNodeList(G, 3); kpeter@374: checkGraphArcList(G, 1); kpeter@374: kpeter@374: checkGraphOutArcList(G, n1, 0); kpeter@374: checkGraphOutArcList(G, n2, 0); kpeter@374: checkGraphOutArcList(G, n3, 1); kpeter@374: checkGraphOutArcList(G, n4, 0); kpeter@374: kpeter@374: checkGraphInArcList(G, n1, 1); kpeter@374: checkGraphInArcList(G, n2, 0); kpeter@374: checkGraphInArcList(G, n3, 0); kpeter@374: checkGraphInArcList(G, n4, 0); kpeter@374: kpeter@374: checkGraphConArcList(G, 1); kpeter@374: } kpeter@374: kpeter@374: kpeter@374: template kpeter@374: void checkDigraphSnapshot() { kpeter@374: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@374: kpeter@374: Digraph G; kpeter@374: Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode(); kpeter@374: Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1), kpeter@374: a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3); kpeter@374: kpeter@374: typename Digraph::Snapshot snapshot(G); kpeter@374: kpeter@374: Node n = G.addNode(); kpeter@374: G.addArc(n3, n); kpeter@374: G.addArc(n, n3); kpeter@374: kpeter@374: checkGraphNodeList(G, 4); kpeter@374: checkGraphArcList(G, 6); kpeter@374: kpeter@374: snapshot.restore(); kpeter@374: 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@374: G.addNode(); kpeter@374: snapshot.save(G); kpeter@374: kpeter@374: G.addArc(G.addNode(), G.addNode()); kpeter@374: kpeter@374: snapshot.restore(); kpeter@740: snapshot.save(G); kpeter@740: kpeter@740: checkGraphNodeList(G, 4); kpeter@740: checkGraphArcList(G, 4); kpeter@740: kpeter@740: G.addArc(G.addNode(), G.addNode()); kpeter@740: kpeter@740: snapshot.restore(); kpeter@374: kpeter@374: checkGraphNodeList(G, 4); kpeter@374: 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@774: { // Checking StaticDigraph kpeter@774: checkConcept(); kpeter@774: checkConcept, StaticDigraph>(); kpeter@774: } kpeter@354: { // Checking FullDigraph kpeter@354: checkConcept(); kpeter@354: } 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: kpeter@774: void checkStaticDigraph() { kpeter@774: SmartDigraph g; kpeter@774: SmartDigraph::NodeMap nref(g); kpeter@774: SmartDigraph::ArcMap aref(g); alpar@877: kpeter@774: StaticDigraph G; alpar@877: kpeter@774: checkGraphNodeList(G, 0); kpeter@774: checkGraphArcList(G, 0); kpeter@774: kpeter@774: G.build(g, nref, aref); kpeter@774: kpeter@774: checkGraphNodeList(G, 0); kpeter@774: checkGraphArcList(G, 0); kpeter@774: kpeter@774: SmartDigraph::Node kpeter@774: n1 = g.addNode(), kpeter@774: n2 = g.addNode(), kpeter@774: n3 = g.addNode(); kpeter@774: kpeter@774: G.build(g, nref, aref); kpeter@774: kpeter@774: checkGraphNodeList(G, 3); kpeter@774: checkGraphArcList(G, 0); kpeter@774: kpeter@774: SmartDigraph::Arc a1 = g.addArc(n1, n2); kpeter@774: kpeter@774: G.build(g, nref, aref); kpeter@774: kpeter@774: check(G.source(aref[a1]) == nref[n1] && G.target(aref[a1]) == nref[n2], kpeter@774: "Wrong arc or wrong references"); kpeter@774: checkGraphNodeList(G, 3); kpeter@774: checkGraphArcList(G, 1); kpeter@774: kpeter@774: checkGraphOutArcList(G, nref[n1], 1); kpeter@774: checkGraphOutArcList(G, nref[n2], 0); kpeter@774: checkGraphOutArcList(G, nref[n3], 0); kpeter@774: kpeter@774: checkGraphInArcList(G, nref[n1], 0); kpeter@774: checkGraphInArcList(G, nref[n2], 1); kpeter@774: checkGraphInArcList(G, nref[n3], 0); kpeter@774: kpeter@774: checkGraphConArcList(G, 1); kpeter@774: kpeter@774: SmartDigraph::Arc kpeter@774: a2 = g.addArc(n2, n1), kpeter@774: a3 = g.addArc(n2, n3), kpeter@774: a4 = g.addArc(n2, n3); kpeter@774: kpeter@774: digraphCopy(g, G).nodeRef(nref).run(); kpeter@774: kpeter@774: checkGraphNodeList(G, 3); kpeter@774: checkGraphArcList(G, 4); kpeter@774: kpeter@774: checkGraphOutArcList(G, nref[n1], 1); kpeter@774: checkGraphOutArcList(G, nref[n2], 3); kpeter@774: checkGraphOutArcList(G, nref[n3], 0); kpeter@774: kpeter@774: checkGraphInArcList(G, nref[n1], 1); kpeter@774: checkGraphInArcList(G, nref[n2], 1); kpeter@774: checkGraphInArcList(G, nref[n3], 2); kpeter@774: kpeter@774: checkGraphConArcList(G, 4); kpeter@774: kpeter@777: std::vector > arcs; kpeter@777: arcs.push_back(std::make_pair(0,1)); kpeter@777: arcs.push_back(std::make_pair(0,2)); kpeter@777: arcs.push_back(std::make_pair(1,3)); kpeter@777: arcs.push_back(std::make_pair(1,2)); kpeter@777: arcs.push_back(std::make_pair(3,0)); kpeter@777: arcs.push_back(std::make_pair(3,3)); kpeter@777: arcs.push_back(std::make_pair(4,2)); kpeter@777: arcs.push_back(std::make_pair(4,3)); kpeter@777: arcs.push_back(std::make_pair(4,1)); kpeter@777: kpeter@777: G.build(6, arcs.begin(), arcs.end()); alpar@877: kpeter@777: checkGraphNodeList(G, 6); kpeter@777: checkGraphArcList(G, 9); kpeter@777: kpeter@777: checkGraphOutArcList(G, G.node(0), 2); kpeter@777: checkGraphOutArcList(G, G.node(1), 2); kpeter@777: checkGraphOutArcList(G, G.node(2), 0); kpeter@777: checkGraphOutArcList(G, G.node(3), 2); kpeter@777: checkGraphOutArcList(G, G.node(4), 3); kpeter@777: checkGraphOutArcList(G, G.node(5), 0); kpeter@777: kpeter@777: checkGraphInArcList(G, G.node(0), 1); kpeter@777: checkGraphInArcList(G, G.node(1), 2); kpeter@777: checkGraphInArcList(G, G.node(2), 3); kpeter@777: checkGraphInArcList(G, G.node(3), 3); kpeter@777: checkGraphInArcList(G, G.node(4), 0); kpeter@777: checkGraphInArcList(G, G.node(5), 0); kpeter@777: kpeter@777: checkGraphConArcList(G, 9); kpeter@777: kpeter@774: checkNodeIds(G); kpeter@774: checkArcIds(G); kpeter@774: checkGraphNodeMap(G); kpeter@774: checkGraphArcMap(G); alpar@877: kpeter@776: int n = G.nodeNum(); kpeter@776: int m = G.arcNum(); kpeter@776: check(G.index(G.node(n-1)) == n-1, "Wrong index."); kpeter@776: check(G.index(G.arc(m-1)) == m-1, "Wrong index."); kpeter@774: } kpeter@774: alpar@375: void checkFullDigraph(int num) { alpar@375: typedef FullDigraph Digraph; alpar@375: DIGRAPH_TYPEDEFS(Digraph); kpeter@737: alpar@375: Digraph G(num); kpeter@737: check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size"); kpeter@737: kpeter@737: G.resize(num); kpeter@737: check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size"); alpar@375: alpar@375: checkGraphNodeList(G, num); alpar@375: checkGraphArcList(G, num * num); alpar@375: alpar@375: for (NodeIt n(G); n != INVALID; ++n) { alpar@375: checkGraphOutArcList(G, n, num); alpar@375: checkGraphInArcList(G, n, num); alpar@375: } alpar@375: alpar@375: checkGraphConArcList(G, num * num); alpar@375: alpar@375: checkNodeIds(G); alpar@375: checkArcIds(G); alpar@375: checkGraphNodeMap(G); alpar@375: checkGraphArcMap(G); alpar@375: alpar@375: for (int i = 0; i < G.nodeNum(); ++i) { alpar@375: check(G.index(G(i)) == i, "Wrong index"); alpar@375: } alpar@375: alpar@375: for (NodeIt s(G); s != INVALID; ++s) { alpar@375: for (NodeIt t(G); t != INVALID; ++t) { alpar@375: Arc a = G.arc(s, t); alpar@375: check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup"); alpar@375: } alpar@375: } alpar@375: } alpar@375: deba@228: void checkDigraphs() { kpeter@171: { // Checking ListDigraph kpeter@374: checkDigraphBuild(); kpeter@374: checkDigraphSplit(); kpeter@374: checkDigraphAlter(); kpeter@374: checkDigraphErase(); kpeter@374: checkDigraphSnapshot(); deba@228: checkDigraphValidityErase(); deba@57: } kpeter@171: { // Checking SmartDigraph kpeter@374: checkDigraphBuild(); kpeter@374: checkDigraphSplit(); kpeter@374: checkDigraphSnapshot(); deba@228: checkDigraphValidity(); kpeter@171: } kpeter@774: { // Checking StaticDigraph kpeter@774: checkStaticDigraph(); kpeter@774: } deba@353: { // Checking FullDigraph deba@353: checkFullDigraph(8); deba@353: } kpeter@171: } deba@57: kpeter@171: int main() { deba@228: checkDigraphs(); deba@228: checkConcepts(); deba@57: return 0; deba@57: }