Free cookie consent management tool by TermsFeed Policy Generator

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

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

Improved logging of persistence framework. (#524)

File size: 7.2 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 enum TestEnum { va1, va2, va3, va8 } ;
16
17  public class RootBase {
18    [Storable]
19    private string baseString = "   Serial  ";
20    [Storable]
21    public TestEnum myEnum = TestEnum.va3;
22  }
23
24  public class Root : RootBase {
25    [Storable]
26    public int[] i = new[] { 3, 4, 5, 6 };
27    [Storable]
28    public string s;
29    [Storable]
30    public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
31    [Storable]
32    public List<int> intList = new List<int>(new[] { 321, 312, 321 });
33    [Storable]
34    public Custom c;
35    [Storable]
36    public List<Root> selfReferences;
37    [Storable]
38    public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
39    [Storable]
40    public bool boolean = true;
41    [Storable]
42    public DateTime dateTime;
43    [Storable]
44    public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("Serial", 123);
45    [Storable]
46    public Dictionary<string, int> dict = new Dictionary<string, int>();
47    [Storable(DefaultValue = "default")]
48    public string uninitialized;
49  }
50
51  public class Custom {
52    [Storable]
53    public int i;
54    [Storable]
55    public Root r;
56    [Storable]
57    public string name = "Serial";
58  }
59
60  public class CloneableRoot {
61    public int[] i = new[] { 3, 4, 5, 6 };
62    public string s;
63    public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
64    public List<int> intList = new List<int>(new[] { 321, 312, 321 });
65    public CloneableCustom c;
66    public List<CloneableRoot> selfReferences;
67    public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
68    public bool boolean = true;
69    public DateTime dateTime;
70    public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("test key", 123);
71    public Dictionary<string, int> dict = new Dictionary<string, int>();
72    public object Clone(Dictionary<object, object> twins) {
73      if (twins.ContainsKey(this))
74        return twins[this];
75      CloneableRoot cr = new CloneableRoot();
76      twins.Add(this, cr);
77      cr.i = i;
78      cr.s = s;
79      cr.intArray = new ArrayList(intArray);
80      cr.intList = new List<int>(intList);
81      cr.c = (CloneableCustom)c.Clone(twins);
82      cr.selfReferences = new List<CloneableRoot>();
83      for (int j = 0; j < selfReferences.Count; j++) {
84        cr.selfReferences.Add(this);
85      }
86      cr.multiDimArray = (double[,])multiDimArray.Clone();
87      cr.dateTime = new DateTime(dateTime.Ticks);
88      cr.kvp = new KeyValuePair<string, int>(kvp.Key, kvp.Value);
89      cr.dict = new Dictionary<string, int>(dict);
90      return cr;
91    }
92  }
93
94  public class CloneableCustom {
95    public int i;
96    public CloneableRoot r;
97    public string name = "Serial";
98    public object Clone(Dictionary<object, object> twins) {
99      if (twins.ContainsKey(this))
100        return twins[this];
101      CloneableCustom cc = new CloneableCustom();
102      twins.Add(this, cc);
103      cc.i = i;
104      cc.r = (CloneableRoot)r.Clone(twins);
105      cc.name = name;
106      return cc;
107    }
108  }
109
110  public class Manager {
111
112    private DateTime lastLoadTime;
113    [Storable]
114    private DateTime lastLoadTimePersistence {
115      get { return lastLoadTime; }
116      // ReSharper disable ValueParameterNotUsed
117      set { lastLoadTime = DateTime.Now; }
118      // ReSharper restore ValueParameterNotUsed
119    }
120    [Storable]
121    private double? dbl;
122  }
123
124  public class StorableObject {
125
126    [Storable]
127    Dictionary<int, string> dict;
128
129    public void Init() {
130      dict = new Dictionary<int, string>();
131      for (int i = 0; i < 1000000; i++) {
132        dict.Add(i, i.ToString());
133      }
134    }
135  }
136
137  public class CloneableObject : ICloneable {
138
139    Dictionary<int, string> dict;
140
141    public void Init() {
142      dict = new Dictionary<int, string>();
143      for (int i = 0; i < 1000000; i++) {
144        dict.Add(i, i.ToString());
145      }
146    }
147    public object Clone() {
148      CloneableObject clone = new CloneableObject {
149        dict = new Dictionary<int, string>(dict)
150      };
151      return clone;
152    }
153  }
154
155  public class C {
156    [Storable]
157    public C[][] allCs;
158    public KeyValuePair<List<C>, C> kvpList;
159  }
160
161  public class NewSerializationTest {
162
163    public static void Test1() {     
164      Root r = new Root();
165      r.selfReferences = new List<Root> {r, r};
166      r.c = new Custom {r = r};
167      r.dict.Add("one", 1);
168      r.dict.Add("two", 2);
169      r.dict.Add("three", 3);
170      r.myEnum = TestEnum.va1;
171      XmlGenerator.Serialize(r, "test.zip");     
172      object o = XmlParser.DeSerialize("test.zip");
173      Console.Out.WriteLine(Util.AutoFormat(o, true));
174      Console.WriteLine(ViewOnlyGenerator.Serialize(r));
175      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
176    }
177
178    public static void Test3() {
179      C c = new C();
180      C[][] cs = new C[2][];
181      cs[0] = new C[] { c };
182      cs[1] = new C[] { c };
183      c.allCs = cs;
184      c.kvpList = new KeyValuePair<List<C>, C>(new List<C> { c }, c);
185      XmlGenerator.Serialize(cs, "test3.zip");
186      object o = XmlParser.DeSerialize("test3.zip");
187      Console.Out.WriteLine(Util.AutoFormat(o, true));
188      Console.WriteLine(ViewOnlyGenerator.Serialize(cs));
189      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
190    }
191
192    public static void Test4() {
193      ArrayList[] arrayListArray = new ArrayList[4];
194      arrayListArray[0] = new ArrayList();
195      arrayListArray[0].Add(arrayListArray);
196      arrayListArray[0].Add(arrayListArray);
197      arrayListArray[1] = new ArrayList();
198      arrayListArray[1].Add(arrayListArray);
199      arrayListArray[2] = new ArrayList();
200      arrayListArray[2].Add(arrayListArray);
201      arrayListArray[2].Add(arrayListArray);
202      Array a = Array.CreateInstance(
203                              typeof(object),
204                              new[] { 1, 2 }, new[] { 3, 4 });
205      arrayListArray[2].Add(a);
206      XmlGenerator.Serialize(arrayListArray, "test4.zip");
207      object o = XmlParser.DeSerialize("test4.zip");
208      Console.Out.WriteLine(Util.AutoFormat(o, true));
209      Console.WriteLine(ViewOnlyGenerator.Serialize(arrayListArray));
210      Console.WriteLine(ViewOnlyGenerator.Serialize(o));
211    }
212
213    public static void Test2() {
214      Manager m = new Manager();     
215      XmlGenerator.Serialize(m, "test2.zip");
216      object o = XmlParser.DeSerialize("test2.zip");
217      Console.Out.WriteLine(Util.AutoFormat(o, true));     
218    }   
219
220    public class Base {
221      [Storable] private int baseInt = 3;     
222    }
223
224    public class Derived: Base {
225      [Storable] private int derivedInt = 5;
226    }
227
228
229    public static void Main() {
230      BasicConfigurator.Configure();
231      Test1();     
232      Test2();
233      Test3();
234      Test4();
235      //SpeedTest();
236      //SpeedTest2();
237      Console.In.ReadLine();
238    }
239  }
240}
Note: See TracBrowser for help on using the repository browser.