Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1348 was 1348, checked in by epitzer, 16 years ago

Rename primitive serializers to formatters and compound/composite serializers to decomposers. (#506)

File size: 4.5 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 NewSerializationTest {
10
11    public static void Test1() {
12      Root r = new Root();
13      r.selfReferences = new List<Root> {r, r};
14      r.c = new Custom {r = r};
15      r.dict.Add("one", 1);
16      r.dict.Add("two", 2);
17      r.dict.Add("three", 3);
18      Serializer s = new Serializer(r);       
19      Persistence.XmlFormatter xmlFormatter = new XmlFormatter();
20      StreamWriter writer = new StreamWriter("test.xml");
21      foreach (ISerializationToken token in s) {
22        string line = xmlFormatter.Format(token);
23        writer.Write(line);
24        Console.Out.Write(line);
25      }
26      writer.Close();
27      XmlParser parser = new XmlParser(new StreamReader("test.xml"));
28      DeSerializer deSerializer = new DeSerializer();       
29      object o = deSerializer.DeSerialize(parser);
30      Root t = CloningFactory.DefaultClone(r);
31      Console.Out.WriteLine(Util.AutoFormat(o, true));     
32    }
33
34    public static void Test2() {
35      Manager m = new Manager();     
36      Serializer s = new Serializer(m);
37      Persistence.XmlFormatter xmlFormatter = new XmlFormatter();
38      StreamWriter writer = new StreamWriter("test2.xml");
39      foreach (ISerializationToken token in s) {
40        string line = xmlFormatter.Format(token);
41        writer.Write(line);
42        Console.Out.Write(line);
43      }
44      writer.Close();
45      XmlParser parser = new XmlParser(new StreamReader("test2.xml"));
46      DeSerializer deSerializer = new DeSerializer();
47      object o = deSerializer.DeSerialize(parser);
48      Manager n = CloningFactory.DefaultClone(m);
49      Console.Out.WriteLine(Util.AutoFormat(o, true));     
50    }
51
52    public static void SpeedTest() {
53      StorableObject storable = new StorableObject();
54      CloneableObject cloneable = new CloneableObject();
55      Console.Write("initializing...");
56      storable.Init();
57      cloneable.Init();
58      Console.WriteLine("done");
59      List<StorableObject> storableClones = new List<StorableObject>();
60      List<CloneableObject> clonableClones = new List<CloneableObject>();
61      CloningFactory cloningFactory = new CloningFactory();
62      for (int i = 0; i < 100000; i++) {
63        Console.Write("cloning storable.. ");
64        storableClones.Add(cloningFactory.Clone(storable));
65        Console.WriteLine();
66        Console.Write("cloning cloneable.. ");
67        clonableClones.Add((CloneableObject)cloneable.Clone());
68        Console.WriteLine();
69      }
70    }
71
72    public static void SpeedTest2() {
73      Root r = new Root();
74      r.selfReferences = new List<Root> {r, r};
75      r.c = new Custom {r = r};
76      r.dict.Add("one", 1);
77      r.dict.Add("two", 2);
78      r.dict.Add("three", 3);
79
80      CloneableRoot cr = new CloneableRoot();
81      cr.selfReferences = new List<CloneableRoot> {cr, cr};
82      cr.c = new CloneableCustom {r = cr};
83      cr.dict.Add("one", 1);
84      cr.dict.Add("two", 2);
85      cr.dict.Add("three", 3);     
86      List<Root> storableClones = new List<Root>();
87      List<CloneableRoot> clonableClones = new List<CloneableRoot>();     
88      CloningFactory cloningFactory = new CloningFactory();
89
90      DateTime start = DateTime.Now;
91      Console.Write("cloning storable.. ");
92      for (int i = 0; i < 100000; i++) {       
93        storableClones.Add(cloningFactory.Clone(r));                       
94      }
95      Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
96
97      start = DateTime.Now;
98      Console.Write("cloning storable again.. ");
99      for (int i = 0; i < 100000; i++) {
100        storableClones.Add(cloningFactory.Clone(r));
101      }
102      Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
103
104      start = DateTime.Now;
105      Console.Write("cloning cloneable.. ");
106      for (int i = 0; i < 100000; i++) {       
107        clonableClones.Add((CloneableRoot)cr.Clone(new Dictionary<object, object>()));
108      }
109      Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));
110      clonableClones.Clear();
111     
112    }
113
114    public class Base {
115      [Storable] private int baseInt = 3;     
116    }
117
118    public class Derived: Base {
119      [Storable] private int derivedInt = 5;
120    }
121
122    public static void Main() {     
123      Test1();
124      //Test2();
125      //SpeedTest();
126      //SpeedTest2();
127      Console.In.ReadLine();
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.