1 | using System;
|
---|
2 | using System.Text;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
6 | using HeuristicLab.Persistence.Core;
|
---|
7 | using System.Collections;
|
---|
8 | using HeuristicLab.Persistence.Default.Xml;
|
---|
9 | using HeuristicLab.Persistence.Default.DebugString;
|
---|
10 | using System.IO;
|
---|
11 | using System.Reflection;
|
---|
12 | using HeuristicLab.Persistence.Default.Decomposers.Storable;
|
---|
13 | using HeuristicLab.Persistence.Interfaces;
|
---|
14 | using HeuristicLab.Persistence.Default.Xml.Primitive;
|
---|
15 | using HeuristicLab.Persistence.Default.Decomposers;
|
---|
16 |
|
---|
17 | namespace HeuristicLab.Persistence.UnitTest {
|
---|
18 |
|
---|
19 | public class NumberTest {
|
---|
20 | [Storable]
|
---|
21 | private bool _bool = true;
|
---|
22 | [Storable]
|
---|
23 | private byte _byte = 0xFF;
|
---|
24 | [Storable]
|
---|
25 | private sbyte _sbyte = 0xF;
|
---|
26 | [Storable]
|
---|
27 | private short _short = -123;
|
---|
28 | [Storable]
|
---|
29 | private ushort _ushort = 123;
|
---|
30 | [Storable]
|
---|
31 | private int _int = -123;
|
---|
32 | [Storable]
|
---|
33 | private uint _uint = 123;
|
---|
34 | [Storable]
|
---|
35 | private long _long = 123456;
|
---|
36 | [Storable]
|
---|
37 | private ulong _ulong = 123456;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public class PrimitivesTest : NumberTest {
|
---|
41 | [Storable]
|
---|
42 | private char c = 'e';
|
---|
43 | [Storable]
|
---|
44 | private long[,] _long_array =
|
---|
45 | new long[,] { { 123, 456, }, { 789, 123 } };
|
---|
46 | [Storable]
|
---|
47 | public List<int> list = new List<int> { 1, 2, 3, 4, 5 };
|
---|
48 | [Storable]
|
---|
49 | private object o = new object();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public enum TestEnum { va1, va2, va3, va8 } ;
|
---|
53 |
|
---|
54 | public class RootBase {
|
---|
55 | [Storable]
|
---|
56 | private string baseString = " Serial ";
|
---|
57 | [Storable]
|
---|
58 | public TestEnum myEnum = TestEnum.va3;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public class Root : RootBase {
|
---|
62 | [Storable]
|
---|
63 | public int[] i = new[] { 3, 4, 5, 6 };
|
---|
64 | [Storable(Name="Test String")]
|
---|
65 | public string s;
|
---|
66 | [Storable]
|
---|
67 | public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
|
---|
68 | [Storable]
|
---|
69 | public List<int> intList = new List<int>(new[] { 321, 312, 321 });
|
---|
70 | [Storable]
|
---|
71 | public Custom c;
|
---|
72 | [Storable]
|
---|
73 | public List<Root> selfReferences;
|
---|
74 | [Storable]
|
---|
75 | public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
|
---|
76 | [Storable]
|
---|
77 | public bool boolean = true;
|
---|
78 | [Storable]
|
---|
79 | public DateTime dateTime;
|
---|
80 | [Storable]
|
---|
81 | public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("Serial", 123);
|
---|
82 | [Storable]
|
---|
83 | public Dictionary<string, int> dict = new Dictionary<string, int>();
|
---|
84 | [Storable(DefaultValue = "default")]
|
---|
85 | public string uninitialized;
|
---|
86 | }
|
---|
87 |
|
---|
88 | public class Custom {
|
---|
89 | [Storable]
|
---|
90 | public int i;
|
---|
91 | [Storable]
|
---|
92 | public Root r;
|
---|
93 | [Storable]
|
---|
94 | public string name = "<![CDATA[<![CDATA[Serial]]>]]>";
|
---|
95 | }
|
---|
96 |
|
---|
97 | public class Manager {
|
---|
98 |
|
---|
99 | public DateTime lastLoadTime;
|
---|
100 | [Storable]
|
---|
101 | private DateTime lastLoadTimePersistence {
|
---|
102 | get { return lastLoadTime; }
|
---|
103 | set { lastLoadTime = DateTime.Now; }
|
---|
104 | }
|
---|
105 | [Storable]
|
---|
106 | public double? dbl;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public class C {
|
---|
110 | [Storable]
|
---|
111 | public C[][] allCs;
|
---|
112 | [Storable]
|
---|
113 | public KeyValuePair<List<C>, C> kvpList;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | [TestClass]
|
---|
118 | public class UseCases {
|
---|
119 |
|
---|
120 | private string tempFile;
|
---|
121 |
|
---|
122 | [TestInitialize()]
|
---|
123 | public void CreateTempFile() {
|
---|
124 | tempFile = Path.GetTempFileName();
|
---|
125 | }
|
---|
126 |
|
---|
127 | [TestCleanup()]
|
---|
128 | public void ClearTempFile() {
|
---|
129 | File.Delete(tempFile);
|
---|
130 | }
|
---|
131 |
|
---|
132 | [TestMethod]
|
---|
133 | public void ComplexStorable() {
|
---|
134 | Root r = new Root();
|
---|
135 | r.selfReferences = new List<Root> { r, r };
|
---|
136 | r.c = new Custom { r = r };
|
---|
137 | r.dict.Add("one", 1);
|
---|
138 | r.dict.Add("two", 2);
|
---|
139 | r.dict.Add("three", 3);
|
---|
140 | r.myEnum = TestEnum.va1;
|
---|
141 | r.i = new[] { 7, 5, 6 };
|
---|
142 | r.s = "new value";
|
---|
143 | r.intArray = new ArrayList { 3, 2, 1 };
|
---|
144 | r.intList = new List<int> { 9, 8, 7 };
|
---|
145 | r.multiDimArray = new double[,] { { 5, 4, 3 }, { 1, 4, 6 } };
|
---|
146 | r.boolean = false;
|
---|
147 | r.dateTime = DateTime.Now;
|
---|
148 | r.kvp = new KeyValuePair<string, int>("string key", 321);
|
---|
149 | r.uninitialized = null;
|
---|
150 | XmlGenerator.Serialize(r, tempFile);
|
---|
151 | Root newR = (Root)XmlParser.DeSerialize(tempFile);
|
---|
152 | Assert.AreEqual(
|
---|
153 | DebugStringGenerator.Serialize(r),
|
---|
154 | DebugStringGenerator.Serialize(newR));
|
---|
155 | Assert.AreSame(newR, newR.selfReferences[0]);
|
---|
156 | Assert.AreNotSame(r, newR);
|
---|
157 | Assert.AreEqual(r.myEnum, TestEnum.va1);
|
---|
158 | Assert.AreEqual(r.i[0], 7);
|
---|
159 | Assert.AreEqual(r.i[1], 5);
|
---|
160 | Assert.AreEqual(r.i[2], 6);
|
---|
161 | Assert.AreEqual(r.s, "new value");
|
---|
162 | Assert.AreEqual(r.intArray[0], 3);
|
---|
163 | Assert.AreEqual(r.intArray[1], 2);
|
---|
164 | Assert.AreEqual(r.intArray[2], 1);
|
---|
165 | Assert.AreEqual(r.intList[0], 9);
|
---|
166 | Assert.AreEqual(r.intList[1], 8);
|
---|
167 | Assert.AreEqual(r.intList[2], 7);
|
---|
168 | Assert.AreEqual(r.multiDimArray[0, 0], 5);
|
---|
169 | Assert.AreEqual(r.multiDimArray[0, 1], 4);
|
---|
170 | Assert.AreEqual(r.multiDimArray[0, 2], 3);
|
---|
171 | Assert.AreEqual(r.multiDimArray[1, 0], 1);
|
---|
172 | Assert.AreEqual(r.multiDimArray[1, 1], 4);
|
---|
173 | Assert.AreEqual(r.multiDimArray[1, 2], 6);
|
---|
174 | Assert.IsFalse(r.boolean);
|
---|
175 | Assert.IsTrue((DateTime.Now - r.dateTime).TotalSeconds < 10);
|
---|
176 | Assert.AreEqual(r.kvp.Key, "string key");
|
---|
177 | Assert.AreEqual(r.kvp.Value, 321);
|
---|
178 | Assert.IsNull(r.uninitialized);
|
---|
179 | Assert.AreEqual(newR.myEnum, TestEnum.va1);
|
---|
180 | Assert.AreEqual(newR.i[0], 7);
|
---|
181 | Assert.AreEqual(newR.i[1], 5);
|
---|
182 | Assert.AreEqual(newR.i[2], 6);
|
---|
183 | Assert.AreEqual(newR.s, "new value");
|
---|
184 | Assert.AreEqual(newR.intArray[0], 3);
|
---|
185 | Assert.AreEqual(newR.intArray[1], 2);
|
---|
186 | Assert.AreEqual(newR.intArray[2], 1);
|
---|
187 | Assert.AreEqual(newR.intList[0], 9);
|
---|
188 | Assert.AreEqual(newR.intList[1], 8);
|
---|
189 | Assert.AreEqual(newR.intList[2], 7);
|
---|
190 | Assert.AreEqual(newR.multiDimArray[0, 0], 5);
|
---|
191 | Assert.AreEqual(newR.multiDimArray[0, 1], 4);
|
---|
192 | Assert.AreEqual(newR.multiDimArray[0, 2], 3);
|
---|
193 | Assert.AreEqual(newR.multiDimArray[1, 0], 1);
|
---|
194 | Assert.AreEqual(newR.multiDimArray[1, 1], 4);
|
---|
195 | Assert.AreEqual(newR.multiDimArray[1, 2], 6);
|
---|
196 | Assert.IsFalse(newR.boolean);
|
---|
197 | Assert.IsTrue((DateTime.Now - newR.dateTime).TotalSeconds < 10);
|
---|
198 | Assert.AreEqual(newR.kvp.Key, "string key");
|
---|
199 | Assert.AreEqual(newR.kvp.Value, 321);
|
---|
200 | Assert.IsNull(newR.uninitialized);
|
---|
201 | }
|
---|
202 |
|
---|
203 | [TestMethod]
|
---|
204 | public void SelfReferences() {
|
---|
205 | C c = new C();
|
---|
206 | C[][] cs = new C[2][];
|
---|
207 | cs[0] = new C[] { c };
|
---|
208 | cs[1] = new C[] { c };
|
---|
209 | c.allCs = cs;
|
---|
210 | c.kvpList = new KeyValuePair<List<C>, C>(new List<C> { c }, c);
|
---|
211 | XmlGenerator.Serialize(cs, tempFile);
|
---|
212 | object o = XmlParser.DeSerialize(tempFile);
|
---|
213 | Assert.AreEqual(
|
---|
214 | DebugStringGenerator.Serialize(cs),
|
---|
215 | DebugStringGenerator.Serialize(o));
|
---|
216 | Assert.AreSame(c, c.allCs[0][0]);
|
---|
217 | Assert.AreSame(c, c.allCs[1][0]);
|
---|
218 | Assert.AreSame(c, c.kvpList.Key[0]);
|
---|
219 | Assert.AreSame(c, c.kvpList.Value);
|
---|
220 | C[][] newCs = (C[][])o;
|
---|
221 | C newC = newCs[0][0];
|
---|
222 | Assert.AreSame(newC, newC.allCs[0][0]);
|
---|
223 | Assert.AreSame(newC, newC.allCs[1][0]);
|
---|
224 | Assert.AreSame(newC, newC.kvpList.Key[0]);
|
---|
225 | Assert.AreSame(newC, newC.kvpList.Value);
|
---|
226 | }
|
---|
227 |
|
---|
228 | [TestMethod]
|
---|
229 | public void ArrayCreation() {
|
---|
230 | ArrayList[] arrayListArray = new ArrayList[4];
|
---|
231 | arrayListArray[0] = new ArrayList();
|
---|
232 | arrayListArray[0].Add(arrayListArray);
|
---|
233 | arrayListArray[0].Add(arrayListArray);
|
---|
234 | arrayListArray[1] = new ArrayList();
|
---|
235 | arrayListArray[1].Add(arrayListArray);
|
---|
236 | arrayListArray[2] = new ArrayList();
|
---|
237 | arrayListArray[2].Add(arrayListArray);
|
---|
238 | arrayListArray[2].Add(arrayListArray);
|
---|
239 | Array a = Array.CreateInstance(
|
---|
240 | typeof(object),
|
---|
241 | new[] { 1, 2 }, new[] { 3, 4 });
|
---|
242 | arrayListArray[2].Add(a);
|
---|
243 | XmlGenerator.Serialize(arrayListArray, tempFile);
|
---|
244 | object o = XmlParser.DeSerialize(tempFile);
|
---|
245 | Assert.AreEqual(
|
---|
246 | DebugStringGenerator.Serialize(arrayListArray),
|
---|
247 | DebugStringGenerator.Serialize(o));
|
---|
248 | ArrayList[] newArray = (ArrayList[])o;
|
---|
249 | Assert.AreSame(arrayListArray, arrayListArray[0][0]);
|
---|
250 | Assert.AreSame(arrayListArray, arrayListArray[2][1]);
|
---|
251 | Assert.AreSame(newArray, newArray[0][0]);
|
---|
252 | Assert.AreSame(newArray, newArray[2][1]);
|
---|
253 | }
|
---|
254 |
|
---|
255 | [TestMethod]
|
---|
256 | public void CustomSerializationProperty() {
|
---|
257 | Manager m = new Manager();
|
---|
258 | XmlGenerator.Serialize(m, tempFile);
|
---|
259 | Manager newM = (Manager)XmlParser.DeSerialize(tempFile);
|
---|
260 | Assert.AreNotEqual(
|
---|
261 | DebugStringGenerator.Serialize(m),
|
---|
262 | DebugStringGenerator.Serialize(newM));
|
---|
263 | Assert.AreEqual(m.dbl, newM.dbl);
|
---|
264 | Assert.AreEqual(m.lastLoadTime, new DateTime());
|
---|
265 | Assert.AreNotEqual(newM.lastLoadTime, new DateTime());
|
---|
266 | Assert.IsTrue((DateTime.Now - newM.lastLoadTime).TotalSeconds < 10);
|
---|
267 | }
|
---|
268 |
|
---|
269 | [TestMethod]
|
---|
270 | public void Primitives() {
|
---|
271 | PrimitivesTest sdt = new PrimitivesTest();
|
---|
272 | XmlGenerator.Serialize(sdt, tempFile);
|
---|
273 | object o = XmlParser.DeSerialize(tempFile);
|
---|
274 | Assert.AreEqual(
|
---|
275 | DebugStringGenerator.Serialize(sdt),
|
---|
276 | DebugStringGenerator.Serialize(o));
|
---|
277 | }
|
---|
278 |
|
---|
279 | [TestMethod]
|
---|
280 | public void MultiDimensionalArray() {
|
---|
281 | string[,] mDimString = new string[,] {
|
---|
282 | {"ora", "et", "labora"},
|
---|
283 | {"Beten", "und", "Arbeiten"}
|
---|
284 | };
|
---|
285 | XmlGenerator.Serialize(mDimString, tempFile);
|
---|
286 | object o = XmlParser.DeSerialize(tempFile);
|
---|
287 | Assert.AreEqual(
|
---|
288 | DebugStringGenerator.Serialize(mDimString),
|
---|
289 | DebugStringGenerator.Serialize(o));
|
---|
290 | }
|
---|
291 |
|
---|
292 | public class NestedType {
|
---|
293 | [Storable]
|
---|
294 | private string value = "value";
|
---|
295 | }
|
---|
296 |
|
---|
297 | [TestMethod]
|
---|
298 | public void NestedTypeTest() {
|
---|
299 | NestedType t = new NestedType();
|
---|
300 | XmlGenerator.Serialize(t, tempFile);
|
---|
301 | object o = XmlParser.DeSerialize(tempFile);
|
---|
302 | Assert.AreEqual(
|
---|
303 | DebugStringGenerator.Serialize(t),
|
---|
304 | DebugStringGenerator.Serialize(o));
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | [TestMethod]
|
---|
309 | public void SimpleArray() {
|
---|
310 | string[] strings = { "ora", "et", "labora" };
|
---|
311 | XmlGenerator.Serialize(strings, tempFile);
|
---|
312 | object o = XmlParser.DeSerialize(tempFile);
|
---|
313 | Assert.AreEqual(
|
---|
314 | DebugStringGenerator.Serialize(strings),
|
---|
315 | DebugStringGenerator.Serialize(o));
|
---|
316 | }
|
---|
317 |
|
---|
318 | [TestMethod]
|
---|
319 | public void BinaryFormatTest() {
|
---|
320 | Root r = new Root();
|
---|
321 | Assert.Fail("Not Implemented");
|
---|
322 | //BinaryGenerator.Serialize(r, "test.bin");
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | [TestMethod]
|
---|
327 | public void PrimitiveRoot() {
|
---|
328 | XmlGenerator.Serialize(12.3f, tempFile);
|
---|
329 | object o = XmlParser.DeSerialize(tempFile);
|
---|
330 | Assert.AreEqual(
|
---|
331 | DebugStringGenerator.Serialize(12.3f),
|
---|
332 | DebugStringGenerator.Serialize(o));
|
---|
333 | }
|
---|
334 |
|
---|
335 | private string formatFullMemberName(MemberInfo mi) {
|
---|
336 | return new StringBuilder()
|
---|
337 | .Append(mi.DeclaringType.Assembly.GetName().Name)
|
---|
338 | .Append(": ")
|
---|
339 | .Append(mi.DeclaringType.Namespace)
|
---|
340 | .Append('.')
|
---|
341 | .Append(mi.DeclaringType.Name)
|
---|
342 | .Append('.')
|
---|
343 | .Append(mi.Name).ToString();
|
---|
344 | }
|
---|
345 |
|
---|
346 | [TestMethod]
|
---|
347 | public void CodingConventions() {
|
---|
348 | List<string> lowerCaseMethodNames = new List<string>();
|
---|
349 | List<string> lowerCaseProperties = new List<string>();
|
---|
350 | List<string> lowerCaseFields = new List<string>();
|
---|
351 | foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) {
|
---|
352 | if (!a.GetName().Name.StartsWith("HeuristicLab"))
|
---|
353 | continue;
|
---|
354 | foreach (Type t in a.GetTypes()) {
|
---|
355 | foreach (MemberInfo mi in t.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) {
|
---|
356 | if (char.IsLower(mi.Name[0])) {
|
---|
357 | if (mi.MemberType == MemberTypes.Field)
|
---|
358 | lowerCaseFields.Add(formatFullMemberName(mi));
|
---|
359 | if (mi.MemberType == MemberTypes.Property)
|
---|
360 | lowerCaseProperties.Add(formatFullMemberName(mi));
|
---|
361 | if (mi.MemberType == MemberTypes.Method &&
|
---|
362 | !mi.Name.StartsWith("get_") &&
|
---|
363 | !mi.Name.StartsWith("set_") &&
|
---|
364 | !mi.Name.StartsWith("add_") &&
|
---|
365 | !mi.Name.StartsWith("remove_"))
|
---|
366 | lowerCaseMethodNames.Add(formatFullMemberName(mi));
|
---|
367 | }
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 | //Assert.AreEqual("", lowerCaseFields.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
372 | Assert.AreEqual("", lowerCaseMethodNames.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
373 | Assert.AreEqual("", lowerCaseProperties.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
374 | }
|
---|
375 |
|
---|
376 | [TestMethod]
|
---|
377 | public void Number2StringDecomposer() {
|
---|
378 | NumberTest sdt = new NumberTest();
|
---|
379 | XmlGenerator.Serialize(sdt, tempFile,
|
---|
380 | new Configuration(new XmlFormat(),
|
---|
381 | new List<IFormatter> { new String2XmlFormatter() },
|
---|
382 | new List<IDecomposer> {
|
---|
383 | new StorableDecomposer(),
|
---|
384 | new Number2StringDecomposer() }));
|
---|
385 | object o = XmlParser.DeSerialize(tempFile);
|
---|
386 | Assert.AreEqual(
|
---|
387 | DebugStringGenerator.Serialize(sdt),
|
---|
388 | DebugStringGenerator.Serialize(o));
|
---|
389 | }
|
---|
390 |
|
---|
391 |
|
---|
392 | [ClassInitialize]
|
---|
393 | public static void Initialize(TestContext testContext) {
|
---|
394 | ConfigurationService.Instance.Reset();
|
---|
395 | }
|
---|
396 | }
|
---|
397 | }
|
---|