Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence.Test/NewSerializationTest.cs @ 1518

Last change on this file since 1518 was 1518, checked in by epitzer, 15 years ago

Make serialization order of array components explicit (in all array decomposers) (#574)

File size: 8.4 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using HeuristicLab.Persistence.Core;
6using HeuristicLab.Persistence.Default.Xml;
7using HeuristicLab.Persistence.Interfaces;
8using HeuristicLab.Persistence.Default.ViewOnly;
9using HeuristicLab.Tracing;
10using log4net;
11using log4net.Config;
12
13namespace HeuristicLab.Persistence.Test {
14
15  public class StringDecomposerTest {
16    [Storable] private bool _bool = true;
17    [Storable] private byte _byte = 0xFF;
18    [Storable] private sbyte _sbyte = 0xF;
19    [Storable] private short _short = -123;
20    [Storable] private ushort _ushort = 123;
21    [Storable] private int _int = -123;
22    [Storable] private uint _uint = 123;
23    [Storable] private long _long = 123456;
24    [Storable] private ulong _ulong = 123456;
25    [Storable] private long[,] _long_array =
26      new long[,] { { 123, 456, },  {789, 123 }} ;
27    [Storable] public List<int> list = new List<int>{ 1, 2, 3, 4, 5};
28  }
29
30  public enum TestEnum { va1, va2, va3, va8 } ;
31
32  public class RootBase {
33    [Storable]
34    private string baseString = "   Serial  ";
35    [Storable]
36    public TestEnum myEnum = TestEnum.va3;
37  }
38
39  public class Root : RootBase {
40    [Storable]
41    public int[] i = new[] { 3, 4, 5, 6 };
42    [Storable]
43    public string s;
44    [Storable]
45    public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
46    [Storable]
47    public List<int> intList = new List<int>(new[] { 321, 312, 321 });
48    [Storable]
49    public Custom c;
50    [Storable]
51    public List<Root> selfReferences;
52    [Storable]
53    public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
54    [Storable]
55    public bool boolean = true;
56    [Storable]
57    public DateTime dateTime;
58    [Storable]
59    public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("Serial", 123);
60    [Storable]
61    public Dictionary<string, int> dict = new Dictionary<string, int>();
62    [Storable(DefaultValue = "default")]
63    public string uninitialized;
64  }
65
66  public class Custom {
67    [Storable]
68    public int i;
69    [Storable]
70    public Root r;
71    [Storable]
72    public string name = "Serial";
73  }
74
75  public class CloneableRoot {
76    public int[] i = new[] { 3, 4, 5, 6 };
77    public string s;
78    public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
79    public List<int> intList = new List<int>(new[] { 321, 312, 321 });
80    public CloneableCustom c;
81    public List<CloneableRoot> selfReferences;
82    public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
83    public bool boolean = true;
84    public DateTime dateTime;
85    public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("test key", 123);
86    public Dictionary<string, int> dict = new Dictionary<string, int>();
87    public object Clone(Dictionary<object, object> twins) {
88      if (twins.ContainsKey(this))
89        return twins[this];
90      CloneableRoot cr = new CloneableRoot();
91      twins.Add(this, cr);
92      cr.i = i;
93      cr.s = s;
94      cr.intArray = new ArrayList(intArray);
95      cr.intList = new List<int>(intList);
96      cr.c = (CloneableCustom)c.Clone(twins);
97      cr.selfReferences = new List<CloneableRoot>();
98      for (int j = 0; j < selfReferences.Count; j++) {
99        cr.selfReferences.Add(this);
100      }
101      cr.multiDimArray = (double[,])multiDimArray.Clone();
102      cr.dateTime = new DateTime(dateTime.Ticks);
103      cr.kvp = new KeyValuePair<string, int>(kvp.Key, kvp.Value);
104      cr.dict = new Dictionary<string, int>(dict);
105      return cr;
106    }
107  }
108
109  public class CloneableCustom {
110    public int i;
111    public CloneableRoot r;
112    public string name = "Serial";
113    public object Clone(Dictionary<object, object> twins) {
114      if (twins.ContainsKey(this))
115        return twins[this];
116      CloneableCustom cc = new CloneableCustom();
117      twins.Add(this, cc);
118      cc.i = i;
119      cc.r = (CloneableRoot)r.Clone(twins);
120      cc.name = name;
121      return cc;
122    }
123  }
124
125  public class Manager {
126
127    private DateTime lastLoadTime;
128    [Storable]
129    private DateTime lastLoadTimePersistence {
130      get { return lastLoadTime; }
131      // ReSharper disable ValueParameterNotUsed
132      set { lastLoadTime = DateTime.Now; }
133      // ReSharper restore ValueParameterNotUsed
134    }
135    [Storable]
136    private double? dbl;
137  }
138
139  public class StorableObject {
140
141    [Storable]
142    Dictionary<int, string> dict;
143
144    public void Init() {
145      dict = new Dictionary<int, string>();
146      for (int i = 0; i < 1000000; i++) {
147        dict.Add(i, i.ToString());
148      }
149    }
150  }
151
152  public class CloneableObject : ICloneable {
153
154    Dictionary<int, string> dict;
155
156    public void Init() {
157      dict = new Dictionary<int, string>();
158      for (int i = 0; i < 1000000; i++) {
159        dict.Add(i, i.ToString());
160      }
161    }
162    public object Clone() {
163      CloneableObject clone = new CloneableObject {
164        dict = new Dictionary<int, string>(dict)
165      };
166      return clone;
167    }
168  }
169
170  public class C {
171    [Storable]
172    public C[][] allCs;
173    public KeyValuePair<List<C>, C> kvpList;
174  }
175
176  public class NewSerializationTest {
177
178    public static void Test1() {     
179      Root r = new Root();
180      r.selfReferences = new List<Root> {r, r};
181      r.c = new Custom {r = r};
182      r.dict.Add("one", 1);
183      r.dict.Add("two", 2);
184      r.dict.Add("three", 3);
185      r.myEnum = TestEnum.va1;
186      XmlGenerator.Serialize(r, "test.zip");     
187      object o = XmlParser.DeSerialize("test.zip");
188      Console.Out.WriteLine(Util.AutoFormat(o, true));
189      Console.WriteLine(ViewOnlyGenerator.Serialize(r));
190      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
191    }
192
193    public static void Test3() {
194      C c = new C();
195      C[][] cs = new C[2][];
196      cs[0] = new C[] { c };
197      cs[1] = new C[] { c };
198      c.allCs = cs;
199      c.kvpList = new KeyValuePair<List<C>, C>(new List<C> { c }, c);
200      XmlGenerator.Serialize(cs, "test3.zip");
201      object o = XmlParser.DeSerialize("test3.zip");
202      Console.Out.WriteLine(Util.AutoFormat(o, true));
203      Console.WriteLine(ViewOnlyGenerator.Serialize(cs));
204      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
205    }
206
207    public static void Test4() {
208      ArrayList[] arrayListArray = new ArrayList[4];
209      arrayListArray[0] = new ArrayList();
210      arrayListArray[0].Add(arrayListArray);
211      arrayListArray[0].Add(arrayListArray);
212      arrayListArray[1] = new ArrayList();
213      arrayListArray[1].Add(arrayListArray);
214      arrayListArray[2] = new ArrayList();
215      arrayListArray[2].Add(arrayListArray);
216      arrayListArray[2].Add(arrayListArray);
217      Array a = Array.CreateInstance(
218                              typeof(object),
219                              new[] { 1, 2 }, new[] { 3, 4 });
220      arrayListArray[2].Add(a);
221      XmlGenerator.Serialize(arrayListArray, "test4.zip");
222      object o = XmlParser.DeSerialize("test4.zip");
223      Console.Out.WriteLine(Util.AutoFormat(o, true));
224      Console.WriteLine(ViewOnlyGenerator.Serialize(arrayListArray));
225      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
226    }
227
228    public static void Test2() {
229      Manager m = new Manager();     
230      XmlGenerator.Serialize(m, "test2.zip");
231      object o = XmlParser.DeSerialize("test2.zip");
232      Console.Out.WriteLine(Util.AutoFormat(o, true));     
233    }   
234
235   
236
237    public static void Test5() {
238      StringDecomposerTest sdt = new StringDecomposerTest();     
239      XmlGenerator.Serialize(sdt, "test5.zip");
240      object o = XmlParser.DeSerialize("test5.zip");
241      Console.WriteLine(ViewOnlyGenerator.Serialize(sdt));
242      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
243    }
244
245    public static void Test6() {
246      string[,] mDimString = new string[,] {
247        {"ora", "et", "labora"},
248        {"Beten", "und", "Arbeiten"}
249      };
250      XmlGenerator.Serialize(mDimString, "test6.zip");
251      object o = XmlParser.DeSerialize("test6.zip");
252      Console.WriteLine(ViewOnlyGenerator.Serialize(mDimString));
253      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
254    }
255
256
257    public static void Main() {
258      BasicConfigurator.Configure();
259      Test1();     
260      Test2();
261      Test3();
262      Test4();
263      Test5();
264      Test6();
265      //SpeedTest();
266      //SpeedTest2();
267      Console.In.ReadLine();
268    }
269  }
270}
Note: See TracBrowser for help on using the repository browser.