alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@200: * alpar@209: * This file is a part of LEMON, a generic C++ optimization library. deba@200: * alpar@1270: * Copyright (C) 2003-2013 deba@200: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@200: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@200: * deba@200: * Permission to use, modify and distribute this software is granted deba@200: * provided that this copyright notice appears in all copies. For deba@200: * precise terms see the accompanying LICENSE file. deba@200: * deba@200: * This software is provided "AS IS" with no warranty of any kind, deba@200: * express or implied, and with no claim as to its suitability for any deba@200: * purpose. deba@200: * deba@200: */ deba@200: deba@200: #include deba@200: #include kpeter@989: #include deba@200: #include deba@200: #include deba@200: deba@200: #include "test_tools.h" deba@200: deba@200: using namespace std; deba@200: using namespace lemon; deba@200: kpeter@989: template deba@200: void digraph_copy_test() { deba@200: const int nn = 10; deba@200: kpeter@980: // Build a digraph deba@200: SmartDigraph from; deba@200: SmartDigraph::NodeMap fnm(from); deba@200: SmartDigraph::ArcMap fam(from); deba@200: SmartDigraph::Node fn = INVALID; deba@200: SmartDigraph::Arc fa = INVALID; deba@200: deba@200: std::vector fnv; deba@200: for (int i = 0; i < nn; ++i) { deba@200: SmartDigraph::Node node = from.addNode(); deba@200: fnv.push_back(node); deba@200: fnm[node] = i * i; deba@200: if (i == 0) fn = node; deba@200: } deba@200: deba@200: for (int i = 0; i < nn; ++i) { deba@200: for (int j = 0; j < nn; ++j) { deba@200: SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]); deba@200: fam[arc] = i + j * j; deba@200: if (i == 0 && j == 0) fa = arc; deba@200: } deba@200: } alpar@1270: kpeter@989: // Test digraph copy kpeter@989: GR to; kpeter@989: typename GR::template NodeMap tnm(to); kpeter@989: typename GR::template ArcMap tam(to); kpeter@989: typename GR::Node tn; kpeter@989: typename GR::Arc ta; deba@200: kpeter@989: SmartDigraph::NodeMap nr(from); kpeter@989: SmartDigraph::ArcMap er(from); deba@200: kpeter@989: typename GR::template NodeMap ncr(to); kpeter@989: typename GR::template ArcMap ecr(to); deba@200: kpeter@282: digraphCopy(from, to). kpeter@282: nodeMap(fnm, tnm).arcMap(fam, tam). deba@200: nodeRef(nr).arcRef(er). deba@200: nodeCrossRef(ncr).arcCrossRef(ecr). kpeter@282: node(fn, tn).arc(fa, ta).run(); alpar@1270: kpeter@980: check(countNodes(from) == countNodes(to), "Wrong copy."); kpeter@980: check(countArcs(from) == countArcs(to), "Wrong copy."); deba@200: deba@200: for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) { deba@200: check(ncr[nr[it]] == it, "Wrong copy."); deba@200: check(fnm[it] == tnm[nr[it]], "Wrong copy."); deba@200: } deba@200: deba@200: for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) { deba@200: check(ecr[er[it]] == it, "Wrong copy."); deba@200: check(fam[it] == tam[er[it]], "Wrong copy."); deba@200: check(nr[from.source(it)] == to.source(er[it]), "Wrong copy."); deba@200: check(nr[from.target(it)] == to.target(er[it]), "Wrong copy."); deba@200: } deba@200: kpeter@989: for (typename GR::NodeIt it(to); it != INVALID; ++it) { deba@200: check(nr[ncr[it]] == it, "Wrong copy."); deba@200: } deba@200: kpeter@989: for (typename GR::ArcIt it(to); it != INVALID; ++it) { deba@200: check(er[ecr[it]] == it, "Wrong copy."); deba@200: } deba@200: check(tn == nr[fn], "Wrong copy."); deba@200: check(ta == er[fa], "Wrong copy."); kpeter@980: kpeter@980: // Test repeated copy kpeter@980: digraphCopy(from, to).run(); alpar@1270: kpeter@980: check(countNodes(from) == countNodes(to), "Wrong copy."); kpeter@980: check(countArcs(from) == countArcs(to), "Wrong copy."); deba@200: } deba@200: kpeter@989: template deba@200: void graph_copy_test() { deba@200: const int nn = 10; deba@200: kpeter@980: // Build a graph deba@200: SmartGraph from; deba@200: SmartGraph::NodeMap fnm(from); deba@200: SmartGraph::ArcMap fam(from); deba@200: SmartGraph::EdgeMap fem(from); deba@200: SmartGraph::Node fn = INVALID; deba@200: SmartGraph::Arc fa = INVALID; deba@200: SmartGraph::Edge fe = INVALID; deba@200: deba@200: std::vector fnv; deba@200: for (int i = 0; i < nn; ++i) { deba@200: SmartGraph::Node node = from.addNode(); deba@200: fnv.push_back(node); deba@200: fnm[node] = i * i; deba@200: if (i == 0) fn = node; deba@200: } deba@200: deba@200: for (int i = 0; i < nn; ++i) { deba@200: for (int j = 0; j < nn; ++j) { deba@200: SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]); deba@200: fem[edge] = i * i + j * j; deba@200: fam[from.direct(edge, true)] = i + j * j; deba@200: fam[from.direct(edge, false)] = i * i + j; deba@200: if (i == 0 && j == 0) fa = from.direct(edge, true); deba@200: if (i == 0 && j == 0) fe = edge; deba@200: } deba@200: } alpar@209: kpeter@980: // Test graph copy kpeter@989: GR to; kpeter@989: typename GR::template NodeMap tnm(to); kpeter@989: typename GR::template ArcMap tam(to); kpeter@989: typename GR::template EdgeMap tem(to); kpeter@989: typename GR::Node tn; kpeter@989: typename GR::Arc ta; kpeter@989: typename GR::Edge te; deba@200: kpeter@989: SmartGraph::NodeMap nr(from); kpeter@989: SmartGraph::ArcMap ar(from); kpeter@989: SmartGraph::EdgeMap er(from); deba@200: kpeter@989: typename GR::template NodeMap ncr(to); kpeter@989: typename GR::template ArcMap acr(to); kpeter@989: typename GR::template EdgeMap ecr(to); deba@200: kpeter@282: graphCopy(from, to). kpeter@282: nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem). deba@200: nodeRef(nr).arcRef(ar).edgeRef(er). deba@200: nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr). kpeter@282: node(fn, tn).arc(fa, ta).edge(fe, te).run(); deba@200: kpeter@980: check(countNodes(from) == countNodes(to), "Wrong copy."); kpeter@980: check(countEdges(from) == countEdges(to), "Wrong copy."); kpeter@980: check(countArcs(from) == countArcs(to), "Wrong copy."); kpeter@980: deba@200: for (SmartGraph::NodeIt it(from); it != INVALID; ++it) { deba@200: check(ncr[nr[it]] == it, "Wrong copy."); deba@200: check(fnm[it] == tnm[nr[it]], "Wrong copy."); deba@200: } deba@200: deba@200: for (SmartGraph::ArcIt it(from); it != INVALID; ++it) { deba@200: check(acr[ar[it]] == it, "Wrong copy."); deba@200: check(fam[it] == tam[ar[it]], "Wrong copy."); deba@200: check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy."); deba@200: check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy."); deba@200: } deba@200: deba@200: for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) { deba@200: check(ecr[er[it]] == it, "Wrong copy."); deba@200: check(fem[it] == tem[er[it]], "Wrong copy."); alpar@209: check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), alpar@209: "Wrong copy."); alpar@209: check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), alpar@209: "Wrong copy."); alpar@209: check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), alpar@209: "Wrong copy."); deba@200: } deba@200: kpeter@989: for (typename GR::NodeIt it(to); it != INVALID; ++it) { deba@200: check(nr[ncr[it]] == it, "Wrong copy."); deba@200: } deba@200: kpeter@989: for (typename GR::ArcIt it(to); it != INVALID; ++it) { deba@200: check(ar[acr[it]] == it, "Wrong copy."); deba@200: } kpeter@989: for (typename GR::EdgeIt it(to); it != INVALID; ++it) { deba@200: check(er[ecr[it]] == it, "Wrong copy."); deba@200: } deba@200: check(tn == nr[fn], "Wrong copy."); deba@200: check(ta == ar[fa], "Wrong copy."); deba@200: check(te == er[fe], "Wrong copy."); kpeter@980: kpeter@980: // Test repeated copy kpeter@980: graphCopy(from, to).run(); alpar@1270: kpeter@980: check(countNodes(from) == countNodes(to), "Wrong copy."); kpeter@980: check(countEdges(from) == countEdges(to), "Wrong copy."); kpeter@980: check(countArcs(from) == countArcs(to), "Wrong copy."); deba@200: } deba@200: deba@1190: template deba@1190: void bpgraph_copy_test() { deba@1190: const int nn = 10; deba@1190: deba@1190: // Build a graph deba@1190: SmartBpGraph from; deba@1190: SmartBpGraph::NodeMap fnm(from); deba@1194: SmartBpGraph::RedNodeMap frnm(from); deba@1194: SmartBpGraph::BlueNodeMap fbnm(from); deba@1190: SmartBpGraph::ArcMap fam(from); deba@1190: SmartBpGraph::EdgeMap fem(from); deba@1190: SmartBpGraph::Node fn = INVALID; deba@1193: SmartBpGraph::RedNode frn = INVALID; deba@1193: SmartBpGraph::BlueNode fbn = INVALID; deba@1190: SmartBpGraph::Arc fa = INVALID; deba@1190: SmartBpGraph::Edge fe = INVALID; deba@1190: deba@1193: std::vector frnv; deba@1190: for (int i = 0; i < nn; ++i) { deba@1193: SmartBpGraph::RedNode node = from.addRedNode(); deba@1190: frnv.push_back(node); deba@1190: fnm[node] = i * i; deba@1190: frnm[node] = i + i; deba@1193: if (i == 0) { deba@1193: fn = node; deba@1193: frn = node; deba@1193: } deba@1190: } deba@1190: deba@1193: std::vector fbnv; deba@1190: for (int i = 0; i < nn; ++i) { deba@1193: SmartBpGraph::BlueNode node = from.addBlueNode(); deba@1190: fbnv.push_back(node); deba@1190: fnm[node] = i * i; deba@1190: fbnm[node] = i + i; deba@1193: if (i == 0) fbn = node; deba@1190: } deba@1190: deba@1190: for (int i = 0; i < nn; ++i) { deba@1190: for (int j = 0; j < nn; ++j) { deba@1190: SmartBpGraph::Edge edge = from.addEdge(frnv[i], fbnv[j]); deba@1190: fem[edge] = i * i + j * j; deba@1190: fam[from.direct(edge, true)] = i + j * j; deba@1190: fam[from.direct(edge, false)] = i * i + j; deba@1190: if (i == 0 && j == 0) fa = from.direct(edge, true); deba@1190: if (i == 0 && j == 0) fe = edge; deba@1190: } deba@1190: } deba@1190: deba@1190: // Test graph copy deba@1190: GR to; deba@1190: typename GR::template NodeMap tnm(to); deba@1194: typename GR::template RedNodeMap trnm(to); deba@1194: typename GR::template BlueNodeMap tbnm(to); deba@1190: typename GR::template ArcMap tam(to); deba@1190: typename GR::template EdgeMap tem(to); deba@1190: typename GR::Node tn; deba@1193: typename GR::RedNode trn; deba@1193: typename GR::BlueNode tbn; deba@1190: typename GR::Arc ta; deba@1190: typename GR::Edge te; deba@1190: deba@1190: SmartBpGraph::NodeMap nr(from); deba@1194: SmartBpGraph::RedNodeMap rnr(from); deba@1194: SmartBpGraph::BlueNodeMap bnr(from); deba@1190: SmartBpGraph::ArcMap ar(from); deba@1190: SmartBpGraph::EdgeMap er(from); deba@1190: deba@1190: typename GR::template NodeMap ncr(to); deba@1194: typename GR::template RedNodeMap rncr(to); deba@1194: typename GR::template BlueNodeMap bncr(to); deba@1190: typename GR::template ArcMap acr(to); deba@1190: typename GR::template EdgeMap ecr(to); deba@1190: deba@1190: bpGraphCopy(from, to). deba@1194: nodeMap(fnm, tnm). deba@1194: redNodeMap(frnm, trnm).blueNodeMap(fbnm, tbnm). deba@1190: arcMap(fam, tam).edgeMap(fem, tem). deba@1190: nodeRef(nr).redRef(rnr).blueRef(bnr). deba@1190: arcRef(ar).edgeRef(er). deba@1190: nodeCrossRef(ncr).redCrossRef(rncr).blueCrossRef(bncr). deba@1190: arcCrossRef(acr).edgeCrossRef(ecr). deba@1193: node(fn, tn).redNode(frn, trn).blueNode(fbn, tbn). deba@1193: arc(fa, ta).edge(fe, te).run(); deba@1190: deba@1190: check(countNodes(from) == countNodes(to), "Wrong copy."); deba@1190: check(countRedNodes(from) == countRedNodes(to), "Wrong copy."); deba@1190: check(countBlueNodes(from) == countBlueNodes(to), "Wrong copy."); deba@1190: check(countEdges(from) == countEdges(to), "Wrong copy."); deba@1190: check(countArcs(from) == countArcs(to), "Wrong copy."); deba@1190: deba@1190: for (SmartBpGraph::NodeIt it(from); it != INVALID; ++it) { deba@1190: check(ncr[nr[it]] == it, "Wrong copy."); deba@1190: check(fnm[it] == tnm[nr[it]], "Wrong copy."); deba@1193: } deba@1193: deba@1194: for (SmartBpGraph::RedNodeIt it(from); it != INVALID; ++it) { deba@1193: check(ncr[nr[it]] == it, "Wrong copy."); deba@1193: check(fnm[it] == tnm[nr[it]], "Wrong copy."); deba@1193: check(rnr[it] == nr[it], "Wrong copy."); deba@1193: check(rncr[rnr[it]] == it, "Wrong copy."); deba@1193: check(frnm[it] == trnm[rnr[it]], "Wrong copy."); deba@1193: check(to.red(rnr[it]), "Wrong copy."); deba@1193: } deba@1193: deba@1194: for (SmartBpGraph::BlueNodeIt it(from); it != INVALID; ++it) { deba@1193: check(ncr[nr[it]] == it, "Wrong copy."); deba@1193: check(fnm[it] == tnm[nr[it]], "Wrong copy."); deba@1193: check(bnr[it] == nr[it], "Wrong copy."); deba@1193: check(bncr[bnr[it]] == it, "Wrong copy."); deba@1193: check(fbnm[it] == tbnm[bnr[it]], "Wrong copy."); deba@1193: check(to.blue(bnr[it]), "Wrong copy."); deba@1190: } deba@1190: deba@1190: for (SmartBpGraph::ArcIt it(from); it != INVALID; ++it) { deba@1190: check(acr[ar[it]] == it, "Wrong copy."); deba@1190: check(fam[it] == tam[ar[it]], "Wrong copy."); deba@1190: check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy."); deba@1190: check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy."); deba@1190: } deba@1190: deba@1190: for (SmartBpGraph::EdgeIt it(from); it != INVALID; ++it) { deba@1190: check(ecr[er[it]] == it, "Wrong copy."); deba@1190: check(fem[it] == tem[er[it]], "Wrong copy."); deba@1190: check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), deba@1190: "Wrong copy."); deba@1190: check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), deba@1190: "Wrong copy."); deba@1190: check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), deba@1190: "Wrong copy."); deba@1190: } deba@1190: deba@1190: for (typename GR::NodeIt it(to); it != INVALID; ++it) { deba@1190: check(nr[ncr[it]] == it, "Wrong copy."); deba@1190: } deba@1194: for (typename GR::RedNodeIt it(to); it != INVALID; ++it) { deba@1190: check(rncr[it] == ncr[it], "Wrong copy."); deba@1190: check(rnr[rncr[it]] == it, "Wrong copy."); deba@1190: } deba@1194: for (typename GR::BlueNodeIt it(to); it != INVALID; ++it) { deba@1190: check(bncr[it] == ncr[it], "Wrong copy."); deba@1190: check(bnr[bncr[it]] == it, "Wrong copy."); deba@1190: } deba@1190: for (typename GR::ArcIt it(to); it != INVALID; ++it) { deba@1190: check(ar[acr[it]] == it, "Wrong copy."); deba@1190: } deba@1190: for (typename GR::EdgeIt it(to); it != INVALID; ++it) { deba@1190: check(er[ecr[it]] == it, "Wrong copy."); deba@1190: } deba@1190: check(tn == nr[fn], "Wrong copy."); deba@1193: check(trn == rnr[frn], "Wrong copy."); deba@1193: check(tbn == bnr[fbn], "Wrong copy."); deba@1190: check(ta == ar[fa], "Wrong copy."); deba@1190: check(te == er[fe], "Wrong copy."); deba@1190: deba@1190: // Test repeated copy deba@1190: bpGraphCopy(from, to).run(); alpar@1270: deba@1190: check(countNodes(from) == countNodes(to), "Wrong copy."); deba@1190: check(countRedNodes(from) == countRedNodes(to), "Wrong copy."); deba@1190: check(countBlueNodes(from) == countBlueNodes(to), "Wrong copy."); deba@1190: check(countEdges(from) == countEdges(to), "Wrong copy."); deba@1190: check(countArcs(from) == countArcs(to), "Wrong copy."); deba@1190: } deba@1190: deba@200: deba@200: int main() { kpeter@989: digraph_copy_test(); kpeter@989: digraph_copy_test(); kpeter@989: digraph_copy_test(); kpeter@989: graph_copy_test(); kpeter@989: graph_copy_test(); deba@1190: bpgraph_copy_test(); deba@1190: bpgraph_copy_test(); deba@200: alpar@209: return 0; deba@200: }