Free cookie consent management tool by TermsFeed Policy Generator

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

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

Convenience method for XML serialization and de-serizliation. (#506)

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