Free cookie consent management tool by TermsFeed Policy Generator

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

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

Transform console tests into unit tests (#593)

File size: 8.1 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;
11
12namespace HeuristicLab.Persistence.UnitTest {
13
14  public class PrimitivesTest {
15    [Storable]
16    private bool _bool = true;
17    [Storable]
18    private byte _byte = 0xFF;
19    [Storable]
20    private sbyte _sbyte = 0xF;
21    [Storable]
22    private short _short = -123;
23    [Storable]
24    private ushort _ushort = 123;
25    [Storable]
26    private int _int = -123;
27    [Storable]
28    private uint _uint = 123;
29    [Storable]
30    private long _long = 123456;
31    [Storable]
32    private ulong _ulong = 123456;
33    [Storable]
34    private long[,] _long_array =
35      new long[,] { { 123, 456, }, { 789, 123 } };
36    [Storable]
37    public List<int> list = new List<int> { 1, 2, 3, 4, 5 };
38  }
39
40  public enum TestEnum { va1, va2, va3, va8 } ;
41
42  public class RootBase {
43    [Storable]
44    private string baseString = "   Serial  ";
45    [Storable]
46    public TestEnum myEnum = TestEnum.va3;
47  }
48
49  public class Root : RootBase {
50    [Storable]
51    public int[] i = new[] { 3, 4, 5, 6 };
52    [Storable]
53    public string s;
54    [Storable]
55    public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
56    [Storable]
57    public List<int> intList = new List<int>(new[] { 321, 312, 321 });
58    [Storable]
59    public Custom c;
60    [Storable]
61    public List<Root> selfReferences;
62    [Storable]
63    public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
64    [Storable]
65    public bool boolean = true;
66    [Storable]
67    public DateTime dateTime;
68    [Storable]
69    public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("Serial", 123);
70    [Storable]
71    public Dictionary<string, int> dict = new Dictionary<string, int>();
72    [Storable(DefaultValue = "default")]
73    public string uninitialized;
74  }
75
76  public class Custom {
77    [Storable]
78    public int i;
79    [Storable]
80    public Root r;
81    [Storable]
82    public string name = "<![CDATA[<![CDATA[Serial]]>]]>";
83  }
84
85  public class Manager {
86
87    public DateTime lastLoadTime;
88    [Storable]
89    private DateTime lastLoadTimePersistence {
90      get { return lastLoadTime; }
91      set { lastLoadTime = DateTime.Now; }
92    }
93    [Storable]
94    public double? dbl;
95  }
96
97  public class C {
98    [Storable]
99    public C[][] allCs;
100    [Storable]
101    public KeyValuePair<List<C>, C> kvpList;
102  }
103
104
105  [TestClass]
106  public class UseCases {
107
108    private string tempFile;
109
110    [TestInitialize()]
111    public void CreateTempFile() {
112      tempFile = Path.GetTempFileName();
113    }
114
115    [TestCleanup()]
116    public void ClearTempFile() {
117      File.Delete(tempFile);
118    }
119
120    [TestMethod]
121    public void ComplexStorable() {
122      Root r = new Root();
123      r.selfReferences = new List<Root> { r, r };
124      r.c = new Custom { r = r };
125      r.dict.Add("one", 1);
126      r.dict.Add("two", 2);
127      r.dict.Add("three", 3);
128      r.myEnum = TestEnum.va1;
129      XmlGenerator.Serialize(r, tempFile);
130      object o = XmlParser.DeSerialize(tempFile);
131      Assert.AreEqual(
132        DebugStringGenerator.Serialize(r),
133        DebugStringGenerator.Serialize(o));
134      Root newR = (Root)o;
135      Assert.AreSame(newR, newR.selfReferences[0]);
136      Assert.AreNotSame(r, newR);
137    }
138
139    [TestMethod]
140    public void SelfReferences() {
141      C c = new C();
142      C[][] cs = new C[2][];
143      cs[0] = new C[] { c };
144      cs[1] = new C[] { c };
145      c.allCs = cs;
146      c.kvpList = new KeyValuePair<List<C>, C>(new List<C> { c }, c);
147      XmlGenerator.Serialize(cs, tempFile);
148      object o = XmlParser.DeSerialize(tempFile);
149      Assert.AreEqual(
150        DebugStringGenerator.Serialize(cs),
151        DebugStringGenerator.Serialize(o));
152      Assert.AreSame(c, c.allCs[0][0]);
153      Assert.AreSame(c, c.allCs[1][0]);
154      Assert.AreSame(c, c.kvpList.Key[0]);
155      Assert.AreSame(c, c.kvpList.Value);
156      C[][] newCs = (C[][])o;
157      C newC = newCs[0][0];
158      Assert.AreSame(newC, newC.allCs[0][0]);
159      Assert.AreSame(newC, newC.allCs[1][0]);
160      Assert.AreSame(newC, newC.kvpList.Key[0]);
161      Assert.AreSame(newC, newC.kvpList.Value);
162    }
163
164    [TestMethod]
165    public void ArrayCreation() {
166      ArrayList[] arrayListArray = new ArrayList[4];
167      arrayListArray[0] = new ArrayList();
168      arrayListArray[0].Add(arrayListArray);
169      arrayListArray[0].Add(arrayListArray);
170      arrayListArray[1] = new ArrayList();
171      arrayListArray[1].Add(arrayListArray);
172      arrayListArray[2] = new ArrayList();
173      arrayListArray[2].Add(arrayListArray);
174      arrayListArray[2].Add(arrayListArray);
175      Array a = Array.CreateInstance(
176                              typeof(object),
177                              new[] { 1, 2 }, new[] { 3, 4 });
178      arrayListArray[2].Add(a);
179      XmlGenerator.Serialize(arrayListArray, tempFile);
180      object o = XmlParser.DeSerialize(tempFile);
181      Assert.AreEqual(
182        DebugStringGenerator.Serialize(arrayListArray),
183        DebugStringGenerator.Serialize(o));
184      ArrayList[] newArray = (ArrayList[])o;
185      Assert.AreSame(arrayListArray, arrayListArray[0][0]);
186      Assert.AreSame(arrayListArray, arrayListArray[2][1]);
187      Assert.AreSame(newArray, newArray[0][0]);
188      Assert.AreSame(newArray, newArray[2][1]);
189    }
190
191    [TestMethod]
192    public void CustomSerializationProperty() {
193      Manager m = new Manager();
194      XmlGenerator.Serialize(m, tempFile);
195      Manager newM = (Manager)XmlParser.DeSerialize(tempFile);
196      Assert.AreNotEqual(
197        DebugStringGenerator.Serialize(m),
198        DebugStringGenerator.Serialize(newM));
199      Assert.AreEqual(m.dbl, newM.dbl);
200      Assert.AreEqual(m.lastLoadTime, new DateTime());
201      Assert.AreNotEqual(newM.lastLoadTime, new DateTime());
202      Assert.IsTrue((DateTime.Now - newM.lastLoadTime).TotalSeconds < 10);
203    }
204
205    [TestMethod]
206    public void Primitives() {
207      PrimitivesTest sdt = new PrimitivesTest();
208      XmlGenerator.Serialize(sdt, tempFile);
209      object o = XmlParser.DeSerialize(tempFile);
210      Assert.AreEqual(
211        DebugStringGenerator.Serialize(sdt),
212        DebugStringGenerator.Serialize(o));
213    }
214
215    [TestMethod]
216    public void MultiDimensionalArray() {
217      string[,] mDimString = new string[,] {
218        {"ora", "et", "labora"},
219        {"Beten", "und", "Arbeiten"}
220      };
221      XmlGenerator.Serialize(mDimString, tempFile);
222      object o = XmlParser.DeSerialize(tempFile);
223      Assert.AreEqual(
224        DebugStringGenerator.Serialize(mDimString),
225        DebugStringGenerator.Serialize(o));
226    }
227
228    public class NestedType {
229      [Storable]
230      private string value = "value";
231    }
232
233    [TestMethod]
234    public void NestedTypeTest() {
235      NestedType t = new NestedType();
236      XmlGenerator.Serialize(t, tempFile);
237      object o = XmlParser.DeSerialize(tempFile);
238      Assert.AreEqual(
239        DebugStringGenerator.Serialize(t),
240        DebugStringGenerator.Serialize(o));
241    }
242
243
244    [TestMethod]
245    public void SimpleArray() {
246      string[] strings = { "ora", "et", "labora" };
247      XmlGenerator.Serialize(strings, tempFile);
248      object o = XmlParser.DeSerialize(tempFile);
249      Assert.AreEqual(
250        DebugStringGenerator.Serialize(strings),
251        DebugStringGenerator.Serialize(o));
252    }
253
254    [TestMethod]
255    public void BinaryFormatTest() {
256      Root r = new Root();
257      Assert.Fail("Not Implemented");
258      //BinaryGenerator.Serialize(r, "test.bin");
259    }
260
261
262    [TestMethod]
263    public void PrimitiveRoot() {
264      XmlGenerator.Serialize(12.3f, tempFile);
265      object o = XmlParser.DeSerialize(tempFile);
266      Assert.AreEqual(
267        DebugStringGenerator.Serialize(12.3f),
268        DebugStringGenerator.Serialize(o));
269    }
270
271
272    [ClassInitialize]
273    public static void Initialize(TestContext testContext) {
274      ConfigurationService.Instance.Reset();
275    }
276  }
277}
Note: See TracBrowser for help on using the repository browser.