Ejemplo n.º 1
0
        public void DeserializeFromGraphML_Throws_InvalidData()
        {
            AssertDeserializationFail <TestGraphArrayDefaultValue, TypeInitializationException>(new TestGraphArrayDefaultValue());
            AssertDeserializationFail <TestGraphNoSetter, TypeInitializationException>(new TestGraphNoSetter());
            AssertDeserializationFail <TestGraphNullDefaultValue, TypeInitializationException>(new TestGraphNullDefaultValue());
            AssertDeserializationFail <TestGraphWrongDefaultValue, TypeInitializationException>(new TestGraphWrongDefaultValue());
            AssertDeserializationFail <TestGraphNotSupportedType, TypeInitializationException>(new TestGraphNotSupportedType());

            var graph = new TestGraph();

            AssertDeserializationFail <TestGraph, ArgumentException>(graph, MissingAttributeTestGraphFileName);
            AssertDeserializationFail <TestGraph, ArgumentException>(graph, MissingSourceTestGraphFileName);
            AssertDeserializationFail <TestGraph, ArgumentException>(graph, MissingTargetTestGraphFileName);
            AssertDeserializationFail <TestGraph, InvalidOperationException>(graph, InvalidTagTestGraphFileName);
            AssertDeserializationFail <TestGraph, InvalidOperationException>(graph, MissingGraphTestGraphFileName);
            AssertDeserializationFail <TestGraph, InvalidOperationException>(graph, MissingGraphMLTestGraphFileName);

            #region Local function

            void AssertDeserializationFail <TGraph, TException>(TGraph g, string fileName = TestGraphFileName)
                where TGraph : IMutableVertexAndEdgeListGraph <TestVertex, TestEdge>
                where TException : Exception
            {
                Assert.Throws <TException>(
                    () => g.DeserializeFromGraphML(
                        GetGraphFilePath(fileName),
                        id => new TestVertex(id),
                        (source, target, id) => new TestEdge(source, target, id)));
            }

            #endregion
        }
Ejemplo n.º 2
0
        private static string SerializeGraph1([NotNull] TestGraph graph)
        {
            string filePath = Path.Combine(
                GetTemporaryTestDirectory(),
                $"serialization_to_graphml_test_{Guid.NewGuid().ToString()}.graphml");

            graph.SerializeToGraphML <TestVertex, TestEdge, TestGraph>(filePath);
            Assert.IsTrue(File.Exists(filePath));
            return(File.ReadAllText(filePath));
        }
Ejemplo n.º 3
0
 private static TestGraph DeserializeGraph([NotNull] string xml)
 {
     using (var reader = new StringReader(xml))
     {
         var serializedGraph = new TestGraph();
         serializedGraph.DeserializeAndValidateFromGraphML(
             reader,
             id => new TestVertex(id),
             (source, target, id) => new TestEdge(source, target, id));
         return(serializedGraph);
     }
 }
Ejemplo n.º 4
0
        private static string SerializeGraph3([NotNull] TestGraph graph)
        {
            using (var writer = new StringWriter())
            {
                var settings = new XmlWriterSettings {
                    Indent = true
                };
                using (XmlWriter xmlWriter = XmlWriter.Create(writer, settings))
                {
                    graph.SerializeToGraphML <TestVertex, TestEdge, TestGraph>(xmlWriter);
                }

                return(writer.ToString());
            }
        }
Ejemplo n.º 5
0
        public void GraphMLSerializationWithValidation_WriteEdge(
            [NotNull, InstantHandle] Func <TestGraph, string> serializeGraph,
            bool keepIds)
        {
            var graph = new TestGraph
            {
                Bool   = true,
                Double = 1.0,
                Float  = 2.0F,
                Int    = 10,
                Long   = 100,
                String = "foo"
            };

            var vertex1 = new TestVertex("v1")
            {
                StringDefault = "foo",
                String        = "string",
                Int           = 10,
                Long          = 20,
                Float         = 25.0F,
                Double        = 30.0,
                Bool          = true
            };

            var vertex2 = new TestVertex("v2")
            {
                StringDefault = "bar",
                String        = "string2",
                Int           = 110,
                Long          = 120,
                Float         = 125.0F,
                Double        = 130.0,
                Bool          = true
            };

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);

            var edge = new TestEdge(
                vertex1, vertex2,
                "e1",
                "edge",
                90,
                100,
                25.0F,
                110.0,
                true);

            graph.AddEdge(edge);

            TestGraph serializedGraph = VerifySerialization(graph, serializeGraph);

            TestEdge serializedEdge = serializedGraph.Edges.First();

            if (keepIds)
            {
                Assert.AreEqual(edge.ID, serializedEdge.ID);
            }
            Assert.AreEqual(edge.String, serializedEdge.String);
            Assert.AreEqual(edge.Int, serializedEdge.Int);
            Assert.AreEqual(edge.Long, serializedEdge.Long);
            Assert.AreEqual(edge.Float, serializedEdge.Float);
            Assert.AreEqual(edge.Double, serializedEdge.Double);
            Assert.AreEqual(edge.Bool, serializedEdge.Bool);
        }
Ejemplo n.º 6
0
        public void GraphMLSerializationWithValidation_WriteVertex(
            [NotNull, InstantHandle] Func <TestGraph, string> serializeGraph,
            bool _)
        {
            var graph = new TestGraph
            {
                Bool        = true,
                Double      = 1.0,
                Float       = 2.0F,
                Int         = 10,
                Long        = 100,
                String      = "foo",
                BoolArray   = Bools,
                IntArray    = Ints,
                LongArray   = Longs,
                FloatArray  = Floats,
                DoubleArray = Doubles,
                StringArray = Strings,
                NullArray   = null,
                BoolIList   = BoolsList,
                IntIList    = IntsList,
                LongIList   = LongsList,
                FloatIList  = FloatsList,
                DoubleIList = DoublesList,
                StringIList = StringsList
            };

            var vertex = new TestVertex("v1")
            {
                StringDefault = "bar",
                String        = "string",
                Int           = 10,
                Long          = 20,
                Float         = 25.0F,
                Double        = 30.0,
                Bool          = true,
                IntArray      = new[] { 1, 2, 3, 4 },
                IntIList      = new[] { 4, 5, 6, 7 }
            };

            graph.AddVertex(vertex);

            TestGraph serializedGraph = VerifySerialization(graph, serializeGraph);

            Assert.AreEqual(graph.Bool, serializedGraph.Bool);
            Assert.AreEqual(graph.Double, serializedGraph.Double);
            Assert.AreEqual(graph.Float, serializedGraph.Float);
            Assert.AreEqual(graph.Int, serializedGraph.Int);
            Assert.AreEqual(graph.Long, serializedGraph.Long);
            Assert.AreEqual(graph.String, serializedGraph.String);
            CollectionAssert.AreEqual(graph.BoolArray, serializedGraph.BoolArray);
            CollectionAssert.AreEqual(graph.IntArray, serializedGraph.IntArray);
            CollectionAssert.AreEqual(graph.LongArray, serializedGraph.LongArray);
            CollectionAssert.AreEqual(graph.StringArray, serializedGraph.StringArray);
            CollectionAssert.AreEqual(graph.FloatArray, serializedGraph.FloatArray, new FloatComparer(0.001F));
            CollectionAssert.AreEqual(graph.DoubleArray, serializedGraph.DoubleArray, new DoubleComparer(0.0001));
            CollectionAssert.AreEqual(graph.BoolIList, serializedGraph.BoolIList);
            CollectionAssert.AreEqual(graph.IntIList, serializedGraph.IntIList);
            CollectionAssert.AreEqual(graph.LongIList, serializedGraph.LongIList);
            CollectionAssert.AreEqual(graph.StringIList, serializedGraph.StringIList);
            CollectionAssert.AreEqual(graph.FloatIList, serializedGraph.FloatIList, new FloatComparer(0.001F));
            CollectionAssert.AreEqual(graph.DoubleIList, serializedGraph.DoubleIList, new DoubleComparer(0.0001));

            TestVertex serializedVertex = serializedGraph.Vertices.First();

            Assert.AreEqual("bar", serializedVertex.StringDefault);
            Assert.AreEqual(vertex.String, serializedVertex.String);
            Assert.AreEqual(vertex.Int, serializedVertex.Int);
            Assert.AreEqual(vertex.Long, serializedVertex.Long);
            Assert.AreEqual(vertex.Float, serializedVertex.Float);
            Assert.AreEqual(vertex.Double, serializedVertex.Double);
            Assert.AreEqual(vertex.Bool, serializedVertex.Bool);
            CollectionAssert.AreEqual(vertex.IntArray, serializedVertex.IntArray);
            CollectionAssert.AreEqual(vertex.IntIList, serializedVertex.IntIList);
        }
Ejemplo n.º 7
0
 private static TestGraph VerifySerialization(
     [NotNull] TestGraph graph,
     [NotNull, InstantHandle] Func <TestGraph, string> serializeGraph)
 {
     return(VerifySerialization(graph, serializeGraph, DeserializeGraph));
 }