Free cookie consent management tool by TermsFeed Policy Generator

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

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

Add serializer class information to type cache. (#506)

File size: 6.6 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      Serializer s = new Serializer(r, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));       
157      XmlGenerator xmlGenerator = new XmlGenerator();
158      StreamWriter writer = new StreamWriter("test.xml");
159      foreach (ISerializationToken token in s) {
160        string line = xmlGenerator.Format(token);
161        writer.Write(line);
162        Console.Out.Write(line);
163      }
164      writer.Close();
165      writer = new StreamWriter("test-types.xml");
166      foreach (string line in xmlGenerator.Format(s.TypeCache)) {
167        writer.WriteLine(line);
168        Console.Out.WriteLine(line);
169      }
170      writer.Close();
171     
172      XmlParser parser = new XmlParser(new StreamReader("test.xml"));
173      DeSerializer deSerializer = new DeSerializer(
174        XmlParser.ParseTypeCache(new StreamReader("test-types.xml")));       
175      object o = deSerializer.DeSerialize(parser);     
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      XmlGenerator xmlGenerator = new XmlGenerator();
184      StreamWriter writer = new StreamWriter("test2.xml");
185      foreach (ISerializationToken token in s) {
186        string line = xmlGenerator.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      object o = deSerializer.DeSerialize(parser);     
195      Console.Out.WriteLine(Util.AutoFormat(o, true));     
196    }   
197
198    public class Base {
199      [Storable] private int baseInt = 3;     
200    }
201
202    public class Derived: Base {
203      [Storable] private int derivedInt = 5;
204    }
205
206
207    public static void Main() {     
208      Test1();     
209      //Test2();
210      //SpeedTest();
211      //SpeedTest2();
212      Console.In.ReadLine();
213    }
214  }
215}
Note: See TracBrowser for help on using the repository browser.