Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added EnumDecomposer. (#549)

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