1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Diagnostics;
|
---|
26 | using System.Drawing;
|
---|
27 | using System.Globalization;
|
---|
28 | using System.IO;
|
---|
29 | using System.Linq;
|
---|
30 | using System.Reflection;
|
---|
31 | using System.Text;
|
---|
32 | using System.Threading.Tasks;
|
---|
33 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
34 | using HeuristicLab.Common;
|
---|
35 | using HeuristicLab.Core;
|
---|
36 | using HeuristicLab.Data;
|
---|
37 | using HeuristicLab.Persistence;
|
---|
38 | using HeuristicLab.Persistence.Auxiliary;
|
---|
39 | using HeuristicLab.Persistence.Core;
|
---|
40 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
41 | using HeuristicLab.Persistence.Default.DebugString;
|
---|
42 | using HeuristicLab.Persistence.Default.Xml;
|
---|
43 | using HeuristicLab.Persistence.Tests;
|
---|
44 | using HeuristicLab.Problems.TestFunctions;
|
---|
45 | using HeuristicLab.Tests;
|
---|
46 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
47 |
|
---|
48 | namespace HeuristicLab.PersistenceNew.Tests {
|
---|
49 | public static class EnumerableTimeSpanExtensions {
|
---|
50 | public static TimeSpan Average(this IEnumerable<TimeSpan> span) {
|
---|
51 | var avg = (long)Math.Round(span.Select(x => x.Ticks).Average());
|
---|
52 | return new TimeSpan(avg);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | #region Test Classes
|
---|
57 | [StorableType("7D9672BD-703D-42BB-9080-9929885D4580")]
|
---|
58 | public class NumberTest {
|
---|
59 | [Storable]
|
---|
60 | private bool _bool = true;
|
---|
61 | [Storable]
|
---|
62 | private byte _byte = 0xFF;
|
---|
63 | [Storable]
|
---|
64 | private sbyte _sbyte = 0xF;
|
---|
65 | [Storable]
|
---|
66 | private short _short = -123;
|
---|
67 | [Storable]
|
---|
68 | private ushort _ushort = 123;
|
---|
69 | [Storable]
|
---|
70 | private int _int = -123;
|
---|
71 | [Storable]
|
---|
72 | private uint _uint = 123;
|
---|
73 | [Storable]
|
---|
74 | private long _long = 123456;
|
---|
75 | [Storable]
|
---|
76 | private ulong _ulong = 123456;
|
---|
77 | public override bool Equals(object obj) {
|
---|
78 | NumberTest nt = obj as NumberTest;
|
---|
79 | if (nt == null)
|
---|
80 | throw new NotSupportedException();
|
---|
81 | return
|
---|
82 | nt._bool == _bool &&
|
---|
83 | nt._byte == _byte &&
|
---|
84 | nt._sbyte == _sbyte &&
|
---|
85 | nt._short == _short &&
|
---|
86 | nt._ushort == _ushort &&
|
---|
87 | nt._int == _int &&
|
---|
88 | nt._uint == _uint &&
|
---|
89 | nt._long == _long &&
|
---|
90 | nt._ulong == _ulong;
|
---|
91 | }
|
---|
92 | public override int GetHashCode() {
|
---|
93 | return
|
---|
94 | _bool.GetHashCode() ^
|
---|
95 | _byte.GetHashCode() ^
|
---|
96 | _sbyte.GetHashCode() ^
|
---|
97 | _short.GetHashCode() ^
|
---|
98 | _short.GetHashCode() ^
|
---|
99 | _int.GetHashCode() ^
|
---|
100 | _uint.GetHashCode() ^
|
---|
101 | _long.GetHashCode() ^
|
---|
102 | _ulong.GetHashCode();
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | [StorableType("EEB19599-D5AC-48ED-A56B-CF213DFAF2E4")]
|
---|
107 | public class NonDefaultConstructorClass {
|
---|
108 | [Storable]
|
---|
109 | int value;
|
---|
110 | public NonDefaultConstructorClass(int value) {
|
---|
111 | this.value = value;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | [StorableType("EE43FE7A-6D07-4D52-9338-C21B3485F82A")]
|
---|
116 | public class IntWrapper {
|
---|
117 |
|
---|
118 | [Storable]
|
---|
119 | public int Value;
|
---|
120 |
|
---|
121 | private IntWrapper() { }
|
---|
122 |
|
---|
123 | public IntWrapper(int value) {
|
---|
124 | this.Value = value;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public override bool Equals(object obj) {
|
---|
128 | if (obj as IntWrapper == null)
|
---|
129 | return false;
|
---|
130 | return Value.Equals(((IntWrapper)obj).Value);
|
---|
131 | }
|
---|
132 | public override int GetHashCode() {
|
---|
133 | return Value.GetHashCode();
|
---|
134 | }
|
---|
135 |
|
---|
136 | }
|
---|
137 |
|
---|
138 | [StorableType("00A8E48E-8E8A-443C-A327-9F6ACCBE7E80")]
|
---|
139 | public class PrimitivesTest : NumberTest {
|
---|
140 | [Storable]
|
---|
141 | private char c = 'e';
|
---|
142 | [Storable]
|
---|
143 | private long[,] _long_array =
|
---|
144 | new long[,] { { 123, 456, }, { 789, 123 } };
|
---|
145 | [Storable]
|
---|
146 | public List<int> list = new List<int> { 1, 2, 3, 4, 5 };
|
---|
147 | [Storable]
|
---|
148 | private object o = new object();
|
---|
149 | public override bool Equals(object obj) {
|
---|
150 | PrimitivesTest pt = obj as PrimitivesTest;
|
---|
151 | if (pt == null)
|
---|
152 | throw new NotSupportedException();
|
---|
153 | return base.Equals(obj) &&
|
---|
154 | c == pt.c &&
|
---|
155 | _long_array == pt._long_array &&
|
---|
156 | list == pt.list &&
|
---|
157 | o == pt.o;
|
---|
158 | }
|
---|
159 | public override int GetHashCode() {
|
---|
160 | return base.GetHashCode() ^
|
---|
161 | c.GetHashCode() ^
|
---|
162 | _long_array.GetHashCode() ^
|
---|
163 | list.GetHashCode() ^
|
---|
164 | o.GetHashCode();
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | public enum TestEnum { va1, va2, va3, va8 };
|
---|
169 |
|
---|
170 | [StorableType("26BA37F6-926D-4665-A10A-1F39E1CF6468")]
|
---|
171 | public class RootBase {
|
---|
172 | [Storable]
|
---|
173 | private string baseString = " Serial ";
|
---|
174 | [Storable]
|
---|
175 | public TestEnum myEnum = TestEnum.va3;
|
---|
176 | public override bool Equals(object obj) {
|
---|
177 | RootBase rb = obj as RootBase;
|
---|
178 | if (rb == null)
|
---|
179 | throw new NotSupportedException();
|
---|
180 | return baseString == rb.baseString &&
|
---|
181 | myEnum == rb.myEnum;
|
---|
182 | }
|
---|
183 | public override int GetHashCode() {
|
---|
184 | return baseString.GetHashCode() ^
|
---|
185 | myEnum.GetHashCode();
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | [StorableType("F6BCB436-B5F2-40F6-8E2F-7A018CD1CBA0")]
|
---|
190 | public class Root : RootBase {
|
---|
191 | [Storable]
|
---|
192 | public Stack<int> intStack = new Stack<int>();
|
---|
193 | [Storable]
|
---|
194 | public int[] i = new[] { 3, 4, 5, 6 };
|
---|
195 | [Storable(Name = "Test String")]
|
---|
196 | public string s;
|
---|
197 | [Storable]
|
---|
198 | public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
|
---|
199 | [Storable]
|
---|
200 | public List<int> intList = new List<int>(new[] { 321, 312, 321 });
|
---|
201 | [Storable]
|
---|
202 | public Custom c;
|
---|
203 | [Storable]
|
---|
204 | public List<Root> selfReferences;
|
---|
205 | [Storable]
|
---|
206 | public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
|
---|
207 | [Storable]
|
---|
208 | public bool boolean = true;
|
---|
209 | [Storable]
|
---|
210 | public DateTime dateTime;
|
---|
211 | [Storable]
|
---|
212 | public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("Serial", 123);
|
---|
213 | [Storable]
|
---|
214 | public Dictionary<string, int> dict = new Dictionary<string, int>();
|
---|
215 | [Storable(DefaultValue = "default")]
|
---|
216 | public string uninitialized;
|
---|
217 | }
|
---|
218 |
|
---|
219 | public enum SimpleEnum { one, two, three }
|
---|
220 | public enum ComplexEnum { one = 1, two = 2, three = 3 }
|
---|
221 | [FlagsAttribute]
|
---|
222 | public enum TrickyEnum { zero = 0, one = 1, two = 2 }
|
---|
223 |
|
---|
224 | [StorableType("2F6326ED-023A-415F-B5C7-9F9241940D05")]
|
---|
225 | public class EnumTest {
|
---|
226 | [Storable]
|
---|
227 | public SimpleEnum simpleEnum = SimpleEnum.one;
|
---|
228 | [Storable]
|
---|
229 | public ComplexEnum complexEnum = (ComplexEnum)2;
|
---|
230 | [Storable]
|
---|
231 | public TrickyEnum trickyEnum = (TrickyEnum)15;
|
---|
232 | }
|
---|
233 |
|
---|
234 | [StorableType("92365E2A-1184-4280-B763-4853C7ADF3E3")]
|
---|
235 | public class Custom {
|
---|
236 | [Storable]
|
---|
237 | public int i;
|
---|
238 | [Storable]
|
---|
239 | public Root r;
|
---|
240 | [Storable]
|
---|
241 | public string name = "<![CDATA[<![CDATA[Serial]]>]]>";
|
---|
242 | }
|
---|
243 |
|
---|
244 | [StorableType("7CF19EBC-1EC4-4FBE-BCA9-DA48E3CFE30D")]
|
---|
245 | public class Manager {
|
---|
246 |
|
---|
247 | public DateTime lastLoadTime;
|
---|
248 | [Storable]
|
---|
249 | private DateTime lastLoadTimePersistence {
|
---|
250 | get { return lastLoadTime; }
|
---|
251 | set { lastLoadTime = DateTime.Now; }
|
---|
252 | }
|
---|
253 | [Storable]
|
---|
254 | public double? dbl;
|
---|
255 | }
|
---|
256 |
|
---|
257 | [StorableType("9092C705-F5E9-4BA9-9750-4357DB29AABF")]
|
---|
258 | public class C {
|
---|
259 | [Storable]
|
---|
260 | public C[][] allCs;
|
---|
261 | [Storable]
|
---|
262 | public KeyValuePair<List<C>, C> kvpList;
|
---|
263 | }
|
---|
264 |
|
---|
265 | public class NonSerializable {
|
---|
266 | int x = 0;
|
---|
267 | public override bool Equals(object obj) {
|
---|
268 | NonSerializable ns = obj as NonSerializable;
|
---|
269 | if (ns == null)
|
---|
270 | throw new NotSupportedException();
|
---|
271 | return ns.x == x;
|
---|
272 | }
|
---|
273 | public override int GetHashCode() {
|
---|
274 | return x.GetHashCode();
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | [StorableType("FD953B0A-BDE6-41E6-91A8-CA3D90C91CDB")]
|
---|
279 | public class SimpleClass {
|
---|
280 | [Storable]
|
---|
281 | public double x { get; set; }
|
---|
282 | [Storable]
|
---|
283 | public int y { get; set; }
|
---|
284 | }
|
---|
285 |
|
---|
286 | #endregion
|
---|
287 |
|
---|
288 | [TestClass]
|
---|
289 | public class UseCasesPersistenceNew {
|
---|
290 | #region Helpers
|
---|
291 | private string tempFile;
|
---|
292 |
|
---|
293 | [ClassInitialize]
|
---|
294 | public static void Initialize(TestContext testContext) {
|
---|
295 | ConfigurationService.Instance.Reset();
|
---|
296 | }
|
---|
297 |
|
---|
298 | [TestInitialize()]
|
---|
299 | public void CreateTempFile() {
|
---|
300 | tempFile = Path.GetTempFileName();
|
---|
301 | }
|
---|
302 |
|
---|
303 | [TestCleanup()]
|
---|
304 | public void ClearTempFile() {
|
---|
305 | StreamReader reader = new StreamReader(tempFile);
|
---|
306 | string s = reader.ReadToEnd();
|
---|
307 | reader.Close();
|
---|
308 | File.Delete(tempFile);
|
---|
309 | }
|
---|
310 | #endregion
|
---|
311 |
|
---|
312 | #region Persistence 4.0 Profiling Helpers
|
---|
313 | public class PerformanceData {
|
---|
314 | public TimeSpan OldSerializingTime { get; set; }
|
---|
315 | public TimeSpan NewSerializingTime { get; set; }
|
---|
316 | public TimeSpan OldDeserializingTime { get; set; }
|
---|
317 | public TimeSpan NewDeserializingTime { get; set; }
|
---|
318 | public long OldFileSize { get; set; }
|
---|
319 | public long NewFileSize { get; set; }
|
---|
320 | public long OldSerializingMemoryConsumed { get; set; }
|
---|
321 | public long NewSerializingMemoryConsumed { get; set; }
|
---|
322 | public long OldDeserializingMemoryConsumed { get; set; }
|
---|
323 | public long NewDeserializingMemoryConsumed { get; set; }
|
---|
324 | }
|
---|
325 |
|
---|
326 | private void SerializeNew(object o) {
|
---|
327 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
328 | serializer.Serialize(o, tempFile);
|
---|
329 | }
|
---|
330 | private void SerializeOld(object o) {
|
---|
331 | XmlGenerator.Serialize(o, tempFile);
|
---|
332 | }
|
---|
333 | private object DeserializeNew() {
|
---|
334 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
335 | return serializer.Deserialize(tempFile);
|
---|
336 | }
|
---|
337 | private object DeserialiezOld() {
|
---|
338 | return XmlParser.Deserialize(tempFile);
|
---|
339 | }
|
---|
340 |
|
---|
341 | private void CollectGarbage() {
|
---|
342 | GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
|
---|
343 | GC.WaitForPendingFinalizers();
|
---|
344 | }
|
---|
345 |
|
---|
346 | public PerformanceData ProfileSingleRun(Func<object> GenerateDataFunc) {
|
---|
347 | PerformanceData performanceData = new PerformanceData();
|
---|
348 | Stopwatch sw = new Stopwatch();
|
---|
349 | object data = GenerateDataFunc();
|
---|
350 | object result = null;
|
---|
351 | long startMem, endMem;
|
---|
352 |
|
---|
353 |
|
---|
354 | //old file format serializing
|
---|
355 | CollectGarbage();
|
---|
356 | startMem = GC.GetTotalMemory(false);
|
---|
357 | sw.Start();
|
---|
358 | SerializeOld(data);
|
---|
359 | sw.Stop();
|
---|
360 | endMem = GC.GetTotalMemory(false);
|
---|
361 | performanceData.OldSerializingTime = sw.Elapsed;
|
---|
362 | performanceData.OldFileSize = new FileInfo(tempFile).Length;
|
---|
363 | performanceData.OldSerializingMemoryConsumed = endMem - startMem;
|
---|
364 | sw.Reset();
|
---|
365 |
|
---|
366 |
|
---|
367 | //old file format deserializing
|
---|
368 | CollectGarbage();
|
---|
369 | startMem = GC.GetTotalMemory(false);
|
---|
370 | sw.Start();
|
---|
371 | result = DeserialiezOld();
|
---|
372 | sw.Stop();
|
---|
373 | endMem = GC.GetTotalMemory(false);
|
---|
374 | performanceData.OldDeserializingTime = sw.Elapsed;
|
---|
375 | performanceData.OldDeserializingMemoryConsumed = endMem - startMem;
|
---|
376 | sw.Reset();
|
---|
377 |
|
---|
378 |
|
---|
379 | //new file format serializing
|
---|
380 | CollectGarbage();
|
---|
381 | startMem = GC.GetTotalMemory(false);
|
---|
382 | sw.Start();
|
---|
383 | SerializeNew(data);
|
---|
384 | sw.Stop();
|
---|
385 | endMem = GC.GetTotalMemory(false);
|
---|
386 | performanceData.NewSerializingTime = sw.Elapsed;
|
---|
387 | performanceData.NewSerializingMemoryConsumed = endMem - startMem;
|
---|
388 | performanceData.NewFileSize = new FileInfo(tempFile).Length;
|
---|
389 | sw.Reset();
|
---|
390 |
|
---|
391 |
|
---|
392 | //new file format deserializing
|
---|
393 | CollectGarbage();
|
---|
394 | startMem = GC.GetTotalMemory(false);
|
---|
395 | sw.Start();
|
---|
396 | result = DeserializeNew();
|
---|
397 | sw.Stop();
|
---|
398 | endMem = GC.GetTotalMemory(false);
|
---|
399 | performanceData.NewDeserializingTime = sw.Elapsed;
|
---|
400 | performanceData.NewDeserializingMemoryConsumed = endMem - startMem;
|
---|
401 | sw.Reset();
|
---|
402 |
|
---|
403 | return performanceData;
|
---|
404 | }
|
---|
405 |
|
---|
406 | public string Profile(Func<object> GenerateDataFunc) {
|
---|
407 | int nrOfRepetitions = 30;
|
---|
408 | StringBuilder report = new StringBuilder();
|
---|
409 | List<PerformanceData> dataList = new List<PerformanceData>();
|
---|
410 |
|
---|
411 | for (int i = 0; i < nrOfRepetitions; i++) {
|
---|
412 | dataList.Add(ProfileSingleRun(GenerateDataFunc));
|
---|
413 | }
|
---|
414 |
|
---|
415 | report.Append("Performance Report for " + GenerateDataFunc.Method.Name + ": " + Environment.NewLine);
|
---|
416 | report.Append(Environment.NewLine);
|
---|
417 | report.AppendFormat("Avg. old vs. new time for serializing a file; {0};{1};{2}",
|
---|
418 | dataList.Select(x => x.OldSerializingTime).Average(),
|
---|
419 | dataList.Select(x => x.NewSerializingTime).Average(),
|
---|
420 | dataList.Select(x => x.OldSerializingTime.TotalMilliseconds).Average() / dataList.Select(x => x.NewSerializingTime.TotalMilliseconds).Average()
|
---|
421 | );
|
---|
422 | report.Append(Environment.NewLine);
|
---|
423 | report.AppendFormat("Avg. old vs. new time for deserializing a file; {0};{1};{2}",
|
---|
424 | dataList.Select(x => x.OldDeserializingTime).Average(),
|
---|
425 | dataList.Select(x => x.NewDeserializingTime).Average(),
|
---|
426 | dataList.Select(x => x.OldDeserializingTime.TotalMilliseconds).Average() / dataList.Select(x => x.NewDeserializingTime.TotalMilliseconds).Average()
|
---|
427 | );
|
---|
428 | report.Append(Environment.NewLine);
|
---|
429 | report.AppendFormat("Avg. old vs. new file size (in bytes); {0};{1};{2}",
|
---|
430 | dataList.Select(x => x.OldFileSize).Average(),
|
---|
431 | dataList.Select(x => x.NewFileSize).Average(),
|
---|
432 | dataList.Select(x => x.OldFileSize).Average() / dataList.Select(x => x.NewFileSize).Average()
|
---|
433 | );
|
---|
434 | report.Append(Environment.NewLine);
|
---|
435 | report.AppendFormat("Avg. old vs. new memory consumption for serializing a file (in bytes); {0};{1};{2}",
|
---|
436 | dataList.Select(x => x.OldSerializingMemoryConsumed).Average(),
|
---|
437 | dataList.Select(x => x.NewSerializingMemoryConsumed).Average(),
|
---|
438 | dataList.Select(x => x.OldSerializingMemoryConsumed).Average() / dataList.Select(x => x.NewSerializingMemoryConsumed).Average()
|
---|
439 | );
|
---|
440 | report.Append(Environment.NewLine);
|
---|
441 | report.AppendFormat("Avg. old vs. new memory consumption for deserializing a file (in bytes); {0};{1};{2}",
|
---|
442 | dataList.Select(x => x.OldDeserializingMemoryConsumed).Average(),
|
---|
443 | dataList.Select(x => x.NewDeserializingMemoryConsumed).Average(),
|
---|
444 | dataList.Select(x => x.OldDeserializingMemoryConsumed).Average() / dataList.Select(x => x.NewDeserializingMemoryConsumed).Average()
|
---|
445 | );
|
---|
446 | report.Append(Environment.NewLine);
|
---|
447 |
|
---|
448 |
|
---|
449 | return report.ToString();
|
---|
450 | }
|
---|
451 | #endregion
|
---|
452 |
|
---|
453 | #region Persistence 4.0 test methods
|
---|
454 | [TestMethod]
|
---|
455 | [TestCategory("Persistence4")]
|
---|
456 | [TestProperty("Time", "short")]
|
---|
457 | public void TestBool() {
|
---|
458 | var test = new Func<object>(() => { return true; });
|
---|
459 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
460 | serializer.Serialize(test(), tempFile);
|
---|
461 | object o = serializer.Deserialize(tempFile);
|
---|
462 | bool result = (bool)o;
|
---|
463 | Assert.AreEqual(test(), result);
|
---|
464 |
|
---|
465 | string msg = Profile(test);
|
---|
466 | Console.WriteLine(msg);
|
---|
467 | }
|
---|
468 |
|
---|
469 | [TestMethod]
|
---|
470 | [TestCategory("Persistence4")]
|
---|
471 | [TestProperty("Time", "short")]
|
---|
472 | public void TestInt() {
|
---|
473 | var test = new Func<object>(() => { return (int)42; });
|
---|
474 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
475 | serializer.Serialize(test(), tempFile);
|
---|
476 | object o = serializer.Deserialize(tempFile);
|
---|
477 | int result = (int)o;
|
---|
478 | Assert.AreEqual(test(), result);
|
---|
479 |
|
---|
480 | string msg = Profile(test);
|
---|
481 | Console.WriteLine(msg);
|
---|
482 | }
|
---|
483 |
|
---|
484 | [TestMethod]
|
---|
485 | [TestCategory("Persistence4")]
|
---|
486 | [TestProperty("Time", "short")]
|
---|
487 | public void TestDouble() {
|
---|
488 | var test = new Func<object>(() => { return 42.5d; });
|
---|
489 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
490 | serializer.Serialize(test(), tempFile);
|
---|
491 | object o = serializer.Deserialize(tempFile);
|
---|
492 | double result = (double)o;
|
---|
493 | Assert.AreEqual(test(), result);
|
---|
494 | Assert.IsTrue(o is double);
|
---|
495 |
|
---|
496 | string msg = Profile(test);
|
---|
497 | Console.WriteLine(msg);
|
---|
498 | }
|
---|
499 |
|
---|
500 | [TestMethod]
|
---|
501 | [TestCategory("Persistence4")]
|
---|
502 | [TestProperty("Time", "short")]
|
---|
503 | public void TestFloat() {
|
---|
504 | var test = new Func<object>(() => { return 42.5f; });
|
---|
505 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
506 | serializer.Serialize(test(), tempFile);
|
---|
507 | object o = serializer.Deserialize(tempFile);
|
---|
508 | float result = (float)o;
|
---|
509 | Assert.AreEqual(test(), result);
|
---|
510 | Assert.IsTrue(o is float);
|
---|
511 |
|
---|
512 | string msg = Profile(test);
|
---|
513 | Console.WriteLine(msg);
|
---|
514 | }
|
---|
515 |
|
---|
516 | [TestMethod]
|
---|
517 | [TestCategory("Persistence4")]
|
---|
518 | [TestProperty("Time", "short")]
|
---|
519 | public void TestDecimal() {
|
---|
520 | var test = new Func<object>(() => { return 42.5m; });
|
---|
521 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
522 | serializer.Serialize(test(), tempFile);
|
---|
523 | object o = serializer.Deserialize(tempFile);
|
---|
524 | decimal result = (decimal)o;
|
---|
525 | Assert.AreEqual(test(), result);
|
---|
526 | Assert.IsTrue(o is decimal);
|
---|
527 |
|
---|
528 | string msg = Profile(test);
|
---|
529 | Console.WriteLine(msg);
|
---|
530 | }
|
---|
531 |
|
---|
532 | [TestMethod]
|
---|
533 | [TestCategory("Persistence4")]
|
---|
534 | [TestProperty("Time", "short")]
|
---|
535 | public void TestLong() {
|
---|
536 | var test = new Func<object>(() => { return 42l; });
|
---|
537 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
538 | serializer.Serialize(test(), tempFile);
|
---|
539 | object o = serializer.Deserialize(tempFile);
|
---|
540 | long result = (long)o;
|
---|
541 | Assert.AreEqual(test(), result);
|
---|
542 | Assert.IsTrue(o is long);
|
---|
543 |
|
---|
544 | string msg = Profile(test);
|
---|
545 | Console.WriteLine(msg);
|
---|
546 | }
|
---|
547 |
|
---|
548 | [TestMethod]
|
---|
549 | [TestCategory("Persistence4")]
|
---|
550 | [TestProperty("Time", "short")]
|
---|
551 | public void TestUInt() {
|
---|
552 | var test = new Func<object>(() => { return 42u; });
|
---|
553 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
554 | serializer.Serialize(test(), tempFile);
|
---|
555 | object o = serializer.Deserialize(tempFile);
|
---|
556 | uint result = (uint)o;
|
---|
557 | Assert.AreEqual(test(), result);
|
---|
558 | Assert.IsTrue(o is uint);
|
---|
559 |
|
---|
560 | string msg = Profile(test);
|
---|
561 | Console.WriteLine(msg);
|
---|
562 | }
|
---|
563 |
|
---|
564 | [TestMethod]
|
---|
565 | [TestCategory("Persistence4")]
|
---|
566 | [TestProperty("Time", "short")]
|
---|
567 | public void TestShort() {
|
---|
568 | var test = new Func<object>(() => { short s = 42; return s; });
|
---|
569 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
570 | serializer.Serialize(test(), tempFile);
|
---|
571 | object o = serializer.Deserialize(tempFile);
|
---|
572 | short result = (short)o;
|
---|
573 | Assert.IsTrue(o is short);
|
---|
574 | Assert.AreEqual(test(), result);
|
---|
575 |
|
---|
576 | string msg = Profile(test);
|
---|
577 | Console.WriteLine(msg);
|
---|
578 | }
|
---|
579 |
|
---|
580 | [TestMethod]
|
---|
581 | [TestCategory("Persistence4")]
|
---|
582 | [TestProperty("Time", "short")]
|
---|
583 | public void TestByte() {
|
---|
584 | var test = new Func<object>(() => { byte b = 42; return b; });
|
---|
585 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
586 | serializer.Serialize(test(), tempFile);
|
---|
587 | object o = serializer.Deserialize(tempFile);
|
---|
588 | byte result = (byte)o;
|
---|
589 | Assert.IsTrue(o is byte);
|
---|
590 | Assert.AreEqual(test(), result);
|
---|
591 |
|
---|
592 | string msg = Profile(test);
|
---|
593 | Console.WriteLine(msg);
|
---|
594 | }
|
---|
595 |
|
---|
596 | [TestMethod]
|
---|
597 | [TestCategory("Persistence4")]
|
---|
598 | [TestProperty("Time", "short")]
|
---|
599 | public void TestEnumSimple() {
|
---|
600 | var test = new Func<object>(() => { return SimpleEnum.two; });
|
---|
601 |
|
---|
602 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
603 | serializer.Serialize(test(), tempFile);
|
---|
604 | object o = serializer.Deserialize(tempFile);
|
---|
605 | SimpleEnum result = (SimpleEnum)o;
|
---|
606 | Assert.AreEqual(test(), result);
|
---|
607 |
|
---|
608 | string msg = Profile(test);
|
---|
609 | Console.WriteLine(msg);
|
---|
610 | }
|
---|
611 |
|
---|
612 | [TestMethod]
|
---|
613 | [TestCategory("Persistence4")]
|
---|
614 | [TestProperty("Time", "short")]
|
---|
615 | public void TestEnumComplex() {
|
---|
616 | var test = new Func<object>(() => { return ComplexEnum.three; });
|
---|
617 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
618 | serializer.Serialize(test(), tempFile);
|
---|
619 | object o = serializer.Deserialize(tempFile);
|
---|
620 | ComplexEnum result = (ComplexEnum)o;
|
---|
621 | Assert.AreEqual(test(), result);
|
---|
622 |
|
---|
623 | string msg = Profile(test);
|
---|
624 | Console.WriteLine(msg);
|
---|
625 | }
|
---|
626 |
|
---|
627 | [TestMethod]
|
---|
628 | [TestCategory("Persistence4")]
|
---|
629 | [TestProperty("Time", "short")]
|
---|
630 | public void TestType() {
|
---|
631 | var test = new Func<object>(() => { return typeof(HeuristicLab.Algorithms.GeneticAlgorithm.GeneticAlgorithm); });
|
---|
632 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
633 | serializer.Serialize(test(), tempFile);
|
---|
634 | object o = serializer.Deserialize(tempFile);
|
---|
635 | Type result = (Type)o;
|
---|
636 | Assert.AreEqual(test(), result);
|
---|
637 |
|
---|
638 | string msg = Profile(test);
|
---|
639 | Console.WriteLine(msg);
|
---|
640 | }
|
---|
641 |
|
---|
642 | [TestMethod]
|
---|
643 | [TestCategory("Persistence4")]
|
---|
644 | [TestProperty("Time", "short")]
|
---|
645 | public void TestBytes() {
|
---|
646 | var test = new Func<byte[]>(() => { return new byte[] { 3, 1 }; });
|
---|
647 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
648 | serializer.Serialize(test(), tempFile);
|
---|
649 | object o = serializer.Deserialize(tempFile);
|
---|
650 | byte[] result = (byte[])o;
|
---|
651 | Assert.AreEqual(test()[0], result[0]);
|
---|
652 | Assert.AreEqual(test()[1], result[1]);
|
---|
653 |
|
---|
654 | string msg = Profile(test);
|
---|
655 | Console.WriteLine(msg);
|
---|
656 | }
|
---|
657 |
|
---|
658 | [TestMethod]
|
---|
659 | [TestCategory("Persistence4")]
|
---|
660 | [TestProperty("Time", "short")]
|
---|
661 | public void TestSBytes() {
|
---|
662 | var test = new Func<sbyte[]>(() => { return new sbyte[] { 3, 1 }; });
|
---|
663 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
664 | serializer.Serialize(test(), tempFile);
|
---|
665 | object o = serializer.Deserialize(tempFile);
|
---|
666 | sbyte[] result = (sbyte[])o;
|
---|
667 | Assert.AreEqual(test()[0], result[0]);
|
---|
668 | Assert.AreEqual(test()[1], result[1]);
|
---|
669 |
|
---|
670 | string msg = Profile(test);
|
---|
671 | Console.WriteLine(msg);
|
---|
672 | }
|
---|
673 |
|
---|
674 | [TestMethod]
|
---|
675 | [TestCategory("Persistence4")]
|
---|
676 | [TestProperty("Time", "short")]
|
---|
677 | public void TestChars() {
|
---|
678 | var test = new Func<char[]>(() => { return new char[] { 'a', 'b' }; });
|
---|
679 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
680 | serializer.Serialize(test(), tempFile);
|
---|
681 | object o = serializer.Deserialize(tempFile);
|
---|
682 | char[] result = (char[])o;
|
---|
683 | Assert.AreEqual(test()[0], result[0]);
|
---|
684 | Assert.AreEqual(test()[1], result[1]);
|
---|
685 |
|
---|
686 | string msg = Profile(test);
|
---|
687 | Console.WriteLine(msg);
|
---|
688 | }
|
---|
689 |
|
---|
690 | [TestMethod]
|
---|
691 | [TestCategory("Persistence4")]
|
---|
692 | [TestProperty("Time", "short")]
|
---|
693 | public void TestShorts() {
|
---|
694 | var test = new Func<short[]>(() => { return new short[] { 3, 1 }; });
|
---|
695 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
696 | serializer.Serialize(test(), tempFile);
|
---|
697 | object o = serializer.Deserialize(tempFile);
|
---|
698 | short[] result = (short[])o;
|
---|
699 | Assert.AreEqual(test()[0], result[0]);
|
---|
700 | Assert.AreEqual(test()[1], result[1]);
|
---|
701 |
|
---|
702 | string msg = Profile(test);
|
---|
703 | Console.WriteLine(msg);
|
---|
704 | }
|
---|
705 |
|
---|
706 | [TestMethod]
|
---|
707 | [TestCategory("Persistence4")]
|
---|
708 | [TestProperty("Time", "short")]
|
---|
709 | public void TestUShorts() {
|
---|
710 | var test = new Func<ushort[]>(() => { return new ushort[] { 3, 1 }; });
|
---|
711 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
712 | serializer.Serialize(test(), tempFile);
|
---|
713 | object o = serializer.Deserialize(tempFile);
|
---|
714 | ushort[] result = (ushort[])o;
|
---|
715 | Assert.AreEqual(test()[0], result[0]);
|
---|
716 | Assert.AreEqual(test()[1], result[1]);
|
---|
717 |
|
---|
718 | string msg = Profile(test);
|
---|
719 | Console.WriteLine(msg);
|
---|
720 | }
|
---|
721 |
|
---|
722 | [TestMethod]
|
---|
723 | [TestCategory("Persistence4")]
|
---|
724 | [TestProperty("Time", "short")]
|
---|
725 | public void TestString() {
|
---|
726 | var test = new Func<object>(() => { return "Hello World!"; });
|
---|
727 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
728 | serializer.Serialize(test(), tempFile);
|
---|
729 | object o = serializer.Deserialize(tempFile);
|
---|
730 | string result = (string)o;
|
---|
731 | Assert.AreEqual(test(), result);
|
---|
732 |
|
---|
733 | string msg = Profile(test);
|
---|
734 | Console.WriteLine(msg);
|
---|
735 | }
|
---|
736 |
|
---|
737 | [TestMethod]
|
---|
738 | [TestCategory("Persistence4")]
|
---|
739 | [TestProperty("Time", "short")]
|
---|
740 | public void TestColor() {
|
---|
741 | var test = new Func<object>(() => { return Color.FromArgb(12, 34, 56, 78); });
|
---|
742 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
743 | serializer.Serialize(test(), tempFile);
|
---|
744 | object o = serializer.Deserialize(tempFile);
|
---|
745 | Color result = (Color)o;
|
---|
746 | Assert.AreEqual(test(), result);
|
---|
747 |
|
---|
748 | string msg = Profile(test);
|
---|
749 | Console.WriteLine(msg);
|
---|
750 | }
|
---|
751 |
|
---|
752 | [TestMethod]
|
---|
753 | [TestCategory("Persistence4")]
|
---|
754 | [TestProperty("Time", "short")]
|
---|
755 | public void TestPoint() {
|
---|
756 | var test = new Func<object>(() => { return new Point(3, 4); });
|
---|
757 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
758 | serializer.Serialize(test(), tempFile);
|
---|
759 | object o = serializer.Deserialize(tempFile);
|
---|
760 | Point result = (Point)o;
|
---|
761 | Assert.AreEqual(((Point)test()).X, result.X);
|
---|
762 | Assert.AreEqual(((Point)test()).Y, result.Y);
|
---|
763 |
|
---|
764 | string msg = Profile(test);
|
---|
765 | Console.WriteLine(msg);
|
---|
766 | }
|
---|
767 |
|
---|
768 | [TestMethod]
|
---|
769 | [TestCategory("Persistence4")]
|
---|
770 | [TestProperty("Time", "short")]
|
---|
771 | public void TestBoolArray() {
|
---|
772 | var test = new Func<bool[]>(() => { return new[] { true, false, true }; });
|
---|
773 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
774 | serializer.Serialize(test(), tempFile);
|
---|
775 | object o = serializer.Deserialize(tempFile);
|
---|
776 | bool[] result = (bool[])o;
|
---|
777 | Assert.AreEqual(test()[0], result[0]);
|
---|
778 | Assert.AreEqual(test()[1], result[1]);
|
---|
779 | Assert.AreEqual(test()[2], result[2]);
|
---|
780 |
|
---|
781 | string msg = Profile(test);
|
---|
782 | Console.WriteLine(msg);
|
---|
783 | }
|
---|
784 |
|
---|
785 | [TestMethod]
|
---|
786 | [TestCategory("Persistence4")]
|
---|
787 | [TestProperty("Time", "short")]
|
---|
788 | public void TestIntArray() {
|
---|
789 | var test = new Func<int[]>(() => { return new[] { 41, 22, 13 }; });
|
---|
790 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
791 | serializer.Serialize(test(), tempFile);
|
---|
792 | object o = serializer.Deserialize(tempFile);
|
---|
793 | int[] result = (int[])o;
|
---|
794 | Assert.AreEqual(test()[0], result[0]);
|
---|
795 | Assert.AreEqual(test()[1], result[1]);
|
---|
796 | Assert.AreEqual(test()[2], result[2]);
|
---|
797 |
|
---|
798 | string msg = Profile(test);
|
---|
799 | Console.WriteLine(msg);
|
---|
800 | }
|
---|
801 |
|
---|
802 | [TestMethod]
|
---|
803 | [TestCategory("Persistence4")]
|
---|
804 | [TestProperty("Time", "short")]
|
---|
805 | public void TestLongArray() {
|
---|
806 | var test = new Func<long[]>(() => { return new[] { 414481188112191633l, 245488586662l, 13546881335845865l }; });
|
---|
807 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
808 | serializer.Serialize(test(), tempFile);
|
---|
809 | object o = serializer.Deserialize(tempFile);
|
---|
810 | long[] result = (long[])o;
|
---|
811 | Assert.AreEqual(test()[0], result[0]);
|
---|
812 | Assert.AreEqual(test()[1], result[1]);
|
---|
813 | Assert.AreEqual(test()[2], result[2]);
|
---|
814 |
|
---|
815 | string msg = Profile(test);
|
---|
816 | Console.WriteLine(msg);
|
---|
817 | }
|
---|
818 |
|
---|
819 | [TestMethod]
|
---|
820 | [TestCategory("Persistence4")]
|
---|
821 | [TestProperty("Time", "short")]
|
---|
822 | public void TestDoubleArray() {
|
---|
823 | var test = new Func<double[]>(() => { return new[] { 41.5, 22.7, 13.8 }; });
|
---|
824 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
825 | serializer.Serialize(test(), tempFile);
|
---|
826 | object o = serializer.Deserialize(tempFile);
|
---|
827 | double[] result = (double[])o;
|
---|
828 | Assert.AreEqual(test()[0], result[0]);
|
---|
829 | Assert.AreEqual(test()[1], result[1]);
|
---|
830 | Assert.AreEqual(test()[2], result[2]);
|
---|
831 |
|
---|
832 | string msg = Profile(test);
|
---|
833 | Console.WriteLine(msg);
|
---|
834 | }
|
---|
835 |
|
---|
836 | [TestMethod]
|
---|
837 | [TestCategory("Persistence4")]
|
---|
838 | [TestProperty("Time", "short")]
|
---|
839 | public void TestObjectArray() {
|
---|
840 | var test = new Func<SimpleClass[]>(() => {
|
---|
841 | return new[] { new SimpleClass() { x = 42, y = 43 },
|
---|
842 | new SimpleClass() { x = 44.44, y = 5677 },
|
---|
843 | new SimpleClass() { x = 533.33, y = 2345 } };
|
---|
844 | });
|
---|
845 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
846 | serializer.Serialize(test(), tempFile);
|
---|
847 | object o = serializer.Deserialize(tempFile);
|
---|
848 | SimpleClass[] result = (SimpleClass[])o;
|
---|
849 | Assert.AreEqual(test()[0].x, result[0].x);
|
---|
850 | Assert.AreEqual(test()[0].y, result[0].y);
|
---|
851 | Assert.AreEqual(test()[1].x, result[1].x);
|
---|
852 | Assert.AreEqual(test()[1].y, result[1].y);
|
---|
853 | Assert.AreEqual(test()[2].x, result[2].x);
|
---|
854 | Assert.AreEqual(test()[2].y, result[2].y);
|
---|
855 |
|
---|
856 | string msg = Profile(test);
|
---|
857 | Console.WriteLine(msg);
|
---|
858 | }
|
---|
859 |
|
---|
860 | [TestMethod]
|
---|
861 | [TestCategory("Persistence4")]
|
---|
862 | [TestProperty("Time", "short")]
|
---|
863 | public void TestStack() {
|
---|
864 | var test = new Func<Stack>(() => {
|
---|
865 | return new Stack(new int[] { 1, 2, 3 });
|
---|
866 | });
|
---|
867 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
868 | serializer.Serialize(test(), tempFile);
|
---|
869 | object o = serializer.Deserialize(tempFile);
|
---|
870 | Stack result = (Stack)o;
|
---|
871 | var actualStack = test();
|
---|
872 | Assert.AreEqual(actualStack.Pop(), result.Pop());
|
---|
873 | Assert.AreEqual(actualStack.Pop(), result.Pop());
|
---|
874 | Assert.AreEqual(actualStack.Pop(), result.Pop());
|
---|
875 |
|
---|
876 | string msg = Profile(test);
|
---|
877 | Console.WriteLine(msg);
|
---|
878 | }
|
---|
879 |
|
---|
880 | [TestMethod]
|
---|
881 | [TestCategory("Persistence4")]
|
---|
882 | [TestProperty("Time", "short")]
|
---|
883 | public void TestArrayOfStack() {
|
---|
884 | var test = new Func<object[]>(() => {
|
---|
885 | return new object[] {
|
---|
886 | new Stack(new int[] { 1, 2, 3 }),
|
---|
887 | new Stack<int>(new int[] { 1, 2, 3 }),
|
---|
888 | };
|
---|
889 | });
|
---|
890 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
891 | serializer.Serialize(test(), tempFile);
|
---|
892 | object o = serializer.Deserialize(tempFile);
|
---|
893 | var result = (object[])o;
|
---|
894 | var firstStack = (Stack)result[0];
|
---|
895 | var secondStack = (Stack<int>)result[1];
|
---|
896 | var actual = test();
|
---|
897 | var actualFirst = (Stack)actual[0];
|
---|
898 | var actualSecond = (Stack<int>)actual[1];
|
---|
899 |
|
---|
900 | Assert.AreEqual(actualFirst.Pop(), firstStack.Pop());
|
---|
901 | Assert.AreEqual(actualFirst.Pop(), firstStack.Pop());
|
---|
902 | Assert.AreEqual(actualFirst.Pop(), firstStack.Pop());
|
---|
903 | Assert.AreEqual(actualSecond.Pop(), secondStack.Pop());
|
---|
904 | Assert.AreEqual(actualSecond.Pop(), secondStack.Pop());
|
---|
905 | Assert.AreEqual(actualSecond.Pop(), secondStack.Pop());
|
---|
906 |
|
---|
907 | string msg = Profile(test);
|
---|
908 | Console.WriteLine(msg);
|
---|
909 | }
|
---|
910 |
|
---|
911 |
|
---|
912 | [TestMethod]
|
---|
913 | [TestCategory("Persistence4")]
|
---|
914 | [TestProperty("Time", "short")]
|
---|
915 | public void TestIntValueArray() {
|
---|
916 | var test = new Func<IntValue[]>(() => { return new[] { new IntValue(41), new IntValue(22), new IntValue(13) }; });
|
---|
917 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
918 | serializer.Serialize(test(), tempFile);
|
---|
919 | object o = serializer.Deserialize(tempFile);
|
---|
920 | IntValue[] result = (IntValue[])o;
|
---|
921 | Assert.AreEqual(test()[0].Value, result[0].Value);
|
---|
922 | Assert.AreEqual(test()[1].Value, result[1].Value);
|
---|
923 | Assert.AreEqual(test()[2].Value, result[2].Value);
|
---|
924 |
|
---|
925 | string msg = Profile(test);
|
---|
926 | Console.WriteLine(msg);
|
---|
927 | }
|
---|
928 |
|
---|
929 | [TestMethod]
|
---|
930 | [TestCategory("Persistence4")]
|
---|
931 | [TestProperty("Time", "short")]
|
---|
932 | public void TestIntValueArrayArray() {
|
---|
933 | var test = new Func<IntValue[][]>(() => { return new IntValue[][] { new IntValue[] { new IntValue(41), new IntValue(22), new IntValue(13) } }; });
|
---|
934 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
935 | serializer.Serialize(test(), tempFile);
|
---|
936 | object o = serializer.Deserialize(tempFile);
|
---|
937 | IntValue[][] result = (IntValue[][])o;
|
---|
938 | Assert.AreEqual(test()[0][0].Value, result[0][0].Value);
|
---|
939 | Assert.AreEqual(test()[0][1].Value, result[0][1].Value);
|
---|
940 | Assert.AreEqual(test()[0][2].Value, result[0][2].Value);
|
---|
941 |
|
---|
942 | string msg = Profile(test);
|
---|
943 | Console.WriteLine(msg);
|
---|
944 | }
|
---|
945 | #endregion
|
---|
946 |
|
---|
947 |
|
---|
948 | #region Old persistence test methods
|
---|
949 | [TestMethod]
|
---|
950 | [TestCategory("Persistence4")]
|
---|
951 | [TestProperty("Time", "short")]
|
---|
952 | public void ComplexStorable() {
|
---|
953 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
954 | Root r = InitializeComplexStorable();
|
---|
955 | serializer.Serialize(r, tempFile);
|
---|
956 | Root newR = (Root)serializer.Deserialize(tempFile);
|
---|
957 | CompareComplexStorables(r, newR);
|
---|
958 | }
|
---|
959 |
|
---|
960 | private static void CompareComplexStorables(Root r, Root newR) {
|
---|
961 | Assert.AreEqual(
|
---|
962 | DebugStringGenerator.Serialize(r),
|
---|
963 | DebugStringGenerator.Serialize(newR));
|
---|
964 | Assert.AreSame(newR, newR.selfReferences[0]);
|
---|
965 | Assert.AreNotSame(r, newR);
|
---|
966 | Assert.AreEqual(r.myEnum, TestEnum.va1);
|
---|
967 | Assert.AreEqual(r.i[0], 7);
|
---|
968 | Assert.AreEqual(r.i[1], 5);
|
---|
969 | Assert.AreEqual(r.i[2], 6);
|
---|
970 | Assert.AreEqual(r.s, "new value");
|
---|
971 | Assert.AreEqual(r.intArray[0], 3);
|
---|
972 | Assert.AreEqual(r.intArray[1], 2);
|
---|
973 | Assert.AreEqual(r.intArray[2], 1);
|
---|
974 | Assert.AreEqual(r.intList[0], 9);
|
---|
975 | Assert.AreEqual(r.intList[1], 8);
|
---|
976 | Assert.AreEqual(r.intList[2], 7);
|
---|
977 | Assert.AreEqual(r.multiDimArray[0, 0], 5);
|
---|
978 | Assert.AreEqual(r.multiDimArray[0, 1], 4);
|
---|
979 | Assert.AreEqual(r.multiDimArray[0, 2], 3);
|
---|
980 | Assert.AreEqual(r.multiDimArray[1, 0], 1);
|
---|
981 | Assert.AreEqual(r.multiDimArray[1, 1], 4);
|
---|
982 | Assert.AreEqual(r.multiDimArray[1, 2], 6);
|
---|
983 | Assert.IsFalse(r.boolean);
|
---|
984 | Assert.IsTrue((DateTime.Now - r.dateTime).TotalSeconds < 10);
|
---|
985 | Assert.AreEqual(r.kvp.Key, "string key");
|
---|
986 | Assert.AreEqual(r.kvp.Value, 321);
|
---|
987 | Assert.IsNull(r.uninitialized);
|
---|
988 | Assert.AreEqual(newR.myEnum, TestEnum.va1);
|
---|
989 | Assert.AreEqual(newR.i[0], 7);
|
---|
990 | Assert.AreEqual(newR.i[1], 5);
|
---|
991 | Assert.AreEqual(newR.i[2], 6);
|
---|
992 | Assert.AreEqual(newR.s, "new value");
|
---|
993 | Assert.AreEqual(newR.intArray[0], 3);
|
---|
994 | Assert.AreEqual(newR.intArray[1], 2);
|
---|
995 | Assert.AreEqual(newR.intArray[2], 1);
|
---|
996 | Assert.AreEqual(newR.intList[0], 9);
|
---|
997 | Assert.AreEqual(newR.intList[1], 8);
|
---|
998 | Assert.AreEqual(newR.intList[2], 7);
|
---|
999 | Assert.AreEqual(newR.multiDimArray[0, 0], 5);
|
---|
1000 | Assert.AreEqual(newR.multiDimArray[0, 1], 4);
|
---|
1001 | Assert.AreEqual(newR.multiDimArray[0, 2], 3);
|
---|
1002 | Assert.AreEqual(newR.multiDimArray[1, 0], 1);
|
---|
1003 | Assert.AreEqual(newR.multiDimArray[1, 1], 4);
|
---|
1004 | Assert.AreEqual(newR.multiDimArray[1, 2], 6);
|
---|
1005 | Assert.AreEqual(newR.intStack.Pop(), 3);
|
---|
1006 | Assert.AreEqual(newR.intStack.Pop(), 2);
|
---|
1007 | Assert.AreEqual(newR.intStack.Pop(), 1);
|
---|
1008 | Assert.IsFalse(newR.boolean);
|
---|
1009 | Assert.IsTrue((DateTime.Now - newR.dateTime).TotalSeconds < 10);
|
---|
1010 | Assert.AreEqual(newR.kvp.Key, "string key");
|
---|
1011 | Assert.AreEqual(newR.kvp.Value, 321);
|
---|
1012 | Assert.IsNull(newR.uninitialized);
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | private static Root InitializeComplexStorable() {
|
---|
1016 | Root r = new Root();
|
---|
1017 | r.intStack.Push(1);
|
---|
1018 | r.intStack.Push(2);
|
---|
1019 | r.intStack.Push(3);
|
---|
1020 | r.selfReferences = new List<Root> { r, r };
|
---|
1021 | r.c = new Custom { r = r };
|
---|
1022 | r.dict.Add("one", 1);
|
---|
1023 | r.dict.Add("two", 2);
|
---|
1024 | r.dict.Add("three", 3);
|
---|
1025 | r.myEnum = TestEnum.va1;
|
---|
1026 | r.i = new[] { 7, 5, 6 };
|
---|
1027 | r.s = "new value";
|
---|
1028 | r.intArray = new ArrayList { 3, 2, 1 };
|
---|
1029 | r.intList = new List<int> { 9, 8, 7 };
|
---|
1030 | r.multiDimArray = new double[,] { { 5, 4, 3 }, { 1, 4, 6 } };
|
---|
1031 | r.boolean = false;
|
---|
1032 | r.dateTime = DateTime.Now;
|
---|
1033 | r.kvp = new KeyValuePair<string, int>("string key", 321);
|
---|
1034 | r.uninitialized = null;
|
---|
1035 |
|
---|
1036 | return r;
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | [TestMethod]
|
---|
1040 | [TestCategory("Persistence4")]
|
---|
1041 | [TestProperty("Time", "short")]
|
---|
1042 | public void SelfReferences() {
|
---|
1043 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1044 | C c = new C();
|
---|
1045 | C[][] cs = new C[2][];
|
---|
1046 | cs[0] = new C[] { c };
|
---|
1047 | cs[1] = new C[] { c };
|
---|
1048 | c.allCs = cs;
|
---|
1049 | c.kvpList = new KeyValuePair<List<C>, C>(new List<C> { c }, c);
|
---|
1050 | serializer.Serialize(cs, tempFile);
|
---|
1051 | object o = serializer.Deserialize(tempFile);
|
---|
1052 | Assert.AreEqual(
|
---|
1053 | DebugStringGenerator.Serialize(cs),
|
---|
1054 | DebugStringGenerator.Serialize(o));
|
---|
1055 | Assert.AreSame(c, c.allCs[0][0]);
|
---|
1056 | Assert.AreSame(c, c.allCs[1][0]);
|
---|
1057 | Assert.AreSame(c, c.kvpList.Key[0]);
|
---|
1058 | Assert.AreSame(c, c.kvpList.Value);
|
---|
1059 | C[][] newCs = (C[][])o;
|
---|
1060 | C newC = newCs[0][0];
|
---|
1061 | Assert.AreSame(newC, newC.allCs[0][0]);
|
---|
1062 | Assert.AreSame(newC, newC.allCs[1][0]);
|
---|
1063 | Assert.AreSame(newC, newC.kvpList.Key[0]);
|
---|
1064 | Assert.AreSame(newC, newC.kvpList.Value);
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | [TestMethod]
|
---|
1068 | [TestCategory("Persistence4")]
|
---|
1069 | [TestProperty("Time", "short")]
|
---|
1070 | public void ArrayCreation() {
|
---|
1071 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1072 | ArrayList[] arrayListArray = new ArrayList[4];
|
---|
1073 | arrayListArray[0] = new ArrayList();
|
---|
1074 | arrayListArray[0].Add(arrayListArray);
|
---|
1075 | arrayListArray[0].Add(arrayListArray);
|
---|
1076 | arrayListArray[1] = new ArrayList();
|
---|
1077 | arrayListArray[1].Add(arrayListArray);
|
---|
1078 | arrayListArray[2] = new ArrayList();
|
---|
1079 | arrayListArray[2].Add(arrayListArray);
|
---|
1080 | arrayListArray[2].Add(arrayListArray);
|
---|
1081 | //Array a = Array.CreateInstance(
|
---|
1082 | // typeof(object),
|
---|
1083 | // new[] { 1, 2 }, new[] { 3, 4 });
|
---|
1084 | //arrayListArray[2].Add(a);
|
---|
1085 | serializer.Serialize(arrayListArray, tempFile);
|
---|
1086 | object o = serializer.Deserialize(tempFile);
|
---|
1087 | Assert.AreEqual(
|
---|
1088 | DebugStringGenerator.Serialize(arrayListArray),
|
---|
1089 | DebugStringGenerator.Serialize(o));
|
---|
1090 | ArrayList[] newArray = (ArrayList[])o;
|
---|
1091 | Assert.AreSame(arrayListArray, arrayListArray[0][0]);
|
---|
1092 | Assert.AreSame(arrayListArray, arrayListArray[2][1]);
|
---|
1093 | Assert.AreSame(newArray, newArray[0][0]);
|
---|
1094 | Assert.AreSame(newArray, newArray[2][1]);
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | [TestMethod]
|
---|
1098 | [TestCategory("Persistence4")]
|
---|
1099 | [TestProperty("Time", "short")]
|
---|
1100 | public void CustomSerializationProperty() {
|
---|
1101 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1102 | Manager m = new Manager();
|
---|
1103 | serializer.Serialize(m, tempFile);
|
---|
1104 | Manager newM = (Manager)serializer.Deserialize(tempFile);
|
---|
1105 | Assert.AreNotEqual(
|
---|
1106 | DebugStringGenerator.Serialize(m),
|
---|
1107 | DebugStringGenerator.Serialize(newM));
|
---|
1108 | Assert.AreEqual(m.dbl, newM.dbl);
|
---|
1109 | Assert.AreEqual(m.lastLoadTime, new DateTime());
|
---|
1110 | Assert.AreNotEqual(newM.lastLoadTime, new DateTime());
|
---|
1111 | Assert.IsTrue((DateTime.Now - newM.lastLoadTime).TotalSeconds < 10);
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | [TestMethod]
|
---|
1115 | [TestCategory("Persistence4")]
|
---|
1116 | [TestProperty("Time", "short")]
|
---|
1117 | public void Primitives() {
|
---|
1118 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1119 | PrimitivesTest sdt = new PrimitivesTest();
|
---|
1120 | serializer.Serialize(sdt, tempFile);
|
---|
1121 | object o = serializer.Deserialize(tempFile);
|
---|
1122 | Assert.AreEqual(
|
---|
1123 | DebugStringGenerator.Serialize(sdt),
|
---|
1124 | DebugStringGenerator.Serialize(o));
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | [TestMethod]
|
---|
1128 | [TestCategory("Persistence4")]
|
---|
1129 | [TestProperty("Time", "short")]
|
---|
1130 | public void MultiDimensionalArray() {
|
---|
1131 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1132 | string[,] mDimString = new string[,] {
|
---|
1133 | {"ora", "et", "labora"},
|
---|
1134 | {"Beten", "und", "Arbeiten"}
|
---|
1135 | };
|
---|
1136 | serializer.Serialize(mDimString, tempFile);
|
---|
1137 | object o = serializer.Deserialize(tempFile);
|
---|
1138 | Assert.AreEqual(
|
---|
1139 | DebugStringGenerator.Serialize(mDimString),
|
---|
1140 | DebugStringGenerator.Serialize(o));
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | [StorableType("87A331AF-14DC-48B3-B577-D49065743BE6")]
|
---|
1144 | public class NestedType {
|
---|
1145 | [Storable]
|
---|
1146 | private string value = "value";
|
---|
1147 | public override bool Equals(object obj) {
|
---|
1148 | NestedType nt = obj as NestedType;
|
---|
1149 | if (nt == null)
|
---|
1150 | throw new NotSupportedException();
|
---|
1151 | return nt.value == value;
|
---|
1152 | }
|
---|
1153 | public override int GetHashCode() {
|
---|
1154 | return value.GetHashCode();
|
---|
1155 | }
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | [TestMethod]
|
---|
1159 | [TestCategory("Persistence4")]
|
---|
1160 | [TestProperty("Time", "short")]
|
---|
1161 | public void NestedTypeTest() {
|
---|
1162 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1163 | NestedType t = new NestedType();
|
---|
1164 | serializer.Serialize(t, tempFile);
|
---|
1165 | object o = serializer.Deserialize(tempFile);
|
---|
1166 | Assert.AreEqual(
|
---|
1167 | DebugStringGenerator.Serialize(t),
|
---|
1168 | DebugStringGenerator.Serialize(o));
|
---|
1169 | Assert.IsTrue(t.Equals(o));
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 |
|
---|
1173 | [TestMethod]
|
---|
1174 | [TestCategory("Persistence4")]
|
---|
1175 | [TestProperty("Time", "short")]
|
---|
1176 | public void SimpleArray() {
|
---|
1177 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1178 | string[] strings = { "ora", "et", "labora" };
|
---|
1179 | serializer.Serialize(strings, tempFile);
|
---|
1180 | object o = serializer.Deserialize(tempFile);
|
---|
1181 | Assert.AreEqual(
|
---|
1182 | DebugStringGenerator.Serialize(strings),
|
---|
1183 | DebugStringGenerator.Serialize(o));
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | [TestMethod]
|
---|
1187 | [TestCategory("Persistence4")]
|
---|
1188 | [TestProperty("Time", "short")]
|
---|
1189 | public void PrimitiveRoot() {
|
---|
1190 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1191 | serializer.Serialize(12.3f, tempFile);
|
---|
1192 | object o = serializer.Deserialize(tempFile);
|
---|
1193 | Assert.AreEqual(
|
---|
1194 | DebugStringGenerator.Serialize(12.3f),
|
---|
1195 | DebugStringGenerator.Serialize(o));
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | private string formatFullMemberName(MemberInfo mi) {
|
---|
1199 | return new StringBuilder()
|
---|
1200 | .Append(mi.DeclaringType.Assembly.GetName().Name)
|
---|
1201 | .Append(": ")
|
---|
1202 | .Append(mi.DeclaringType.Namespace)
|
---|
1203 | .Append('.')
|
---|
1204 | .Append(mi.DeclaringType.Name)
|
---|
1205 | .Append('.')
|
---|
1206 | .Append(mi.Name).ToString();
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | public void CodingConventions() {
|
---|
1210 | List<string> lowerCaseMethodNames = new List<string>();
|
---|
1211 | List<string> lowerCaseProperties = new List<string>();
|
---|
1212 | List<string> lowerCaseFields = new List<string>();
|
---|
1213 | foreach (Assembly a in PluginLoader.Assemblies) {
|
---|
1214 | if (!a.GetName().Name.StartsWith("HeuristicLab"))
|
---|
1215 | continue;
|
---|
1216 | foreach (Type t in a.GetTypes()) {
|
---|
1217 | foreach (MemberInfo mi in t.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) {
|
---|
1218 | if (mi.DeclaringType.Name.StartsWith("<>"))
|
---|
1219 | continue;
|
---|
1220 | if (char.IsLower(mi.Name[0])) {
|
---|
1221 | if (mi.MemberType == MemberTypes.Field)
|
---|
1222 | lowerCaseFields.Add(formatFullMemberName(mi));
|
---|
1223 | if (mi.MemberType == MemberTypes.Property)
|
---|
1224 | lowerCaseProperties.Add(formatFullMemberName(mi));
|
---|
1225 | if (mi.MemberType == MemberTypes.Method &&
|
---|
1226 | !mi.Name.StartsWith("get_") &&
|
---|
1227 | !mi.Name.StartsWith("set_") &&
|
---|
1228 | !mi.Name.StartsWith("add_") &&
|
---|
1229 | !mi.Name.StartsWith("remove_") &&
|
---|
1230 | !mi.Name.StartsWith("op_"))
|
---|
1231 | lowerCaseMethodNames.Add(formatFullMemberName(mi));
|
---|
1232 | }
|
---|
1233 | }
|
---|
1234 | }
|
---|
1235 | }
|
---|
1236 | //Assert.AreEqual("", lowerCaseFields.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
1237 | Assert.AreEqual("", lowerCaseMethodNames.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
1238 | Assert.AreEqual("", lowerCaseProperties.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | [TestMethod]
|
---|
1242 | [TestCategory("Persistence4")]
|
---|
1243 | [TestProperty("Time", "short")]
|
---|
1244 | public void Enums() {
|
---|
1245 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1246 | EnumTest et = new EnumTest();
|
---|
1247 | et.simpleEnum = SimpleEnum.two;
|
---|
1248 | et.complexEnum = ComplexEnum.three;
|
---|
1249 | et.trickyEnum = TrickyEnum.two | TrickyEnum.one;
|
---|
1250 | serializer.Serialize(et, tempFile);
|
---|
1251 | EnumTest newEt = (EnumTest)serializer.Deserialize(tempFile);
|
---|
1252 | Assert.AreEqual(et.simpleEnum, SimpleEnum.two);
|
---|
1253 | Assert.AreEqual(et.complexEnum, ComplexEnum.three);
|
---|
1254 | Assert.AreEqual(et.trickyEnum, (TrickyEnum)3);
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | [TestMethod]
|
---|
1258 | [TestCategory("Persistence4")]
|
---|
1259 | [TestProperty("Time", "short")]
|
---|
1260 | public void TestAliasingWithOverriddenEquals() {
|
---|
1261 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1262 | List<IntWrapper> ints = new List<IntWrapper>();
|
---|
1263 | ints.Add(new IntWrapper(1));
|
---|
1264 | ints.Add(new IntWrapper(1));
|
---|
1265 | Assert.AreEqual(ints[0], ints[1]);
|
---|
1266 | Assert.AreNotSame(ints[0], ints[1]);
|
---|
1267 | serializer.Serialize(ints, tempFile);
|
---|
1268 | List<IntWrapper> newInts = (List<IntWrapper>)serializer.Deserialize(tempFile);
|
---|
1269 | Assert.AreEqual(newInts[0].Value, 1);
|
---|
1270 | Assert.AreEqual(newInts[1].Value, 1);
|
---|
1271 | Assert.AreEqual(newInts[0], newInts[1]);
|
---|
1272 | Assert.AreNotSame(newInts[0], newInts[1]);
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | [TestMethod]
|
---|
1276 | [TestCategory("Persistence4")]
|
---|
1277 | [TestProperty("Time", "short")]
|
---|
1278 | public void NonDefaultConstructorTest() {
|
---|
1279 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1280 | NonDefaultConstructorClass c = new NonDefaultConstructorClass(1);
|
---|
1281 | try {
|
---|
1282 | serializer.Serialize(c, tempFile);
|
---|
1283 | Assert.Fail("Exception not thrown");
|
---|
1284 | } catch (PersistenceException) {
|
---|
1285 | }
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | [TestMethod]
|
---|
1289 | [TestCategory("Persistence4")]
|
---|
1290 | [TestProperty("Time", "short")]
|
---|
1291 | public void TestSavingException() {
|
---|
1292 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1293 | List<int> list = new List<int> { 1, 2, 3 };
|
---|
1294 | serializer.Serialize(list, tempFile);
|
---|
1295 | NonSerializable s = new NonSerializable();
|
---|
1296 | try {
|
---|
1297 | serializer.Serialize(s, tempFile);
|
---|
1298 | Assert.Fail("Exception expected");
|
---|
1299 | } catch (PersistenceException) { }
|
---|
1300 | List<int> newList = (List<int>)serializer.Deserialize(tempFile);
|
---|
1301 | Assert.AreEqual(list[0], newList[0]);
|
---|
1302 | Assert.AreEqual(list[1], newList[1]);
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | [TestMethod]
|
---|
1306 | [TestCategory("Persistence4")]
|
---|
1307 | [TestProperty("Time", "short")]
|
---|
1308 | public void TestTypeStringConversion() {
|
---|
1309 | string name = typeof(List<int>[]).AssemblyQualifiedName;
|
---|
1310 | string shortName =
|
---|
1311 | "System.Collections.Generic.List`1[[System.Int32, mscorlib]][], mscorlib";
|
---|
1312 | Assert.AreEqual(name, TypeNameParser.Parse(name).ToString());
|
---|
1313 | Assert.AreEqual(shortName, TypeNameParser.Parse(name).ToString(false));
|
---|
1314 | Assert.AreEqual(shortName, typeof(List<int>[]).VersionInvariantName());
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | [TestMethod]
|
---|
1318 | [TestCategory("Persistence4")]
|
---|
1319 | [TestProperty("Time", "short")]
|
---|
1320 | public void TestHexadecimalPublicKeyToken() {
|
---|
1321 | string name = "TestClass, TestAssembly, Version=1.2.3.4, PublicKey=1234abc";
|
---|
1322 | string shortName = "TestClass, TestAssembly";
|
---|
1323 | Assert.AreEqual(name, TypeNameParser.Parse(name).ToString());
|
---|
1324 | Assert.AreEqual(shortName, TypeNameParser.Parse(name).ToString(false));
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | [TestMethod]
|
---|
1328 | [TestCategory("Persistence4")]
|
---|
1329 | [TestProperty("Time", "short")]
|
---|
1330 | public void InheritanceTest() {
|
---|
1331 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1332 | New n = new New();
|
---|
1333 | serializer.Serialize(n, tempFile);
|
---|
1334 | New nn = (New)serializer.Deserialize(tempFile);
|
---|
1335 | Assert.AreEqual(n.Name, nn.Name);
|
---|
1336 | Assert.AreEqual(((Override)n).Name, ((Override)nn).Name);
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | [StorableType("B963EF51-12B4-432E-8C54-88F026F9ACE2")]
|
---|
1340 | class Child {
|
---|
1341 | [Storable]
|
---|
1342 | public GrandParent grandParent;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | [StorableType("E66E9606-967A-4C35-A361-F6F0D21C064A")]
|
---|
1346 | class Parent {
|
---|
1347 | [Storable]
|
---|
1348 | public Child child;
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | [StorableType("34D3893A-57AD-4F72-878B-81D6FA3F14A9")]
|
---|
1352 | class GrandParent {
|
---|
1353 | [Storable]
|
---|
1354 | public Parent parent;
|
---|
1355 | }
|
---|
1356 |
|
---|
1357 | [TestMethod]
|
---|
1358 | [TestCategory("Persistence4")]
|
---|
1359 | [TestProperty("Time", "short")]
|
---|
1360 | public void InstantiateParentChainReference() {
|
---|
1361 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1362 | GrandParent gp = new GrandParent();
|
---|
1363 | gp.parent = new Parent();
|
---|
1364 | gp.parent.child = new Child();
|
---|
1365 | gp.parent.child.grandParent = gp;
|
---|
1366 | Assert.AreSame(gp, gp.parent.child.grandParent);
|
---|
1367 | serializer.Serialize(gp, tempFile);
|
---|
1368 | GrandParent newGp = (GrandParent)serializer.Deserialize(tempFile);
|
---|
1369 | Assert.AreSame(newGp, newGp.parent.child.grandParent);
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | [StorableType("15DF777F-B12D-4FD4-88C3-8CB4C9CE4F0C")]
|
---|
1373 | struct TestStruct {
|
---|
1374 | int value;
|
---|
1375 | int PropertyValue { get; set; }
|
---|
1376 | public TestStruct(int value)
|
---|
1377 | : this() {
|
---|
1378 | this.value = value;
|
---|
1379 | PropertyValue = value;
|
---|
1380 | }
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | [TestMethod]
|
---|
1384 | [TestCategory("Persistence4")]
|
---|
1385 | [TestProperty("Time", "short")]
|
---|
1386 | public void StructTest() {
|
---|
1387 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1388 | TestStruct s = new TestStruct(10);
|
---|
1389 | serializer.Serialize(s, tempFile);
|
---|
1390 | TestStruct newS = (TestStruct)serializer.Deserialize(tempFile);
|
---|
1391 | Assert.AreEqual(s, newS);
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 | [TestMethod]
|
---|
1395 | [TestCategory("Persistence4")]
|
---|
1396 | [TestProperty("Time", "short")]
|
---|
1397 | public void PointTest() {
|
---|
1398 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1399 | Point p = new Point(12, 34);
|
---|
1400 | serializer.Serialize(p, tempFile);
|
---|
1401 | Point newP = (Point)serializer.Deserialize(tempFile);
|
---|
1402 | Assert.AreEqual(p, newP);
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 | [TestMethod]
|
---|
1406 | [TestCategory("Persistence4")]
|
---|
1407 | [TestProperty("Time", "short")]
|
---|
1408 | public void NullableValueTypes() {
|
---|
1409 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1410 | double?[] d = new double?[] { null, 1, 2, 3 };
|
---|
1411 | serializer.Serialize(d, tempFile);
|
---|
1412 | double?[] newD = (double?[])serializer.Deserialize(tempFile);
|
---|
1413 | Assert.AreEqual(d[0], newD[0]);
|
---|
1414 | Assert.AreEqual(d[1], newD[1]);
|
---|
1415 | Assert.AreEqual(d[2], newD[2]);
|
---|
1416 | Assert.AreEqual(d[3], newD[3]);
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | [TestMethod]
|
---|
1420 | [TestCategory("Persistence4")]
|
---|
1421 | [TestProperty("Time", "short")]
|
---|
1422 | public void BitmapTest() {
|
---|
1423 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1424 | Icon icon = System.Drawing.SystemIcons.Hand;
|
---|
1425 | Bitmap bitmap = icon.ToBitmap();
|
---|
1426 | serializer.Serialize(bitmap, tempFile);
|
---|
1427 | Bitmap newBitmap = (Bitmap)serializer.Deserialize(tempFile);
|
---|
1428 |
|
---|
1429 | Assert.AreEqual(bitmap.Size, newBitmap.Size);
|
---|
1430 | for (int i = 0; i < bitmap.Size.Width; i++)
|
---|
1431 | for (int j = 0; j < bitmap.Size.Height; j++)
|
---|
1432 | Assert.AreEqual(bitmap.GetPixel(i, j), newBitmap.GetPixel(i, j));
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 | [StorableType("E846BC49-20F3-4D3F-A3F3-73D4F2DB1C2E")]
|
---|
1436 | private class PersistenceHooks {
|
---|
1437 | [Storable]
|
---|
1438 | public int a;
|
---|
1439 | [Storable]
|
---|
1440 | public int b;
|
---|
1441 | public int sum;
|
---|
1442 | public bool WasSerialized { get; private set; }
|
---|
1443 | [StorableHook(HookType.BeforeSerialization)]
|
---|
1444 | void PreSerializationHook() {
|
---|
1445 | WasSerialized = true;
|
---|
1446 | }
|
---|
1447 | [StorableHook(HookType.AfterDeserialization)]
|
---|
1448 | void PostDeserializationHook() {
|
---|
1449 | sum = a + b;
|
---|
1450 | }
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | [TestMethod]
|
---|
1454 | [TestCategory("Persistence4")]
|
---|
1455 | [TestProperty("Time", "short")]
|
---|
1456 | public void HookTest() {
|
---|
1457 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1458 | PersistenceHooks hookTest = new PersistenceHooks();
|
---|
1459 | hookTest.a = 2;
|
---|
1460 | hookTest.b = 5;
|
---|
1461 | Assert.IsFalse(hookTest.WasSerialized);
|
---|
1462 | Assert.AreEqual(hookTest.sum, 0);
|
---|
1463 | serializer.Serialize(hookTest, tempFile);
|
---|
1464 | Assert.IsTrue(hookTest.WasSerialized);
|
---|
1465 | Assert.AreEqual(hookTest.sum, 0);
|
---|
1466 | PersistenceHooks newHookTest = (PersistenceHooks)serializer.Deserialize(tempFile);
|
---|
1467 | Assert.AreEqual(newHookTest.a, hookTest.a);
|
---|
1468 | Assert.AreEqual(newHookTest.b, hookTest.b);
|
---|
1469 | Assert.AreEqual(newHookTest.sum, newHookTest.a + newHookTest.b);
|
---|
1470 | Assert.IsFalse(newHookTest.WasSerialized);
|
---|
1471 | }
|
---|
1472 |
|
---|
1473 | [StorableType("A35D71DF-397F-4910-A950-ED6923BE9483")]
|
---|
1474 | private class CustomConstructor {
|
---|
1475 | public string Value = "none";
|
---|
1476 | public CustomConstructor() {
|
---|
1477 | Value = "default";
|
---|
1478 | }
|
---|
1479 | [StorableConstructor]
|
---|
1480 | private CustomConstructor(bool deserializing) {
|
---|
1481 | Assert.IsTrue(deserializing);
|
---|
1482 | Value = "persistence";
|
---|
1483 | }
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | [TestMethod]
|
---|
1487 | [TestCategory("Persistence4")]
|
---|
1488 | [TestProperty("Time", "short")]
|
---|
1489 | public void TestCustomConstructor() {
|
---|
1490 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1491 | CustomConstructor cc = new CustomConstructor();
|
---|
1492 | Assert.AreEqual(cc.Value, "default");
|
---|
1493 | serializer.Serialize(cc, tempFile);
|
---|
1494 | CustomConstructor newCC = (CustomConstructor)serializer.Deserialize(tempFile);
|
---|
1495 | Assert.AreEqual(newCC.Value, "persistence");
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 | [StorableType("D276E825-1F35-4BAC-8937-9ABC91D5C316")]
|
---|
1499 | public class ExplodingDefaultConstructor {
|
---|
1500 | public ExplodingDefaultConstructor() {
|
---|
1501 | throw new Exception("this constructor will always fail");
|
---|
1502 | }
|
---|
1503 | public ExplodingDefaultConstructor(string password) {
|
---|
1504 | }
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | [TestMethod]
|
---|
1508 | [TestCategory("Persistence4")]
|
---|
1509 | [TestProperty("Time", "short")]
|
---|
1510 | public void TestConstructorExceptionUnwrapping() {
|
---|
1511 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1512 | ExplodingDefaultConstructor x = new ExplodingDefaultConstructor("password");
|
---|
1513 | serializer.Serialize(x, tempFile);
|
---|
1514 | try {
|
---|
1515 | ExplodingDefaultConstructor newX = (ExplodingDefaultConstructor)serializer.Deserialize(tempFile);
|
---|
1516 | Assert.Fail("Exception expected");
|
---|
1517 | } catch (PersistenceException pe) {
|
---|
1518 | Assert.AreEqual(pe.InnerException.Message, "this constructor will always fail");
|
---|
1519 | }
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | [TestMethod]
|
---|
1523 | [TestCategory("Persistence4")]
|
---|
1524 | [TestProperty("Time", "short")]
|
---|
1525 | public void TestStreaming() {
|
---|
1526 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1527 | using (MemoryStream stream = new MemoryStream()) {
|
---|
1528 | Root r = InitializeComplexStorable();
|
---|
1529 | serializer.Serialize(r, stream);
|
---|
1530 | using (MemoryStream stream2 = new MemoryStream(stream.ToArray())) {
|
---|
1531 | Root newR = (Root)serializer.Deserialize(stream2);
|
---|
1532 | CompareComplexStorables(r, newR);
|
---|
1533 | }
|
---|
1534 | }
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | [StorableType("4921031B-CB61-4677-97AD-9236A4CEC200")]
|
---|
1538 | public class HookInheritanceTestBase {
|
---|
1539 | [Storable]
|
---|
1540 | public object a;
|
---|
1541 | public object link;
|
---|
1542 | [StorableHook(HookType.AfterDeserialization)]
|
---|
1543 | private void relink() {
|
---|
1544 | link = a;
|
---|
1545 | }
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | [StorableType("321CEE0A-5201-4CE2-B135-2343890D96BF")]
|
---|
1549 | public class HookInheritanceTestDerivedClass : HookInheritanceTestBase {
|
---|
1550 | [Storable]
|
---|
1551 | public object b;
|
---|
1552 | [StorableHook(HookType.AfterDeserialization)]
|
---|
1553 | private void relink() {
|
---|
1554 | Assert.AreSame(a, link);
|
---|
1555 | link = b;
|
---|
1556 | }
|
---|
1557 | }
|
---|
1558 |
|
---|
1559 | [TestMethod]
|
---|
1560 | [TestCategory("Persistence4")]
|
---|
1561 | [TestProperty("Time", "short")]
|
---|
1562 | public void TestLinkInheritance() {
|
---|
1563 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1564 | HookInheritanceTestDerivedClass c = new HookInheritanceTestDerivedClass();
|
---|
1565 | c.a = new object();
|
---|
1566 | serializer.Serialize(c, tempFile);
|
---|
1567 | HookInheritanceTestDerivedClass newC = (HookInheritanceTestDerivedClass)serializer.Deserialize(tempFile);
|
---|
1568 | Assert.AreSame(c.b, c.link);
|
---|
1569 | }
|
---|
1570 |
|
---|
1571 | [StorableType(StorableMemberSelection.AllFields, "B9AB42E8-1932-425B-B4CF-F31F07EAC599")]
|
---|
1572 | public class AllFieldsStorable {
|
---|
1573 | public int Value1 = 1;
|
---|
1574 | [Storable]
|
---|
1575 | public int Value2 = 2;
|
---|
1576 | public int Value3 { get; private set; }
|
---|
1577 | public int Value4 { get; private set; }
|
---|
1578 | [StorableConstructor]
|
---|
1579 | public AllFieldsStorable(bool isDeserializing) {
|
---|
1580 | if (!isDeserializing) {
|
---|
1581 | Value1 = 12;
|
---|
1582 | Value2 = 23;
|
---|
1583 | Value3 = 34;
|
---|
1584 | Value4 = 56;
|
---|
1585 | }
|
---|
1586 | }
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | [TestMethod]
|
---|
1590 | [TestCategory("Persistence4")]
|
---|
1591 | [TestProperty("Time", "short")]
|
---|
1592 | public void TestStorableTypeDiscoveryAllFields() {
|
---|
1593 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1594 | AllFieldsStorable afs = new AllFieldsStorable(false);
|
---|
1595 | serializer.Serialize(afs, tempFile);
|
---|
1596 | AllFieldsStorable newAfs = (AllFieldsStorable)serializer.Deserialize(tempFile);
|
---|
1597 | Assert.AreEqual(afs.Value1, newAfs.Value1);
|
---|
1598 | Assert.AreEqual(afs.Value2, newAfs.Value2);
|
---|
1599 | Assert.AreEqual(0, newAfs.Value3);
|
---|
1600 | Assert.AreEqual(0, newAfs.Value4);
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 | [StorableType(StorableMemberSelection.AllProperties, "CB7DC31C-AEF3-4EB8-91CA-248B767E9F92")]
|
---|
1604 | public class AllPropertiesStorable {
|
---|
1605 | public int Value1 = 1;
|
---|
1606 | [Storable]
|
---|
1607 | public int Value2 = 2;
|
---|
1608 | public int Value3 { get; private set; }
|
---|
1609 | public int Value4 { get; private set; }
|
---|
1610 | [StorableConstructor]
|
---|
1611 | public AllPropertiesStorable(bool isDeserializing) {
|
---|
1612 | if (!isDeserializing) {
|
---|
1613 | Value1 = 12;
|
---|
1614 | Value2 = 23;
|
---|
1615 | Value3 = 34;
|
---|
1616 | Value4 = 56;
|
---|
1617 | }
|
---|
1618 | }
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 | [TestMethod]
|
---|
1622 | [TestCategory("Persistence4")]
|
---|
1623 | [TestProperty("Time", "short")]
|
---|
1624 | public void TestStorableTypeDiscoveryAllProperties() {
|
---|
1625 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1626 | AllPropertiesStorable afs = new AllPropertiesStorable(false);
|
---|
1627 | serializer.Serialize(afs, tempFile);
|
---|
1628 | AllPropertiesStorable newAfs = (AllPropertiesStorable)serializer.Deserialize(tempFile);
|
---|
1629 | Assert.AreEqual(1, newAfs.Value1);
|
---|
1630 | Assert.AreEqual(2, newAfs.Value2);
|
---|
1631 | Assert.AreEqual(afs.Value3, newAfs.Value3);
|
---|
1632 | Assert.AreEqual(afs.Value4, newAfs.Value4);
|
---|
1633 |
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | [StorableType(StorableMemberSelection.AllFieldsAndAllProperties, "0AD8D68F-E0FF-4FA8-8A72-1148CD91A2B9")]
|
---|
1637 | public class AllFieldsAndAllPropertiesStorable {
|
---|
1638 | public int Value1 = 1;
|
---|
1639 | [Storable]
|
---|
1640 | public int Value2 = 2;
|
---|
1641 | public int Value3 { get; private set; }
|
---|
1642 | public int Value4 { get; private set; }
|
---|
1643 | [StorableConstructor]
|
---|
1644 | public AllFieldsAndAllPropertiesStorable(bool isDeserializing) {
|
---|
1645 | if (!isDeserializing) {
|
---|
1646 | Value1 = 12;
|
---|
1647 | Value2 = 23;
|
---|
1648 | Value3 = 34;
|
---|
1649 | Value4 = 56;
|
---|
1650 | }
|
---|
1651 | }
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | [TestMethod]
|
---|
1655 | [TestCategory("Persistence4")]
|
---|
1656 | [TestProperty("Time", "short")]
|
---|
1657 | public void TestStorableTypeDiscoveryAllFieldsAndAllProperties() {
|
---|
1658 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1659 | AllFieldsAndAllPropertiesStorable afs = new AllFieldsAndAllPropertiesStorable(false);
|
---|
1660 | serializer.Serialize(afs, tempFile);
|
---|
1661 | AllFieldsAndAllPropertiesStorable newAfs = (AllFieldsAndAllPropertiesStorable)serializer.Deserialize(tempFile);
|
---|
1662 | Assert.AreEqual(afs.Value1, newAfs.Value1);
|
---|
1663 | Assert.AreEqual(afs.Value2, newAfs.Value2);
|
---|
1664 | Assert.AreEqual(afs.Value3, newAfs.Value3);
|
---|
1665 | Assert.AreEqual(afs.Value4, newAfs.Value4);
|
---|
1666 | }
|
---|
1667 |
|
---|
1668 | [StorableType(StorableMemberSelection.MarkedOnly, "0D94E6D4-64E3-4637-B1EE-DEF2B3F6E2E0")]
|
---|
1669 | public class MarkedOnlyStorable {
|
---|
1670 | public int Value1 = 1;
|
---|
1671 | [Storable]
|
---|
1672 | public int Value2 = 2;
|
---|
1673 | public int Value3 { get; private set; }
|
---|
1674 | public int Value4 { get; private set; }
|
---|
1675 | [StorableConstructor]
|
---|
1676 | public MarkedOnlyStorable(bool isDeserializing) {
|
---|
1677 | if (!isDeserializing) {
|
---|
1678 | Value1 = 12;
|
---|
1679 | Value2 = 23;
|
---|
1680 | Value3 = 34;
|
---|
1681 | Value4 = 56;
|
---|
1682 | }
|
---|
1683 | }
|
---|
1684 | }
|
---|
1685 |
|
---|
1686 | [TestMethod]
|
---|
1687 | [TestCategory("Persistence4")]
|
---|
1688 | [TestProperty("Time", "short")]
|
---|
1689 | public void TestStorableTypeDiscoveryMarkedOnly() {
|
---|
1690 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1691 | MarkedOnlyStorable afs = new MarkedOnlyStorable(false);
|
---|
1692 | serializer.Serialize(afs, tempFile);
|
---|
1693 | MarkedOnlyStorable newAfs = (MarkedOnlyStorable)serializer.Deserialize(tempFile);
|
---|
1694 | Assert.AreEqual(1, newAfs.Value1);
|
---|
1695 | Assert.AreEqual(afs.Value2, newAfs.Value2);
|
---|
1696 | Assert.AreEqual(0, newAfs.Value3);
|
---|
1697 | Assert.AreEqual(0, newAfs.Value4);
|
---|
1698 | }
|
---|
1699 |
|
---|
1700 | [TestMethod]
|
---|
1701 | [TestCategory("Persistence4")]
|
---|
1702 | [TestProperty("Time", "short")]
|
---|
1703 | public void TestLineEndings() {
|
---|
1704 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1705 | List<string> lineBreaks = new List<string> { "\r\n", "\n", "\r", "\n\r", Environment.NewLine };
|
---|
1706 | List<string> lines = new List<string>();
|
---|
1707 | foreach (var br in lineBreaks)
|
---|
1708 | lines.Add("line1" + br + "line2");
|
---|
1709 | serializer.Serialize(lines, tempFile);
|
---|
1710 | List<string> newLines = (List<string>)serializer.Deserialize(tempFile);
|
---|
1711 | Assert.AreEqual(lines.Count, newLines.Count);
|
---|
1712 | for (int i = 0; i < lineBreaks.Count; i++) {
|
---|
1713 | Assert.AreEqual(lines[i], newLines[i]);
|
---|
1714 | }
|
---|
1715 | }
|
---|
1716 |
|
---|
1717 | [TestMethod]
|
---|
1718 | [TestCategory("Persistence4")]
|
---|
1719 | [TestProperty("Time", "short")]
|
---|
1720 | public void TestSpecialNumbers() {
|
---|
1721 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1722 | List<double> specials = new List<double>() { 1.0 / 0, -1.0 / 0, 0.0 / 0 };
|
---|
1723 | Assert.IsTrue(double.IsPositiveInfinity(specials[0]));
|
---|
1724 | Assert.IsTrue(double.IsNegativeInfinity(specials[1]));
|
---|
1725 | Assert.IsTrue(double.IsNaN(specials[2]));
|
---|
1726 | serializer.Serialize(specials, tempFile);
|
---|
1727 | List<double> newSpecials = (List<double>)serializer.Deserialize(tempFile);
|
---|
1728 | Assert.IsTrue(double.IsPositiveInfinity(newSpecials[0]));
|
---|
1729 | Assert.IsTrue(double.IsNegativeInfinity(newSpecials[1]));
|
---|
1730 | Assert.IsTrue(double.IsNaN(newSpecials[2]));
|
---|
1731 | }
|
---|
1732 |
|
---|
1733 | [TestMethod]
|
---|
1734 | [TestCategory("Persistence4")]
|
---|
1735 | [TestProperty("Time", "short")]
|
---|
1736 | public void TestStringSplit() {
|
---|
1737 | string s = "1.2;2.3;3.4;;;4.9";
|
---|
1738 | var l = s.EnumerateSplit(';').ToList();
|
---|
1739 | Assert.AreEqual("1.2", l[0]);
|
---|
1740 | Assert.AreEqual("2.3", l[1]);
|
---|
1741 | Assert.AreEqual("3.4", l[2]);
|
---|
1742 | Assert.AreEqual("4.9", l[3]);
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | [StorableType("B13CB1B0-D2DA-47B8-A715-B166A28B1F03")]
|
---|
1746 | private class IdentityComparer<T> : IEqualityComparer<T> {
|
---|
1747 |
|
---|
1748 | public bool Equals(T x, T y) {
|
---|
1749 | return x.Equals(y);
|
---|
1750 | }
|
---|
1751 |
|
---|
1752 | public int GetHashCode(T obj) {
|
---|
1753 | return obj.GetHashCode();
|
---|
1754 | }
|
---|
1755 | }
|
---|
1756 |
|
---|
1757 | [TestMethod]
|
---|
1758 | [TestCategory("Persistence4")]
|
---|
1759 | [TestProperty("Time", "short")]
|
---|
1760 | public void TestHashSetSerializer() {
|
---|
1761 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1762 | var hashSets = new List<HashSet<int>>() {
|
---|
1763 | new HashSet<int>(new[] { 1, 2, 3 }),
|
---|
1764 | new HashSet<int>(new[] { 4, 5, 6 }, new IdentityComparer<int>()),
|
---|
1765 | };
|
---|
1766 | serializer.Serialize(hashSets, tempFile);
|
---|
1767 | var newHashSets = (List<HashSet<int>>)serializer.Deserialize(tempFile);
|
---|
1768 | Assert.IsTrue(newHashSets[0].Contains(1));
|
---|
1769 | Assert.IsTrue(newHashSets[0].Contains(2));
|
---|
1770 | Assert.IsTrue(newHashSets[0].Contains(3));
|
---|
1771 | Assert.IsTrue(newHashSets[1].Contains(4));
|
---|
1772 | Assert.IsTrue(newHashSets[1].Contains(5));
|
---|
1773 | Assert.IsTrue(newHashSets[1].Contains(6));
|
---|
1774 | Assert.AreEqual(newHashSets[0].Comparer.GetType(), new HashSet<int>().Comparer.GetType());
|
---|
1775 | Assert.AreEqual(newHashSets[1].Comparer.GetType(), typeof(IdentityComparer<int>));
|
---|
1776 | }
|
---|
1777 |
|
---|
1778 | [TestMethod]
|
---|
1779 | [TestCategory("Persistence4")]
|
---|
1780 | [TestProperty("Time", "short")]
|
---|
1781 | public void TestConcreteDictionarySerializer() {
|
---|
1782 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1783 | var dictionaries = new List<Dictionary<int, int>>() {
|
---|
1784 | new Dictionary<int, int>(),
|
---|
1785 | new Dictionary<int, int>(new IdentityComparer<int>()),
|
---|
1786 | };
|
---|
1787 | dictionaries[0].Add(1, 1);
|
---|
1788 | dictionaries[0].Add(2, 2);
|
---|
1789 | dictionaries[0].Add(3, 3);
|
---|
1790 | dictionaries[1].Add(4, 4);
|
---|
1791 | dictionaries[1].Add(5, 5);
|
---|
1792 | dictionaries[1].Add(6, 6);
|
---|
1793 | serializer.Serialize(dictionaries, tempFile);
|
---|
1794 | var newDictionaries = (List<Dictionary<int, int>>)serializer.Deserialize(tempFile);
|
---|
1795 | Assert.IsTrue(newDictionaries[0].ContainsKey(1));
|
---|
1796 | Assert.IsTrue(newDictionaries[0].ContainsKey(2));
|
---|
1797 | Assert.IsTrue(newDictionaries[0].ContainsKey(3));
|
---|
1798 | Assert.IsTrue(newDictionaries[1].ContainsKey(4));
|
---|
1799 | Assert.IsTrue(newDictionaries[1].ContainsKey(5));
|
---|
1800 | Assert.IsTrue(newDictionaries[1].ContainsKey(6));
|
---|
1801 | Assert.IsTrue(newDictionaries[0].ContainsValue(1));
|
---|
1802 | Assert.IsTrue(newDictionaries[0].ContainsValue(2));
|
---|
1803 | Assert.IsTrue(newDictionaries[0].ContainsValue(3));
|
---|
1804 | Assert.IsTrue(newDictionaries[1].ContainsValue(4));
|
---|
1805 | Assert.IsTrue(newDictionaries[1].ContainsValue(5));
|
---|
1806 | Assert.IsTrue(newDictionaries[1].ContainsValue(6));
|
---|
1807 | Assert.AreEqual(new Dictionary<int, int>().Comparer.GetType(), newDictionaries[0].Comparer.GetType());
|
---|
1808 | Assert.AreEqual(typeof(IdentityComparer<int>), newDictionaries[1].Comparer.GetType());
|
---|
1809 | }
|
---|
1810 |
|
---|
1811 | [StorableType("A9B0D7FB-0CAF-4DD7-9045-EA136F9176F7")]
|
---|
1812 | public class ReadOnlyFail {
|
---|
1813 | [Storable]
|
---|
1814 | public string ReadOnly {
|
---|
1815 | get { return "fail"; }
|
---|
1816 | }
|
---|
1817 | }
|
---|
1818 |
|
---|
1819 | [TestMethod]
|
---|
1820 | [TestCategory("Persistence4")]
|
---|
1821 | [TestProperty("Time", "short")]
|
---|
1822 | public void TestReadOnlyFail() {
|
---|
1823 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1824 | try {
|
---|
1825 | serializer.Serialize(new ReadOnlyFail(), tempFile);
|
---|
1826 | Assert.Fail("Exception expected");
|
---|
1827 | } catch (PersistenceException) {
|
---|
1828 | } catch {
|
---|
1829 | Assert.Fail("PersistenceException expected");
|
---|
1830 | }
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 |
|
---|
1834 | [StorableType("2C9CC576-6823-4784-817B-37C8AF0B1C29")]
|
---|
1835 | public class WriteOnlyFail {
|
---|
1836 | [Storable]
|
---|
1837 | public string WriteOnly {
|
---|
1838 | set { throw new InvalidOperationException("this property should never be set."); }
|
---|
1839 | }
|
---|
1840 | }
|
---|
1841 |
|
---|
1842 | [TestMethod]
|
---|
1843 | [TestCategory("Persistence4")]
|
---|
1844 | [TestProperty("Time", "short")]
|
---|
1845 | public void TestWriteOnlyFail() {
|
---|
1846 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1847 | try {
|
---|
1848 | serializer.Serialize(new WriteOnlyFail(), tempFile);
|
---|
1849 | Assert.Fail("Exception expected");
|
---|
1850 | } catch (PersistenceException) {
|
---|
1851 | } catch {
|
---|
1852 | Assert.Fail("PersistenceException expected.");
|
---|
1853 | }
|
---|
1854 | }
|
---|
1855 |
|
---|
1856 | [StorableType("8052D9E3-6DDD-4AE1-9B5B-67C6D5436512")]
|
---|
1857 | public class OneWayTest {
|
---|
1858 | public OneWayTest() { this.value = "default"; }
|
---|
1859 | public string value;
|
---|
1860 | [Storable(AllowOneWay = true)]
|
---|
1861 | public string ReadOnly {
|
---|
1862 | get { return "ReadOnly"; }
|
---|
1863 | }
|
---|
1864 | [Storable(AllowOneWay = true)]
|
---|
1865 | public string WriteOnly {
|
---|
1866 | set { this.value = value; }
|
---|
1867 | }
|
---|
1868 | }
|
---|
1869 |
|
---|
1870 | //TODO
|
---|
1871 | /* [TestMethod]
|
---|
1872 | [TestCategory("Persistence4")]
|
---|
1873 | [TestProperty("Time", "short")]
|
---|
1874 | public void TestTypeCacheExport() {
|
---|
1875 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1876 | var test = new List<List<int>>();
|
---|
1877 | test.Add(new List<int>() { 1, 2, 3 });
|
---|
1878 | IEnumerable<Type> types;
|
---|
1879 | using (var stream = new MemoryStream()) {
|
---|
1880 | XmlGenerator.Serialize(test, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, out types);
|
---|
1881 | }
|
---|
1882 | List<Type> t = new List<Type>(types);
|
---|
1883 | // Assert.IsTrue(t.Contains(typeof(int))); not serialized as an int list is directly transformed into a string
|
---|
1884 | Assert.IsTrue(t.Contains(typeof(List<int>)));
|
---|
1885 | Assert.IsTrue(t.Contains(typeof(List<List<int>>)));
|
---|
1886 | Assert.AreEqual(t.Count, 2);
|
---|
1887 | }*/
|
---|
1888 |
|
---|
1889 | [TestMethod]
|
---|
1890 | [TestCategory("Persistence4")]
|
---|
1891 | [TestProperty("Time", "short")]
|
---|
1892 | public void TupleTest() {
|
---|
1893 | var t1 = Tuple.Create(1);
|
---|
1894 | var t2 = Tuple.Create('1', "2");
|
---|
1895 | var t3 = Tuple.Create(3.0, 3f, 5);
|
---|
1896 | var t4 = Tuple.Create(Tuple.Create(1, 2, 3), Tuple.Create(4, 5, 6), Tuple.Create(8, 9, 10));
|
---|
1897 | var tuple = Tuple.Create(t1, t2, t3, t4);
|
---|
1898 | SerializeNew(tuple);
|
---|
1899 | var newTuple = DeserializeNew();
|
---|
1900 | Assert.AreEqual(tuple, newTuple);
|
---|
1901 | }
|
---|
1902 |
|
---|
1903 | [TestMethod]
|
---|
1904 | [TestCategory("Persistence4")]
|
---|
1905 | [TestProperty("Time", "short")]
|
---|
1906 | public void FontTest() {
|
---|
1907 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1908 | List<Font> fonts = new List<Font>() {
|
---|
1909 | new Font(FontFamily.GenericSansSerif, 12),
|
---|
1910 | new Font("Times New Roman", 21, FontStyle.Bold, GraphicsUnit.Pixel),
|
---|
1911 | new Font("Courier New", 10, FontStyle.Underline, GraphicsUnit.Document),
|
---|
1912 | new Font("Helvetica", 21, FontStyle.Strikeout, GraphicsUnit.Inch, 0, true),
|
---|
1913 | };
|
---|
1914 | serializer.Serialize(fonts, tempFile);
|
---|
1915 | var newFonts = (List<Font>)serializer.Deserialize(tempFile);
|
---|
1916 | Assert.AreEqual(fonts[0], newFonts[0]);
|
---|
1917 | Assert.AreEqual(fonts[1], newFonts[1]);
|
---|
1918 | Assert.AreEqual(fonts[2], newFonts[2]);
|
---|
1919 | Assert.AreEqual(fonts[3], newFonts[3]);
|
---|
1920 | }
|
---|
1921 |
|
---|
1922 | [TestMethod]
|
---|
1923 | [TestCategory("Persistence4")]
|
---|
1924 | [TestProperty("Time", "medium")]
|
---|
1925 | public void ConcurrencyTest() {
|
---|
1926 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1927 | int n = 20;
|
---|
1928 | Task[] tasks = new Task[n];
|
---|
1929 | for (int i = 0; i < n; i++) {
|
---|
1930 | tasks[i] = Task.Factory.StartNew((idx) => {
|
---|
1931 | byte[] data;
|
---|
1932 | using (var stream = new MemoryStream()) {
|
---|
1933 | serializer.Serialize(new GeneticAlgorithm(), stream);
|
---|
1934 | data = stream.ToArray();
|
---|
1935 | }
|
---|
1936 | }, i);
|
---|
1937 | }
|
---|
1938 | Task.WaitAll(tasks);
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | [TestMethod]
|
---|
1942 | [TestCategory("Persistence4")]
|
---|
1943 | [TestProperty("Time", "medium")]
|
---|
1944 | public void ConcurrentBitmapTest() {
|
---|
1945 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1946 | Bitmap b = new Bitmap(300, 300);
|
---|
1947 | System.Random r = new System.Random();
|
---|
1948 | for (int x = 0; x < b.Height; x++) {
|
---|
1949 | for (int y = 0; y < b.Width; y++) {
|
---|
1950 | b.SetPixel(x, y, Color.FromArgb(r.Next()));
|
---|
1951 | }
|
---|
1952 | }
|
---|
1953 | Task[] tasks = new Task[20];
|
---|
1954 | byte[][] datas = new byte[tasks.Length][];
|
---|
1955 | for (int i = 0; i < tasks.Length; i++) {
|
---|
1956 | tasks[i] = Task.Factory.StartNew((idx) => {
|
---|
1957 | using (var stream = new MemoryStream()) {
|
---|
1958 | serializer.Serialize(b, stream);
|
---|
1959 | datas[(int)idx] = stream.ToArray();
|
---|
1960 | }
|
---|
1961 | }, i);
|
---|
1962 | }
|
---|
1963 | Task.WaitAll(tasks);
|
---|
1964 | }
|
---|
1965 |
|
---|
1966 | public class G<T, T2> {
|
---|
1967 | public class S { }
|
---|
1968 | public class S2<T3, T4> { }
|
---|
1969 | }
|
---|
1970 |
|
---|
1971 |
|
---|
1972 | [TestMethod]
|
---|
1973 | [TestCategory("Persistence4")]
|
---|
1974 | [TestProperty("Time", "short")]
|
---|
1975 | public void TestSpecialCharacters() {
|
---|
1976 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1977 | var s = "abc" + "\x15" + "def";
|
---|
1978 | serializer.Serialize(s, tempFile);
|
---|
1979 | var newS = serializer.Deserialize(tempFile);
|
---|
1980 | Assert.AreEqual(s, newS);
|
---|
1981 | }
|
---|
1982 |
|
---|
1983 | [TestMethod]
|
---|
1984 | [TestCategory("Persistence4")]
|
---|
1985 | [TestProperty("Time", "short")]
|
---|
1986 | public void TestByteArray() {
|
---|
1987 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
1988 | var b = new byte[3];
|
---|
1989 | b[0] = 0;
|
---|
1990 | b[1] = 200;
|
---|
1991 | b[2] = byte.MaxValue;
|
---|
1992 | serializer.Serialize(b, tempFile);
|
---|
1993 | var newB = (byte[])serializer.Deserialize(tempFile);
|
---|
1994 | CollectionAssert.AreEqual(b, newB);
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | [TestMethod]
|
---|
1998 | [TestCategory("Persistence4")]
|
---|
1999 | [TestProperty("Time", "short")]
|
---|
2000 | public void TestOptionalNumberEnumerable() {
|
---|
2001 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
2002 | var values = new List<double?> { 0, null, double.NaN, double.PositiveInfinity, double.MaxValue, 1 };
|
---|
2003 | serializer.Serialize(values, tempFile);
|
---|
2004 | var newValues = (List<double?>)serializer.Deserialize(tempFile);
|
---|
2005 | CollectionAssert.AreEqual(values, newValues);
|
---|
2006 | }
|
---|
2007 |
|
---|
2008 | [TestMethod]
|
---|
2009 | [TestCategory("Persistence4")]
|
---|
2010 | [TestProperty("Time", "short")]
|
---|
2011 | public void TestOptionalDateTimeEnumerable() {
|
---|
2012 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
2013 | var values = new List<DateTime?> { DateTime.MinValue, null, DateTime.Now, DateTime.Now.Add(TimeSpan.FromDays(1)),
|
---|
2014 | DateTime.ParseExact("10.09.2014 12:21", "dd.MM.yyyy hh:mm", CultureInfo.InvariantCulture), DateTime.MaxValue};
|
---|
2015 | serializer.Serialize(values, tempFile);
|
---|
2016 | var newValues = (List<DateTime?>)serializer.Deserialize(tempFile);
|
---|
2017 | CollectionAssert.AreEqual(values, newValues);
|
---|
2018 | }
|
---|
2019 |
|
---|
2020 | [TestMethod]
|
---|
2021 | [TestCategory("Persistence4")]
|
---|
2022 | [TestProperty("Time", "short")]
|
---|
2023 | public void TestStringEnumerable() {
|
---|
2024 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
2025 | var values = new List<string> { "", null, "s", "string", string.Empty, "123", "<![CDATA[nice]]>", "<![CDATA[nasty unterminated" };
|
---|
2026 | serializer.Serialize(values, tempFile);
|
---|
2027 | var newValues = (List<String>)serializer.Deserialize(tempFile);
|
---|
2028 | CollectionAssert.AreEqual(values, newValues);
|
---|
2029 | }
|
---|
2030 |
|
---|
2031 | [TestMethod]
|
---|
2032 | [TestCategory("Persistence4")]
|
---|
2033 | [TestProperty("Time", "short")]
|
---|
2034 | public void TestUnicodeCharArray() {
|
---|
2035 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
2036 | var s = Encoding.UTF8.GetChars(new byte[] { 0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb });
|
---|
2037 | serializer.Serialize(s, tempFile);
|
---|
2038 | var newS = (char[])serializer.Deserialize(tempFile);
|
---|
2039 | CollectionAssert.AreEqual(s, newS);
|
---|
2040 | }
|
---|
2041 |
|
---|
2042 | [TestMethod]
|
---|
2043 | [TestCategory("Persistence4")]
|
---|
2044 | [TestProperty("Time", "short")]
|
---|
2045 | public void TestUnicode() {
|
---|
2046 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
2047 | var s = Encoding.UTF8.GetString(new byte[] { 0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb });
|
---|
2048 | serializer.Serialize(s, tempFile);
|
---|
2049 | var newS = serializer.Deserialize(tempFile);
|
---|
2050 | Assert.AreEqual(s, newS);
|
---|
2051 | }
|
---|
2052 |
|
---|
2053 | [TestMethod]
|
---|
2054 | [TestCategory("Persistence4")]
|
---|
2055 | [TestProperty("Time", "short")]
|
---|
2056 | public void TestQueue() {
|
---|
2057 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
2058 | var q = new Queue<int>(new[] { 1, 2, 3, 4, 0 });
|
---|
2059 | serializer.Serialize(q, tempFile);
|
---|
2060 | var newQ = (Queue<int>)serializer.Deserialize(tempFile);
|
---|
2061 | CollectionAssert.AreEqual(q, newQ);
|
---|
2062 | }
|
---|
2063 | #endregion
|
---|
2064 |
|
---|
2065 | [StorableType("6075F1E8-948A-4AD8-8F5A-942B777852EC")]
|
---|
2066 | public class A {
|
---|
2067 | [Storable]
|
---|
2068 | public B B { get; set; }
|
---|
2069 |
|
---|
2070 | [Storable]
|
---|
2071 | public int i;
|
---|
2072 | }
|
---|
2073 | [StorableType("287BFEA0-6E27-4839-BCEF-D134FE738AC8")]
|
---|
2074 | public class B {
|
---|
2075 | [Storable]
|
---|
2076 | public A A { get; set; }
|
---|
2077 |
|
---|
2078 | [StorableHook(HookType.AfterDeserialization)]
|
---|
2079 | void PostDeserializationHook() {
|
---|
2080 | //Assert.AreEqual(3, A.i);
|
---|
2081 | }
|
---|
2082 | }
|
---|
2083 |
|
---|
2084 | [TestMethod]
|
---|
2085 | [TestCategory("Persistence4")]
|
---|
2086 | [TestProperty("Time", "short")]
|
---|
2087 | public void TestCyclicReferencesWithTuple() {
|
---|
2088 | var test = new Func<A>(() => {
|
---|
2089 | var a = new A { i = 4 };
|
---|
2090 | var b = new B { A = a };
|
---|
2091 | a.B = b;
|
---|
2092 | return a;
|
---|
2093 | });
|
---|
2094 |
|
---|
2095 | //ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
2096 | //serializer.Serialize(test(), tempFile);
|
---|
2097 | //object o = serializer.Deserialize(tempFile);
|
---|
2098 | //A result = (A)o;
|
---|
2099 |
|
---|
2100 | XmlGenerator.Serialize(test(), tempFile);
|
---|
2101 | object o = XmlParser.Deserialize(tempFile);
|
---|
2102 |
|
---|
2103 | string msg = Profile(test);
|
---|
2104 | Console.WriteLine(msg);
|
---|
2105 | }
|
---|
2106 |
|
---|
2107 |
|
---|
2108 | #region conversion
|
---|
2109 |
|
---|
2110 | [StorableType("F9F51075-490C-48E3-BF64-14514A210149", 1)]
|
---|
2111 | private class OldBaseType {
|
---|
2112 | [Storable]
|
---|
2113 | public ItemCollection<IItem> items;
|
---|
2114 | }
|
---|
2115 | [StorableType("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 1)]
|
---|
2116 | private class V1 : OldBaseType {
|
---|
2117 | [Storable]
|
---|
2118 | public IntValue a;
|
---|
2119 |
|
---|
2120 | [Storable]
|
---|
2121 | public ItemList<IntValue> vals;
|
---|
2122 |
|
---|
2123 | [Storable]
|
---|
2124 | public V1 mySelf;
|
---|
2125 |
|
---|
2126 | [Storable]
|
---|
2127 | public Tuple<int, int> tup;
|
---|
2128 |
|
---|
2129 | [Storable]
|
---|
2130 | public int x;
|
---|
2131 | [Storable]
|
---|
2132 | public int y;
|
---|
2133 | }
|
---|
2134 |
|
---|
2135 |
|
---|
2136 | [StorableType("B72C58C8-3321-4706-AA94-578F57337070", 2)]
|
---|
2137 | private class NewBaseType {
|
---|
2138 | [Storable]
|
---|
2139 | public DoubleValue[] items;
|
---|
2140 | }
|
---|
2141 | [StorableType("00000000-0000-0000-0000-BADCAFFEE000", 2)] // for testing (version 2)
|
---|
2142 | private partial class V2 : NewBaseType {
|
---|
2143 | [Storable]
|
---|
2144 | public int a;
|
---|
2145 |
|
---|
2146 | [Storable]
|
---|
2147 | public int[] val;
|
---|
2148 |
|
---|
2149 | [Storable]
|
---|
2150 | public V2 mySelf;
|
---|
2151 |
|
---|
2152 | [Storable]
|
---|
2153 | public int TupItem1;
|
---|
2154 |
|
---|
2155 | [Storable]
|
---|
2156 | public int TupItem2;
|
---|
2157 |
|
---|
2158 | [Storable]
|
---|
2159 | public Point coords;
|
---|
2160 | }
|
---|
2161 |
|
---|
2162 | // conversion part
|
---|
2163 | private partial class V2 : NewBaseType {
|
---|
2164 | [StorableConversion(srcVersion: 1)]
|
---|
2165 | private static Dictionary<string, object> ConvertV1(Dictionary<string, object> values) {
|
---|
2166 | var newValues = new Dictionary<string, object>();
|
---|
2167 | var items = (ItemCollection<IItem>)values["OldBaseType.items"];
|
---|
2168 | newValues["NewBaseType.items"] = items.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray();
|
---|
2169 | newValues["V2.a"] = ((IntValue)values["V1.a"]).Value;
|
---|
2170 | newValues["V2.val"] = ((ItemList<IntValue>)values["V1.vals"]).Select(iv => iv.Value).ToArray();
|
---|
2171 |
|
---|
2172 | newValues["V2.mySelf"] = values["V1.mySelf"]; // myself type will be mapped correctly
|
---|
2173 |
|
---|
2174 | var tup = (Tuple<int, int>)values["V1.tup"];
|
---|
2175 | if (tup != null) {
|
---|
2176 | newValues["V2.TupItem1"] = tup.Item1;
|
---|
2177 | newValues["V2.TupItem2"] = tup.Item2;
|
---|
2178 | }
|
---|
2179 |
|
---|
2180 | newValues["V2.coords"] = new Point((int)values["V1.x"], (int)values["V1.y"]);
|
---|
2181 |
|
---|
2182 | return newValues;
|
---|
2183 | }
|
---|
2184 | }
|
---|
2185 |
|
---|
2186 | [StorableType("00000000-0000-0000-0000-BADCAFFEE200", 3)] // for testing (version 3)
|
---|
2187 | private partial class V3 : NewBaseType {
|
---|
2188 | [Storable]
|
---|
2189 | public int a;
|
---|
2190 |
|
---|
2191 | [Storable]
|
---|
2192 | public int[] val;
|
---|
2193 |
|
---|
2194 | [Storable]
|
---|
2195 | public V3 mySelf;
|
---|
2196 |
|
---|
2197 | [Storable]
|
---|
2198 | public Tuple<int, int> tup;
|
---|
2199 |
|
---|
2200 | [Storable]
|
---|
2201 | public Point coords;
|
---|
2202 | }
|
---|
2203 |
|
---|
2204 | // conversion part
|
---|
2205 | private partial class V3 {
|
---|
2206 | [StorableConversion(srcVersion: 1)]
|
---|
2207 | private static Dictionary<string, object> ConvertV1(Dictionary<string, object> values) {
|
---|
2208 | var newValues = new Dictionary<string, object>();
|
---|
2209 | var items = (ItemCollection<IItem>)values["OldBaseType.items"];
|
---|
2210 | newValues["NewBaseType.items"] = items.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray();
|
---|
2211 | newValues["V2.a"] = ((IntValue)values["V1.a"]).Value;
|
---|
2212 | newValues["V2.val"] = ((ItemList<IntValue>)values["V1.vals"]).Select(iv => iv.Value).ToArray();
|
---|
2213 |
|
---|
2214 | newValues["V2.mySelf"] = values["V1.mySelf"]; // myself type will be mapped correctly
|
---|
2215 |
|
---|
2216 | var tup = (Tuple<int, int>)values["V1.tup"];
|
---|
2217 | if (tup != null) {
|
---|
2218 | newValues["V2.TupItem1"] = tup.Item1;
|
---|
2219 | newValues["V2.TupItem2"] = tup.Item2;
|
---|
2220 | }
|
---|
2221 |
|
---|
2222 | newValues["V2.coords"] = new Point((int)values["V1.x"], (int)values["V1.y"]);
|
---|
2223 |
|
---|
2224 | return newValues;
|
---|
2225 | }
|
---|
2226 |
|
---|
2227 | [StorableConversion(srcVersion: 2)]
|
---|
2228 | private static Dictionary<string, object> ConvertV2(Dictionary<string, object> values) {
|
---|
2229 | var newValues = new Dictionary<string, object>();
|
---|
2230 | newValues["NewBaseType.items"] = values["NewBaseType.items"];
|
---|
2231 | newValues["V3.a"] = values["V2.a"];
|
---|
2232 | newValues["V3.val"] = values["V2.val"];
|
---|
2233 | newValues["V3.mySelf"] = values["V2.mySelf"];
|
---|
2234 |
|
---|
2235 | newValues["V3.tup"] = Tuple.Create((int)values["V2.TupItem1"], (int)values["V2.TupItem2"]);
|
---|
2236 | newValues["V3.coords"] = values["V2.coords"];
|
---|
2237 |
|
---|
2238 | return newValues;
|
---|
2239 | }
|
---|
2240 | }
|
---|
2241 |
|
---|
2242 | [TestMethod]
|
---|
2243 | [TestCategory("Persistence4")]
|
---|
2244 | [TestProperty("Time", "short")]
|
---|
2245 | public void TestConversion() {
|
---|
2246 | var test = new Func<V1>(() => {
|
---|
2247 | var p = new V1();
|
---|
2248 | p.a = new IntValue(1);
|
---|
2249 | p.mySelf = p;
|
---|
2250 | p.vals = new ItemList<IntValue>(new IntValue[] { p.a, new IntValue(2), new IntValue(3) });
|
---|
2251 | p.tup = Tuple.Create(17, 4);
|
---|
2252 | var dv = new DoubleValue(1.0);
|
---|
2253 | p.items = new ItemCollection<IItem>(new IItem[] { dv, dv, p.a });
|
---|
2254 | return p;
|
---|
2255 | });
|
---|
2256 |
|
---|
2257 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
2258 | var old = test();
|
---|
2259 | serializer.Serialize(old, tempFile);
|
---|
2260 | Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid);
|
---|
2261 | Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid);
|
---|
2262 |
|
---|
2263 | Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid, typeof(V2));
|
---|
2264 |
|
---|
2265 | object o = serializer.Deserialize(tempFile);
|
---|
2266 | var restored = (V2)o;
|
---|
2267 | Assert.AreEqual(restored.a, old.a.Value);
|
---|
2268 | Assert.IsTrue(restored.val.SequenceEqual(old.vals.Select(iv => iv.Value)));
|
---|
2269 | Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value)));
|
---|
2270 | // Assert.AreSame(restored.items[0], restored.items[1]);
|
---|
2271 | Assert.AreSame(restored, restored.mySelf);
|
---|
2272 |
|
---|
2273 | //string msg = Profile(test);
|
---|
2274 | //Console.WriteLine(msg);
|
---|
2275 | }
|
---|
2276 |
|
---|
2277 | [TestMethod]
|
---|
2278 | [TestCategory("Persistence4")]
|
---|
2279 | [TestProperty("Time", "short")]
|
---|
2280 | public void TestConversion2() {
|
---|
2281 | var test = new Func<V2>(() => {
|
---|
2282 | var p = new V2();
|
---|
2283 | p.a = 1;
|
---|
2284 | p.mySelf = p;
|
---|
2285 | p.val = new int[] {2, 3, 4};
|
---|
2286 | p.TupItem1 = 17;
|
---|
2287 | p.TupItem2 = 4;
|
---|
2288 | p.items = new DoubleValue[] {new DoubleValue(1.0), new DoubleValue(2.0) };
|
---|
2289 | return p;
|
---|
2290 | });
|
---|
2291 |
|
---|
2292 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
2293 | var old = test();
|
---|
2294 | serializer.Serialize(old, tempFile);
|
---|
2295 | Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid);
|
---|
2296 | Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid);
|
---|
2297 | Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V3)).Guid);
|
---|
2298 |
|
---|
2299 | Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid, typeof(V3));
|
---|
2300 |
|
---|
2301 | object o = serializer.Deserialize(tempFile);
|
---|
2302 | var restored = (V3)o;
|
---|
2303 | Assert.AreEqual(restored.a, old.a);
|
---|
2304 | Assert.IsTrue(restored.val.SequenceEqual(old.val));
|
---|
2305 | Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value)));
|
---|
2306 | // Assert.AreSame(restored.items[0], restored.items[1]);
|
---|
2307 | Assert.AreSame(restored, restored.mySelf);
|
---|
2308 |
|
---|
2309 | //string msg = Profile(test);
|
---|
2310 | //Console.WriteLine(msg);
|
---|
2311 | }
|
---|
2312 |
|
---|
2313 | #endregion
|
---|
2314 |
|
---|
2315 | [TestMethod]
|
---|
2316 | [TestCategory("Persistence4")]
|
---|
2317 | [TestProperty("Time", "short")]
|
---|
2318 | public void TestGASerializeDeserializeExecute() {
|
---|
2319 | var test = new Func<GeneticAlgorithm>(() => {
|
---|
2320 | var ga = new GeneticAlgorithm();
|
---|
2321 | ga.Problem = new SingleObjectiveTestFunctionProblem();
|
---|
2322 | ga.MaximumGenerations.Value = 100;
|
---|
2323 | ga.SetSeedRandomly.Value = false;
|
---|
2324 | return ga;
|
---|
2325 | });
|
---|
2326 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
2327 | serializer.Serialize(test(), tempFile);
|
---|
2328 | object o = serializer.Deserialize(tempFile);
|
---|
2329 | GeneticAlgorithm result = (GeneticAlgorithm)o;
|
---|
2330 | SamplesUtils.RunAlgorithm(result);
|
---|
2331 | GeneticAlgorithm original = test();
|
---|
2332 | SamplesUtils.RunAlgorithm(original);
|
---|
2333 | //Assert.AreEqual(original.Results[""], result);
|
---|
2334 |
|
---|
2335 | //string msg = Profile(test);
|
---|
2336 | //Console.WriteLine(msg);
|
---|
2337 | }
|
---|
2338 | [TestMethod]
|
---|
2339 | [TestCategory("Persistence4")]
|
---|
2340 | [TestProperty("Time", "short")]
|
---|
2341 | public void TestLoadingSamples() {
|
---|
2342 | var path = @"D:\hl\branches\PersistenceOverhaul\HeuristicLab.Optimizer\3.3\Documents";
|
---|
2343 | var serializer = new ProtoBufSerializer();
|
---|
2344 | foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) {
|
---|
2345 | var original = XmlParser.Deserialize(fileName);
|
---|
2346 | var ok = true;
|
---|
2347 | // foreach (var t in original.GetObjectGraphObjects().Select(o => o.GetType())) {
|
---|
2348 | // if (
|
---|
2349 | // t.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
|
---|
2350 | // .Any(ctor => StorableConstructorAttribute.IsStorableConstructor(ctor))) {
|
---|
2351 | // try {
|
---|
2352 | // if (t.IsGenericType) {
|
---|
2353 | // var g = Mapper.StaticCache.GetGuid(t.GetGenericTypeDefinition());
|
---|
2354 | // } else {
|
---|
2355 | // var g = Mapper.StaticCache.GetGuid(t);
|
---|
2356 | // }
|
---|
2357 | // } catch (Exception e) {
|
---|
2358 | // Console.WriteLine(t.FullName);
|
---|
2359 | // ok = false;
|
---|
2360 | // }
|
---|
2361 | // }
|
---|
2362 | // }
|
---|
2363 | if (ok) {
|
---|
2364 | serializer.Serialize(original, fileName + ".proto");
|
---|
2365 | // var newVersion = serializer.Deserialize(fileName + ".proto");
|
---|
2366 | var p = Profile(() => original);
|
---|
2367 | Console.WriteLine(p);
|
---|
2368 | }
|
---|
2369 | }
|
---|
2370 | }
|
---|
2371 | }
|
---|
2372 | }
|
---|