//tests that both XML and Binary serialisation is invertible private void TestSerialisationIsInvertible(string xiFilename) { Console.Out.WriteLine("Testing file {0}", xiFilename); using (Stream inStr = File.OpenRead(xiFilename)) { Level levelFromBinary = new Level(); levelFromBinary.Deserialise(inStr); StringWriter sw = new StringWriter(); XmlSerializer xs = new XmlSerializer(typeof(Level)); xs.Serialize(sw, levelFromBinary); //now the XML version of the level is in sw Level levelFromXMLfromBinary = (Level)xs.Deserialize(new StringReader(sw.ToString())); //first check that the XML serialisation is the same StringWriter sw2 = new StringWriter(); xs.Serialize(sw2, levelFromBinary); Assert.AreEqual(sw.ToString(), sw2.ToString()); //now see if the XML-ified level will still write binary correctly: MemoryStream memStr = new MemoryStream(); inStr.Seek(0, SeekOrigin.Begin); DebugOutputStreamWithExpectations outStr = new DebugOutputStreamWithExpectations(inStr, memStr); levelFromXMLfromBinary.Serialise(outStr); inStr.Seek(0, SeekOrigin.Begin); memStr.Seek(0, SeekOrigin.Begin); Assert.IsTrue(StreamUtils.StreamsAreEqual(inStr, memStr)); } }
//this is a weaker test than TestSerialisationIsInvertible //but has been left here as it's sometimes handy for debugging private void TestBinaryUnkSerialisationIsInvertible(string xiFilename) { Console.Out.WriteLine("Testing file {0}", xiFilename); using (Stream inStr = File.OpenRead(xiFilename)) { FileChunk deser = new FileChunk(); deser.Deserialise(inStr); MemoryStream memStr = new MemoryStream(); inStr.Seek(0, SeekOrigin.Begin); DebugOutputStreamWithExpectations outStr = new DebugOutputStreamWithExpectations(inStr, memStr); deser.Serialise(outStr); inStr.Seek(0, SeekOrigin.Begin); memStr.Seek(0, SeekOrigin.Begin); Assert.IsTrue(StreamUtils.StreamsAreEqual(inStr, memStr)); } }