Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1564 was 1554, checked in by epitzer, 16 years ago

XML formatters for all primitive numeric types. (#548)

File size: 9.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.WriteLine(ViewOnlyGenerator.Serialize(r));
189      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
190    }
191
192    public static void Test3() {
193      C c = new C();
194      C[][] cs = new C[2][];
195      cs[0] = new C[] { c };
196      cs[1] = new C[] { c };
197      c.allCs = cs;
198      c.kvpList = new KeyValuePair<List<C>, C>(new List<C> { c }, c);
199      XmlGenerator.Serialize(cs, "test3.zip");
200      object o = XmlParser.DeSerialize("test3.zip");     
201      Console.WriteLine(ViewOnlyGenerator.Serialize(cs));
202      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
203    }
204
205    public static void Test4() {
206      ArrayList[] arrayListArray = new ArrayList[4];
207      arrayListArray[0] = new ArrayList();
208      arrayListArray[0].Add(arrayListArray);
209      arrayListArray[0].Add(arrayListArray);
210      arrayListArray[1] = new ArrayList();
211      arrayListArray[1].Add(arrayListArray);
212      arrayListArray[2] = new ArrayList();
213      arrayListArray[2].Add(arrayListArray);
214      arrayListArray[2].Add(arrayListArray);
215      Array a = Array.CreateInstance(
216                              typeof(object),
217                              new[] { 1, 2 }, new[] { 3, 4 });
218      arrayListArray[2].Add(a);
219      XmlGenerator.Serialize(arrayListArray, "test4.zip");
220      object o = XmlParser.DeSerialize("test4.zip");     
221      Console.WriteLine(ViewOnlyGenerator.Serialize(arrayListArray));
222      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
223    }
224
225    public static void Test2() {
226      Manager m = new Manager();     
227      XmlGenerator.Serialize(m, "test2.zip");
228      object o = XmlParser.DeSerialize("test2.zip");
229      Console.WriteLine(ViewOnlyGenerator.Serialize(m));
230      Console.WriteLine(ViewOnlyGenerator.Serialize(o));     
231    }   
232
233   
234
235    public static void Test5() {
236      StringDecomposerTest sdt = new StringDecomposerTest();     
237      XmlGenerator.Serialize(sdt, "test5.zip");
238      object o = XmlParser.DeSerialize("test5.zip");
239      //Console.WriteLine(ViewOnlyGenerator.Serialize(sdt));
240      //Console.WriteLine(ViewOnlyGenerator.Serialize(o));
241    }
242
243    public static void Test6() {
244      string[,] mDimString = new string[,] {
245        {"ora", "et", "labora"},
246        {"Beten", "und", "Arbeiten"}
247      };
248      XmlGenerator.Serialize(mDimString, "test6.zip");
249      object o = XmlParser.DeSerialize("test6.zip");
250      Console.WriteLine(ViewOnlyGenerator.Serialize(mDimString));
251      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
252    }
253
254    public class NestedType {
255      [Storable]
256      private string value = "value";
257    }
258
259    public static void Test7() {
260      NestedType t = new NestedType();
261      XmlGenerator.Serialize(t, "test7.zip");
262      object o = XmlParser.DeSerialize("test7.zip");
263      Console.WriteLine(ViewOnlyGenerator.Serialize(t));
264      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
265    }
266
267
268    public static void Test8() {
269      string[] strings = { "ora", "et", "labora" };     
270      XmlGenerator.Serialize(strings, "test8.zip");
271      object o = XmlParser.DeSerialize("test8.zip");
272      Console.WriteLine(ViewOnlyGenerator.Serialize(strings));
273      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
274    }
275   
276    class Float {
277      [Storable]
278      float f = 12.3f;
279    }
280
281    public static void Test9() {           
282      XmlGenerator.Serialize(12.3f, "test9.zip");
283      object o = XmlParser.DeSerialize("test9.zip");
284      Console.WriteLine(ViewOnlyGenerator.Serialize(12.3f));
285      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
286    }
287
288
289    public static void Main() {
290      BasicConfigurator.Configure();
291      ConfigurationService.Instance.Reset();
292      Test1();     
293      Test2();
294      Test3();
295      Test4();
296      Test5();
297      Test6();
298      Test7();
299      Test8();
300      Test9();
301      //SpeedTest();
302      //SpeedTest2();
303      Console.In.ReadLine();
304    }
305  }
306}
Note: See TracBrowser for help on using the repository browser.