Skip to content

Commit 3d367fc

Browse files
committed
Add tests for GraphML serialization.
1 parent 684919b commit 3d367fc

1 file changed

Lines changed: 200 additions & 9 deletions

File tree

tests/QuikGraph.Serialization.Tests/GraphMLSerializationTests.cs

Lines changed: 200 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
using System.Xml.XPath;
1111
using JetBrains.Annotations;
1212
using NUnit.Framework;
13+
using QuikGraph.Algorithms;
1314
using QuikGraph.Tests;
15+
using static QuikGraph.Serialization.Tests.SerializationTestCaseSources;
1416
using static QuikGraph.Tests.QuikGraphUnitTestsHelpers;
1517

1618
namespace QuikGraph.Serialization.Tests
@@ -30,17 +32,17 @@ private static TGraph VerifySerialization<TGraph>(
3032
[NotNull, InstantHandle] Func<TGraph, string> serializeGraph,
3133
[NotNull, InstantHandle] Func<string, TGraph> deserializeGraph)
3234
{
33-
// Serialize the graph to XML
34-
string xml = serializeGraph(graph);
35+
// Serialize the graph to GraphML
36+
string graphml = serializeGraph(graph);
3537

3638
// Deserialize a graph from previous serialization
37-
TGraph serializedGraph = deserializeGraph(xml);
39+
TGraph serializedGraph = deserializeGraph(graphml);
3840

3941
// Serialize the deserialized graph
40-
string newXml = serializeGraph(serializedGraph);
42+
string newGraphml = serializeGraph(serializedGraph);
4143

4244
// => Serialization should produce the same result
43-
Assert.AreEqual(xml, newXml);
45+
Assert.AreEqual(graphml, newGraphml);
4446

4547
Assert.AreNotSame(graph, serializeGraph);
4648

@@ -122,9 +124,9 @@ public void GraphMLSerialization_HeaderCheck(bool emitDeclarationOnSerialize, bo
122124
return writer.ToString();
123125
}
124126
},
125-
xml =>
127+
graphml =>
126128
{
127-
using (var reader = new StringReader(xml))
129+
using (var reader = new StringReader(graphml))
128130
{
129131
var serializer = new GraphMLDeserializer<EquatableTestVertex, EquatableTestEdge, EquatableTestGraph>
130132
{
@@ -616,9 +618,9 @@ private static string SerializeGraph4([NotNull] TestGraph graph)
616618

617619
[Pure]
618620
[NotNull]
619-
private static TestGraph DeserializeGraph([NotNull] string xml)
621+
private static TestGraph DeserializeGraph([NotNull] string graphml)
620622
{
621-
using (var reader = new StringReader(xml))
623+
using (var reader = new StringReader(graphml))
622624
{
623625
var serializedGraph = new TestGraph();
624626
serializedGraph.DeserializeAndValidateFromGraphML(
@@ -842,5 +844,194 @@ public void DeserializeAndValidateFromGraphML_Throws()
842844
#endif
843845

844846
#endregion
847+
848+
#region Serialization/Deserialization
849+
850+
[Pure]
851+
[NotNull]
852+
private static TOutGraph SerializeDeserialize<TInEdge, TOutEdge, TInGraph, TOutGraph>(
853+
[NotNull] TInGraph graph,
854+
[NotNull, InstantHandle] Func<XmlReader, TOutGraph> deserialize)
855+
where TInEdge : IEdge<int>, IEquatable<TInEdge>
856+
where TOutEdge : IEdge<int>, IEquatable<TOutEdge>
857+
where TInGraph : IEdgeListGraph<int, TInEdge>
858+
where TOutGraph : IEdgeListGraph<int, TOutEdge>
859+
{
860+
Assert.IsNotNull(graph);
861+
862+
using (var stream = new MemoryStream())
863+
{
864+
// Serialize
865+
using (XmlWriter writer = XmlWriter.Create(stream))
866+
{
867+
graph.SerializeToGraphML(
868+
writer,
869+
vertex => vertex.ToString(),
870+
graph.GetEdgeIdentity());
871+
}
872+
873+
// Deserialize
874+
stream.Position = 0;
875+
876+
// Deserialize
877+
using (XmlReader reader = XmlReader.Create(stream))
878+
{
879+
TOutGraph deserializedGraph = deserialize(reader);
880+
Assert.IsNotNull(deserializedGraph);
881+
Assert.AreNotSame(graph, deserializedGraph);
882+
return deserializedGraph;
883+
}
884+
}
885+
}
886+
887+
[Pure]
888+
[NotNull]
889+
private static TOutGraph SerializeDeserialize<TInGraph, TOutGraph>([NotNull] TInGraph graph)
890+
where TInGraph : IEdgeListGraph<int, EquatableEdge<int>>
891+
where TOutGraph : class, IMutableVertexAndEdgeSet<int, EquatableEdge<int>>, new()
892+
{
893+
return SerializeDeserialize<EquatableEdge<int>, EquatableEdge<int>, TInGraph, TOutGraph>(graph, reader =>
894+
{
895+
var deserializedGraph = new TOutGraph();
896+
deserializedGraph.DeserializeFromGraphML(
897+
reader,
898+
int.Parse,
899+
(source, target, _) => new EquatableEdge<int>(source, target));
900+
return deserializedGraph;
901+
});
902+
}
903+
904+
[Pure]
905+
[NotNull]
906+
private static TOutGraph SerializeDeserialize_SEdge<TInGraph, TOutGraph>([NotNull] TInGraph graph)
907+
where TInGraph : IEdgeListGraph<int, SEquatableEdge<int>>
908+
where TOutGraph : class, IMutableVertexAndEdgeSet<int, SEquatableEdge<int>>, new()
909+
{
910+
return SerializeDeserialize<SEquatableEdge<int>, SEquatableEdge<int>, TInGraph, TOutGraph>(graph, reader =>
911+
{
912+
var deserializedGraph = new TOutGraph();
913+
deserializedGraph.DeserializeFromGraphML(
914+
reader,
915+
int.Parse,
916+
(source, target, _) => new SEquatableEdge<int>(source, target));
917+
return deserializedGraph;
918+
});
919+
}
920+
921+
[Pure]
922+
[NotNull]
923+
private static TOutGraph SerializeDeserialize_Reversed<TInGraph, TOutGraph>([NotNull] TInGraph graph)
924+
where TInGraph : IEdgeListGraph<int, SReversedEdge<int, EquatableEdge<int>>>
925+
where TOutGraph : class, IMutableVertexAndEdgeSet<int, EquatableEdge<int>>, new()
926+
{
927+
return SerializeDeserialize<SReversedEdge<int, EquatableEdge<int>>, EquatableEdge<int>, TInGraph, TOutGraph>(graph, reader =>
928+
{
929+
var deserializedGraph = new TOutGraph();
930+
deserializedGraph.DeserializeFromGraphML(
931+
reader,
932+
int.Parse,
933+
(source, target, _) => new EquatableEdge<int>(source, target));
934+
return deserializedGraph;
935+
});
936+
}
937+
938+
[TestCaseSource(typeof(SerializationTestCaseSources), nameof(SerializationAdjacencyGraphTestCases))]
939+
public void GraphMLSerialization_AdjacencyGraph([NotNull] AdjacencyGraph<int, EquatableEdge<int>> graph)
940+
{
941+
AdjacencyGraph<int, EquatableEdge<int>> deserializedGraph1 =
942+
SerializeDeserialize<AdjacencyGraph<int, EquatableEdge<int>>, AdjacencyGraph<int, EquatableEdge<int>>>(graph);
943+
Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph1));
944+
945+
var arrayGraph = new ArrayAdjacencyGraph<int, EquatableEdge<int>>(graph);
946+
AdjacencyGraph<int, EquatableEdge<int>> deserializedGraph2 =
947+
SerializeDeserialize<ArrayAdjacencyGraph<int, EquatableEdge<int>>, AdjacencyGraph<int, EquatableEdge<int>>>(arrayGraph);
948+
Assert.IsTrue(EquateGraphs.Equate(arrayGraph, deserializedGraph2));
949+
}
950+
951+
[TestCaseSource(typeof(SerializationTestCaseSources), nameof(SerializationAdjacencyGraphTestCases))]
952+
public void GraphMLSerialization_AdapterGraph([NotNull] AdjacencyGraph<int, EquatableEdge<int>> graph)
953+
{
954+
var bidirectionalAdapterGraph = new BidirectionalAdapterGraph<int, EquatableEdge<int>>(graph);
955+
AdjacencyGraph<int, EquatableEdge<int>> deserializedGraph =
956+
SerializeDeserialize<BidirectionalAdapterGraph<int, EquatableEdge<int>>, AdjacencyGraph<int, EquatableEdge<int>>>(bidirectionalAdapterGraph);
957+
Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph));
958+
}
959+
960+
[TestCaseSource(typeof(SerializationTestCaseSources), nameof(SerializationClusteredAdjacencyGraphTestCases))]
961+
public void GraphMLSerialization_ClusteredGraph([NotNull] ClusteredAdjacencyGraph<int, EquatableEdge<int>> graph)
962+
{
963+
AdjacencyGraph<int, EquatableEdge<int>> deserializedGraph =
964+
SerializeDeserialize<ClusteredAdjacencyGraph<int, EquatableEdge<int>>, AdjacencyGraph<int, EquatableEdge<int>>>(graph);
965+
Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph));
966+
}
967+
968+
[TestCaseSource(typeof(SerializationTestCaseSources), nameof(SerializationCompressedGraphTestCases))]
969+
public void GraphMLSerialization_CompressedGraph([NotNull] CompressedSparseRowGraph<int> graph)
970+
{
971+
AdjacencyGraph<int, SEquatableEdge<int>> deserializedGraph =
972+
SerializeDeserialize_SEdge<CompressedSparseRowGraph<int>, AdjacencyGraph<int, SEquatableEdge<int>>>(graph);
973+
Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph));
974+
}
975+
976+
[TestCaseSource(typeof(SerializationTestCaseSources), nameof(SerializationBidirectionalGraphTestCases))]
977+
public void GraphMLSerialization_BidirectionalGraph([NotNull] BidirectionalGraph<int, EquatableEdge<int>> graph)
978+
{
979+
AdjacencyGraph<int, EquatableEdge<int>> deserializedGraph =
980+
SerializeDeserialize<BidirectionalGraph<int, EquatableEdge<int>>, AdjacencyGraph<int, EquatableEdge<int>>>(graph);
981+
Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph));
982+
983+
var arrayGraph = new ArrayBidirectionalGraph<int, EquatableEdge<int>>(graph);
984+
AdjacencyGraph<int, EquatableEdge<int>> deserializedGraph2 =
985+
SerializeDeserialize<ArrayBidirectionalGraph<int, EquatableEdge<int>>, AdjacencyGraph<int, EquatableEdge<int>>>(arrayGraph);
986+
Assert.IsTrue(EquateGraphs.Equate(arrayGraph, deserializedGraph2));
987+
988+
var reversedGraph = new ReversedBidirectionalGraph<int, EquatableEdge<int>>(graph);
989+
BidirectionalGraph<int, EquatableEdge<int>> deserializedGraph3 =
990+
SerializeDeserialize_Reversed<ReversedBidirectionalGraph<int, EquatableEdge<int>>, BidirectionalGraph<int, EquatableEdge<int>>>(reversedGraph);
991+
Assert.IsTrue(
992+
EquateGraphs.Equate(
993+
graph,
994+
deserializedGraph3,
995+
EqualityComparer<int>.Default,
996+
LambdaEqualityComparer<EquatableEdge<int>>.Create(
997+
(edge1, edge2) => Equals(edge1.Source, edge2.Target) && Equals(edge1.Target, edge2.Source),
998+
edge => edge.GetHashCode())));
999+
1000+
var undirectedBidirectionalGraph = new UndirectedBidirectionalGraph<int, EquatableEdge<int>>(graph);
1001+
UndirectedGraph<int, EquatableEdge<int>> deserializedGraph4 =
1002+
SerializeDeserialize<UndirectedBidirectionalGraph<int, EquatableEdge<int>>, UndirectedGraph<int, EquatableEdge<int>>>(undirectedBidirectionalGraph);
1003+
Assert.IsTrue(EquateGraphs.Equate(undirectedBidirectionalGraph, deserializedGraph4));
1004+
}
1005+
1006+
[TestCaseSource(typeof(SerializationTestCaseSources), nameof(SerializationBidirectionalMatrixGraphTestCases))]
1007+
public void GraphMLSerialization_BidirectionalMatrixGraph([NotNull] BidirectionalMatrixGraph<EquatableEdge<int>> graph)
1008+
{
1009+
AdjacencyGraph<int, EquatableEdge<int>> deserializedGraph =
1010+
SerializeDeserialize<BidirectionalMatrixGraph<EquatableEdge<int>>, AdjacencyGraph<int, EquatableEdge<int>>>(graph);
1011+
Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph));
1012+
}
1013+
1014+
[TestCaseSource(typeof(SerializationTestCaseSources), nameof(SerializationUndirectedGraphTestCases))]
1015+
public void GraphMLSerialization_UndirectedGraph([NotNull] UndirectedGraph<int, EquatableEdge<int>> graph)
1016+
{
1017+
UndirectedGraph<int, EquatableEdge<int>> deserializedGraph1 =
1018+
SerializeDeserialize<UndirectedGraph<int, EquatableEdge<int>>, UndirectedGraph<int, EquatableEdge<int>>>(graph);
1019+
Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph1));
1020+
1021+
var arrayGraph = new ArrayUndirectedGraph<int, EquatableEdge<int>>(graph);
1022+
UndirectedGraph<int, EquatableEdge<int>> deserializedGraph2 =
1023+
SerializeDeserialize<ArrayUndirectedGraph<int, EquatableEdge<int>>, UndirectedGraph<int, EquatableEdge<int>>>(arrayGraph);
1024+
Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph2));
1025+
}
1026+
1027+
[TestCaseSource(typeof(SerializationTestCaseSources), nameof(SerializationEdgeListGraphTestCases))]
1028+
public void GraphMLSerialization_EdgeListGraph([NotNull] EdgeListGraph<int, EquatableEdge<int>> graph)
1029+
{
1030+
AdjacencyGraph<int, EquatableEdge<int>> deserializedGraph =
1031+
SerializeDeserialize<EdgeListGraph<int, EquatableEdge<int>>, AdjacencyGraph<int, EquatableEdge<int>>>(graph);
1032+
Assert.IsTrue(EquateGraphs.Equate(graph, deserializedGraph));
1033+
}
1034+
1035+
#endregion
8451036
}
8461037
}

0 commit comments

Comments
 (0)