#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using HeuristicLab.Algorithms.GeneticAlgorithm; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Persistence; using HeuristicLab.Persistence.Auxiliary; using HeuristicLab.Persistence.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Persistence.Default.DebugString; using HeuristicLab.Persistence.Default.Xml; using HeuristicLab.Persistence.Tests; using HeuristicLab.Problems.TestFunctions; using HeuristicLab.Tests; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.PersistenceNew.Tests { public static class EnumerableTimeSpanExtensions { public static TimeSpan Average(this IEnumerable span) { var avg = (long)Math.Round(span.Select(x => x.Ticks).Average()); return new TimeSpan(avg); } } #region Test Classes [StorableType("7D9672BD-703D-42BB-9080-9929885D4580")] public class NumberTest { [Storable] private bool _bool = true; [Storable] private byte _byte = 0xFF; [Storable] private sbyte _sbyte = 0xF; [Storable] private short _short = -123; [Storable] private ushort _ushort = 123; [Storable] private int _int = -123; [Storable] private uint _uint = 123; [Storable] private long _long = 123456; [Storable] private ulong _ulong = 123456; public override bool Equals(object obj) { NumberTest nt = obj as NumberTest; if (nt == null) throw new NotSupportedException(); return nt._bool == _bool && nt._byte == _byte && nt._sbyte == _sbyte && nt._short == _short && nt._ushort == _ushort && nt._int == _int && nt._uint == _uint && nt._long == _long && nt._ulong == _ulong; } public override int GetHashCode() { return _bool.GetHashCode() ^ _byte.GetHashCode() ^ _sbyte.GetHashCode() ^ _short.GetHashCode() ^ _short.GetHashCode() ^ _int.GetHashCode() ^ _uint.GetHashCode() ^ _long.GetHashCode() ^ _ulong.GetHashCode(); } } [StorableType("EEB19599-D5AC-48ED-A56B-CF213DFAF2E4")] public class NonDefaultConstructorClass { [Storable] int value; public NonDefaultConstructorClass(int value) { this.value = value; } } [StorableType("EE43FE7A-6D07-4D52-9338-C21B3485F82A")] public class IntWrapper { [Storable] public int Value; private IntWrapper() { } public IntWrapper(int value) { this.Value = value; } public override bool Equals(object obj) { if (obj as IntWrapper == null) return false; return Value.Equals(((IntWrapper)obj).Value); } public override int GetHashCode() { return Value.GetHashCode(); } } [StorableType("00A8E48E-8E8A-443C-A327-9F6ACCBE7E80")] public class PrimitivesTest : NumberTest { [Storable] private char c = 'e'; [Storable] private long[,] _long_array = new long[,] { { 123, 456, }, { 789, 123 } }; [Storable] public List list = new List { 1, 2, 3, 4, 5 }; [Storable] private object o = new object(); public override bool Equals(object obj) { PrimitivesTest pt = obj as PrimitivesTest; if (pt == null) throw new NotSupportedException(); return base.Equals(obj) && c == pt.c && _long_array == pt._long_array && list == pt.list && o == pt.o; } public override int GetHashCode() { return base.GetHashCode() ^ c.GetHashCode() ^ _long_array.GetHashCode() ^ list.GetHashCode() ^ o.GetHashCode(); } } public enum TestEnum { va1, va2, va3, va8 }; [StorableType("26BA37F6-926D-4665-A10A-1F39E1CF6468")] public class RootBase { [Storable] private string baseString = " Serial "; [Storable] public TestEnum myEnum = TestEnum.va3; public override bool Equals(object obj) { RootBase rb = obj as RootBase; if (rb == null) throw new NotSupportedException(); return baseString == rb.baseString && myEnum == rb.myEnum; } public override int GetHashCode() { return baseString.GetHashCode() ^ myEnum.GetHashCode(); } } [StorableType("F6BCB436-B5F2-40F6-8E2F-7A018CD1CBA0")] public class Root : RootBase { [Storable] public Stack intStack = new Stack(); [Storable] public int[] i = new[] { 3, 4, 5, 6 }; [Storable(Name = "Test String")] public string s; [Storable] public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 }); [Storable] public List intList = new List(new[] { 321, 312, 321 }); [Storable] public Custom c; [Storable] public List selfReferences; [Storable] public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } }; [Storable] public bool boolean = true; [Storable] public DateTime dateTime; [Storable] public KeyValuePair kvp = new KeyValuePair("Serial", 123); [Storable] public Dictionary dict = new Dictionary(); [Storable(DefaultValue = "default")] public string uninitialized; } public enum SimpleEnum { one, two, three } public enum ComplexEnum { one = 1, two = 2, three = 3 } [FlagsAttribute] public enum TrickyEnum { zero = 0, one = 1, two = 2 } [StorableType("2F6326ED-023A-415F-B5C7-9F9241940D05")] public class EnumTest { [Storable] public SimpleEnum simpleEnum = SimpleEnum.one; [Storable] public ComplexEnum complexEnum = (ComplexEnum)2; [Storable] public TrickyEnum trickyEnum = (TrickyEnum)15; } [StorableType("92365E2A-1184-4280-B763-4853C7ADF3E3")] public class Custom { [Storable] public int i; [Storable] public Root r; [Storable] public string name = "]]>"; } [StorableType("7CF19EBC-1EC4-4FBE-BCA9-DA48E3CFE30D")] public class Manager { public DateTime lastLoadTime; [Storable] private DateTime lastLoadTimePersistence { get { return lastLoadTime; } set { lastLoadTime = DateTime.Now; } } [Storable] public double? dbl; } [StorableType("9092C705-F5E9-4BA9-9750-4357DB29AABF")] public class C { [Storable] public C[][] allCs; [Storable] public KeyValuePair, C> kvpList; } public class NonSerializable { int x = 0; public override bool Equals(object obj) { NonSerializable ns = obj as NonSerializable; if (ns == null) throw new NotSupportedException(); return ns.x == x; } public override int GetHashCode() { return x.GetHashCode(); } } [StorableType("FD953B0A-BDE6-41E6-91A8-CA3D90C91CDB")] public class SimpleClass { [Storable] public double x { get; set; } [Storable] public int y { get; set; } } #endregion [TestClass] public class UseCasesPersistenceNew { #region Helpers private string tempFile; [ClassInitialize] public static void Initialize(TestContext testContext) { ConfigurationService.Instance.Reset(); } [TestInitialize()] public void CreateTempFile() { tempFile = Path.GetTempFileName(); } [TestCleanup()] public void ClearTempFile() { StreamReader reader = new StreamReader(tempFile); string s = reader.ReadToEnd(); reader.Close(); File.Delete(tempFile); } #endregion #region Persistence 4.0 Profiling Helpers public class PerformanceData { public TimeSpan OldSerializingTime { get; set; } public TimeSpan NewSerializingTime { get; set; } public TimeSpan OldDeserializingTime { get; set; } public TimeSpan NewDeserializingTime { get; set; } public long OldFileSize { get; set; } public long NewFileSize { get; set; } public long OldSerializingMemoryConsumed { get; set; } public long NewSerializingMemoryConsumed { get; set; } public long OldDeserializingMemoryConsumed { get; set; } public long NewDeserializingMemoryConsumed { get; set; } } private void SerializeNew(object o) { ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(o, tempFile); } private void SerializeOld(object o) { XmlGenerator.Serialize(o, tempFile); } private object DeserializeNew() { ProtoBufSerializer serializer = new ProtoBufSerializer(); return serializer.Deserialize(tempFile); } private object DeserialiezOld() { return XmlParser.Deserialize(tempFile); } private void CollectGarbage() { GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); } public PerformanceData ProfileSingleRun(Func GenerateDataFunc) { PerformanceData performanceData = new PerformanceData(); Stopwatch sw = new Stopwatch(); object data = GenerateDataFunc(); object result = null; long startMem, endMem; //old file format serializing CollectGarbage(); startMem = GC.GetTotalMemory(false); sw.Start(); SerializeOld(data); sw.Stop(); endMem = GC.GetTotalMemory(false); performanceData.OldSerializingTime = sw.Elapsed; performanceData.OldFileSize = new FileInfo(tempFile).Length; performanceData.OldSerializingMemoryConsumed = endMem - startMem; sw.Reset(); //old file format deserializing CollectGarbage(); startMem = GC.GetTotalMemory(false); sw.Start(); result = DeserialiezOld(); sw.Stop(); endMem = GC.GetTotalMemory(false); performanceData.OldDeserializingTime = sw.Elapsed; performanceData.OldDeserializingMemoryConsumed = endMem - startMem; sw.Reset(); //new file format serializing CollectGarbage(); startMem = GC.GetTotalMemory(false); sw.Start(); SerializeNew(data); sw.Stop(); endMem = GC.GetTotalMemory(false); performanceData.NewSerializingTime = sw.Elapsed; performanceData.NewSerializingMemoryConsumed = endMem - startMem; performanceData.NewFileSize = new FileInfo(tempFile).Length; sw.Reset(); //new file format deserializing CollectGarbage(); startMem = GC.GetTotalMemory(false); sw.Start(); result = DeserializeNew(); sw.Stop(); endMem = GC.GetTotalMemory(false); performanceData.NewDeserializingTime = sw.Elapsed; performanceData.NewDeserializingMemoryConsumed = endMem - startMem; sw.Reset(); return performanceData; } public string Profile(Func GenerateDataFunc) { int nrOfRepetitions = 30; StringBuilder report = new StringBuilder(); List dataList = new List(); for (int i = 0; i < nrOfRepetitions; i++) { dataList.Add(ProfileSingleRun(GenerateDataFunc)); } report.Append("Performance Report for " + GenerateDataFunc.Method.Name + ": " + Environment.NewLine); report.Append(Environment.NewLine); report.AppendFormat("Avg. old vs. new time for serializing a file; {0};{1};{2}", dataList.Select(x => x.OldSerializingTime).Average(), dataList.Select(x => x.NewSerializingTime).Average(), dataList.Select(x => x.OldSerializingTime.TotalMilliseconds).Average() / dataList.Select(x => x.NewSerializingTime.TotalMilliseconds).Average() ); report.Append(Environment.NewLine); report.AppendFormat("Avg. old vs. new time for deserializing a file; {0};{1};{2}", dataList.Select(x => x.OldDeserializingTime).Average(), dataList.Select(x => x.NewDeserializingTime).Average(), dataList.Select(x => x.OldDeserializingTime.TotalMilliseconds).Average() / dataList.Select(x => x.NewDeserializingTime.TotalMilliseconds).Average() ); report.Append(Environment.NewLine); report.AppendFormat("Avg. old vs. new file size (in bytes); {0};{1};{2}", dataList.Select(x => x.OldFileSize).Average(), dataList.Select(x => x.NewFileSize).Average(), dataList.Select(x => x.OldFileSize).Average() / dataList.Select(x => x.NewFileSize).Average() ); report.Append(Environment.NewLine); report.AppendFormat("Avg. old vs. new memory consumption for serializing a file (in bytes); {0};{1};{2}", dataList.Select(x => x.OldSerializingMemoryConsumed).Average(), dataList.Select(x => x.NewSerializingMemoryConsumed).Average(), dataList.Select(x => x.OldSerializingMemoryConsumed).Average() / dataList.Select(x => x.NewSerializingMemoryConsumed).Average() ); report.Append(Environment.NewLine); report.AppendFormat("Avg. old vs. new memory consumption for deserializing a file (in bytes); {0};{1};{2}", dataList.Select(x => x.OldDeserializingMemoryConsumed).Average(), dataList.Select(x => x.NewDeserializingMemoryConsumed).Average(), dataList.Select(x => x.OldDeserializingMemoryConsumed).Average() / dataList.Select(x => x.NewDeserializingMemoryConsumed).Average() ); report.Append(Environment.NewLine); return report.ToString(); } #endregion #region Persistence 4.0 test methods [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestBool() { var test = new Func(() => { return true; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); bool result = (bool)o; Assert.AreEqual(test(), result); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestInt() { var test = new Func(() => { return (int)42; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); int result = (int)o; Assert.AreEqual(test(), result); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestDouble() { var test = new Func(() => { return 42.5d; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); double result = (double)o; Assert.AreEqual(test(), result); Assert.IsTrue(o is double); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestFloat() { var test = new Func(() => { return 42.5f; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); float result = (float)o; Assert.AreEqual(test(), result); Assert.IsTrue(o is float); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestDecimal() { var test = new Func(() => { return 42.5m; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); decimal result = (decimal)o; Assert.AreEqual(test(), result); Assert.IsTrue(o is decimal); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestLong() { var test = new Func(() => { return 42l; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); long result = (long)o; Assert.AreEqual(test(), result); Assert.IsTrue(o is long); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestUInt() { var test = new Func(() => { return 42u; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); uint result = (uint)o; Assert.AreEqual(test(), result); Assert.IsTrue(o is uint); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestShort() { var test = new Func(() => { short s = 42; return s; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); short result = (short)o; Assert.IsTrue(o is short); Assert.AreEqual(test(), result); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestByte() { var test = new Func(() => { byte b = 42; return b; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); byte result = (byte)o; Assert.IsTrue(o is byte); Assert.AreEqual(test(), result); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestEnumSimple() { var test = new Func(() => { return SimpleEnum.two; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); SimpleEnum result = (SimpleEnum)o; Assert.AreEqual(test(), result); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestEnumComplex() { var test = new Func(() => { return ComplexEnum.three; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); ComplexEnum result = (ComplexEnum)o; Assert.AreEqual(test(), result); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestType() { var test = new Func(() => { return typeof(HeuristicLab.Algorithms.GeneticAlgorithm.GeneticAlgorithm); }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); Type result = (Type)o; Assert.AreEqual(test(), result); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestBytes() { var test = new Func(() => { return new byte[] { 3, 1 }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); byte[] result = (byte[])o; Assert.AreEqual(test()[0], result[0]); Assert.AreEqual(test()[1], result[1]); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestSBytes() { var test = new Func(() => { return new sbyte[] { 3, 1 }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); sbyte[] result = (sbyte[])o; Assert.AreEqual(test()[0], result[0]); Assert.AreEqual(test()[1], result[1]); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestChars() { var test = new Func(() => { return new char[] { 'a', 'b' }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); char[] result = (char[])o; Assert.AreEqual(test()[0], result[0]); Assert.AreEqual(test()[1], result[1]); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestShorts() { var test = new Func(() => { return new short[] { 3, 1 }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); short[] result = (short[])o; Assert.AreEqual(test()[0], result[0]); Assert.AreEqual(test()[1], result[1]); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestUShorts() { var test = new Func(() => { return new ushort[] { 3, 1 }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); ushort[] result = (ushort[])o; Assert.AreEqual(test()[0], result[0]); Assert.AreEqual(test()[1], result[1]); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestString() { var test = new Func(() => { return "Hello World!"; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); string result = (string)o; Assert.AreEqual(test(), result); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestColor() { var test = new Func(() => { return Color.FromArgb(12, 34, 56, 78); }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); Color result = (Color)o; Assert.AreEqual(test(), result); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestPoint() { var test = new Func(() => { return new Point(3, 4); }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); Point result = (Point)o; Assert.AreEqual(((Point)test()).X, result.X); Assert.AreEqual(((Point)test()).Y, result.Y); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestBoolArray() { var test = new Func(() => { return new[] { true, false, true }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); bool[] result = (bool[])o; Assert.AreEqual(test()[0], result[0]); Assert.AreEqual(test()[1], result[1]); Assert.AreEqual(test()[2], result[2]); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestIntArray() { var test = new Func(() => { return new[] { 41, 22, 13 }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); int[] result = (int[])o; Assert.AreEqual(test()[0], result[0]); Assert.AreEqual(test()[1], result[1]); Assert.AreEqual(test()[2], result[2]); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestLongArray() { var test = new Func(() => { return new[] { 414481188112191633l, 245488586662l, 13546881335845865l }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); long[] result = (long[])o; Assert.AreEqual(test()[0], result[0]); Assert.AreEqual(test()[1], result[1]); Assert.AreEqual(test()[2], result[2]); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestDoubleArray() { var test = new Func(() => { return new[] { 41.5, 22.7, 13.8 }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); double[] result = (double[])o; Assert.AreEqual(test()[0], result[0]); Assert.AreEqual(test()[1], result[1]); Assert.AreEqual(test()[2], result[2]); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestObjectArray() { var test = new Func(() => { return new[] { new SimpleClass() { x = 42, y = 43 }, new SimpleClass() { x = 44.44, y = 5677 }, new SimpleClass() { x = 533.33, y = 2345 } }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); SimpleClass[] result = (SimpleClass[])o; Assert.AreEqual(test()[0].x, result[0].x); Assert.AreEqual(test()[0].y, result[0].y); Assert.AreEqual(test()[1].x, result[1].x); Assert.AreEqual(test()[1].y, result[1].y); Assert.AreEqual(test()[2].x, result[2].x); Assert.AreEqual(test()[2].y, result[2].y); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestStack() { var test = new Func(() => { return new Stack(new int[] { 1, 2, 3 }); }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); Stack result = (Stack)o; var actualStack = test(); Assert.AreEqual(actualStack.Pop(), result.Pop()); Assert.AreEqual(actualStack.Pop(), result.Pop()); Assert.AreEqual(actualStack.Pop(), result.Pop()); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestArrayOfStack() { var test = new Func(() => { return new object[] { new Stack(new int[] { 1, 2, 3 }), new Stack(new int[] { 1, 2, 3 }), }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); var result = (object[])o; var firstStack = (Stack)result[0]; var secondStack = (Stack)result[1]; var actual = test(); var actualFirst = (Stack)actual[0]; var actualSecond = (Stack)actual[1]; Assert.AreEqual(actualFirst.Pop(), firstStack.Pop()); Assert.AreEqual(actualFirst.Pop(), firstStack.Pop()); Assert.AreEqual(actualFirst.Pop(), firstStack.Pop()); Assert.AreEqual(actualSecond.Pop(), secondStack.Pop()); Assert.AreEqual(actualSecond.Pop(), secondStack.Pop()); Assert.AreEqual(actualSecond.Pop(), secondStack.Pop()); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestIntValueArray() { var test = new Func(() => { return new[] { new IntValue(41), new IntValue(22), new IntValue(13) }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); IntValue[] result = (IntValue[])o; Assert.AreEqual(test()[0].Value, result[0].Value); Assert.AreEqual(test()[1].Value, result[1].Value); Assert.AreEqual(test()[2].Value, result[2].Value); string msg = Profile(test); Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestIntValueArrayArray() { var test = new Func(() => { return new IntValue[][] { new IntValue[] { new IntValue(41), new IntValue(22), new IntValue(13) } }; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); IntValue[][] result = (IntValue[][])o; Assert.AreEqual(test()[0][0].Value, result[0][0].Value); Assert.AreEqual(test()[0][1].Value, result[0][1].Value); Assert.AreEqual(test()[0][2].Value, result[0][2].Value); string msg = Profile(test); Console.WriteLine(msg); } #endregion #region Old persistence test methods [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void ComplexStorable() { ProtoBufSerializer serializer = new ProtoBufSerializer(); Root r = InitializeComplexStorable(); serializer.Serialize(r, tempFile); Root newR = (Root)serializer.Deserialize(tempFile); CompareComplexStorables(r, newR); } private static void CompareComplexStorables(Root r, Root newR) { Assert.AreEqual( DebugStringGenerator.Serialize(r), DebugStringGenerator.Serialize(newR)); Assert.AreSame(newR, newR.selfReferences[0]); Assert.AreNotSame(r, newR); Assert.AreEqual(r.myEnum, TestEnum.va1); Assert.AreEqual(r.i[0], 7); Assert.AreEqual(r.i[1], 5); Assert.AreEqual(r.i[2], 6); Assert.AreEqual(r.s, "new value"); Assert.AreEqual(r.intArray[0], 3); Assert.AreEqual(r.intArray[1], 2); Assert.AreEqual(r.intArray[2], 1); Assert.AreEqual(r.intList[0], 9); Assert.AreEqual(r.intList[1], 8); Assert.AreEqual(r.intList[2], 7); Assert.AreEqual(r.multiDimArray[0, 0], 5); Assert.AreEqual(r.multiDimArray[0, 1], 4); Assert.AreEqual(r.multiDimArray[0, 2], 3); Assert.AreEqual(r.multiDimArray[1, 0], 1); Assert.AreEqual(r.multiDimArray[1, 1], 4); Assert.AreEqual(r.multiDimArray[1, 2], 6); Assert.IsFalse(r.boolean); Assert.IsTrue((DateTime.Now - r.dateTime).TotalSeconds < 10); Assert.AreEqual(r.kvp.Key, "string key"); Assert.AreEqual(r.kvp.Value, 321); Assert.IsNull(r.uninitialized); Assert.AreEqual(newR.myEnum, TestEnum.va1); Assert.AreEqual(newR.i[0], 7); Assert.AreEqual(newR.i[1], 5); Assert.AreEqual(newR.i[2], 6); Assert.AreEqual(newR.s, "new value"); Assert.AreEqual(newR.intArray[0], 3); Assert.AreEqual(newR.intArray[1], 2); Assert.AreEqual(newR.intArray[2], 1); Assert.AreEqual(newR.intList[0], 9); Assert.AreEqual(newR.intList[1], 8); Assert.AreEqual(newR.intList[2], 7); Assert.AreEqual(newR.multiDimArray[0, 0], 5); Assert.AreEqual(newR.multiDimArray[0, 1], 4); Assert.AreEqual(newR.multiDimArray[0, 2], 3); Assert.AreEqual(newR.multiDimArray[1, 0], 1); Assert.AreEqual(newR.multiDimArray[1, 1], 4); Assert.AreEqual(newR.multiDimArray[1, 2], 6); Assert.AreEqual(newR.intStack.Pop(), 3); Assert.AreEqual(newR.intStack.Pop(), 2); Assert.AreEqual(newR.intStack.Pop(), 1); Assert.IsFalse(newR.boolean); Assert.IsTrue((DateTime.Now - newR.dateTime).TotalSeconds < 10); Assert.AreEqual(newR.kvp.Key, "string key"); Assert.AreEqual(newR.kvp.Value, 321); Assert.IsNull(newR.uninitialized); } private static Root InitializeComplexStorable() { Root r = new Root(); r.intStack.Push(1); r.intStack.Push(2); r.intStack.Push(3); r.selfReferences = new List { r, r }; r.c = new Custom { r = r }; r.dict.Add("one", 1); r.dict.Add("two", 2); r.dict.Add("three", 3); r.myEnum = TestEnum.va1; r.i = new[] { 7, 5, 6 }; r.s = "new value"; r.intArray = new ArrayList { 3, 2, 1 }; r.intList = new List { 9, 8, 7 }; r.multiDimArray = new double[,] { { 5, 4, 3 }, { 1, 4, 6 } }; r.boolean = false; r.dateTime = DateTime.Now; r.kvp = new KeyValuePair("string key", 321); r.uninitialized = null; return r; } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void SelfReferences() { ProtoBufSerializer serializer = new ProtoBufSerializer(); C c = new C(); C[][] cs = new C[2][]; cs[0] = new C[] { c }; cs[1] = new C[] { c }; c.allCs = cs; c.kvpList = new KeyValuePair, C>(new List { c }, c); serializer.Serialize(cs, tempFile); object o = serializer.Deserialize(tempFile); Assert.AreEqual( DebugStringGenerator.Serialize(cs), DebugStringGenerator.Serialize(o)); Assert.AreSame(c, c.allCs[0][0]); Assert.AreSame(c, c.allCs[1][0]); Assert.AreSame(c, c.kvpList.Key[0]); Assert.AreSame(c, c.kvpList.Value); C[][] newCs = (C[][])o; C newC = newCs[0][0]; Assert.AreSame(newC, newC.allCs[0][0]); Assert.AreSame(newC, newC.allCs[1][0]); Assert.AreSame(newC, newC.kvpList.Key[0]); Assert.AreSame(newC, newC.kvpList.Value); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void ArrayCreation() { ProtoBufSerializer serializer = new ProtoBufSerializer(); ArrayList[] arrayListArray = new ArrayList[4]; arrayListArray[0] = new ArrayList(); arrayListArray[0].Add(arrayListArray); arrayListArray[0].Add(arrayListArray); arrayListArray[1] = new ArrayList(); arrayListArray[1].Add(arrayListArray); arrayListArray[2] = new ArrayList(); arrayListArray[2].Add(arrayListArray); arrayListArray[2].Add(arrayListArray); //Array a = Array.CreateInstance( // typeof(object), // new[] { 1, 2 }, new[] { 3, 4 }); //arrayListArray[2].Add(a); serializer.Serialize(arrayListArray, tempFile); object o = serializer.Deserialize(tempFile); Assert.AreEqual( DebugStringGenerator.Serialize(arrayListArray), DebugStringGenerator.Serialize(o)); ArrayList[] newArray = (ArrayList[])o; Assert.AreSame(arrayListArray, arrayListArray[0][0]); Assert.AreSame(arrayListArray, arrayListArray[2][1]); Assert.AreSame(newArray, newArray[0][0]); Assert.AreSame(newArray, newArray[2][1]); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void CustomSerializationProperty() { ProtoBufSerializer serializer = new ProtoBufSerializer(); Manager m = new Manager(); serializer.Serialize(m, tempFile); Manager newM = (Manager)serializer.Deserialize(tempFile); Assert.AreNotEqual( DebugStringGenerator.Serialize(m), DebugStringGenerator.Serialize(newM)); Assert.AreEqual(m.dbl, newM.dbl); Assert.AreEqual(m.lastLoadTime, new DateTime()); Assert.AreNotEqual(newM.lastLoadTime, new DateTime()); Assert.IsTrue((DateTime.Now - newM.lastLoadTime).TotalSeconds < 10); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void Primitives() { ProtoBufSerializer serializer = new ProtoBufSerializer(); PrimitivesTest sdt = new PrimitivesTest(); serializer.Serialize(sdt, tempFile); object o = serializer.Deserialize(tempFile); Assert.AreEqual( DebugStringGenerator.Serialize(sdt), DebugStringGenerator.Serialize(o)); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void MultiDimensionalArray() { ProtoBufSerializer serializer = new ProtoBufSerializer(); string[,] mDimString = new string[,] { {"ora", "et", "labora"}, {"Beten", "und", "Arbeiten"} }; serializer.Serialize(mDimString, tempFile); object o = serializer.Deserialize(tempFile); Assert.AreEqual( DebugStringGenerator.Serialize(mDimString), DebugStringGenerator.Serialize(o)); } [StorableType("87A331AF-14DC-48B3-B577-D49065743BE6")] public class NestedType { [Storable] private string value = "value"; public override bool Equals(object obj) { NestedType nt = obj as NestedType; if (nt == null) throw new NotSupportedException(); return nt.value == value; } public override int GetHashCode() { return value.GetHashCode(); } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void NestedTypeTest() { ProtoBufSerializer serializer = new ProtoBufSerializer(); NestedType t = new NestedType(); serializer.Serialize(t, tempFile); object o = serializer.Deserialize(tempFile); Assert.AreEqual( DebugStringGenerator.Serialize(t), DebugStringGenerator.Serialize(o)); Assert.IsTrue(t.Equals(o)); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void SimpleArray() { ProtoBufSerializer serializer = new ProtoBufSerializer(); string[] strings = { "ora", "et", "labora" }; serializer.Serialize(strings, tempFile); object o = serializer.Deserialize(tempFile); Assert.AreEqual( DebugStringGenerator.Serialize(strings), DebugStringGenerator.Serialize(o)); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void PrimitiveRoot() { ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(12.3f, tempFile); object o = serializer.Deserialize(tempFile); Assert.AreEqual( DebugStringGenerator.Serialize(12.3f), DebugStringGenerator.Serialize(o)); } private string formatFullMemberName(MemberInfo mi) { return new StringBuilder() .Append(mi.DeclaringType.Assembly.GetName().Name) .Append(": ") .Append(mi.DeclaringType.Namespace) .Append('.') .Append(mi.DeclaringType.Name) .Append('.') .Append(mi.Name).ToString(); } public void CodingConventions() { List lowerCaseMethodNames = new List(); List lowerCaseProperties = new List(); List lowerCaseFields = new List(); foreach (Assembly a in PluginLoader.Assemblies) { if (!a.GetName().Name.StartsWith("HeuristicLab")) continue; foreach (Type t in a.GetTypes()) { foreach (MemberInfo mi in t.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) { if (mi.DeclaringType.Name.StartsWith("<>")) continue; if (char.IsLower(mi.Name[0])) { if (mi.MemberType == MemberTypes.Field) lowerCaseFields.Add(formatFullMemberName(mi)); if (mi.MemberType == MemberTypes.Property) lowerCaseProperties.Add(formatFullMemberName(mi)); if (mi.MemberType == MemberTypes.Method && !mi.Name.StartsWith("get_") && !mi.Name.StartsWith("set_") && !mi.Name.StartsWith("add_") && !mi.Name.StartsWith("remove_") && !mi.Name.StartsWith("op_")) lowerCaseMethodNames.Add(formatFullMemberName(mi)); } } } } //Assert.AreEqual("", lowerCaseFields.Aggregate("", (a, b) => a + "\r\n" + b)); Assert.AreEqual("", lowerCaseMethodNames.Aggregate("", (a, b) => a + "\r\n" + b)); Assert.AreEqual("", lowerCaseProperties.Aggregate("", (a, b) => a + "\r\n" + b)); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void Enums() { ProtoBufSerializer serializer = new ProtoBufSerializer(); EnumTest et = new EnumTest(); et.simpleEnum = SimpleEnum.two; et.complexEnum = ComplexEnum.three; et.trickyEnum = TrickyEnum.two | TrickyEnum.one; serializer.Serialize(et, tempFile); EnumTest newEt = (EnumTest)serializer.Deserialize(tempFile); Assert.AreEqual(et.simpleEnum, SimpleEnum.two); Assert.AreEqual(et.complexEnum, ComplexEnum.three); Assert.AreEqual(et.trickyEnum, (TrickyEnum)3); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestAliasingWithOverriddenEquals() { ProtoBufSerializer serializer = new ProtoBufSerializer(); List ints = new List(); ints.Add(new IntWrapper(1)); ints.Add(new IntWrapper(1)); Assert.AreEqual(ints[0], ints[1]); Assert.AreNotSame(ints[0], ints[1]); serializer.Serialize(ints, tempFile); List newInts = (List)serializer.Deserialize(tempFile); Assert.AreEqual(newInts[0].Value, 1); Assert.AreEqual(newInts[1].Value, 1); Assert.AreEqual(newInts[0], newInts[1]); Assert.AreNotSame(newInts[0], newInts[1]); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void NonDefaultConstructorTest() { ProtoBufSerializer serializer = new ProtoBufSerializer(); NonDefaultConstructorClass c = new NonDefaultConstructorClass(1); try { serializer.Serialize(c, tempFile); Assert.Fail("Exception not thrown"); } catch (PersistenceException) { } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestSavingException() { ProtoBufSerializer serializer = new ProtoBufSerializer(); List list = new List { 1, 2, 3 }; serializer.Serialize(list, tempFile); NonSerializable s = new NonSerializable(); try { serializer.Serialize(s, tempFile); Assert.Fail("Exception expected"); } catch (PersistenceException) { } List newList = (List)serializer.Deserialize(tempFile); Assert.AreEqual(list[0], newList[0]); Assert.AreEqual(list[1], newList[1]); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestTypeStringConversion() { string name = typeof(List[]).AssemblyQualifiedName; string shortName = "System.Collections.Generic.List`1[[System.Int32, mscorlib]][], mscorlib"; Assert.AreEqual(name, TypeNameParser.Parse(name).ToString()); Assert.AreEqual(shortName, TypeNameParser.Parse(name).ToString(false)); Assert.AreEqual(shortName, typeof(List[]).VersionInvariantName()); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestHexadecimalPublicKeyToken() { string name = "TestClass, TestAssembly, Version=1.2.3.4, PublicKey=1234abc"; string shortName = "TestClass, TestAssembly"; Assert.AreEqual(name, TypeNameParser.Parse(name).ToString()); Assert.AreEqual(shortName, TypeNameParser.Parse(name).ToString(false)); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void InheritanceTest() { ProtoBufSerializer serializer = new ProtoBufSerializer(); New n = new New(); serializer.Serialize(n, tempFile); New nn = (New)serializer.Deserialize(tempFile); Assert.AreEqual(n.Name, nn.Name); Assert.AreEqual(((Override)n).Name, ((Override)nn).Name); } [StorableType("B963EF51-12B4-432E-8C54-88F026F9ACE2")] class Child { [Storable] public GrandParent grandParent; } [StorableType("E66E9606-967A-4C35-A361-F6F0D21C064A")] class Parent { [Storable] public Child child; } [StorableType("34D3893A-57AD-4F72-878B-81D6FA3F14A9")] class GrandParent { [Storable] public Parent parent; } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void InstantiateParentChainReference() { ProtoBufSerializer serializer = new ProtoBufSerializer(); GrandParent gp = new GrandParent(); gp.parent = new Parent(); gp.parent.child = new Child(); gp.parent.child.grandParent = gp; Assert.AreSame(gp, gp.parent.child.grandParent); serializer.Serialize(gp, tempFile); GrandParent newGp = (GrandParent)serializer.Deserialize(tempFile); Assert.AreSame(newGp, newGp.parent.child.grandParent); } [StorableType("15DF777F-B12D-4FD4-88C3-8CB4C9CE4F0C")] struct TestStruct { int value; int PropertyValue { get; set; } public TestStruct(int value) : this() { this.value = value; PropertyValue = value; } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void StructTest() { ProtoBufSerializer serializer = new ProtoBufSerializer(); TestStruct s = new TestStruct(10); serializer.Serialize(s, tempFile); TestStruct newS = (TestStruct)serializer.Deserialize(tempFile); Assert.AreEqual(s, newS); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void PointTest() { ProtoBufSerializer serializer = new ProtoBufSerializer(); Point p = new Point(12, 34); serializer.Serialize(p, tempFile); Point newP = (Point)serializer.Deserialize(tempFile); Assert.AreEqual(p, newP); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void NullableValueTypes() { ProtoBufSerializer serializer = new ProtoBufSerializer(); double?[] d = new double?[] { null, 1, 2, 3 }; serializer.Serialize(d, tempFile); double?[] newD = (double?[])serializer.Deserialize(tempFile); Assert.AreEqual(d[0], newD[0]); Assert.AreEqual(d[1], newD[1]); Assert.AreEqual(d[2], newD[2]); Assert.AreEqual(d[3], newD[3]); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void BitmapTest() { ProtoBufSerializer serializer = new ProtoBufSerializer(); Icon icon = System.Drawing.SystemIcons.Hand; Bitmap bitmap = icon.ToBitmap(); serializer.Serialize(bitmap, tempFile); Bitmap newBitmap = (Bitmap)serializer.Deserialize(tempFile); Assert.AreEqual(bitmap.Size, newBitmap.Size); for (int i = 0; i < bitmap.Size.Width; i++) for (int j = 0; j < bitmap.Size.Height; j++) Assert.AreEqual(bitmap.GetPixel(i, j), newBitmap.GetPixel(i, j)); } [StorableType("E846BC49-20F3-4D3F-A3F3-73D4F2DB1C2E")] private class PersistenceHooks { [Storable] public int a; [Storable] public int b; public int sum; public bool WasSerialized { get; private set; } [StorableHook(HookType.BeforeSerialization)] void PreSerializationHook() { WasSerialized = true; } [StorableHook(HookType.AfterDeserialization)] void PostDeserializationHook() { sum = a + b; } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void HookTest() { ProtoBufSerializer serializer = new ProtoBufSerializer(); PersistenceHooks hookTest = new PersistenceHooks(); hookTest.a = 2; hookTest.b = 5; Assert.IsFalse(hookTest.WasSerialized); Assert.AreEqual(hookTest.sum, 0); serializer.Serialize(hookTest, tempFile); Assert.IsTrue(hookTest.WasSerialized); Assert.AreEqual(hookTest.sum, 0); PersistenceHooks newHookTest = (PersistenceHooks)serializer.Deserialize(tempFile); Assert.AreEqual(newHookTest.a, hookTest.a); Assert.AreEqual(newHookTest.b, hookTest.b); Assert.AreEqual(newHookTest.sum, newHookTest.a + newHookTest.b); Assert.IsFalse(newHookTest.WasSerialized); } [StorableType("A35D71DF-397F-4910-A950-ED6923BE9483")] private class CustomConstructor { public string Value = "none"; public CustomConstructor() { Value = "default"; } [StorableConstructor] private CustomConstructor(bool deserializing) { Assert.IsTrue(deserializing); Value = "persistence"; } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestCustomConstructor() { ProtoBufSerializer serializer = new ProtoBufSerializer(); CustomConstructor cc = new CustomConstructor(); Assert.AreEqual(cc.Value, "default"); serializer.Serialize(cc, tempFile); CustomConstructor newCC = (CustomConstructor)serializer.Deserialize(tempFile); Assert.AreEqual(newCC.Value, "persistence"); } [StorableType("D276E825-1F35-4BAC-8937-9ABC91D5C316")] public class ExplodingDefaultConstructor { public ExplodingDefaultConstructor() { throw new Exception("this constructor will always fail"); } public ExplodingDefaultConstructor(string password) { } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestConstructorExceptionUnwrapping() { ProtoBufSerializer serializer = new ProtoBufSerializer(); ExplodingDefaultConstructor x = new ExplodingDefaultConstructor("password"); serializer.Serialize(x, tempFile); try { ExplodingDefaultConstructor newX = (ExplodingDefaultConstructor)serializer.Deserialize(tempFile); Assert.Fail("Exception expected"); } catch (PersistenceException pe) { Assert.AreEqual(pe.InnerException.Message, "this constructor will always fail"); } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestStreaming() { ProtoBufSerializer serializer = new ProtoBufSerializer(); using (MemoryStream stream = new MemoryStream()) { Root r = InitializeComplexStorable(); serializer.Serialize(r, stream); using (MemoryStream stream2 = new MemoryStream(stream.ToArray())) { Root newR = (Root)serializer.Deserialize(stream2); CompareComplexStorables(r, newR); } } } [StorableType("4921031B-CB61-4677-97AD-9236A4CEC200")] public class HookInheritanceTestBase { [Storable] public object a; public object link; [StorableHook(HookType.AfterDeserialization)] private void relink() { link = a; } } [StorableType("321CEE0A-5201-4CE2-B135-2343890D96BF")] public class HookInheritanceTestDerivedClass : HookInheritanceTestBase { [Storable] public object b; [StorableHook(HookType.AfterDeserialization)] private void relink() { Assert.AreSame(a, link); link = b; } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestLinkInheritance() { ProtoBufSerializer serializer = new ProtoBufSerializer(); HookInheritanceTestDerivedClass c = new HookInheritanceTestDerivedClass(); c.a = new object(); serializer.Serialize(c, tempFile); HookInheritanceTestDerivedClass newC = (HookInheritanceTestDerivedClass)serializer.Deserialize(tempFile); Assert.AreSame(c.b, c.link); } [StorableType(StorableMemberSelection.AllFields, "B9AB42E8-1932-425B-B4CF-F31F07EAC599")] public class AllFieldsStorable { public int Value1 = 1; [Storable] public int Value2 = 2; public int Value3 { get; private set; } public int Value4 { get; private set; } [StorableConstructor] public AllFieldsStorable(bool isDeserializing) { if (!isDeserializing) { Value1 = 12; Value2 = 23; Value3 = 34; Value4 = 56; } } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestStorableTypeDiscoveryAllFields() { ProtoBufSerializer serializer = new ProtoBufSerializer(); AllFieldsStorable afs = new AllFieldsStorable(false); serializer.Serialize(afs, tempFile); AllFieldsStorable newAfs = (AllFieldsStorable)serializer.Deserialize(tempFile); Assert.AreEqual(afs.Value1, newAfs.Value1); Assert.AreEqual(afs.Value2, newAfs.Value2); Assert.AreEqual(0, newAfs.Value3); Assert.AreEqual(0, newAfs.Value4); } [StorableType(StorableMemberSelection.AllProperties, "CB7DC31C-AEF3-4EB8-91CA-248B767E9F92")] public class AllPropertiesStorable { public int Value1 = 1; [Storable] public int Value2 = 2; public int Value3 { get; private set; } public int Value4 { get; private set; } [StorableConstructor] public AllPropertiesStorable(bool isDeserializing) { if (!isDeserializing) { Value1 = 12; Value2 = 23; Value3 = 34; Value4 = 56; } } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestStorableTypeDiscoveryAllProperties() { ProtoBufSerializer serializer = new ProtoBufSerializer(); AllPropertiesStorable afs = new AllPropertiesStorable(false); serializer.Serialize(afs, tempFile); AllPropertiesStorable newAfs = (AllPropertiesStorable)serializer.Deserialize(tempFile); Assert.AreEqual(1, newAfs.Value1); Assert.AreEqual(2, newAfs.Value2); Assert.AreEqual(afs.Value3, newAfs.Value3); Assert.AreEqual(afs.Value4, newAfs.Value4); } [StorableType(StorableMemberSelection.AllFieldsAndAllProperties, "0AD8D68F-E0FF-4FA8-8A72-1148CD91A2B9")] public class AllFieldsAndAllPropertiesStorable { public int Value1 = 1; [Storable] public int Value2 = 2; public int Value3 { get; private set; } public int Value4 { get; private set; } [StorableConstructor] public AllFieldsAndAllPropertiesStorable(bool isDeserializing) { if (!isDeserializing) { Value1 = 12; Value2 = 23; Value3 = 34; Value4 = 56; } } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestStorableTypeDiscoveryAllFieldsAndAllProperties() { ProtoBufSerializer serializer = new ProtoBufSerializer(); AllFieldsAndAllPropertiesStorable afs = new AllFieldsAndAllPropertiesStorable(false); serializer.Serialize(afs, tempFile); AllFieldsAndAllPropertiesStorable newAfs = (AllFieldsAndAllPropertiesStorable)serializer.Deserialize(tempFile); Assert.AreEqual(afs.Value1, newAfs.Value1); Assert.AreEqual(afs.Value2, newAfs.Value2); Assert.AreEqual(afs.Value3, newAfs.Value3); Assert.AreEqual(afs.Value4, newAfs.Value4); } [StorableType(StorableMemberSelection.MarkedOnly, "0D94E6D4-64E3-4637-B1EE-DEF2B3F6E2E0")] public class MarkedOnlyStorable { public int Value1 = 1; [Storable] public int Value2 = 2; public int Value3 { get; private set; } public int Value4 { get; private set; } [StorableConstructor] public MarkedOnlyStorable(bool isDeserializing) { if (!isDeserializing) { Value1 = 12; Value2 = 23; Value3 = 34; Value4 = 56; } } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestStorableTypeDiscoveryMarkedOnly() { ProtoBufSerializer serializer = new ProtoBufSerializer(); MarkedOnlyStorable afs = new MarkedOnlyStorable(false); serializer.Serialize(afs, tempFile); MarkedOnlyStorable newAfs = (MarkedOnlyStorable)serializer.Deserialize(tempFile); Assert.AreEqual(1, newAfs.Value1); Assert.AreEqual(afs.Value2, newAfs.Value2); Assert.AreEqual(0, newAfs.Value3); Assert.AreEqual(0, newAfs.Value4); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestLineEndings() { ProtoBufSerializer serializer = new ProtoBufSerializer(); List lineBreaks = new List { "\r\n", "\n", "\r", "\n\r", Environment.NewLine }; List lines = new List(); foreach (var br in lineBreaks) lines.Add("line1" + br + "line2"); serializer.Serialize(lines, tempFile); List newLines = (List)serializer.Deserialize(tempFile); Assert.AreEqual(lines.Count, newLines.Count); for (int i = 0; i < lineBreaks.Count; i++) { Assert.AreEqual(lines[i], newLines[i]); } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestSpecialNumbers() { ProtoBufSerializer serializer = new ProtoBufSerializer(); List specials = new List() { 1.0 / 0, -1.0 / 0, 0.0 / 0 }; Assert.IsTrue(double.IsPositiveInfinity(specials[0])); Assert.IsTrue(double.IsNegativeInfinity(specials[1])); Assert.IsTrue(double.IsNaN(specials[2])); serializer.Serialize(specials, tempFile); List newSpecials = (List)serializer.Deserialize(tempFile); Assert.IsTrue(double.IsPositiveInfinity(newSpecials[0])); Assert.IsTrue(double.IsNegativeInfinity(newSpecials[1])); Assert.IsTrue(double.IsNaN(newSpecials[2])); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestStringSplit() { string s = "1.2;2.3;3.4;;;4.9"; var l = s.EnumerateSplit(';').ToList(); Assert.AreEqual("1.2", l[0]); Assert.AreEqual("2.3", l[1]); Assert.AreEqual("3.4", l[2]); Assert.AreEqual("4.9", l[3]); } [StorableType("B13CB1B0-D2DA-47B8-A715-B166A28B1F03")] private class IdentityComparer : IEqualityComparer { public bool Equals(T x, T y) { return x.Equals(y); } public int GetHashCode(T obj) { return obj.GetHashCode(); } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestHashSetSerializer() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var hashSets = new List>() { new HashSet(new[] { 1, 2, 3 }), new HashSet(new[] { 4, 5, 6 }, new IdentityComparer()), }; serializer.Serialize(hashSets, tempFile); var newHashSets = (List>)serializer.Deserialize(tempFile); Assert.IsTrue(newHashSets[0].Contains(1)); Assert.IsTrue(newHashSets[0].Contains(2)); Assert.IsTrue(newHashSets[0].Contains(3)); Assert.IsTrue(newHashSets[1].Contains(4)); Assert.IsTrue(newHashSets[1].Contains(5)); Assert.IsTrue(newHashSets[1].Contains(6)); Assert.AreEqual(newHashSets[0].Comparer.GetType(), new HashSet().Comparer.GetType()); Assert.AreEqual(newHashSets[1].Comparer.GetType(), typeof(IdentityComparer)); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestConcreteDictionarySerializer() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var dictionaries = new List>() { new Dictionary(), new Dictionary(new IdentityComparer()), }; dictionaries[0].Add(1, 1); dictionaries[0].Add(2, 2); dictionaries[0].Add(3, 3); dictionaries[1].Add(4, 4); dictionaries[1].Add(5, 5); dictionaries[1].Add(6, 6); serializer.Serialize(dictionaries, tempFile); var newDictionaries = (List>)serializer.Deserialize(tempFile); Assert.IsTrue(newDictionaries[0].ContainsKey(1)); Assert.IsTrue(newDictionaries[0].ContainsKey(2)); Assert.IsTrue(newDictionaries[0].ContainsKey(3)); Assert.IsTrue(newDictionaries[1].ContainsKey(4)); Assert.IsTrue(newDictionaries[1].ContainsKey(5)); Assert.IsTrue(newDictionaries[1].ContainsKey(6)); Assert.IsTrue(newDictionaries[0].ContainsValue(1)); Assert.IsTrue(newDictionaries[0].ContainsValue(2)); Assert.IsTrue(newDictionaries[0].ContainsValue(3)); Assert.IsTrue(newDictionaries[1].ContainsValue(4)); Assert.IsTrue(newDictionaries[1].ContainsValue(5)); Assert.IsTrue(newDictionaries[1].ContainsValue(6)); Assert.AreEqual(new Dictionary().Comparer.GetType(), newDictionaries[0].Comparer.GetType()); Assert.AreEqual(typeof(IdentityComparer), newDictionaries[1].Comparer.GetType()); } [StorableType("A9B0D7FB-0CAF-4DD7-9045-EA136F9176F7")] public class ReadOnlyFail { [Storable] public string ReadOnly { get { return "fail"; } } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestReadOnlyFail() { ProtoBufSerializer serializer = new ProtoBufSerializer(); try { serializer.Serialize(new ReadOnlyFail(), tempFile); Assert.Fail("Exception expected"); } catch (PersistenceException) { } catch { Assert.Fail("PersistenceException expected"); } } [StorableType("2C9CC576-6823-4784-817B-37C8AF0B1C29")] public class WriteOnlyFail { [Storable] public string WriteOnly { set { throw new InvalidOperationException("this property should never be set."); } } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestWriteOnlyFail() { ProtoBufSerializer serializer = new ProtoBufSerializer(); try { serializer.Serialize(new WriteOnlyFail(), tempFile); Assert.Fail("Exception expected"); } catch (PersistenceException) { } catch { Assert.Fail("PersistenceException expected."); } } [StorableType("8052D9E3-6DDD-4AE1-9B5B-67C6D5436512")] public class OneWayTest { public OneWayTest() { this.value = "default"; } public string value; [Storable(AllowOneWay = true)] public string ReadOnly { get { return "ReadOnly"; } } [Storable(AllowOneWay = true)] public string WriteOnly { set { this.value = value; } } } //TODO /* [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestTypeCacheExport() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var test = new List>(); test.Add(new List() { 1, 2, 3 }); IEnumerable types; using (var stream = new MemoryStream()) { XmlGenerator.Serialize(test, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, out types); } List t = new List(types); // Assert.IsTrue(t.Contains(typeof(int))); not serialized as an int list is directly transformed into a string Assert.IsTrue(t.Contains(typeof(List))); Assert.IsTrue(t.Contains(typeof(List>))); Assert.AreEqual(t.Count, 2); }*/ [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TupleTest() { var t1 = Tuple.Create(1); var t2 = Tuple.Create('1', "2"); var t3 = Tuple.Create(3.0, 3f, 5); var t4 = Tuple.Create(Tuple.Create(1, 2, 3), Tuple.Create(4, 5, 6), Tuple.Create(8, 9, 10)); var tuple = Tuple.Create(t1, t2, t3, t4); SerializeNew(tuple); var newTuple = DeserializeNew(); Assert.AreEqual(tuple, newTuple); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void FontTest() { ProtoBufSerializer serializer = new ProtoBufSerializer(); List fonts = new List() { new Font(FontFamily.GenericSansSerif, 12), new Font("Times New Roman", 21, FontStyle.Bold, GraphicsUnit.Pixel), new Font("Courier New", 10, FontStyle.Underline, GraphicsUnit.Document), new Font("Helvetica", 21, FontStyle.Strikeout, GraphicsUnit.Inch, 0, true), }; serializer.Serialize(fonts, tempFile); var newFonts = (List)serializer.Deserialize(tempFile); Assert.AreEqual(fonts[0], newFonts[0]); Assert.AreEqual(fonts[1], newFonts[1]); Assert.AreEqual(fonts[2], newFonts[2]); Assert.AreEqual(fonts[3], newFonts[3]); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "medium")] public void ConcurrencyTest() { ProtoBufSerializer serializer = new ProtoBufSerializer(); int n = 20; Task[] tasks = new Task[n]; for (int i = 0; i < n; i++) { tasks[i] = Task.Factory.StartNew((idx) => { byte[] data; using (var stream = new MemoryStream()) { serializer.Serialize(new GeneticAlgorithm(), stream); data = stream.ToArray(); } }, i); } Task.WaitAll(tasks); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "medium")] public void ConcurrentBitmapTest() { ProtoBufSerializer serializer = new ProtoBufSerializer(); Bitmap b = new Bitmap(300, 300); System.Random r = new System.Random(); for (int x = 0; x < b.Height; x++) { for (int y = 0; y < b.Width; y++) { b.SetPixel(x, y, Color.FromArgb(r.Next())); } } Task[] tasks = new Task[20]; byte[][] datas = new byte[tasks.Length][]; for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Factory.StartNew((idx) => { using (var stream = new MemoryStream()) { serializer.Serialize(b, stream); datas[(int)idx] = stream.ToArray(); } }, i); } Task.WaitAll(tasks); } public class G { public class S { } public class S2 { } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestSpecialCharacters() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var s = "abc" + "\x15" + "def"; serializer.Serialize(s, tempFile); var newS = serializer.Deserialize(tempFile); Assert.AreEqual(s, newS); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestByteArray() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var b = new byte[3]; b[0] = 0; b[1] = 200; b[2] = byte.MaxValue; serializer.Serialize(b, tempFile); var newB = (byte[])serializer.Deserialize(tempFile); CollectionAssert.AreEqual(b, newB); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestOptionalNumberEnumerable() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var values = new List { 0, null, double.NaN, double.PositiveInfinity, double.MaxValue, 1 }; serializer.Serialize(values, tempFile); var newValues = (List)serializer.Deserialize(tempFile); CollectionAssert.AreEqual(values, newValues); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestOptionalDateTimeEnumerable() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var values = new List { DateTime.MinValue, null, DateTime.Now, DateTime.Now.Add(TimeSpan.FromDays(1)), DateTime.ParseExact("10.09.2014 12:21", "dd.MM.yyyy hh:mm", CultureInfo.InvariantCulture), DateTime.MaxValue}; serializer.Serialize(values, tempFile); var newValues = (List)serializer.Deserialize(tempFile); CollectionAssert.AreEqual(values, newValues); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestStringEnumerable() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var values = new List { "", null, "s", "string", string.Empty, "123", "", ")serializer.Deserialize(tempFile); CollectionAssert.AreEqual(values, newValues); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestUnicodeCharArray() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var s = Encoding.UTF8.GetChars(new byte[] { 0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb }); serializer.Serialize(s, tempFile); var newS = (char[])serializer.Deserialize(tempFile); CollectionAssert.AreEqual(s, newS); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestUnicode() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var s = Encoding.UTF8.GetString(new byte[] { 0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb }); serializer.Serialize(s, tempFile); var newS = serializer.Deserialize(tempFile); Assert.AreEqual(s, newS); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestQueue() { ProtoBufSerializer serializer = new ProtoBufSerializer(); var q = new Queue(new[] { 1, 2, 3, 4, 0 }); serializer.Serialize(q, tempFile); var newQ = (Queue)serializer.Deserialize(tempFile); CollectionAssert.AreEqual(q, newQ); } #endregion [StorableType("6075F1E8-948A-4AD8-8F5A-942B777852EC")] public class A { [Storable] public B B { get; set; } [Storable] public int i; } [StorableType("287BFEA0-6E27-4839-BCEF-D134FE738AC8")] public class B { [Storable] public A A { get; set; } [StorableHook(HookType.AfterDeserialization)] void PostDeserializationHook() { //Assert.AreEqual(3, A.i); } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestCyclicReferencesWithTuple() { var test = new Func(() => { var a = new A { i = 4 }; var b = new B { A = a }; a.B = b; return a; }); //ProtoBufSerializer serializer = new ProtoBufSerializer(); //serializer.Serialize(test(), tempFile); //object o = serializer.Deserialize(tempFile); //A result = (A)o; XmlGenerator.Serialize(test(), tempFile); object o = XmlParser.Deserialize(tempFile); string msg = Profile(test); Console.WriteLine(msg); } #region conversion [StorableType("F9F51075-490C-48E3-BF64-14514A210149", 1)] private class OldBaseType { [Storable] public ItemCollection items; } [StorableType("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 1)] private class V1 : OldBaseType { [Storable] public IntValue a; [Storable] public ItemList vals; [Storable] public V1 mySelf; [Storable] public Tuple tup; [Storable] public int x; [Storable] public int y; } [StorableType("B72C58C8-3321-4706-AA94-578F57337070", 2)] private class NewBaseType { [Storable] public DoubleValue[] items; } [StorableType("00000000-0000-0000-0000-BADCAFFEE000", 2)] // for testing (version 2) private partial class V2 : NewBaseType { [Storable] public int a; [Storable] public int[] val; [Storable] public V2 mySelf; [Storable] public int TupItem1; [Storable] public int TupItem2; [Storable] public Point coords; } // conversion part private partial class V2 : NewBaseType { [StorableConversion(srcVersion: 1)] private static Dictionary ConvertV1(Dictionary values) { var newValues = new Dictionary(); var items = (ItemCollection)values["OldBaseType.items"]; newValues["NewBaseType.items"] = items.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray(); newValues["V2.a"] = ((IntValue)values["V1.a"]).Value; newValues["V2.val"] = ((ItemList)values["V1.vals"]).Select(iv => iv.Value).ToArray(); newValues["V2.mySelf"] = values["V1.mySelf"]; // myself type will be mapped correctly var tup = (Tuple)values["V1.tup"]; if (tup != null) { newValues["V2.TupItem1"] = tup.Item1; newValues["V2.TupItem2"] = tup.Item2; } newValues["V2.coords"] = new Point((int)values["V1.x"], (int)values["V1.y"]); return newValues; } } [StorableType("00000000-0000-0000-0000-BADCAFFEE200", 3)] // for testing (version 3) private partial class V3 : NewBaseType { [Storable] public int a; [Storable] public int[] val; [Storable] public V3 mySelf; [Storable] public Tuple tup; [Storable] public Point coords; } // conversion part private partial class V3 { [StorableConversion(srcVersion: 1)] private static Dictionary ConvertV1(Dictionary values) { var newValues = new Dictionary(); var items = (ItemCollection)values["OldBaseType.items"]; newValues["NewBaseType.items"] = items.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray(); newValues["V2.a"] = ((IntValue)values["V1.a"]).Value; newValues["V2.val"] = ((ItemList)values["V1.vals"]).Select(iv => iv.Value).ToArray(); newValues["V2.mySelf"] = values["V1.mySelf"]; // myself type will be mapped correctly var tup = (Tuple)values["V1.tup"]; if (tup != null) { newValues["V2.TupItem1"] = tup.Item1; newValues["V2.TupItem2"] = tup.Item2; } newValues["V2.coords"] = new Point((int)values["V1.x"], (int)values["V1.y"]); return newValues; } [StorableConversion(srcVersion: 2)] private static Dictionary ConvertV2(Dictionary values) { var newValues = new Dictionary(); newValues["NewBaseType.items"] = values["NewBaseType.items"]; newValues["V3.a"] = values["V2.a"]; newValues["V3.val"] = values["V2.val"]; newValues["V3.mySelf"] = values["V2.mySelf"]; newValues["V3.tup"] = Tuple.Create((int)values["V2.TupItem1"], (int)values["V2.TupItem2"]); newValues["V3.coords"] = values["V2.coords"]; return newValues; } } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestConversion() { var test = new Func(() => { var p = new V1(); p.a = new IntValue(1); p.mySelf = p; p.vals = new ItemList(new IntValue[] { p.a, new IntValue(2), new IntValue(3) }); p.tup = Tuple.Create(17, 4); var dv = new DoubleValue(1.0); p.items = new ItemCollection(new IItem[] { dv, dv, p.a }); return p; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); var old = test(); serializer.Serialize(old, tempFile); Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid); Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid); Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid, typeof(V2)); object o = serializer.Deserialize(tempFile); var restored = (V2)o; Assert.AreEqual(restored.a, old.a.Value); Assert.IsTrue(restored.val.SequenceEqual(old.vals.Select(iv => iv.Value))); Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value))); // Assert.AreSame(restored.items[0], restored.items[1]); Assert.AreSame(restored, restored.mySelf); //string msg = Profile(test); //Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestConversion2() { var test = new Func(() => { var p = new V2(); p.a = 1; p.mySelf = p; p.val = new int[] {2, 3, 4}; p.TupItem1 = 17; p.TupItem2 = 4; p.items = new DoubleValue[] {new DoubleValue(1.0), new DoubleValue(2.0) }; return p; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); var old = test(); serializer.Serialize(old, tempFile); Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid); Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid); Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V3)).Guid); Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid, typeof(V3)); object o = serializer.Deserialize(tempFile); var restored = (V3)o; Assert.AreEqual(restored.a, old.a); Assert.IsTrue(restored.val.SequenceEqual(old.val)); Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value))); // Assert.AreSame(restored.items[0], restored.items[1]); Assert.AreSame(restored, restored.mySelf); //string msg = Profile(test); //Console.WriteLine(msg); } #endregion [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestGASerializeDeserializeExecute() { var test = new Func(() => { var ga = new GeneticAlgorithm(); ga.Problem = new SingleObjectiveTestFunctionProblem(); ga.MaximumGenerations.Value = 100; ga.SetSeedRandomly.Value = false; return ga; }); ProtoBufSerializer serializer = new ProtoBufSerializer(); serializer.Serialize(test(), tempFile); object o = serializer.Deserialize(tempFile); GeneticAlgorithm result = (GeneticAlgorithm)o; SamplesUtils.RunAlgorithm(result); GeneticAlgorithm original = test(); SamplesUtils.RunAlgorithm(original); //Assert.AreEqual(original.Results[""], result); //string msg = Profile(test); //Console.WriteLine(msg); } [TestMethod] [TestCategory("Persistence4")] [TestProperty("Time", "short")] public void TestLoadingSamples() { var path = @"D:\hl\branches\PersistenceOverhaul\HeuristicLab.Optimizer\3.3\Documents"; var serializer = new ProtoBufSerializer(); foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) { var original = XmlParser.Deserialize(fileName); var ok = true; // foreach (var t in original.GetObjectGraphObjects().Select(o => o.GetType())) { // if ( // t.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) // .Any(ctor => StorableConstructorAttribute.IsStorableConstructor(ctor))) { // try { // if (t.IsGenericType) { // var g = Mapper.StaticCache.GetGuid(t.GetGenericTypeDefinition()); // } else { // var g = Mapper.StaticCache.GetGuid(t); // } // } catch (Exception e) { // Console.WriteLine(t.FullName); // ok = false; // } // } // } if (ok) { serializer.Serialize(original, fileName + ".proto"); // var newVersion = serializer.Deserialize(fileName + ".proto"); var p = Profile(() => original); Console.WriteLine(p); } } } } }