[6846] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6846] | 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.Drawing;
|
---|
[11352] | 26 | using System.Globalization;
|
---|
[6846] | 27 | using System.IO;
|
---|
| 28 | using System.Linq;
|
---|
| 29 | using System.Reflection;
|
---|
| 30 | using System.Text;
|
---|
| 31 | using System.Text.RegularExpressions;
|
---|
| 32 | using System.Threading.Tasks;
|
---|
| 33 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
| 34 | using HeuristicLab.Persistence.Auxiliary;
|
---|
| 35 | using HeuristicLab.Persistence.Core;
|
---|
| 36 | using HeuristicLab.Persistence.Core.Tokens;
|
---|
| 37 | using HeuristicLab.Persistence.Default.CompositeSerializers;
|
---|
| 38 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 39 | using HeuristicLab.Persistence.Default.DebugString;
|
---|
| 40 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 41 | using HeuristicLab.Persistence.Default.Xml.Primitive;
|
---|
| 42 | using HeuristicLab.Persistence.Interfaces;
|
---|
[9764] | 43 | using HeuristicLab.Tests;
|
---|
[6846] | 44 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 45 |
|
---|
[9764] | 46 | namespace HeuristicLab.Persistence.Tests {
|
---|
[6846] | 47 |
|
---|
| 48 | [StorableClass]
|
---|
| 49 | public class NumberTest {
|
---|
| 50 | [Storable]
|
---|
| 51 | private bool _bool = true;
|
---|
| 52 | [Storable]
|
---|
| 53 | private byte _byte = 0xFF;
|
---|
| 54 | [Storable]
|
---|
| 55 | private sbyte _sbyte = 0xF;
|
---|
| 56 | [Storable]
|
---|
| 57 | private short _short = -123;
|
---|
| 58 | [Storable]
|
---|
| 59 | private ushort _ushort = 123;
|
---|
| 60 | [Storable]
|
---|
| 61 | private int _int = -123;
|
---|
| 62 | [Storable]
|
---|
| 63 | private uint _uint = 123;
|
---|
| 64 | [Storable]
|
---|
| 65 | private long _long = 123456;
|
---|
| 66 | [Storable]
|
---|
| 67 | private ulong _ulong = 123456;
|
---|
| 68 | public override bool Equals(object obj) {
|
---|
| 69 | NumberTest nt = obj as NumberTest;
|
---|
| 70 | if (nt == null)
|
---|
| 71 | throw new NotSupportedException();
|
---|
| 72 | return
|
---|
| 73 | nt._bool == _bool &&
|
---|
| 74 | nt._byte == _byte &&
|
---|
| 75 | nt._sbyte == _sbyte &&
|
---|
| 76 | nt._short == _short &&
|
---|
| 77 | nt._ushort == _ushort &&
|
---|
| 78 | nt._int == _int &&
|
---|
| 79 | nt._uint == _uint &&
|
---|
| 80 | nt._long == _long &&
|
---|
| 81 | nt._ulong == _ulong;
|
---|
| 82 | }
|
---|
| 83 | public override int GetHashCode() {
|
---|
| 84 | return
|
---|
| 85 | _bool.GetHashCode() ^
|
---|
| 86 | _byte.GetHashCode() ^
|
---|
| 87 | _sbyte.GetHashCode() ^
|
---|
| 88 | _short.GetHashCode() ^
|
---|
| 89 | _short.GetHashCode() ^
|
---|
| 90 | _int.GetHashCode() ^
|
---|
| 91 | _uint.GetHashCode() ^
|
---|
| 92 | _long.GetHashCode() ^
|
---|
| 93 | _ulong.GetHashCode();
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | [StorableClass]
|
---|
| 98 | public class NonDefaultConstructorClass {
|
---|
| 99 | [Storable]
|
---|
| 100 | int value;
|
---|
| 101 | public NonDefaultConstructorClass(int value) {
|
---|
| 102 | this.value = value;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | [StorableClass]
|
---|
| 107 | public class IntWrapper {
|
---|
| 108 |
|
---|
| 109 | [Storable]
|
---|
| 110 | public int Value;
|
---|
| 111 |
|
---|
| 112 | private IntWrapper() { }
|
---|
| 113 |
|
---|
| 114 | public IntWrapper(int value) {
|
---|
| 115 | this.Value = value;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | public override bool Equals(object obj) {
|
---|
| 119 | if (obj as IntWrapper == null)
|
---|
| 120 | return false;
|
---|
| 121 | return Value.Equals(((IntWrapper)obj).Value);
|
---|
| 122 | }
|
---|
| 123 | public override int GetHashCode() {
|
---|
| 124 | return Value.GetHashCode();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | [StorableClass]
|
---|
| 130 | public class PrimitivesTest : NumberTest {
|
---|
| 131 | [Storable]
|
---|
| 132 | private char c = 'e';
|
---|
| 133 | [Storable]
|
---|
| 134 | private long[,] _long_array =
|
---|
| 135 | new long[,] { { 123, 456, }, { 789, 123 } };
|
---|
| 136 | [Storable]
|
---|
| 137 | public List<int> list = new List<int> { 1, 2, 3, 4, 5 };
|
---|
| 138 | [Storable]
|
---|
| 139 | private object o = new object();
|
---|
| 140 | public override bool Equals(object obj) {
|
---|
| 141 | PrimitivesTest pt = obj as PrimitivesTest;
|
---|
| 142 | if (pt == null)
|
---|
| 143 | throw new NotSupportedException();
|
---|
| 144 | return base.Equals(obj) &&
|
---|
| 145 | c == pt.c &&
|
---|
| 146 | _long_array == pt._long_array &&
|
---|
| 147 | list == pt.list &&
|
---|
| 148 | o == pt.o;
|
---|
| 149 | }
|
---|
| 150 | public override int GetHashCode() {
|
---|
| 151 | return base.GetHashCode() ^
|
---|
| 152 | c.GetHashCode() ^
|
---|
| 153 | _long_array.GetHashCode() ^
|
---|
| 154 | list.GetHashCode() ^
|
---|
| 155 | o.GetHashCode();
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | public enum TestEnum { va1, va2, va3, va8 } ;
|
---|
| 160 |
|
---|
| 161 | [StorableClass]
|
---|
| 162 | public class RootBase {
|
---|
| 163 | [Storable]
|
---|
| 164 | private string baseString = " Serial ";
|
---|
| 165 | [Storable]
|
---|
| 166 | public TestEnum myEnum = TestEnum.va3;
|
---|
| 167 | public override bool Equals(object obj) {
|
---|
| 168 | RootBase rb = obj as RootBase;
|
---|
| 169 | if (rb == null)
|
---|
| 170 | throw new NotSupportedException();
|
---|
| 171 | return baseString == rb.baseString &&
|
---|
| 172 | myEnum == rb.myEnum;
|
---|
| 173 | }
|
---|
| 174 | public override int GetHashCode() {
|
---|
| 175 | return baseString.GetHashCode() ^
|
---|
| 176 | myEnum.GetHashCode();
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | [StorableClass]
|
---|
| 181 | public class Root : RootBase {
|
---|
| 182 | [Storable]
|
---|
| 183 | public Stack<int> intStack = new Stack<int>();
|
---|
| 184 | [Storable]
|
---|
| 185 | public int[] i = new[] { 3, 4, 5, 6 };
|
---|
| 186 | [Storable(Name = "Test String")]
|
---|
| 187 | public string s;
|
---|
| 188 | [Storable]
|
---|
| 189 | public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
|
---|
| 190 | [Storable]
|
---|
| 191 | public List<int> intList = new List<int>(new[] { 321, 312, 321 });
|
---|
| 192 | [Storable]
|
---|
| 193 | public Custom c;
|
---|
| 194 | [Storable]
|
---|
| 195 | public List<Root> selfReferences;
|
---|
| 196 | [Storable]
|
---|
| 197 | public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
|
---|
| 198 | [Storable]
|
---|
| 199 | public bool boolean = true;
|
---|
| 200 | [Storable]
|
---|
| 201 | public DateTime dateTime;
|
---|
| 202 | [Storable]
|
---|
| 203 | public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("Serial", 123);
|
---|
| 204 | [Storable]
|
---|
| 205 | public Dictionary<string, int> dict = new Dictionary<string, int>();
|
---|
| 206 | [Storable(DefaultValue = "default")]
|
---|
| 207 | public string uninitialized;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | public enum SimpleEnum { one, two, three }
|
---|
| 211 | public enum ComplexEnum { one = 1, two = 2, three = 3 }
|
---|
| 212 | [FlagsAttribute]
|
---|
| 213 | public enum TrickyEnum { zero = 0, one = 1, two = 2 }
|
---|
| 214 |
|
---|
| 215 | [StorableClass]
|
---|
| 216 | public class EnumTest {
|
---|
| 217 | [Storable]
|
---|
| 218 | public SimpleEnum simpleEnum = SimpleEnum.one;
|
---|
| 219 | [Storable]
|
---|
| 220 | public ComplexEnum complexEnum = (ComplexEnum)2;
|
---|
| 221 | [Storable]
|
---|
| 222 | public TrickyEnum trickyEnum = (TrickyEnum)15;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | [StorableClass]
|
---|
| 226 | public class Custom {
|
---|
| 227 | [Storable]
|
---|
| 228 | public int i;
|
---|
| 229 | [Storable]
|
---|
| 230 | public Root r;
|
---|
| 231 | [Storable]
|
---|
| 232 | public string name = "<![CDATA[<![CDATA[Serial]]>]]>";
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | [StorableClass]
|
---|
| 236 | public class Manager {
|
---|
| 237 |
|
---|
| 238 | public DateTime lastLoadTime;
|
---|
| 239 | [Storable]
|
---|
| 240 | private DateTime lastLoadTimePersistence {
|
---|
| 241 | get { return lastLoadTime; }
|
---|
| 242 | set { lastLoadTime = DateTime.Now; }
|
---|
| 243 | }
|
---|
| 244 | [Storable]
|
---|
| 245 | public double? dbl;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | [StorableClass]
|
---|
| 249 | public class C {
|
---|
| 250 | [Storable]
|
---|
| 251 | public C[][] allCs;
|
---|
| 252 | [Storable]
|
---|
| 253 | public KeyValuePair<List<C>, C> kvpList;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | public class NonSerializable {
|
---|
| 257 | int x = 0;
|
---|
| 258 | public override bool Equals(object obj) {
|
---|
| 259 | NonSerializable ns = obj as NonSerializable;
|
---|
| 260 | if (ns == null)
|
---|
| 261 | throw new NotSupportedException();
|
---|
| 262 | return ns.x == x;
|
---|
| 263 | }
|
---|
| 264 | public override int GetHashCode() {
|
---|
| 265 | return x.GetHashCode();
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 |
|
---|
| 270 | [TestClass]
|
---|
| 271 | public class UseCases {
|
---|
| 272 |
|
---|
| 273 | private string tempFile;
|
---|
| 274 |
|
---|
| 275 | [TestInitialize()]
|
---|
| 276 | public void CreateTempFile() {
|
---|
| 277 | tempFile = Path.GetTempFileName();
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | [TestCleanup()]
|
---|
| 281 | public void ClearTempFile() {
|
---|
| 282 | StreamReader reader = new StreamReader(tempFile);
|
---|
| 283 | string s = reader.ReadToEnd();
|
---|
| 284 | reader.Close();
|
---|
| 285 | File.Delete(tempFile);
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | [TestMethod]
|
---|
[9778] | 289 | [TestCategory("Persistence")]
|
---|
| 290 | [TestProperty("Time", "short")]
|
---|
[6846] | 291 | public void ComplexStorable() {
|
---|
| 292 | Root r = InitializeComplexStorable();
|
---|
| 293 | XmlGenerator.Serialize(r, tempFile);
|
---|
| 294 | Root newR = (Root)XmlParser.Deserialize(tempFile);
|
---|
| 295 | CompareComplexStorables(r, newR);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | [TestMethod]
|
---|
[9778] | 299 | [TestCategory("Persistence")]
|
---|
| 300 | [TestProperty("Time", "short")]
|
---|
[6846] | 301 | public void ComplexEasyStorable() {
|
---|
| 302 | Root r = InitializeComplexStorable();
|
---|
| 303 | ReadableXmlGenerator.Serialize(r, tempFile);
|
---|
| 304 | using (var reader = new StreamReader(tempFile)) {
|
---|
| 305 | string text = reader.ReadToEnd();
|
---|
| 306 | Assert.IsTrue(text.StartsWith("<Root"));
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | private static void CompareComplexStorables(Root r, Root newR) {
|
---|
| 311 | Assert.AreEqual(
|
---|
| 312 | DebugStringGenerator.Serialize(r),
|
---|
| 313 | DebugStringGenerator.Serialize(newR));
|
---|
| 314 | Assert.AreSame(newR, newR.selfReferences[0]);
|
---|
| 315 | Assert.AreNotSame(r, newR);
|
---|
| 316 | Assert.AreEqual(r.myEnum, TestEnum.va1);
|
---|
| 317 | Assert.AreEqual(r.i[0], 7);
|
---|
| 318 | Assert.AreEqual(r.i[1], 5);
|
---|
| 319 | Assert.AreEqual(r.i[2], 6);
|
---|
| 320 | Assert.AreEqual(r.s, "new value");
|
---|
| 321 | Assert.AreEqual(r.intArray[0], 3);
|
---|
| 322 | Assert.AreEqual(r.intArray[1], 2);
|
---|
| 323 | Assert.AreEqual(r.intArray[2], 1);
|
---|
| 324 | Assert.AreEqual(r.intList[0], 9);
|
---|
| 325 | Assert.AreEqual(r.intList[1], 8);
|
---|
| 326 | Assert.AreEqual(r.intList[2], 7);
|
---|
| 327 | Assert.AreEqual(r.multiDimArray[0, 0], 5);
|
---|
| 328 | Assert.AreEqual(r.multiDimArray[0, 1], 4);
|
---|
| 329 | Assert.AreEqual(r.multiDimArray[0, 2], 3);
|
---|
| 330 | Assert.AreEqual(r.multiDimArray[1, 0], 1);
|
---|
| 331 | Assert.AreEqual(r.multiDimArray[1, 1], 4);
|
---|
| 332 | Assert.AreEqual(r.multiDimArray[1, 2], 6);
|
---|
| 333 | Assert.IsFalse(r.boolean);
|
---|
| 334 | Assert.IsTrue((DateTime.Now - r.dateTime).TotalSeconds < 10);
|
---|
| 335 | Assert.AreEqual(r.kvp.Key, "string key");
|
---|
| 336 | Assert.AreEqual(r.kvp.Value, 321);
|
---|
| 337 | Assert.IsNull(r.uninitialized);
|
---|
| 338 | Assert.AreEqual(newR.myEnum, TestEnum.va1);
|
---|
| 339 | Assert.AreEqual(newR.i[0], 7);
|
---|
| 340 | Assert.AreEqual(newR.i[1], 5);
|
---|
| 341 | Assert.AreEqual(newR.i[2], 6);
|
---|
| 342 | Assert.AreEqual(newR.s, "new value");
|
---|
| 343 | Assert.AreEqual(newR.intArray[0], 3);
|
---|
| 344 | Assert.AreEqual(newR.intArray[1], 2);
|
---|
| 345 | Assert.AreEqual(newR.intArray[2], 1);
|
---|
| 346 | Assert.AreEqual(newR.intList[0], 9);
|
---|
| 347 | Assert.AreEqual(newR.intList[1], 8);
|
---|
| 348 | Assert.AreEqual(newR.intList[2], 7);
|
---|
| 349 | Assert.AreEqual(newR.multiDimArray[0, 0], 5);
|
---|
| 350 | Assert.AreEqual(newR.multiDimArray[0, 1], 4);
|
---|
| 351 | Assert.AreEqual(newR.multiDimArray[0, 2], 3);
|
---|
| 352 | Assert.AreEqual(newR.multiDimArray[1, 0], 1);
|
---|
| 353 | Assert.AreEqual(newR.multiDimArray[1, 1], 4);
|
---|
| 354 | Assert.AreEqual(newR.multiDimArray[1, 2], 6);
|
---|
| 355 | Assert.AreEqual(newR.intStack.Pop(), 3);
|
---|
| 356 | Assert.AreEqual(newR.intStack.Pop(), 2);
|
---|
| 357 | Assert.AreEqual(newR.intStack.Pop(), 1);
|
---|
| 358 | Assert.IsFalse(newR.boolean);
|
---|
| 359 | Assert.IsTrue((DateTime.Now - newR.dateTime).TotalSeconds < 10);
|
---|
| 360 | Assert.AreEqual(newR.kvp.Key, "string key");
|
---|
| 361 | Assert.AreEqual(newR.kvp.Value, 321);
|
---|
| 362 | Assert.IsNull(newR.uninitialized);
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | private static Root InitializeComplexStorable() {
|
---|
| 366 | Root r = new Root();
|
---|
| 367 | r.intStack.Push(1);
|
---|
| 368 | r.intStack.Push(2);
|
---|
| 369 | r.intStack.Push(3);
|
---|
| 370 | r.selfReferences = new List<Root> { r, r };
|
---|
| 371 | r.c = new Custom { r = r };
|
---|
| 372 | r.dict.Add("one", 1);
|
---|
| 373 | r.dict.Add("two", 2);
|
---|
| 374 | r.dict.Add("three", 3);
|
---|
| 375 | r.myEnum = TestEnum.va1;
|
---|
| 376 | r.i = new[] { 7, 5, 6 };
|
---|
| 377 | r.s = "new value";
|
---|
| 378 | r.intArray = new ArrayList { 3, 2, 1 };
|
---|
| 379 | r.intList = new List<int> { 9, 8, 7 };
|
---|
| 380 | r.multiDimArray = new double[,] { { 5, 4, 3 }, { 1, 4, 6 } };
|
---|
| 381 | r.boolean = false;
|
---|
| 382 | r.dateTime = DateTime.Now;
|
---|
| 383 | r.kvp = new KeyValuePair<string, int>("string key", 321);
|
---|
| 384 | r.uninitialized = null;
|
---|
| 385 |
|
---|
| 386 | return r;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | [TestMethod]
|
---|
[9778] | 390 | [TestCategory("Persistence")]
|
---|
| 391 | [TestProperty("Time", "short")]
|
---|
[6846] | 392 | public void SelfReferences() {
|
---|
| 393 | C c = new C();
|
---|
| 394 | C[][] cs = new C[2][];
|
---|
| 395 | cs[0] = new C[] { c };
|
---|
| 396 | cs[1] = new C[] { c };
|
---|
| 397 | c.allCs = cs;
|
---|
| 398 | c.kvpList = new KeyValuePair<List<C>, C>(new List<C> { c }, c);
|
---|
| 399 | XmlGenerator.Serialize(cs, tempFile);
|
---|
| 400 | object o = XmlParser.Deserialize(tempFile);
|
---|
| 401 | Assert.AreEqual(
|
---|
| 402 | DebugStringGenerator.Serialize(cs),
|
---|
| 403 | DebugStringGenerator.Serialize(o));
|
---|
| 404 | Assert.AreSame(c, c.allCs[0][0]);
|
---|
| 405 | Assert.AreSame(c, c.allCs[1][0]);
|
---|
| 406 | Assert.AreSame(c, c.kvpList.Key[0]);
|
---|
| 407 | Assert.AreSame(c, c.kvpList.Value);
|
---|
| 408 | C[][] newCs = (C[][])o;
|
---|
| 409 | C newC = newCs[0][0];
|
---|
| 410 | Assert.AreSame(newC, newC.allCs[0][0]);
|
---|
| 411 | Assert.AreSame(newC, newC.allCs[1][0]);
|
---|
| 412 | Assert.AreSame(newC, newC.kvpList.Key[0]);
|
---|
| 413 | Assert.AreSame(newC, newC.kvpList.Value);
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | [TestMethod]
|
---|
[9778] | 417 | [TestCategory("Persistence")]
|
---|
| 418 | [TestProperty("Time", "short")]
|
---|
[6846] | 419 | public void ArrayCreation() {
|
---|
| 420 | ArrayList[] arrayListArray = new ArrayList[4];
|
---|
| 421 | arrayListArray[0] = new ArrayList();
|
---|
| 422 | arrayListArray[0].Add(arrayListArray);
|
---|
| 423 | arrayListArray[0].Add(arrayListArray);
|
---|
| 424 | arrayListArray[1] = new ArrayList();
|
---|
| 425 | arrayListArray[1].Add(arrayListArray);
|
---|
| 426 | arrayListArray[2] = new ArrayList();
|
---|
| 427 | arrayListArray[2].Add(arrayListArray);
|
---|
| 428 | arrayListArray[2].Add(arrayListArray);
|
---|
| 429 | Array a = Array.CreateInstance(
|
---|
| 430 | typeof(object),
|
---|
| 431 | new[] { 1, 2 }, new[] { 3, 4 });
|
---|
| 432 | arrayListArray[2].Add(a);
|
---|
| 433 | XmlGenerator.Serialize(arrayListArray, tempFile);
|
---|
| 434 | object o = XmlParser.Deserialize(tempFile);
|
---|
| 435 | Assert.AreEqual(
|
---|
| 436 | DebugStringGenerator.Serialize(arrayListArray),
|
---|
| 437 | DebugStringGenerator.Serialize(o));
|
---|
| 438 | ArrayList[] newArray = (ArrayList[])o;
|
---|
| 439 | Assert.AreSame(arrayListArray, arrayListArray[0][0]);
|
---|
| 440 | Assert.AreSame(arrayListArray, arrayListArray[2][1]);
|
---|
| 441 | Assert.AreSame(newArray, newArray[0][0]);
|
---|
| 442 | Assert.AreSame(newArray, newArray[2][1]);
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | [TestMethod]
|
---|
[9778] | 446 | [TestCategory("Persistence")]
|
---|
| 447 | [TestProperty("Time", "short")]
|
---|
[6846] | 448 | public void CustomSerializationProperty() {
|
---|
| 449 | Manager m = new Manager();
|
---|
| 450 | XmlGenerator.Serialize(m, tempFile);
|
---|
| 451 | Manager newM = (Manager)XmlParser.Deserialize(tempFile);
|
---|
| 452 | Assert.AreNotEqual(
|
---|
| 453 | DebugStringGenerator.Serialize(m),
|
---|
| 454 | DebugStringGenerator.Serialize(newM));
|
---|
| 455 | Assert.AreEqual(m.dbl, newM.dbl);
|
---|
| 456 | Assert.AreEqual(m.lastLoadTime, new DateTime());
|
---|
| 457 | Assert.AreNotEqual(newM.lastLoadTime, new DateTime());
|
---|
| 458 | Assert.IsTrue((DateTime.Now - newM.lastLoadTime).TotalSeconds < 10);
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | [TestMethod]
|
---|
[9778] | 462 | [TestCategory("Persistence")]
|
---|
| 463 | [TestProperty("Time", "short")]
|
---|
[6846] | 464 | public void Primitives() {
|
---|
| 465 | PrimitivesTest sdt = new PrimitivesTest();
|
---|
| 466 | XmlGenerator.Serialize(sdt, tempFile);
|
---|
| 467 | object o = XmlParser.Deserialize(tempFile);
|
---|
| 468 | Assert.AreEqual(
|
---|
| 469 | DebugStringGenerator.Serialize(sdt),
|
---|
| 470 | DebugStringGenerator.Serialize(o));
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | [TestMethod]
|
---|
[9778] | 474 | [TestCategory("Persistence")]
|
---|
| 475 | [TestProperty("Time", "short")]
|
---|
[6846] | 476 | public void MultiDimensionalArray() {
|
---|
| 477 | string[,] mDimString = new string[,] {
|
---|
| 478 | {"ora", "et", "labora"},
|
---|
| 479 | {"Beten", "und", "Arbeiten"}
|
---|
| 480 | };
|
---|
| 481 | XmlGenerator.Serialize(mDimString, tempFile);
|
---|
| 482 | object o = XmlParser.Deserialize(tempFile);
|
---|
| 483 | Assert.AreEqual(
|
---|
| 484 | DebugStringGenerator.Serialize(mDimString),
|
---|
| 485 | DebugStringGenerator.Serialize(o));
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | [StorableClass]
|
---|
| 489 | public class NestedType {
|
---|
| 490 | [Storable]
|
---|
| 491 | private string value = "value";
|
---|
| 492 | public override bool Equals(object obj) {
|
---|
| 493 | NestedType nt = obj as NestedType;
|
---|
| 494 | if (nt == null)
|
---|
| 495 | throw new NotSupportedException();
|
---|
| 496 | return nt.value == value;
|
---|
| 497 | }
|
---|
| 498 | public override int GetHashCode() {
|
---|
| 499 | return value.GetHashCode();
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | [TestMethod]
|
---|
[9778] | 504 | [TestCategory("Persistence")]
|
---|
| 505 | [TestProperty("Time", "short")]
|
---|
[6846] | 506 | public void NestedTypeTest() {
|
---|
| 507 | NestedType t = new NestedType();
|
---|
| 508 | XmlGenerator.Serialize(t, tempFile);
|
---|
| 509 | object o = XmlParser.Deserialize(tempFile);
|
---|
| 510 | Assert.AreEqual(
|
---|
| 511 | DebugStringGenerator.Serialize(t),
|
---|
| 512 | DebugStringGenerator.Serialize(o));
|
---|
| 513 | Assert.IsTrue(t.Equals(o));
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 |
|
---|
| 517 | [TestMethod]
|
---|
[9778] | 518 | [TestCategory("Persistence")]
|
---|
| 519 | [TestProperty("Time", "short")]
|
---|
[6846] | 520 | public void SimpleArray() {
|
---|
| 521 | string[] strings = { "ora", "et", "labora" };
|
---|
| 522 | XmlGenerator.Serialize(strings, tempFile);
|
---|
| 523 | object o = XmlParser.Deserialize(tempFile);
|
---|
| 524 | Assert.AreEqual(
|
---|
| 525 | DebugStringGenerator.Serialize(strings),
|
---|
| 526 | DebugStringGenerator.Serialize(o));
|
---|
| 527 | }
|
---|
| 528 |
|
---|
| 529 | [TestMethod]
|
---|
[9778] | 530 | [TestCategory("Persistence")]
|
---|
| 531 | [TestProperty("Time", "short")]
|
---|
[6846] | 532 | public void PrimitiveRoot() {
|
---|
| 533 | XmlGenerator.Serialize(12.3f, tempFile);
|
---|
| 534 | object o = XmlParser.Deserialize(tempFile);
|
---|
| 535 | Assert.AreEqual(
|
---|
| 536 | DebugStringGenerator.Serialize(12.3f),
|
---|
| 537 | DebugStringGenerator.Serialize(o));
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | private string formatFullMemberName(MemberInfo mi) {
|
---|
| 541 | return new StringBuilder()
|
---|
| 542 | .Append(mi.DeclaringType.Assembly.GetName().Name)
|
---|
| 543 | .Append(": ")
|
---|
| 544 | .Append(mi.DeclaringType.Namespace)
|
---|
| 545 | .Append('.')
|
---|
| 546 | .Append(mi.DeclaringType.Name)
|
---|
| 547 | .Append('.')
|
---|
| 548 | .Append(mi.Name).ToString();
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | public void CodingConventions() {
|
---|
| 552 | List<string> lowerCaseMethodNames = new List<string>();
|
---|
| 553 | List<string> lowerCaseProperties = new List<string>();
|
---|
| 554 | List<string> lowerCaseFields = new List<string>();
|
---|
[6912] | 555 | foreach (Assembly a in PluginLoader.Assemblies) {
|
---|
[6846] | 556 | if (!a.GetName().Name.StartsWith("HeuristicLab"))
|
---|
| 557 | continue;
|
---|
| 558 | foreach (Type t in a.GetTypes()) {
|
---|
| 559 | foreach (MemberInfo mi in t.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) {
|
---|
| 560 | if (mi.DeclaringType.Name.StartsWith("<>"))
|
---|
| 561 | continue;
|
---|
| 562 | if (char.IsLower(mi.Name[0])) {
|
---|
| 563 | if (mi.MemberType == MemberTypes.Field)
|
---|
| 564 | lowerCaseFields.Add(formatFullMemberName(mi));
|
---|
| 565 | if (mi.MemberType == MemberTypes.Property)
|
---|
| 566 | lowerCaseProperties.Add(formatFullMemberName(mi));
|
---|
| 567 | if (mi.MemberType == MemberTypes.Method &&
|
---|
| 568 | !mi.Name.StartsWith("get_") &&
|
---|
| 569 | !mi.Name.StartsWith("set_") &&
|
---|
| 570 | !mi.Name.StartsWith("add_") &&
|
---|
| 571 | !mi.Name.StartsWith("remove_") &&
|
---|
| 572 | !mi.Name.StartsWith("op_"))
|
---|
| 573 | lowerCaseMethodNames.Add(formatFullMemberName(mi));
|
---|
| 574 | }
|
---|
| 575 | }
|
---|
| 576 | }
|
---|
| 577 | }
|
---|
| 578 | //Assert.AreEqual("", lowerCaseFields.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
| 579 | Assert.AreEqual("", lowerCaseMethodNames.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
| 580 | Assert.AreEqual("", lowerCaseProperties.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | [TestMethod]
|
---|
[9778] | 584 | [TestCategory("Persistence")]
|
---|
| 585 | [TestProperty("Time", "short")]
|
---|
[6846] | 586 | public void Number2StringDecomposer() {
|
---|
| 587 | NumberTest sdt = new NumberTest();
|
---|
| 588 | XmlGenerator.Serialize(sdt, tempFile,
|
---|
| 589 | new Configuration(new XmlFormat(),
|
---|
| 590 | new List<IPrimitiveSerializer> { new String2XmlSerializer() },
|
---|
| 591 | new List<ICompositeSerializer> {
|
---|
| 592 | new StorableSerializer(),
|
---|
| 593 | new Number2StringSerializer() }));
|
---|
| 594 | object o = XmlParser.Deserialize(tempFile);
|
---|
| 595 | Assert.AreEqual(
|
---|
| 596 | DebugStringGenerator.Serialize(sdt),
|
---|
| 597 | DebugStringGenerator.Serialize(o));
|
---|
| 598 | Assert.IsTrue(sdt.Equals(o));
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | [TestMethod]
|
---|
[9778] | 602 | [TestCategory("Persistence")]
|
---|
| 603 | [TestProperty("Time", "short")]
|
---|
[6846] | 604 | public void Enums() {
|
---|
| 605 | EnumTest et = new EnumTest();
|
---|
| 606 | et.simpleEnum = SimpleEnum.two;
|
---|
| 607 | et.complexEnum = ComplexEnum.three;
|
---|
| 608 | et.trickyEnum = TrickyEnum.two | TrickyEnum.one;
|
---|
| 609 | XmlGenerator.Serialize(et, tempFile);
|
---|
| 610 | EnumTest newEt = (EnumTest)XmlParser.Deserialize(tempFile);
|
---|
| 611 | Assert.AreEqual(et.simpleEnum, SimpleEnum.two);
|
---|
| 612 | Assert.AreEqual(et.complexEnum, ComplexEnum.three);
|
---|
| 613 | Assert.AreEqual(et.trickyEnum, (TrickyEnum)3);
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | [TestMethod]
|
---|
[9778] | 617 | [TestCategory("Persistence")]
|
---|
| 618 | [TestProperty("Time", "short")]
|
---|
[6846] | 619 | public void TestAliasingWithOverriddenEquals() {
|
---|
| 620 | List<IntWrapper> ints = new List<IntWrapper>();
|
---|
| 621 | ints.Add(new IntWrapper(1));
|
---|
| 622 | ints.Add(new IntWrapper(1));
|
---|
| 623 | Assert.AreEqual(ints[0], ints[1]);
|
---|
| 624 | Assert.AreNotSame(ints[0], ints[1]);
|
---|
| 625 | XmlGenerator.Serialize(ints, tempFile);
|
---|
| 626 | List<IntWrapper> newInts = (List<IntWrapper>)XmlParser.Deserialize(tempFile);
|
---|
| 627 | Assert.AreEqual(newInts[0].Value, 1);
|
---|
| 628 | Assert.AreEqual(newInts[1].Value, 1);
|
---|
| 629 | Assert.AreEqual(newInts[0], newInts[1]);
|
---|
| 630 | Assert.AreNotSame(newInts[0], newInts[1]);
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | [TestMethod]
|
---|
[9778] | 634 | [TestCategory("Persistence")]
|
---|
| 635 | [TestProperty("Time", "short")]
|
---|
[6846] | 636 | public void NonDefaultConstructorTest() {
|
---|
| 637 | NonDefaultConstructorClass c = new NonDefaultConstructorClass(1);
|
---|
| 638 | try {
|
---|
| 639 | XmlGenerator.Serialize(c, tempFile);
|
---|
| 640 | Assert.Fail("Exception not thrown");
|
---|
[9778] | 641 | } catch (PersistenceException) {
|
---|
[6846] | 642 | }
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | [TestMethod]
|
---|
[9778] | 646 | [TestCategory("Persistence")]
|
---|
| 647 | [TestProperty("Time", "short")]
|
---|
[6846] | 648 | public void TestSavingException() {
|
---|
| 649 | List<int> list = new List<int> { 1, 2, 3 };
|
---|
| 650 | XmlGenerator.Serialize(list, tempFile);
|
---|
| 651 | NonSerializable s = new NonSerializable();
|
---|
| 652 | try {
|
---|
| 653 | XmlGenerator.Serialize(s, tempFile);
|
---|
| 654 | Assert.Fail("Exception expected");
|
---|
[9778] | 655 | } catch (PersistenceException) { }
|
---|
[6846] | 656 | List<int> newList = (List<int>)XmlParser.Deserialize(tempFile);
|
---|
| 657 | Assert.AreEqual(list[0], newList[0]);
|
---|
| 658 | Assert.AreEqual(list[1], newList[1]);
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | [TestMethod]
|
---|
[9778] | 662 | [TestCategory("Persistence")]
|
---|
| 663 | [TestProperty("Time", "short")]
|
---|
[6846] | 664 | public void TestTypeStringConversion() {
|
---|
| 665 | string name = typeof(List<int>[]).AssemblyQualifiedName;
|
---|
| 666 | string shortName =
|
---|
| 667 | "System.Collections.Generic.List`1[[System.Int32, mscorlib]][], mscorlib";
|
---|
| 668 | Assert.AreEqual(name, TypeNameParser.Parse(name).ToString());
|
---|
| 669 | Assert.AreEqual(shortName, TypeNameParser.Parse(name).ToString(false));
|
---|
| 670 | Assert.AreEqual(shortName, typeof(List<int>[]).VersionInvariantName());
|
---|
| 671 | }
|
---|
| 672 |
|
---|
| 673 | [TestMethod]
|
---|
[9778] | 674 | [TestCategory("Persistence")]
|
---|
| 675 | [TestProperty("Time", "short")]
|
---|
[6846] | 676 | public void TestHexadecimalPublicKeyToken() {
|
---|
| 677 | string name = "TestClass, TestAssembly, Version=1.2.3.4, PublicKey=1234abc";
|
---|
| 678 | string shortName = "TestClass, TestAssembly";
|
---|
| 679 | Assert.AreEqual(name, TypeNameParser.Parse(name).ToString());
|
---|
| 680 | Assert.AreEqual(shortName, TypeNameParser.Parse(name).ToString(false));
|
---|
| 681 | }
|
---|
| 682 |
|
---|
| 683 | [TestMethod]
|
---|
[9778] | 684 | [TestCategory("Persistence")]
|
---|
| 685 | [TestProperty("Time", "short")]
|
---|
[6846] | 686 | public void TestMultipleFailure() {
|
---|
| 687 | List<NonSerializable> l = new List<NonSerializable>();
|
---|
| 688 | l.Add(new NonSerializable());
|
---|
| 689 | l.Add(new NonSerializable());
|
---|
| 690 | l.Add(new NonSerializable());
|
---|
| 691 | try {
|
---|
| 692 | Serializer s = new Serializer(l,
|
---|
| 693 | ConfigurationService.Instance.GetConfiguration(new XmlFormat()),
|
---|
| 694 | "ROOT", true);
|
---|
| 695 | StringBuilder tokens = new StringBuilder();
|
---|
| 696 | foreach (var token in s) {
|
---|
| 697 | tokens.Append(token.ToString());
|
---|
| 698 | }
|
---|
| 699 | Assert.Fail("Exception expected");
|
---|
[9778] | 700 | } catch (PersistenceException px) {
|
---|
[6846] | 701 | Assert.AreEqual(3, px.Data.Count);
|
---|
| 702 | }
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | [TestMethod]
|
---|
[9778] | 706 | [TestCategory("Persistence")]
|
---|
| 707 | [TestProperty("Time", "short")]
|
---|
[6846] | 708 | public void TestAssemblyVersionCheck() {
|
---|
| 709 | IntWrapper i = new IntWrapper(1);
|
---|
| 710 | Serializer s = new Serializer(i, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat()));
|
---|
| 711 | XmlGenerator g = new XmlGenerator();
|
---|
| 712 | StringBuilder dataString = new StringBuilder();
|
---|
| 713 | foreach (var token in s) {
|
---|
| 714 | dataString.Append(g.Format(token));
|
---|
| 715 | }
|
---|
| 716 | StringBuilder typeString = new StringBuilder();
|
---|
| 717 | foreach (var line in g.Format(s.TypeCache))
|
---|
| 718 | typeString.Append(line);
|
---|
| 719 | Deserializer d = new Deserializer(XmlParser.ParseTypeCache(new StringReader(typeString.ToString())));
|
---|
| 720 | XmlParser p = new XmlParser(new StringReader(dataString.ToString()));
|
---|
| 721 | IntWrapper newI = (IntWrapper)d.Deserialize(p);
|
---|
| 722 | Assert.AreEqual(i.Value, newI.Value);
|
---|
| 723 |
|
---|
| 724 | string newTypeString = Regex.Replace(typeString.ToString(),
|
---|
| 725 | "Version=\\d+\\.\\d+\\.\\d+\\.\\d+",
|
---|
| 726 | "Version=0.0.9999.9999");
|
---|
| 727 | try {
|
---|
| 728 | d = new Deserializer(XmlParser.ParseTypeCache(new StringReader(newTypeString)));
|
---|
| 729 | Assert.Fail("Exception expected");
|
---|
[9778] | 730 | } catch (PersistenceException x) {
|
---|
[6846] | 731 | Assert.IsTrue(x.Message.Contains("incompatible"));
|
---|
| 732 | }
|
---|
| 733 | newTypeString = Regex.Replace(typeString.ToString(),
|
---|
| 734 | "Version=(\\d+\\.\\d+)\\.\\d+\\.\\d+",
|
---|
| 735 | "Version=$1.9999.9999");
|
---|
| 736 | try {
|
---|
| 737 | d = new Deserializer(XmlParser.ParseTypeCache(new StringReader(newTypeString)));
|
---|
| 738 | Assert.Fail("Exception expected");
|
---|
[9778] | 739 | } catch (PersistenceException x) {
|
---|
[6846] | 740 | Assert.IsTrue(x.Message.Contains("newer"));
|
---|
| 741 | }
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | [TestMethod]
|
---|
[9778] | 745 | [TestCategory("Persistence")]
|
---|
| 746 | [TestProperty("Time", "short")]
|
---|
[6846] | 747 | public void InheritanceTest() {
|
---|
| 748 | New n = new New();
|
---|
| 749 | XmlGenerator.Serialize(n, tempFile);
|
---|
| 750 | New nn = (New)XmlParser.Deserialize(tempFile);
|
---|
| 751 | Assert.AreEqual(n.Name, nn.Name);
|
---|
| 752 | Assert.AreEqual(((Override)n).Name, ((Override)nn).Name);
|
---|
| 753 | }
|
---|
| 754 |
|
---|
| 755 | [StorableClass]
|
---|
| 756 | class Child {
|
---|
| 757 | [Storable]
|
---|
| 758 | public GrandParent grandParent;
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | [StorableClass]
|
---|
| 762 | class Parent {
|
---|
| 763 | [Storable]
|
---|
| 764 | public Child child;
|
---|
| 765 | }
|
---|
| 766 |
|
---|
| 767 | [StorableClass]
|
---|
| 768 | class GrandParent {
|
---|
| 769 | [Storable]
|
---|
| 770 | public Parent parent;
|
---|
| 771 | }
|
---|
| 772 |
|
---|
| 773 | [TestMethod]
|
---|
[9778] | 774 | [TestCategory("Persistence")]
|
---|
| 775 | [TestProperty("Time", "short")]
|
---|
[6846] | 776 | public void InstantiateParentChainReference() {
|
---|
| 777 | GrandParent gp = new GrandParent();
|
---|
| 778 | gp.parent = new Parent();
|
---|
| 779 | gp.parent.child = new Child();
|
---|
| 780 | gp.parent.child.grandParent = gp;
|
---|
| 781 | Assert.AreSame(gp, gp.parent.child.grandParent);
|
---|
| 782 | XmlGenerator.Serialize(gp, tempFile);
|
---|
| 783 | GrandParent newGp = (GrandParent)XmlParser.Deserialize(tempFile);
|
---|
| 784 | Assert.AreSame(newGp, newGp.parent.child.grandParent);
|
---|
| 785 | }
|
---|
| 786 |
|
---|
| 787 | struct TestStruct {
|
---|
| 788 | int value;
|
---|
| 789 | int PropertyValue { get; set; }
|
---|
| 790 | public TestStruct(int value)
|
---|
| 791 | : this() {
|
---|
| 792 | this.value = value;
|
---|
| 793 | PropertyValue = value;
|
---|
| 794 | }
|
---|
| 795 | }
|
---|
| 796 |
|
---|
| 797 | [TestMethod]
|
---|
[9778] | 798 | [TestCategory("Persistence")]
|
---|
| 799 | [TestProperty("Time", "short")]
|
---|
[6846] | 800 | public void StructTest() {
|
---|
| 801 | TestStruct s = new TestStruct(10);
|
---|
| 802 | XmlGenerator.Serialize(s, tempFile);
|
---|
| 803 | TestStruct newS = (TestStruct)XmlParser.Deserialize(tempFile);
|
---|
| 804 | Assert.AreEqual(s, newS);
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | [TestMethod]
|
---|
[9778] | 808 | [TestCategory("Persistence")]
|
---|
| 809 | [TestProperty("Time", "short")]
|
---|
[6846] | 810 | public void PointTest() {
|
---|
| 811 | Point p = new Point(12, 34);
|
---|
| 812 | XmlGenerator.Serialize(p, tempFile);
|
---|
| 813 | Point newP = (Point)XmlParser.Deserialize(tempFile);
|
---|
| 814 | Assert.AreEqual(p, newP);
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | [TestMethod]
|
---|
[9778] | 818 | [TestCategory("Persistence")]
|
---|
| 819 | [TestProperty("Time", "short")]
|
---|
[6846] | 820 | public void NullableValueTypes() {
|
---|
| 821 | double?[] d = new double?[] { null, 1, 2, 3 };
|
---|
| 822 | XmlGenerator.Serialize(d, tempFile);
|
---|
| 823 | double?[] newD = (double?[])XmlParser.Deserialize(tempFile);
|
---|
| 824 | Assert.AreEqual(d[0], newD[0]);
|
---|
| 825 | Assert.AreEqual(d[1], newD[1]);
|
---|
| 826 | Assert.AreEqual(d[2], newD[2]);
|
---|
| 827 | Assert.AreEqual(d[3], newD[3]);
|
---|
| 828 | }
|
---|
| 829 |
|
---|
| 830 | [TestMethod]
|
---|
[9778] | 831 | [TestCategory("Persistence")]
|
---|
| 832 | [TestProperty("Time", "short")]
|
---|
[6846] | 833 | public void BitmapTest() {
|
---|
| 834 | Icon icon = System.Drawing.SystemIcons.Hand;
|
---|
| 835 | Bitmap bitmap = icon.ToBitmap();
|
---|
| 836 | XmlGenerator.Serialize(bitmap, tempFile);
|
---|
| 837 | Bitmap newBitmap = (Bitmap)XmlParser.Deserialize(tempFile);
|
---|
| 838 |
|
---|
| 839 | Assert.AreEqual(bitmap.Size, newBitmap.Size);
|
---|
| 840 | for (int i = 0; i < bitmap.Size.Width; i++)
|
---|
| 841 | for (int j = 0; j < bitmap.Size.Height; j++)
|
---|
| 842 | Assert.AreEqual(bitmap.GetPixel(i, j), newBitmap.GetPixel(i, j));
|
---|
| 843 | }
|
---|
| 844 |
|
---|
| 845 | [StorableClass]
|
---|
| 846 | private class PersistenceHooks {
|
---|
| 847 | [Storable]
|
---|
| 848 | public int a;
|
---|
| 849 | [Storable]
|
---|
| 850 | public int b;
|
---|
| 851 | public int sum;
|
---|
| 852 | public bool WasSerialized { get; private set; }
|
---|
| 853 | [StorableHook(HookType.BeforeSerialization)]
|
---|
| 854 | void PreSerializationHook() {
|
---|
| 855 | WasSerialized = true;
|
---|
| 856 | }
|
---|
| 857 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 858 | void PostDeserializationHook() {
|
---|
| 859 | sum = a + b;
|
---|
| 860 | }
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 | [TestMethod]
|
---|
[9778] | 864 | [TestCategory("Persistence")]
|
---|
| 865 | [TestProperty("Time", "short")]
|
---|
[6846] | 866 | public void HookTest() {
|
---|
| 867 | PersistenceHooks hookTest = new PersistenceHooks();
|
---|
| 868 | hookTest.a = 2;
|
---|
| 869 | hookTest.b = 5;
|
---|
| 870 | Assert.IsFalse(hookTest.WasSerialized);
|
---|
| 871 | Assert.AreEqual(hookTest.sum, 0);
|
---|
| 872 | XmlGenerator.Serialize(hookTest, tempFile);
|
---|
| 873 | Assert.IsTrue(hookTest.WasSerialized);
|
---|
| 874 | Assert.AreEqual(hookTest.sum, 0);
|
---|
| 875 | PersistenceHooks newHookTest = (PersistenceHooks)XmlParser.Deserialize(tempFile);
|
---|
| 876 | Assert.AreEqual(newHookTest.a, hookTest.a);
|
---|
| 877 | Assert.AreEqual(newHookTest.b, hookTest.b);
|
---|
| 878 | Assert.AreEqual(newHookTest.sum, newHookTest.a + newHookTest.b);
|
---|
| 879 | Assert.IsFalse(newHookTest.WasSerialized);
|
---|
| 880 | }
|
---|
| 881 |
|
---|
| 882 | [StorableClass]
|
---|
| 883 | private class CustomConstructor {
|
---|
| 884 | public string Value = "none";
|
---|
| 885 | public CustomConstructor() {
|
---|
| 886 | Value = "default";
|
---|
| 887 | }
|
---|
| 888 | [StorableConstructor]
|
---|
| 889 | private CustomConstructor(bool deserializing) {
|
---|
| 890 | Assert.IsTrue(deserializing);
|
---|
| 891 | Value = "persistence";
|
---|
| 892 | }
|
---|
| 893 | }
|
---|
| 894 |
|
---|
| 895 | [TestMethod]
|
---|
[9778] | 896 | [TestCategory("Persistence")]
|
---|
| 897 | [TestProperty("Time", "short")]
|
---|
[6846] | 898 | public void TestCustomConstructor() {
|
---|
| 899 | CustomConstructor cc = new CustomConstructor();
|
---|
| 900 | Assert.AreEqual(cc.Value, "default");
|
---|
| 901 | XmlGenerator.Serialize(cc, tempFile);
|
---|
| 902 | CustomConstructor newCC = (CustomConstructor)XmlParser.Deserialize(tempFile);
|
---|
| 903 | Assert.AreEqual(newCC.Value, "persistence");
|
---|
| 904 | }
|
---|
| 905 |
|
---|
| 906 | [StorableClass]
|
---|
| 907 | public class ExplodingDefaultConstructor {
|
---|
| 908 | public ExplodingDefaultConstructor() {
|
---|
| 909 | throw new Exception("this constructor will always fail");
|
---|
| 910 | }
|
---|
| 911 | public ExplodingDefaultConstructor(string password) {
|
---|
| 912 | }
|
---|
| 913 | }
|
---|
| 914 |
|
---|
| 915 | [TestMethod]
|
---|
[9778] | 916 | [TestCategory("Persistence")]
|
---|
| 917 | [TestProperty("Time", "short")]
|
---|
[6846] | 918 | public void TestConstructorExceptionUnwrapping() {
|
---|
| 919 | ExplodingDefaultConstructor x = new ExplodingDefaultConstructor("password");
|
---|
| 920 | XmlGenerator.Serialize(x, tempFile);
|
---|
| 921 | try {
|
---|
| 922 | ExplodingDefaultConstructor newX = (ExplodingDefaultConstructor)XmlParser.Deserialize(tempFile);
|
---|
| 923 | Assert.Fail("Exception expected");
|
---|
[9778] | 924 | } catch (PersistenceException pe) {
|
---|
[6846] | 925 | Assert.AreEqual(pe.InnerException.Message, "this constructor will always fail");
|
---|
| 926 | }
|
---|
| 927 | }
|
---|
| 928 |
|
---|
| 929 | [TestMethod]
|
---|
[9778] | 930 | [TestCategory("Persistence")]
|
---|
| 931 | [TestProperty("Time", "short")]
|
---|
[6846] | 932 | public void TestRejectionJustifications() {
|
---|
| 933 | NonSerializable ns = new NonSerializable();
|
---|
| 934 | try {
|
---|
| 935 | XmlGenerator.Serialize(ns, tempFile);
|
---|
| 936 | Assert.Fail("PersistenceException expected");
|
---|
[9778] | 937 | } catch (PersistenceException x) {
|
---|
[6846] | 938 | Assert.IsTrue(x.Message.Contains(new StorableSerializer().JustifyRejection(typeof(NonSerializable))));
|
---|
| 939 | }
|
---|
| 940 | }
|
---|
| 941 |
|
---|
| 942 | [TestMethod]
|
---|
[9778] | 943 | [TestCategory("Persistence")]
|
---|
| 944 | [TestProperty("Time", "short")]
|
---|
[6846] | 945 | public void TestStreaming() {
|
---|
| 946 | using (MemoryStream stream = new MemoryStream()) {
|
---|
| 947 | Root r = InitializeComplexStorable();
|
---|
| 948 | XmlGenerator.Serialize(r, stream);
|
---|
| 949 | using (MemoryStream stream2 = new MemoryStream(stream.ToArray())) {
|
---|
| 950 | Root newR = (Root)XmlParser.Deserialize(stream2);
|
---|
| 951 | CompareComplexStorables(r, newR);
|
---|
| 952 | }
|
---|
| 953 | }
|
---|
| 954 | }
|
---|
| 955 |
|
---|
| 956 | [StorableClass]
|
---|
| 957 | public class HookInheritanceTestBase {
|
---|
| 958 | [Storable]
|
---|
| 959 | public object a;
|
---|
| 960 | public object link;
|
---|
| 961 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 962 | private void relink() {
|
---|
| 963 | link = a;
|
---|
| 964 | }
|
---|
| 965 | }
|
---|
| 966 |
|
---|
| 967 | [StorableClass]
|
---|
| 968 | public class HookInheritanceTestDerivedClass : HookInheritanceTestBase {
|
---|
| 969 | [Storable]
|
---|
| 970 | public object b;
|
---|
| 971 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 972 | private void relink() {
|
---|
| 973 | Assert.AreSame(a, link);
|
---|
| 974 | link = b;
|
---|
| 975 | }
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 | [TestMethod]
|
---|
[9778] | 979 | [TestCategory("Persistence")]
|
---|
| 980 | [TestProperty("Time", "short")]
|
---|
[6846] | 981 | public void TestLinkInheritance() {
|
---|
| 982 | HookInheritanceTestDerivedClass c = new HookInheritanceTestDerivedClass();
|
---|
| 983 | c.a = new object();
|
---|
| 984 | XmlGenerator.Serialize(c, tempFile);
|
---|
| 985 | HookInheritanceTestDerivedClass newC = (HookInheritanceTestDerivedClass)XmlParser.Deserialize(tempFile);
|
---|
| 986 | Assert.AreSame(c.b, c.link);
|
---|
| 987 | }
|
---|
| 988 |
|
---|
| 989 | [StorableClass(StorableClassType.AllFields)]
|
---|
| 990 | public class AllFieldsStorable {
|
---|
| 991 | public int Value1 = 1;
|
---|
| 992 | [Storable]
|
---|
| 993 | public int Value2 = 2;
|
---|
| 994 | public int Value3 { get; private set; }
|
---|
| 995 | public int Value4 { get; private set; }
|
---|
| 996 | [StorableConstructor]
|
---|
| 997 | public AllFieldsStorable(bool isDeserializing) {
|
---|
| 998 | if (!isDeserializing) {
|
---|
| 999 | Value1 = 12;
|
---|
| 1000 | Value2 = 23;
|
---|
| 1001 | Value3 = 34;
|
---|
| 1002 | Value4 = 56;
|
---|
| 1003 | }
|
---|
| 1004 | }
|
---|
| 1005 | }
|
---|
| 1006 |
|
---|
| 1007 | [TestMethod]
|
---|
[9778] | 1008 | [TestCategory("Persistence")]
|
---|
| 1009 | [TestProperty("Time", "short")]
|
---|
[6846] | 1010 | public void TestStorableClassDiscoveryAllFields() {
|
---|
| 1011 | AllFieldsStorable afs = new AllFieldsStorable(false);
|
---|
| 1012 | XmlGenerator.Serialize(afs, tempFile);
|
---|
| 1013 | AllFieldsStorable newAfs = (AllFieldsStorable)XmlParser.Deserialize(tempFile);
|
---|
| 1014 | Assert.AreEqual(afs.Value1, newAfs.Value1);
|
---|
| 1015 | Assert.AreEqual(afs.Value2, newAfs.Value2);
|
---|
| 1016 | Assert.AreEqual(0, newAfs.Value3);
|
---|
| 1017 | Assert.AreEqual(0, newAfs.Value4);
|
---|
| 1018 | }
|
---|
| 1019 |
|
---|
| 1020 | [StorableClass(StorableClassType.AllProperties)]
|
---|
| 1021 | public class AllPropertiesStorable {
|
---|
| 1022 | public int Value1 = 1;
|
---|
| 1023 | [Storable]
|
---|
| 1024 | public int Value2 = 2;
|
---|
| 1025 | public int Value3 { get; private set; }
|
---|
| 1026 | public int Value4 { get; private set; }
|
---|
| 1027 | [StorableConstructor]
|
---|
| 1028 | public AllPropertiesStorable(bool isDeserializing) {
|
---|
| 1029 | if (!isDeserializing) {
|
---|
| 1030 | Value1 = 12;
|
---|
| 1031 | Value2 = 23;
|
---|
| 1032 | Value3 = 34;
|
---|
| 1033 | Value4 = 56;
|
---|
| 1034 | }
|
---|
| 1035 | }
|
---|
| 1036 | }
|
---|
| 1037 |
|
---|
| 1038 | [TestMethod]
|
---|
[9778] | 1039 | [TestCategory("Persistence")]
|
---|
| 1040 | [TestProperty("Time", "short")]
|
---|
[6846] | 1041 | public void TestStorableClassDiscoveryAllProperties() {
|
---|
| 1042 | AllPropertiesStorable afs = new AllPropertiesStorable(false);
|
---|
| 1043 | XmlGenerator.Serialize(afs, tempFile);
|
---|
| 1044 | AllPropertiesStorable newAfs = (AllPropertiesStorable)XmlParser.Deserialize(tempFile);
|
---|
| 1045 | Assert.AreEqual(1, newAfs.Value1);
|
---|
| 1046 | Assert.AreEqual(2, newAfs.Value2);
|
---|
| 1047 | Assert.AreEqual(afs.Value3, newAfs.Value3);
|
---|
| 1048 | Assert.AreEqual(afs.Value4, newAfs.Value4);
|
---|
| 1049 |
|
---|
| 1050 | }
|
---|
| 1051 |
|
---|
| 1052 | [StorableClass(StorableClassType.AllFieldsAndAllProperties)]
|
---|
| 1053 | public class AllFieldsAndAllPropertiesStorable {
|
---|
| 1054 | public int Value1 = 1;
|
---|
| 1055 | [Storable]
|
---|
| 1056 | public int Value2 = 2;
|
---|
| 1057 | public int Value3 { get; private set; }
|
---|
| 1058 | public int Value4 { get; private set; }
|
---|
| 1059 | [StorableConstructor]
|
---|
| 1060 | public AllFieldsAndAllPropertiesStorable(bool isDeserializing) {
|
---|
| 1061 | if (!isDeserializing) {
|
---|
| 1062 | Value1 = 12;
|
---|
| 1063 | Value2 = 23;
|
---|
| 1064 | Value3 = 34;
|
---|
| 1065 | Value4 = 56;
|
---|
| 1066 | }
|
---|
| 1067 | }
|
---|
| 1068 | }
|
---|
| 1069 |
|
---|
| 1070 | [TestMethod]
|
---|
[9778] | 1071 | [TestCategory("Persistence")]
|
---|
| 1072 | [TestProperty("Time", "short")]
|
---|
[6846] | 1073 | public void TestStorableClassDiscoveryAllFieldsAndAllProperties() {
|
---|
| 1074 | AllFieldsAndAllPropertiesStorable afs = new AllFieldsAndAllPropertiesStorable(false);
|
---|
| 1075 | XmlGenerator.Serialize(afs, tempFile);
|
---|
| 1076 | AllFieldsAndAllPropertiesStorable newAfs = (AllFieldsAndAllPropertiesStorable)XmlParser.Deserialize(tempFile);
|
---|
| 1077 | Assert.AreEqual(afs.Value1, newAfs.Value1);
|
---|
| 1078 | Assert.AreEqual(afs.Value2, newAfs.Value2);
|
---|
| 1079 | Assert.AreEqual(afs.Value3, newAfs.Value3);
|
---|
| 1080 | Assert.AreEqual(afs.Value4, newAfs.Value4);
|
---|
| 1081 | }
|
---|
| 1082 |
|
---|
| 1083 | [StorableClass(StorableClassType.MarkedOnly)]
|
---|
| 1084 | public class MarkedOnlyStorable {
|
---|
| 1085 | public int Value1 = 1;
|
---|
| 1086 | [Storable]
|
---|
| 1087 | public int Value2 = 2;
|
---|
| 1088 | public int Value3 { get; private set; }
|
---|
| 1089 | public int Value4 { get; private set; }
|
---|
| 1090 | [StorableConstructor]
|
---|
| 1091 | public MarkedOnlyStorable(bool isDeserializing) {
|
---|
| 1092 | if (!isDeserializing) {
|
---|
| 1093 | Value1 = 12;
|
---|
| 1094 | Value2 = 23;
|
---|
| 1095 | Value3 = 34;
|
---|
| 1096 | Value4 = 56;
|
---|
| 1097 | }
|
---|
| 1098 | }
|
---|
| 1099 | }
|
---|
| 1100 |
|
---|
| 1101 | [TestMethod]
|
---|
[9778] | 1102 | [TestCategory("Persistence")]
|
---|
| 1103 | [TestProperty("Time", "short")]
|
---|
[6846] | 1104 | public void TestStorableClassDiscoveryMarkedOnly() {
|
---|
| 1105 | MarkedOnlyStorable afs = new MarkedOnlyStorable(false);
|
---|
| 1106 | XmlGenerator.Serialize(afs, tempFile);
|
---|
| 1107 | MarkedOnlyStorable newAfs = (MarkedOnlyStorable)XmlParser.Deserialize(tempFile);
|
---|
| 1108 | Assert.AreEqual(1, newAfs.Value1);
|
---|
| 1109 | Assert.AreEqual(afs.Value2, newAfs.Value2);
|
---|
| 1110 | Assert.AreEqual(0, newAfs.Value3);
|
---|
| 1111 | Assert.AreEqual(0, newAfs.Value4);
|
---|
| 1112 | }
|
---|
| 1113 |
|
---|
| 1114 | [TestMethod]
|
---|
[9778] | 1115 | [TestCategory("Persistence")]
|
---|
| 1116 | [TestProperty("Time", "short")]
|
---|
[6846] | 1117 | public void TestLineEndings() {
|
---|
| 1118 | List<string> lineBreaks = new List<string> { "\r\n", "\n", "\r", "\n\r", Environment.NewLine };
|
---|
| 1119 | List<string> lines = new List<string>();
|
---|
| 1120 | foreach (var br in lineBreaks)
|
---|
| 1121 | lines.Add("line1" + br + "line2");
|
---|
| 1122 | XmlGenerator.Serialize(lines, tempFile);
|
---|
| 1123 | List<string> newLines = XmlParser.Deserialize<List<string>>(tempFile);
|
---|
| 1124 | Assert.AreEqual(lines.Count, newLines.Count);
|
---|
| 1125 | for (int i = 0; i < lineBreaks.Count; i++) {
|
---|
| 1126 | Assert.AreEqual(lines[i], newLines[i]);
|
---|
| 1127 | }
|
---|
| 1128 | }
|
---|
| 1129 |
|
---|
| 1130 | [TestMethod]
|
---|
[9778] | 1131 | [TestCategory("Persistence")]
|
---|
| 1132 | [TestProperty("Time", "short")]
|
---|
[6846] | 1133 | public void TestSpecialNumbers() {
|
---|
| 1134 | List<double> specials = new List<double>() { 1.0 / 0, -1.0 / 0, 0.0 / 0 };
|
---|
| 1135 | Assert.IsTrue(double.IsPositiveInfinity(specials[0]));
|
---|
| 1136 | Assert.IsTrue(double.IsNegativeInfinity(specials[1]));
|
---|
| 1137 | Assert.IsTrue(double.IsNaN(specials[2]));
|
---|
| 1138 | XmlGenerator.Serialize(specials, tempFile);
|
---|
| 1139 | List<double> newSpecials = XmlParser.Deserialize<List<double>>(tempFile);
|
---|
| 1140 | Assert.IsTrue(double.IsPositiveInfinity(newSpecials[0]));
|
---|
| 1141 | Assert.IsTrue(double.IsNegativeInfinity(newSpecials[1]));
|
---|
| 1142 | Assert.IsTrue(double.IsNaN(newSpecials[2]));
|
---|
| 1143 | }
|
---|
| 1144 |
|
---|
| 1145 | [TestMethod]
|
---|
[9778] | 1146 | [TestCategory("Persistence")]
|
---|
| 1147 | [TestProperty("Time", "short")]
|
---|
[6846] | 1148 | public void TestStringSplit() {
|
---|
| 1149 | string s = "1.2;2.3;3.4;;;4.9";
|
---|
| 1150 | var l = s.EnumerateSplit(';').ToList();
|
---|
| 1151 | Assert.AreEqual("1.2", l[0]);
|
---|
| 1152 | Assert.AreEqual("2.3", l[1]);
|
---|
| 1153 | Assert.AreEqual("3.4", l[2]);
|
---|
| 1154 | Assert.AreEqual("4.9", l[3]);
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
| 1157 | [TestMethod]
|
---|
[9778] | 1158 | [TestCategory("Persistence")]
|
---|
| 1159 | [TestProperty("Time", "medium")]
|
---|
[6846] | 1160 | public void TestCompactNumberArraySerializer() {
|
---|
| 1161 | System.Random r = new System.Random();
|
---|
| 1162 | double[] a = new double[CompactNumberArray2StringSerializer.SPLIT_THRESHOLD * 2 + 1];
|
---|
| 1163 | for (int i = 0; i < a.Length; i++)
|
---|
| 1164 | a[i] = r.Next(10);
|
---|
| 1165 | var config = ConfigurationService.Instance.GetDefaultConfig(new XmlFormat());
|
---|
| 1166 | config = new Configuration(config.Format,
|
---|
| 1167 | config.PrimitiveSerializers.Where(s => s.SourceType != typeof(double[])),
|
---|
| 1168 | config.CompositeSerializers);
|
---|
| 1169 | XmlGenerator.Serialize(a, tempFile, config);
|
---|
| 1170 | double[] newA = XmlParser.Deserialize<double[]>(tempFile);
|
---|
| 1171 | Assert.AreEqual(a.Length, newA.Length);
|
---|
| 1172 | for (int i = 0; i < a.Rank; i++) {
|
---|
| 1173 | Assert.AreEqual(a.GetLength(i), newA.GetLength(i));
|
---|
| 1174 | Assert.AreEqual(a.GetLowerBound(i), newA.GetLowerBound(i));
|
---|
| 1175 | }
|
---|
| 1176 | for (int i = 0; i < a.Length; i++) {
|
---|
| 1177 | Assert.AreEqual(a[i], newA[i]);
|
---|
| 1178 | }
|
---|
| 1179 | }
|
---|
| 1180 | private class IdentityComparer<T> : IEqualityComparer<T> {
|
---|
| 1181 |
|
---|
| 1182 | public bool Equals(T x, T y) {
|
---|
| 1183 | return x.Equals(y);
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
| 1186 | public int GetHashCode(T obj) {
|
---|
| 1187 | return obj.GetHashCode();
|
---|
| 1188 | }
|
---|
| 1189 | }
|
---|
| 1190 |
|
---|
| 1191 | [TestMethod]
|
---|
[9778] | 1192 | [TestCategory("Persistence")]
|
---|
| 1193 | [TestProperty("Time", "short")]
|
---|
[6846] | 1194 | public void TestHashSetSerializer() {
|
---|
| 1195 | var hashSets = new List<HashSet<int>>() {
|
---|
| 1196 | new HashSet<int>(new[] { 1, 2, 3 }),
|
---|
| 1197 | new HashSet<int>(new[] { 4, 5, 6 }, new IdentityComparer<int>()),
|
---|
| 1198 | };
|
---|
| 1199 | XmlGenerator.Serialize(hashSets, tempFile);
|
---|
| 1200 | var newHashSets = XmlParser.Deserialize<List<HashSet<int>>>(tempFile);
|
---|
| 1201 | Assert.IsTrue(newHashSets[0].Contains(1));
|
---|
| 1202 | Assert.IsTrue(newHashSets[0].Contains(2));
|
---|
| 1203 | Assert.IsTrue(newHashSets[0].Contains(3));
|
---|
| 1204 | Assert.IsTrue(newHashSets[1].Contains(4));
|
---|
| 1205 | Assert.IsTrue(newHashSets[1].Contains(5));
|
---|
| 1206 | Assert.IsTrue(newHashSets[1].Contains(6));
|
---|
| 1207 | Assert.AreEqual(newHashSets[0].Comparer.GetType(), new HashSet<int>().Comparer.GetType());
|
---|
| 1208 | Assert.AreEqual(newHashSets[1].Comparer.GetType(), typeof(IdentityComparer<int>));
|
---|
| 1209 | }
|
---|
| 1210 |
|
---|
| 1211 | [TestMethod]
|
---|
[9778] | 1212 | [TestCategory("Persistence")]
|
---|
| 1213 | [TestProperty("Time", "short")]
|
---|
[6846] | 1214 | public void TestConcreteDictionarySerializer() {
|
---|
| 1215 | var dictionaries = new List<Dictionary<int, int>>() {
|
---|
| 1216 | new Dictionary<int, int>(),
|
---|
| 1217 | new Dictionary<int, int>(new IdentityComparer<int>()),
|
---|
| 1218 | };
|
---|
| 1219 | dictionaries[0].Add(1, 1);
|
---|
| 1220 | dictionaries[0].Add(2, 2);
|
---|
| 1221 | dictionaries[0].Add(3, 3);
|
---|
| 1222 | dictionaries[1].Add(4, 4);
|
---|
| 1223 | dictionaries[1].Add(5, 5);
|
---|
| 1224 | dictionaries[1].Add(6, 6);
|
---|
| 1225 | XmlGenerator.Serialize(dictionaries, tempFile, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat()));
|
---|
| 1226 | var newDictionaries = XmlParser.Deserialize<List<Dictionary<int, int>>>(tempFile);
|
---|
| 1227 | Assert.IsTrue(newDictionaries[0].ContainsKey(1));
|
---|
| 1228 | Assert.IsTrue(newDictionaries[0].ContainsKey(2));
|
---|
| 1229 | Assert.IsTrue(newDictionaries[0].ContainsKey(3));
|
---|
| 1230 | Assert.IsTrue(newDictionaries[1].ContainsKey(4));
|
---|
| 1231 | Assert.IsTrue(newDictionaries[1].ContainsKey(5));
|
---|
| 1232 | Assert.IsTrue(newDictionaries[1].ContainsKey(6));
|
---|
| 1233 | Assert.IsTrue(newDictionaries[0].ContainsValue(1));
|
---|
| 1234 | Assert.IsTrue(newDictionaries[0].ContainsValue(2));
|
---|
| 1235 | Assert.IsTrue(newDictionaries[0].ContainsValue(3));
|
---|
| 1236 | Assert.IsTrue(newDictionaries[1].ContainsValue(4));
|
---|
| 1237 | Assert.IsTrue(newDictionaries[1].ContainsValue(5));
|
---|
| 1238 | Assert.IsTrue(newDictionaries[1].ContainsValue(6));
|
---|
| 1239 | Assert.AreEqual(new Dictionary<int, int>().Comparer.GetType(), newDictionaries[0].Comparer.GetType());
|
---|
| 1240 | Assert.AreEqual(typeof(IdentityComparer<int>), newDictionaries[1].Comparer.GetType());
|
---|
| 1241 | }
|
---|
| 1242 |
|
---|
| 1243 | [StorableClass]
|
---|
| 1244 | public class ReadOnlyFail {
|
---|
| 1245 | [Storable]
|
---|
| 1246 | public string ReadOnly {
|
---|
| 1247 | get { return "fail"; }
|
---|
| 1248 | }
|
---|
| 1249 | }
|
---|
| 1250 |
|
---|
| 1251 | [TestMethod]
|
---|
[9778] | 1252 | [TestCategory("Persistence")]
|
---|
| 1253 | [TestProperty("Time", "short")]
|
---|
[6846] | 1254 | public void TestReadOnlyFail() {
|
---|
| 1255 | try {
|
---|
| 1256 | XmlGenerator.Serialize(new ReadOnlyFail(), tempFile);
|
---|
| 1257 | Assert.Fail("Exception expected");
|
---|
[9778] | 1258 | } catch (PersistenceException) {
|
---|
| 1259 | } catch {
|
---|
[6846] | 1260 | Assert.Fail("PersistenceException expected");
|
---|
| 1261 | }
|
---|
| 1262 | }
|
---|
| 1263 |
|
---|
| 1264 |
|
---|
| 1265 | [StorableClass]
|
---|
| 1266 | public class WriteOnlyFail {
|
---|
| 1267 | [Storable]
|
---|
| 1268 | public string WriteOnly {
|
---|
| 1269 | set { throw new InvalidOperationException("this property should never be set."); }
|
---|
| 1270 | }
|
---|
| 1271 | }
|
---|
| 1272 |
|
---|
| 1273 | [TestMethod]
|
---|
[9778] | 1274 | [TestCategory("Persistence")]
|
---|
| 1275 | [TestProperty("Time", "short")]
|
---|
[6846] | 1276 | public void TestWriteOnlyFail() {
|
---|
| 1277 | try {
|
---|
| 1278 | XmlGenerator.Serialize(new WriteOnlyFail(), tempFile);
|
---|
| 1279 | Assert.Fail("Exception expected");
|
---|
[9778] | 1280 | } catch (PersistenceException) {
|
---|
| 1281 | } catch {
|
---|
[6846] | 1282 | Assert.Fail("PersistenceException expected.");
|
---|
| 1283 | }
|
---|
| 1284 | }
|
---|
| 1285 |
|
---|
| 1286 | [StorableClass]
|
---|
| 1287 | public class OneWayTest {
|
---|
| 1288 | public OneWayTest() { this.value = "default"; }
|
---|
| 1289 | public string value;
|
---|
| 1290 | [Storable(AllowOneWay = true)]
|
---|
| 1291 | public string ReadOnly {
|
---|
| 1292 | get { return "ReadOnly"; }
|
---|
| 1293 | }
|
---|
| 1294 | [Storable(AllowOneWay = true)]
|
---|
| 1295 | public string WriteOnly {
|
---|
| 1296 | set { this.value = value; }
|
---|
| 1297 | }
|
---|
| 1298 | }
|
---|
| 1299 |
|
---|
| 1300 | [TestMethod]
|
---|
[9778] | 1301 | [TestCategory("Persistence")]
|
---|
| 1302 | [TestProperty("Time", "short")]
|
---|
[6846] | 1303 | public void TestOneWaySerialization() {
|
---|
| 1304 | var test = new OneWayTest();
|
---|
| 1305 | var serializer = new Serializer(test, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat()));
|
---|
| 1306 | var it = serializer.GetEnumerator();
|
---|
| 1307 | it.MoveNext();
|
---|
| 1308 | Assert.AreEqual("ROOT", ((BeginToken)it.Current).Name); it.MoveNext();
|
---|
| 1309 | Assert.AreEqual("ReadOnly", ((PrimitiveToken)it.Current).Name); it.MoveNext();
|
---|
| 1310 | Assert.AreEqual("ROOT", ((EndToken)it.Current).Name); it.MoveNext();
|
---|
| 1311 | var deserializer = new Deserializer(new[] {
|
---|
| 1312 | new TypeMapping(0, typeof(OneWayTest).AssemblyQualifiedName, typeof(StorableSerializer).AssemblyQualifiedName),
|
---|
| 1313 | new TypeMapping(1, typeof(string).AssemblyQualifiedName, typeof(String2XmlSerializer).AssemblyQualifiedName) });
|
---|
| 1314 | var newTest = (OneWayTest)deserializer.Deserialize(new ISerializationToken[] {
|
---|
| 1315 | new BeginToken("ROOT", 0, 0),
|
---|
| 1316 | new PrimitiveToken("WriteOnly", 1, 1, new XmlString("<![CDATA[serial data]]>")),
|
---|
| 1317 | new EndToken("ROOT", 0, 0)
|
---|
| 1318 | });
|
---|
| 1319 | Assert.AreEqual("serial data", newTest.value);
|
---|
| 1320 | }
|
---|
| 1321 |
|
---|
| 1322 | [TestMethod]
|
---|
[9778] | 1323 | [TestCategory("Persistence")]
|
---|
| 1324 | [TestProperty("Time", "short")]
|
---|
[6846] | 1325 | public void TestTypeCacheExport() {
|
---|
| 1326 | var test = new List<List<int>>();
|
---|
| 1327 | test.Add(new List<int>() { 1, 2, 3 });
|
---|
| 1328 | IEnumerable<Type> types;
|
---|
| 1329 | using (var stream = new MemoryStream()) {
|
---|
| 1330 | XmlGenerator.Serialize(test, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, out types);
|
---|
| 1331 | }
|
---|
| 1332 | List<Type> t = new List<Type>(types);
|
---|
| 1333 | // Assert.IsTrue(t.Contains(typeof(int))); not serialized as an int list is directly transformed into a string
|
---|
| 1334 | Assert.IsTrue(t.Contains(typeof(List<int>)));
|
---|
| 1335 | Assert.IsTrue(t.Contains(typeof(List<List<int>>)));
|
---|
| 1336 | Assert.AreEqual(t.Count, 2);
|
---|
| 1337 | }
|
---|
| 1338 |
|
---|
| 1339 | [TestMethod]
|
---|
[9778] | 1340 | [TestCategory("Persistence")]
|
---|
| 1341 | [TestProperty("Time", "short")]
|
---|
[6846] | 1342 | public void TupleTest() {
|
---|
| 1343 | var t1 = Tuple.Create(1);
|
---|
| 1344 | var t2 = Tuple.Create('1', "2");
|
---|
| 1345 | var t3 = Tuple.Create(3.0, 3f, 5);
|
---|
| 1346 | var t4 = Tuple.Create(Tuple.Create(1, 2, 3), Tuple.Create(4, 5, 6), Tuple.Create(8, 9, 10));
|
---|
| 1347 | var tuple = Tuple.Create(t1, t2, t3, t4);
|
---|
| 1348 | XmlGenerator.Serialize(tuple, tempFile);
|
---|
| 1349 | 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);
|
---|
| 1350 | Assert.AreEqual(tuple, newTuple);
|
---|
| 1351 | }
|
---|
| 1352 |
|
---|
| 1353 | [TestMethod]
|
---|
[9778] | 1354 | [TestCategory("Persistence")]
|
---|
| 1355 | [TestProperty("Time", "short")]
|
---|
[6846] | 1356 | public void FontTest() {
|
---|
| 1357 | List<Font> fonts = new List<Font>() {
|
---|
| 1358 | new Font(FontFamily.GenericSansSerif, 12),
|
---|
| 1359 | new Font("Times New Roman", 21, FontStyle.Bold, GraphicsUnit.Pixel),
|
---|
| 1360 | new Font("Courier New", 10, FontStyle.Underline, GraphicsUnit.Document),
|
---|
| 1361 | new Font("Helvetica", 21, FontStyle.Strikeout, GraphicsUnit.Inch, 0, true),
|
---|
| 1362 | };
|
---|
| 1363 | XmlGenerator.Serialize(fonts, tempFile);
|
---|
| 1364 | var newFonts = XmlParser.Deserialize<List<Font>>(tempFile);
|
---|
| 1365 | Assert.AreEqual(fonts[0], newFonts[0]);
|
---|
| 1366 | Assert.AreEqual(fonts[1], newFonts[1]);
|
---|
| 1367 | Assert.AreEqual(fonts[2], newFonts[2]);
|
---|
| 1368 | Assert.AreEqual(fonts[3], newFonts[3]);
|
---|
| 1369 | }
|
---|
| 1370 |
|
---|
| 1371 | [TestMethod]
|
---|
[9778] | 1372 | [TestCategory("Persistence")]
|
---|
| 1373 | [TestProperty("Time", "medium")]
|
---|
[6846] | 1374 | public void ConcurrencyTest() {
|
---|
| 1375 | int n = 20;
|
---|
| 1376 | Task[] tasks = new Task[n];
|
---|
| 1377 | for (int i = 0; i < n; i++) {
|
---|
| 1378 | tasks[i] = Task.Factory.StartNew((idx) => {
|
---|
| 1379 | byte[] data;
|
---|
| 1380 | using (var stream = new MemoryStream()) {
|
---|
| 1381 | XmlGenerator.Serialize(new GeneticAlgorithm(), stream);
|
---|
| 1382 | data = stream.ToArray();
|
---|
| 1383 | }
|
---|
| 1384 | }, i);
|
---|
| 1385 | }
|
---|
| 1386 | Task.WaitAll(tasks);
|
---|
| 1387 | }
|
---|
| 1388 |
|
---|
| 1389 | [TestMethod]
|
---|
[9778] | 1390 | [TestCategory("Persistence")]
|
---|
| 1391 | [TestProperty("Time", "medium")]
|
---|
[6846] | 1392 | public void ConcurrentBitmapTest() {
|
---|
| 1393 | Bitmap b = new Bitmap(300, 300);
|
---|
| 1394 | System.Random r = new System.Random();
|
---|
| 1395 | for (int x = 0; x < b.Height; x++) {
|
---|
| 1396 | for (int y = 0; y < b.Width; y++) {
|
---|
| 1397 | b.SetPixel(x, y, Color.FromArgb(r.Next()));
|
---|
| 1398 | }
|
---|
| 1399 | }
|
---|
| 1400 | Task[] tasks = new Task[20];
|
---|
| 1401 | byte[][] datas = new byte[tasks.Length][];
|
---|
| 1402 | for (int i = 0; i < tasks.Length; i++) {
|
---|
| 1403 | tasks[i] = Task.Factory.StartNew((idx) => {
|
---|
| 1404 | using (var stream = new MemoryStream()) {
|
---|
| 1405 | XmlGenerator.Serialize(b, stream);
|
---|
| 1406 | datas[(int)idx] = stream.ToArray();
|
---|
| 1407 | }
|
---|
| 1408 | }, i);
|
---|
| 1409 | }
|
---|
| 1410 | Task.WaitAll(tasks);
|
---|
| 1411 | }
|
---|
| 1412 |
|
---|
[9778] | 1413 | public class G<T, T2> {
|
---|
| 1414 | public class S { }
|
---|
| 1415 | public class S2<T3, T4> { }
|
---|
[9005] | 1416 | }
|
---|
| 1417 |
|
---|
| 1418 | [TestMethod]
|
---|
[9778] | 1419 | [TestCategory("Persistence")]
|
---|
| 1420 | [TestProperty("Time", "short")]
|
---|
[9005] | 1421 | public void TestInternalClassOfGeneric() {
|
---|
| 1422 | var s = new G<int, char>.S();
|
---|
| 1423 | var typeName = s.GetType().AssemblyQualifiedName;
|
---|
| 1424 | Assert.AreEqual(
|
---|
| 1425 | "UseCases.G<Int32,Char>.S",
|
---|
| 1426 | TypeNameParser.Parse(typeName).GetTypeNameInCode(false));
|
---|
| 1427 | XmlGenerator.Serialize(s, tempFile);
|
---|
| 1428 | var s1 = XmlParser.Deserialize(tempFile);
|
---|
| 1429 | }
|
---|
| 1430 |
|
---|
| 1431 | [TestMethod]
|
---|
[9778] | 1432 | [TestCategory("Persistence")]
|
---|
| 1433 | [TestProperty("Time", "short")]
|
---|
[9005] | 1434 | public void TestInternalClassOfGeneric2() {
|
---|
| 1435 | var s = new G<int, float>.S2<int, char>();
|
---|
| 1436 | var typeName = s.GetType().AssemblyQualifiedName;
|
---|
| 1437 | Assert.AreEqual(
|
---|
| 1438 | "UseCases.G<Int32,Single>.S2<Int32,Char>",
|
---|
| 1439 | TypeNameParser.Parse(typeName).GetTypeNameInCode(false));
|
---|
| 1440 | XmlGenerator.Serialize(s, tempFile);
|
---|
| 1441 | var s1 = XmlParser.Deserialize(tempFile);
|
---|
| 1442 | }
|
---|
| 1443 |
|
---|
[10895] | 1444 | [TestMethod]
|
---|
| 1445 | [TestCategory("Persistence")]
|
---|
| 1446 | [TestProperty("Time", "short")]
|
---|
| 1447 | public void TestSpecialCharacters() {
|
---|
| 1448 | var s = "abc" + "\x15" + "def";
|
---|
| 1449 | XmlGenerator.Serialize(s, tempFile);
|
---|
| 1450 | var newS = XmlParser.Deserialize(tempFile);
|
---|
| 1451 | Assert.AreEqual(s, newS);
|
---|
| 1452 | }
|
---|
| 1453 |
|
---|
| 1454 | [TestMethod]
|
---|
| 1455 | [TestCategory("Persistence")]
|
---|
| 1456 | [TestProperty("Time", "short")]
|
---|
| 1457 | public void TestByteArray() {
|
---|
| 1458 | var b = new byte[3];
|
---|
| 1459 | b[0] = 0;
|
---|
| 1460 | b[1] = 200;
|
---|
| 1461 | b[2] = byte.MaxValue;
|
---|
| 1462 | XmlGenerator.Serialize(b, tempFile);
|
---|
| 1463 | var newB = (byte[]) XmlParser.Deserialize(tempFile);
|
---|
| 1464 | CollectionAssert.AreEqual(b, newB);
|
---|
| 1465 | }
|
---|
| 1466 |
|
---|
[10896] | 1467 | [TestMethod]
|
---|
| 1468 | [TestCategory("Persistence")]
|
---|
| 1469 | [TestProperty("Time", "short")]
|
---|
| 1470 | public void TestOptionalNumberEnumerable() {
|
---|
| 1471 | var values = new List<double?> {0, null, double.NaN, double.PositiveInfinity, double.MaxValue, 1};
|
---|
| 1472 | XmlGenerator.Serialize(values, tempFile);
|
---|
| 1473 | var newValues = (List<double?>) XmlParser.Deserialize(tempFile);
|
---|
| 1474 | CollectionAssert.AreEqual(values, newValues);
|
---|
| 1475 | }
|
---|
[10895] | 1476 |
|
---|
[11352] | 1477 | [TestMethod]
|
---|
| 1478 | [TestCategory("Persistence")]
|
---|
| 1479 | [TestProperty("Time", "short")]
|
---|
| 1480 | public void TestOptionalDateTimeEnumerable() {
|
---|
| 1481 | var values = new List<DateTime?> { DateTime.MinValue, null, DateTime.Now, DateTime.Now.Add(TimeSpan.FromDays(1)),
|
---|
| 1482 | DateTime.ParseExact("10.09.2014 12:21", "dd.MM.yyyy hh:mm", CultureInfo.InvariantCulture), DateTime.MaxValue};
|
---|
| 1483 | XmlGenerator.Serialize(values, tempFile);
|
---|
| 1484 | var newValues = (List<DateTime?>) XmlParser.Deserialize(tempFile);
|
---|
| 1485 | CollectionAssert.AreEqual(values, newValues);
|
---|
| 1486 | }
|
---|
[10896] | 1487 |
|
---|
[11352] | 1488 | [TestMethod]
|
---|
| 1489 | [TestCategory("Persistence")]
|
---|
| 1490 | [TestProperty("Time", "short")]
|
---|
| 1491 | public void TestStringEnumerable() {
|
---|
| 1492 | var values = new List<string> {"", null, "s", "string", string.Empty, "123", "<![CDATA[nice]]>", "<![CDATA[nasty unterminated"};
|
---|
| 1493 | XmlGenerator.Serialize(values, tempFile);
|
---|
| 1494 | var newValues = (List<String>) XmlParser.Deserialize(tempFile);
|
---|
| 1495 | CollectionAssert.AreEqual(values, newValues);
|
---|
| 1496 | }
|
---|
| 1497 |
|
---|
| 1498 | [TestMethod]
|
---|
| 1499 | [TestCategory("Persistence")]
|
---|
| 1500 | [TestProperty("Time", "short")]
|
---|
| 1501 | public void TestUnicodeCharArray() {
|
---|
| 1502 | var s = Encoding.UTF8.GetChars(new byte[] {0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb});
|
---|
| 1503 | XmlGenerator.Serialize(s, tempFile);
|
---|
| 1504 | var newS = (char[])XmlParser.Deserialize(tempFile);
|
---|
| 1505 | CollectionAssert.AreEqual(s, newS);
|
---|
| 1506 | }
|
---|
| 1507 |
|
---|
| 1508 | [TestMethod]
|
---|
| 1509 | [TestCategory("Persistence")]
|
---|
| 1510 | [TestProperty("Time", "short")]
|
---|
| 1511 | public void TestUnicode() {
|
---|
| 1512 | var s = Encoding.UTF8.GetString(new byte[] {0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb});
|
---|
| 1513 | XmlGenerator.Serialize(s, tempFile);
|
---|
| 1514 | var newS = XmlParser.Deserialize(tempFile);
|
---|
| 1515 | Assert.AreEqual(s, newS);
|
---|
| 1516 | }
|
---|
| 1517 |
|
---|
[11353] | 1518 | [TestMethod]
|
---|
| 1519 | [TestCategory("Persistence")]
|
---|
| 1520 | [TestProperty("Time", "short")]
|
---|
| 1521 | public void TestQueue() {
|
---|
| 1522 | var q = new Queue<int>(new[] {1, 2, 3, 4, 0});
|
---|
| 1523 | XmlGenerator.Serialize(q, tempFile);
|
---|
| 1524 | var newQ = (Queue<int>)XmlParser.Deserialize(tempFile);
|
---|
| 1525 | CollectionAssert.AreEqual(q, newQ);
|
---|
| 1526 | }
|
---|
[11352] | 1527 |
|
---|
[11353] | 1528 |
|
---|
| 1529 |
|
---|
[6846] | 1530 | [ClassInitialize]
|
---|
| 1531 | public static void Initialize(TestContext testContext) {
|
---|
| 1532 | ConfigurationService.Instance.Reset();
|
---|
| 1533 | }
|
---|
| 1534 | }
|
---|
| 1535 | }
|
---|