Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/UnitTests/UseCases.cs @ 1644

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

support for default disabled decomposers, re-activate number2string decomposer with negative priority. (#548)

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