Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs @ 1358

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

Better formatting configuration interface. (#506)

File size: 9.1 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using HeuristicLab.Persistence.Interfaces;
6
7namespace HeuristicLab.Persistence.Test {
8
9  public class RootBase {
10    [Storable]
11    private string baseString = "Serial";
12  }
13
14  public class Root : RootBase {
15    [Storable]
16    public int[] i = new[] { 3, 4, 5, 6 };
17    [Storable]
18    public string s;
19    [Storable]
20    public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
21    [Storable]
22    public List<int> intList = new List<int>(new[] { 321, 312, 321 });
23    [Storable]
24    public Custom c;
25    [Storable]
26    public List<Root> selfReferences;
27    [Storable]
28    public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
29    [Storable]
30    public bool boolean = true;
31    [Storable]
32    public DateTime dateTime;
33    [Storable]
34    public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("Serial", 123);
35    [Storable]
36    public Dictionary<string, int> dict = new Dictionary<string, int>();
37    [Storable(DefaultValue = "default")]
38    public string uninitialized;
39  }
40
41  public class Custom {
42    [Storable]
43    public int i;
44    [Storable]
45    public Root r;
46    [Storable]
47    public string name = "Serial";
48  }
49
50  public class CloneableRoot {
51    public int[] i = new[] { 3, 4, 5, 6 };
52    public string s;
53    public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
54    public List<int> intList = new List<int>(new[] { 321, 312, 321 });
55    public CloneableCustom c;
56    public List<CloneableRoot> selfReferences;
57    public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
58    public bool boolean = true;
59    public DateTime dateTime;
60    public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("test key", 123);
61    public Dictionary<string, int> dict = new Dictionary<string, int>();
62    public object Clone(Dictionary<object, object> twins) {
63      if (twins.ContainsKey(this))
64        return twins[this];
65      CloneableRoot cr = new CloneableRoot();
66      twins.Add(this, cr);
67      cr.i = i;
68      cr.s = s;
69      cr.intArray = new ArrayList(intArray);
70      cr.intList = new List<int>(intList);
71      cr.c = (CloneableCustom)c.Clone(twins);
72      cr.selfReferences = new List<CloneableRoot>();
73      for (int j = 0; j < selfReferences.Count; j++) {
74        cr.selfReferences.Add(this);
75      }
76      cr.multiDimArray = (double[,])multiDimArray.Clone();
77      cr.dateTime = new DateTime(dateTime.Ticks);
78      cr.kvp = new KeyValuePair<string, int>(kvp.Key, kvp.Value);
79      cr.dict = new Dictionary<string, int>(dict);
80      return cr;
81    }
82  }
83
84  public class CloneableCustom {
85    public int i;
86    public CloneableRoot r;
87    public string name = "Serial";
88    public object Clone(Dictionary<object, object> twins) {
89      if (twins.ContainsKey(this))
90        return twins[this];
91      CloneableCustom cc = new CloneableCustom();
92      twins.Add(this, cc);
93      cc.i = i;
94      cc.r = (CloneableRoot)r.Clone(twins);
95      cc.name = name;
96      return cc;
97    }
98  }
99
100  public class Manager {
101
102    private DateTime lastLoadTime;
103    [Storable]
104    private DateTime lastLoadTimePersistence {
105      get { return lastLoadTime; }
106      // ReSharper disable ValueParameterNotUsed
107      set { lastLoadTime = DateTime.Now; }
108      // ReSharper restore ValueParameterNotUsed
109    }
110    [Storable]
111    private double? dbl;
112  }
113
114  public class StorableObject {
115
116    [Storable]
117    Dictionary<int, string> dict;
118
119    public void Init() {
120      dict = new Dictionary<int, string>();
121      for (int i = 0; i < 1000000; i++) {
122        dict.Add(i, i.ToString());
123      }
124    }
125  }
126
127  public class CloneableObject : ICloneable {
128
129    Dictionary<int, string> dict;
130
131    public void Init() {
132      dict = new Dictionary<int, string>();
133      for (int i = 0; i < 1000000; i++) {
134        dict.Add(i, i.ToString());
135      }
136    }
137    public object Clone() {
138      CloneableObject clone = new CloneableObject {
139        dict = new Dictionary<int, string>(dict)
140      };
141      return clone;
142    }
143  } 
144
145  public class NewSerializationTest {
146
147    public static void Test1() {     
148      Root r = new Root();
149      r.selfReferences = new List<Root> {r, r};
150      r.c = new Custom {r = r};
151      r.dict.Add("one", 1);
152      r.dict.Add("two", 2);
153      r.dict.Add("three", 3);
154      Serializer s = new Serializer(r, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));       
155      Persistence.XmlFormatter xmlFormatter = new XmlFormatter();
156      StreamWriter writer = new StreamWriter("test.xml");
157      foreach (ISerializationToken token in s) {
158        string line = xmlFormatter.Format(token);
159        writer.Write(line);
160        Console.Out.Write(line);
161      }
162      writer.Close();
163      writer = new StreamWriter("test-types.xml");
164      foreach (string line in xmlFormatter.Format(s.TypeCache)) {
165        writer.WriteLine(line);
166        Console.Out.WriteLine(line);
167      }
168      writer.Close();
169     
170      XmlParser parser = new XmlParser(new StreamReader("test.xml"));
171      DeSerializer deSerializer = new DeSerializer(
172        XmlParser.ParseTypeCache(new StreamReader("test-types.xml")),
173        ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));       
174      object o = deSerializer.DeSerialize(parser);
175      Root t = CloningFactory.DefaultClone(r);
176      Console.Out.WriteLine(Util.AutoFormat(o, true));     
177    }
178
179    public static void Test2() {
180      Manager m = new Manager();     
181      Serializer s = new Serializer(m,
182        ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));
183      Persistence.XmlFormatter xmlFormatter = new XmlFormatter();
184      StreamWriter writer = new StreamWriter("test2.xml");
185      foreach (ISerializationToken token in s) {
186        string line = xmlFormatter.Format(token);
187        writer.Write(line);
188        Console.Out.Write(line);
189      }
190      writer.Close();
191      XmlParser parser = new XmlParser(new StreamReader("test2.xml"));
192      DeSerializer deSerializer = new DeSerializer(
193        XmlParser.ParseTypeCache(new StreamReader("test-types.xml")),
194        ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));       
195      object o = deSerializer.DeSerialize(parser);
196      Manager n = CloningFactory.DefaultClone(m);
197      Console.Out.WriteLine(Util.AutoFormat(o, true));     
198    }
199
200    public static void SpeedTest() {
201      StorableObject storable = new StorableObject();
202      CloneableObject cloneable = new CloneableObject();
203      Console.Write("initializing...");
204      storable.Init();
205      cloneable.Init();
206      Console.WriteLine("done");
207      List<StorableObject> storableClones = new List<StorableObject>();
208      List<CloneableObject> clonableClones = new List<CloneableObject>();
209      CloningFactory cloningFactory = new CloningFactory();
210      for (int i = 0; i < 100000; i++) {
211        Console.Write("cloning storable.. ");
212        storableClones.Add(cloningFactory.Clone(storable));
213        Console.WriteLine();
214        Console.Write("cloning cloneable.. ");
215        clonableClones.Add((CloneableObject)cloneable.Clone());
216        Console.WriteLine();
217      }
218    }
219
220    public static void SpeedTest2() {
221      Root r = new Root();
222      r.selfReferences = new List<Root> {r, r};
223      r.c = new Custom {r = r};
224      r.dict.Add("one", 1);
225      r.dict.Add("two", 2);
226      r.dict.Add("three", 3);
227
228      CloneableRoot cr = new CloneableRoot();
229      cr.selfReferences = new List<CloneableRoot> {cr, cr};
230      cr.c = new CloneableCustom {r = cr};
231      cr.dict.Add("one", 1);
232      cr.dict.Add("two", 2);
233      cr.dict.Add("three", 3);     
234      List<Root> storableClones = new List<Root>();
235      List<CloneableRoot> clonableClones = new List<CloneableRoot>();     
236      CloningFactory cloningFactory = new CloningFactory();
237
238      DateTime start = DateTime.Now;
239      Console.Write("cloning storable.. ");
240      for (int i = 0; i < 100000; i++) {       
241        storableClones.Add(cloningFactory.Clone(r));                       
242      }
243      Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
244
245      start = DateTime.Now;
246      Console.Write("cloning storable again.. ");
247      for (int i = 0; i < 100000; i++) {
248        storableClones.Add(cloningFactory.Clone(r));
249      }
250      Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
251
252      start = DateTime.Now;
253      Console.Write("cloning cloneable.. ");
254      for (int i = 0; i < 100000; i++) {       
255        clonableClones.Add((CloneableRoot)cr.Clone(new Dictionary<object, object>()));
256      }
257      Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
258      clonableClones.Clear();
259     
260    }
261
262    public class Base {
263      [Storable] private int baseInt = 3;     
264    }
265
266    public class Derived: Base {
267      [Storable] private int derivedInt = 5;
268    }
269
270
271    public static void Main() {     
272      Test1();     
273      //Test2();
274      //SpeedTest();
275      //SpeedTest2();
276      Console.In.ReadLine();
277    }
278  }
279}
Note: See TracBrowser for help on using the repository browser.