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