1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
#include "test_util.h"
#include "nomnigraph/Converters/Dot.h"
#include "nomnigraph/Graph/Algorithms.h"
#include "nomnigraph/Graph/Graph.h"
#include "nomnigraph/Support/Casting.h"
#include <gtest/gtest.h>
using TestGraph = nom::Graph<TestClass>;
TEST(Basic, CreateNodeAndEdge) {
TestGraph g;
auto n1 = createTestNode(g);
auto n2 = createTestNode(g);
g.createEdge(n1, n2);
EXPECT_TRUE(g.hasNode(n1));
EXPECT_TRUE(g.hasNode(n2));
}
TEST(Basic, DeleteNode) {
TestGraph g;
auto n1 = createTestNode(g);
auto n2 = createTestNode(g);
g.createEdge(n1, n2);
EXPECT_TRUE(g.hasNode(n1));
g.deleteNode(n1);
EXPECT_FALSE(g.hasNode(n1));
}
TEST(Basic, DeleteEdge) {
TestGraph g;
auto n1 = createTestNode(g);
auto n2 = createTestNode(g);
auto e = g.createEdge(n1, n2);
g.deleteEdge(e);
}
TEST(Basic, ReplaceEdges) {
TestGraph g;
auto n1 = createTestNode(g);
auto n2 = createTestNode(g);
auto n3 = createTestNode(g);
auto n4 = createTestNode(g);
auto n5 = createTestNode(g);
g.createEdge(n1, n3);
g.createEdge(n2, n3);
g.createEdge(n3, n4);
/*
1 2 5
|
3
|
4
*/
EXPECT_FALSE(g.hasEdge(n1, n5));
EXPECT_FALSE(g.hasEdge(n2, n5));
g.replaceInEdges(n3, n5);
/*
1 2 3
| |
5 4
*/
EXPECT_TRUE(g.hasEdge(n1, n5));
EXPECT_TRUE(g.hasEdge(n2, n5));
EXPECT_FALSE(g.hasEdge(n5, n4));
g.replaceOutEdges(n3, n5);
/*
1 2 3
|
5
|
4
*/
EXPECT_TRUE(g.hasEdge(n5, n4));
g.replaceNode(n5, n3);
// Back to the original graph.
/*
1 2 5
|
3
|
4
*/
EXPECT_TRUE(g.hasEdge(n1, n3));
EXPECT_TRUE(g.hasEdge(n2, n3));
}
TEST(Basic, HasNode) {
TestGraph g;
auto n1 = createTestNode(g);
auto n2 = createTestNode(g);
auto n3 = createTestNode(g);
g.createEdge(n1, n2);
g.createEdge(n1, n3);
// Current graph: 1 -> 2 -> 3
EXPECT_TRUE(g.hasNode(n1));
EXPECT_TRUE(g.hasNode(n2));
EXPECT_TRUE(g.hasNode(n3));
g.swapNodes(n1, n3);
// Current graph: 3 -> 2 -> 1
EXPECT_TRUE(g.hasNode(n1));
EXPECT_TRUE(g.hasNode(n3));
g.deleteNode(n1);
// Current graph: 3 -> 2
EXPECT_FALSE(g.hasNode(n1));
auto n4 = createTestNode(g);
EXPECT_TRUE(g.hasNode(n4));
g.replaceNode(n2, n4);
// Current graph: 3 -> 4 , 2
// replaceNode doesn't delete n2.
EXPECT_TRUE(g.hasNode(n2));
// Create a second graph g2, and move the nodes from g2 to g.
TestClass t5;
nom::Graph<TestClass> g2;
nom::Graph<TestClass>::NodeRef n5 = g2.createNode(std::move(t5));
EXPECT_TRUE(g2.hasNode(n5));
EXPECT_FALSE(g.hasNode(n5));
g2.moveNode(n5, &g);
// Current graph (g1): 3 -> 4, 2, 5
EXPECT_TRUE(g.hasNode(n5));
}
TEST(Basic, Moves) {
TestGraph g;
auto n1 = createTestNode(g);
auto n2 = createTestNode(g);
auto n3 = createTestNode(g);
auto e1 = g.createEdge(n1, n2);
auto e2 = g.createEdge(n1, n3);
// Current graph: 1 -> 2 -> 3
TestGraph g2;
g.deleteEdge(e2);
g.moveNode(n1, &g2);
g.moveNode(n2, &g2);
g.moveEdge(e1, &g2);
EXPECT_TRUE(g.isValid());
EXPECT_TRUE(g2.isValid());
EXPECT_EQ(g.getMutableNodes().size(), 1);
EXPECT_EQ(g2.getMutableNodes().size(), 2);
EXPECT_EQ(g.getMutableEdges().size(), 0);
EXPECT_EQ(g2.getMutableEdges().size(), 1);
}
TEST(Basic, MoveSubgraph) {
TestGraph g;
auto n1 = createTestNode(g);
auto n2 = createTestNode(g);
auto n3 = createTestNode(g);
auto e1 = g.createEdge(n1, n2);
auto e2 = g.createEdge(n1, n3);
// Current graph: 1 -> 2 -> 3
TestGraph g2;
g.deleteEdge(e2);
TestGraph::SubgraphType sg;
sg.addNode(n1);
sg.addNode(n2);
sg.addEdge(e1);
g.moveSubgraph(sg, &g2);
EXPECT_TRUE(g.isValid());
EXPECT_TRUE(g2.isValid());
EXPECT_EQ(g.getMutableNodes().size(), 1);
EXPECT_EQ(g2.getMutableNodes().size(), 2);
EXPECT_EQ(g.getMutableEdges().size(), 0);
EXPECT_EQ(g2.getMutableEdges().size(), 1);
}
TEST(Basic, DotGenerator) {
TestGraph g;
auto n1 = createTestNode(g);
auto n2 = createTestNode(g);
auto n3 = createTestNode(g);
auto e12 = g.createEdge(n1, n2);
g.createEdge(n1, n3);
std::string dot = nom::converters::convertToDotString(&g, TestNodePrinter);
// sanity check
std::string prefix = "digraph G";
// Full string comparison of the output is not stable because the dot
// string includes node pointer address as node id. We should switch to
// comparing full output once dot generator no longer uses addresses.
EXPECT_TRUE(dot.compare(0, prefix.length(), prefix) == 0);
TestGraph::SubgraphType sg;
sg.addNode(n1);
sg.addNode(n2);
sg.addEdge(e12);
// Convert to dot with subgraph clusters.
dot = nom::converters::convertToDotString<TestGraph>(
&g, {&sg}, TestNodePrinter);
// sanity check
EXPECT_TRUE(dot.compare(0, prefix.length(), prefix) == 0);
// Convert a single subgraph to dot.
dot = nom::converters::convertToDotString<TestGraph>(sg, TestNodePrinter);
// sanity check
EXPECT_TRUE(dot.compare(0, prefix.length(), prefix) == 0);
dot =
nom::converters::convertToDotRecordString<TestGraph>(&g, TestNodePrinter);
// sanity check
EXPECT_TRUE(dot.compare(0, prefix.length(), prefix) == 0);
}
|