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