Free cookie consent management tool by TermsFeed Policy Generator

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

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

Initialize non-serialized values with specified default values. (#506)

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