Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/25/19 17:18:21 (5 years ago)
Author:
gkronber
Message:

#2936: merged r16117:16706 from trunk/HeuristicLab.Tests to branch/HeuristicLab.Tests

Location:
branches/2936_GQAPIntegration/HeuristicLab.Tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2936_GQAPIntegration/HeuristicLab.Tests

  • branches/2936_GQAPIntegration/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCases.cs

    r15583 r16711  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    3131using System.Text.RegularExpressions;
    3232using System.Threading.Tasks;
     33using HEAL.Attic;
    3334using HeuristicLab.Algorithms.GeneticAlgorithm;
    3435using HeuristicLab.Persistence.Auxiliary;
     
    4344using HeuristicLab.Tests;
    4445using Microsoft.VisualStudio.TestTools.UnitTesting;
     46using Serializer = HeuristicLab.Persistence.Core.Serializer;
    4547
    4648namespace HeuristicLab.Persistence.Tests {
    4749
    48   [StorableClass]
     50  [StorableType("25A94A36-F3AA-4207-A3C9-54260D2A1724")]
    4951  public class NumberTest {
    5052    [Storable]
     
    9395        _ulong.GetHashCode();
    9496    }
     97
     98    [StorableConstructor]
     99    protected NumberTest(StorableConstructorFlag _) {
     100    }
     101    public NumberTest() {
     102    }
    95103  }
    96104
    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]
     105  [StorableType("BE44EB33-C8B7-4CDA-A564-3165558FEBE7")]
    107106  public class IntWrapper {
    108107
    109108    [Storable]
    110109    public int Value;
     110
     111    [StorableConstructor]
     112    protected IntWrapper(StorableConstructorFlag _) {
     113    }
    111114
    112115    private IntWrapper() { }
     
    127130  }
    128131
    129   [StorableClass]
     132  [StorableType("A7865A50-E5B7-4656-8648-F375FC678BA3")]
    130133  public class PrimitivesTest : NumberTest {
    131134    [Storable]
     
    155158        o.GetHashCode();
    156159    }
     160
     161    [StorableConstructor]
     162    protected PrimitivesTest(StorableConstructorFlag _) : base(_) {
     163    }
     164    public PrimitivesTest() {
     165    }
    157166  }
    158167
    159   public enum TestEnum { va1, va2, va3, va8 } ;
    160 
    161   [StorableClass]
     168  [StorableType("18245FB6-D650-4378-9197-CCE6BB678F66")]
     169  public enum TestEnum { va1, va2, va3, va8 };
     170
     171  [StorableType("F5646F6C-DCD3-47D7-BBE5-2B33812B06D7")]
    162172  public class RootBase {
    163173    [Storable]
     
    176186        myEnum.GetHashCode();
    177187    }
     188
     189    [StorableConstructor]
     190    protected RootBase(StorableConstructorFlag _) {
     191    }
     192    public RootBase() {
     193    }
    178194  }
    179195
    180   [StorableClass]
     196  [StorableType("A9F5AFC4-20D3-4898-A7B6-E78A2CCE343B")]
    181197  public class Root : RootBase {
    182198    [Storable]
     
    206222    [Storable(DefaultValue = "default")]
    207223    public string uninitialized;
     224
     225    [StorableConstructor]
     226    protected Root(StorableConstructorFlag _) : base(_) {
     227    }
     228    public Root() {
     229    }
    208230  }
    209231
     
    213235  public enum TrickyEnum { zero = 0, one = 1, two = 2 }
    214236
    215   [StorableClass]
     237  [StorableType("969350FD-1E94-4A6F-BA95-6D2D48014E73")]
    216238  public class EnumTest {
    217239    [Storable]
     
    221243    [Storable]
    222244    public TrickyEnum trickyEnum = (TrickyEnum)15;
     245
     246    [StorableConstructor]
     247    protected EnumTest(StorableConstructorFlag _) {
     248    }
     249    public EnumTest() {
     250    }
    223251  }
    224252
    225   [StorableClass]
     253  [StorableType("03CC855E-1DCA-451E-9BFB-3B06E7F96CAA")]
    226254  public class Custom {
    227255    [Storable]
     
    231259    [Storable]
    232260    public string name = "<![CDATA[<![CDATA[Serial]]>]]>";
     261
     262    [StorableConstructor]
     263    protected Custom(StorableConstructorFlag _) {
     264    }
     265
     266    public Custom() {
     267
     268    }
    233269  }
    234270
    235   [StorableClass]
     271  [StorableType("C3A632A5-F356-4E22-8659-BBE515638CDC")]
    236272  public class Manager {
    237273
     
    244280    [Storable]
    245281    public double? dbl;
     282
     283    [StorableConstructor]
     284    protected Manager(StorableConstructorFlag _) {
     285    }
     286    public Manager() {
     287    }
    246288  }
    247289
    248   [StorableClass]
     290  [StorableType("D694E112-47AF-47DF-8A89-F12F95FF43AB")]
    249291  public class C {
    250292    [Storable]
     
    252294    [Storable]
    253295    public KeyValuePair<List<C>, C> kvpList;
     296
     297    [StorableConstructor]
     298    protected C(StorableConstructorFlag _) {
     299    }
     300    public C() {
     301    }
    254302  }
    255303
     
    486534    }
    487535
    488     [StorableClass]
     536    [StorableType("8EA1D97E-91CD-4643-80B2-D66724E3ED4C")]
    489537    public class NestedType {
    490538      [Storable]
     
    498546      public override int GetHashCode() {
    499547        return value.GetHashCode();
     548      }
     549
     550      [StorableConstructor]
     551      protected NestedType(StorableConstructorFlag _) {
     552      }
     553      public NestedType() {
    500554      }
    501555    }
     
    589643        new Configuration(new XmlFormat(),
    590644          new List<IPrimitiveSerializer> { new String2XmlSerializer() },
    591           new List<ICompositeSerializer> { 
     645          new List<ICompositeSerializer> {
    592646            new StorableSerializer(),
    593647            new Number2StringSerializer() }));
     
    629683      Assert.AreEqual(newInts[0], newInts[1]);
    630684      Assert.AreNotSame(newInts[0], newInts[1]);
    631     }
    632 
    633     [TestMethod]
    634     [TestCategory("Persistence")]
    635     [TestProperty("Time", "short")]
    636     public void NonDefaultConstructorTest() {
    637       NonDefaultConstructorClass c = new NonDefaultConstructorClass(1);
    638       try {
    639         XmlGenerator.Serialize(c, tempFile);
    640         Assert.Fail("Exception not thrown");
    641       } catch (PersistenceException) {
    642       }
    643685    }
    644686
     
    753795    }
    754796
    755     [StorableClass]
     797    [StorableType("B7140365-BA53-4E12-A095-86A3AAF5D159")]
    756798    class Child {
    757799      [Storable]
    758800      public GrandParent grandParent;
    759     }
    760 
    761     [StorableClass]
     801
     802      [StorableConstructor]
     803      protected Child(StorableConstructorFlag _) {
     804      }
     805      public Child() {
     806      }
     807    }
     808
     809    [StorableType("45FAEF81-D8CD-47DB-B8DD-BBFE81F37181")]
    762810    class Parent {
    763811      [Storable]
    764812      public Child child;
    765     }
    766 
    767     [StorableClass]
     813
     814      [StorableConstructor]
     815      protected Parent(StorableConstructorFlag _) {
     816      }
     817      public Parent() {
     818      }
     819    }
     820
     821    [StorableType("0668DB55-89ED-4943-B0AB-245AB3167348")]
    768822    class GrandParent {
    769823      [Storable]
    770824      public Parent parent;
     825
     826      [StorableConstructor]
     827      protected GrandParent(StorableConstructorFlag _) {
     828      }
     829      public GrandParent() {
     830      }
    771831    }
    772832
     
    843903    }
    844904
    845     [StorableClass]
     905    [StorableType("92DD18D7-A43C-4DEE-9354-81196F4DC96D")]
    846906    private class PersistenceHooks {
    847907      [Storable]
     
    858918      void PostDeserializationHook() {
    859919        sum = a + b;
     920      }
     921
     922      [StorableConstructor]
     923      protected PersistenceHooks(StorableConstructorFlag _) {
     924      }
     925      public PersistenceHooks() {
    860926      }
    861927    }
     
    880946    }
    881947
    882     [StorableClass]
     948    [StorableType("3E9A9875-875F-41DD-AEEC-2924643C8E45")]
    883949    private class CustomConstructor {
    884950      public string Value = "none";
     
    887953      }
    888954      [StorableConstructor]
    889       private CustomConstructor(bool deserializing) {
    890         Assert.IsTrue(deserializing);
     955      private CustomConstructor(StorableConstructorFlag _) {
    891956        Value = "persistence";
    892957      }
     
    902967      CustomConstructor newCC = (CustomConstructor)XmlParser.Deserialize(tempFile);
    903968      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]
    916     [TestCategory("Persistence")]
    917     [TestProperty("Time", "short")]
    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");
    924       } catch (PersistenceException pe) {
    925         Assert.AreEqual(pe.InnerException.Message, "this constructor will always fail");
    926       }
    927969    }
    928970
     
    954996    }
    955997
    956     [StorableClass]
     998    [StorableType("BC0996AA-6544-4D0F-8CBD-1FCCF08F4637")]
    957999    public class HookInheritanceTestBase {
    9581000      [Storable]
     
    9631005        link = a;
    9641006      }
    965     }
    966 
    967     [StorableClass]
     1007
     1008      [StorableConstructor]
     1009      protected HookInheritanceTestBase(StorableConstructorFlag _) {
     1010      }
     1011      public HookInheritanceTestBase() {
     1012      }
     1013    }
     1014
     1015    [StorableType("E0CABA1E-EA8E-4B50-9840-1F80D23E8DC7")]
    9681016    public class HookInheritanceTestDerivedClass : HookInheritanceTestBase {
    9691017      [Storable]
     
    9741022        link = b;
    9751023      }
     1024
     1025      [StorableConstructor]
     1026      protected HookInheritanceTestDerivedClass(StorableConstructorFlag _) : base(_) {
     1027      }
     1028      public HookInheritanceTestDerivedClass() {
     1029      }
    9761030    }
    9771031
     
    9871041    }
    9881042
    989     [StorableClass(StorableClassType.AllFields)]
     1043    [StorableType(StorableMemberSelection.AllFields, "45B11367-791E-4967-86AA-D6B83EC3CDAE")]
    9901044    public class AllFieldsStorable {
    9911045      public int Value1 = 1;
     
    9951049      public int Value4 { get; private set; }
    9961050      [StorableConstructor]
    997       public AllFieldsStorable(bool isDeserializing) {
    998         if (!isDeserializing) {
    999           Value1 = 12;
    1000           Value2 = 23;
    1001           Value3 = 34;
    1002           Value4 = 56;
    1003         }
     1051      protected AllFieldsStorable(StorableConstructorFlag _) { }
     1052      public AllFieldsStorable() {
     1053        Value1 = 12;
     1054        Value2 = 23;
     1055        Value3 = 34;
     1056        Value4 = 56;
    10041057      }
    10051058    }
     
    10091062    [TestProperty("Time", "short")]
    10101063    public void TestStorableClassDiscoveryAllFields() {
    1011       AllFieldsStorable afs = new AllFieldsStorable(false);
     1064      AllFieldsStorable afs = new AllFieldsStorable();
    10121065      XmlGenerator.Serialize(afs, tempFile);
    10131066      AllFieldsStorable newAfs = (AllFieldsStorable)XmlParser.Deserialize(tempFile);
     
    10181071    }
    10191072
    1020     [StorableClass(StorableClassType.AllProperties)]
     1073    [StorableType(StorableMemberSelection.AllProperties, "BB7931E4-2900-430C-80FA-1CF3C41BA2C9")]
    10211074    public class AllPropertiesStorable {
    10221075      public int Value1 = 1;
     
    10261079      public int Value4 { get; private set; }
    10271080      [StorableConstructor]
    1028       public AllPropertiesStorable(bool isDeserializing) {
    1029         if (!isDeserializing) {
    1030           Value1 = 12;
    1031           Value2 = 23;
    1032           Value3 = 34;
    1033           Value4 = 56;
    1034         }
     1081      protected AllPropertiesStorable(StorableConstructorFlag _) { }
     1082      public AllPropertiesStorable() {
     1083        Value1 = 12;
     1084        Value2 = 23;
     1085        Value3 = 34;
     1086        Value4 = 56;
    10351087      }
    10361088    }
     
    10401092    [TestProperty("Time", "short")]
    10411093    public void TestStorableClassDiscoveryAllProperties() {
    1042       AllPropertiesStorable afs = new AllPropertiesStorable(false);
     1094      AllPropertiesStorable afs = new AllPropertiesStorable();
    10431095      XmlGenerator.Serialize(afs, tempFile);
    10441096      AllPropertiesStorable newAfs = (AllPropertiesStorable)XmlParser.Deserialize(tempFile);
     
    10501102    }
    10511103
    1052     [StorableClass(StorableClassType.AllFieldsAndAllProperties)]
     1104    [StorableType(StorableMemberSelection.AllFieldsAndAllProperties, "D81160A5-BCF9-409A-9082-EFB803C882BF")]
    10531105    public class AllFieldsAndAllPropertiesStorable {
    10541106      public int Value1 = 1;
     
    10581110      public int Value4 { get; private set; }
    10591111      [StorableConstructor]
    1060       public AllFieldsAndAllPropertiesStorable(bool isDeserializing) {
    1061         if (!isDeserializing) {
    1062           Value1 = 12;
    1063           Value2 = 23;
    1064           Value3 = 34;
    1065           Value4 = 56;
    1066         }
     1112      protected AllFieldsAndAllPropertiesStorable(StorableConstructorFlag _) { }
     1113      public AllFieldsAndAllPropertiesStorable() {
     1114        Value1 = 12;
     1115        Value2 = 23;
     1116        Value3 = 34;
     1117        Value4 = 56;
    10671118      }
    10681119    }
     
    10721123    [TestProperty("Time", "short")]
    10731124    public void TestStorableClassDiscoveryAllFieldsAndAllProperties() {
    1074       AllFieldsAndAllPropertiesStorable afs = new AllFieldsAndAllPropertiesStorable(false);
     1125      AllFieldsAndAllPropertiesStorable afs = new AllFieldsAndAllPropertiesStorable();
    10751126      XmlGenerator.Serialize(afs, tempFile);
    10761127      AllFieldsAndAllPropertiesStorable newAfs = (AllFieldsAndAllPropertiesStorable)XmlParser.Deserialize(tempFile);
     
    10811132    }
    10821133
    1083     [StorableClass(StorableClassType.MarkedOnly)]
     1134    [StorableType(StorableMemberSelection.MarkedOnly, "979BEF0C-D150-4809-9EF4-467B7CCB9665")]
    10841135    public class MarkedOnlyStorable {
    10851136      public int Value1 = 1;
     
    10891140      public int Value4 { get; private set; }
    10901141      [StorableConstructor]
    1091       public MarkedOnlyStorable(bool isDeserializing) {
    1092         if (!isDeserializing) {
    1093           Value1 = 12;
    1094           Value2 = 23;
    1095           Value3 = 34;
    1096           Value4 = 56;
    1097         }
     1142      protected MarkedOnlyStorable(StorableConstructorFlag _) { }
     1143      public MarkedOnlyStorable() {
     1144        Value1 = 12;
     1145        Value2 = 23;
     1146        Value3 = 34;
     1147        Value4 = 56;
    10981148      }
    10991149    }
     
    11031153    [TestProperty("Time", "short")]
    11041154    public void TestStorableClassDiscoveryMarkedOnly() {
    1105       MarkedOnlyStorable afs = new MarkedOnlyStorable(false);
     1155      MarkedOnlyStorable afs = new MarkedOnlyStorable();
    11061156      XmlGenerator.Serialize(afs, tempFile);
    11071157      MarkedOnlyStorable newAfs = (MarkedOnlyStorable)XmlParser.Deserialize(tempFile);
     
    12411291    }
    12421292
    1243     [StorableClass]
     1293    [StorableType("5F96AAB5-F68E-4A41-BA66-E56069822719")]
    12441294    public class ReadOnlyFail {
    12451295      [Storable]
    12461296      public string ReadOnly {
    12471297        get { return "fail"; }
     1298      }
     1299
     1300      [StorableConstructor]
     1301      protected ReadOnlyFail(StorableConstructorFlag _) {
     1302      }
     1303      public ReadOnlyFail() {
    12481304      }
    12491305    }
     
    12631319
    12641320
    1265     [StorableClass]
     1321    [StorableType("9809F12C-B787-4BB5-9930-705673964D45")]
    12661322    public class WriteOnlyFail {
    12671323      [Storable]
    12681324      public string WriteOnly {
    12691325        set { throw new InvalidOperationException("this property should never be set."); }
     1326      }
     1327
     1328      [StorableConstructor]
     1329      protected WriteOnlyFail(StorableConstructorFlag _) {
     1330      }
     1331      public WriteOnlyFail() {
    12701332      }
    12711333    }
     
    12841346    }
    12851347
    1286     [StorableClass]
     1348    [StorableType("749C5247-ABB8-4286-8208-058CC7B9C781")]
    12871349    public class OneWayTest {
     1350      [StorableConstructor]
     1351      protected OneWayTest(StorableConstructorFlag _) {
     1352      }
     1353
    12881354      public OneWayTest() { this.value = "default"; }
    12891355      public string value;
     
    13091375      Assert.AreEqual("ReadOnly", ((PrimitiveToken)it.Current).Name); it.MoveNext();
    13101376      Assert.AreEqual("ROOT", ((EndToken)it.Current).Name); it.MoveNext();
    1311       var deserializer = new Deserializer(new[] { 
     1377      var deserializer = new Deserializer(new[] {
    13121378        new TypeMapping(0, typeof(OneWayTest).AssemblyQualifiedName, typeof(StorableSerializer).AssemblyQualifiedName),
    13131379        new TypeMapping(1, typeof(string).AssemblyQualifiedName, typeof(String2XmlSerializer).AssemblyQualifiedName) });
     
    14611527      b[2] = byte.MaxValue;
    14621528      XmlGenerator.Serialize(b, tempFile);
    1463       var newB = (byte[]) XmlParser.Deserialize(tempFile);
     1529      var newB = (byte[])XmlParser.Deserialize(tempFile);
    14641530      CollectionAssert.AreEqual(b, newB);
    14651531    }
     
    14691535    [TestProperty("Time", "short")]
    14701536    public void TestOptionalNumberEnumerable() {
    1471       var values = new List<double?> {0, null, double.NaN, double.PositiveInfinity, double.MaxValue, 1};
     1537      var values = new List<double?> { 0, null, double.NaN, double.PositiveInfinity, double.MaxValue, 1 };
    14721538      XmlGenerator.Serialize(values, tempFile);
    1473       var newValues = (List<double?>) XmlParser.Deserialize(tempFile);
     1539      var newValues = (List<double?>)XmlParser.Deserialize(tempFile);
    14741540      CollectionAssert.AreEqual(values, newValues);
    14751541    }
     
    14821548        DateTime.ParseExact("10.09.2014 12:21", "dd.MM.yyyy hh:mm", CultureInfo.InvariantCulture), DateTime.MaxValue};
    14831549      XmlGenerator.Serialize(values, tempFile);
    1484       var newValues = (List<DateTime?>) XmlParser.Deserialize(tempFile);
     1550      var newValues = (List<DateTime?>)XmlParser.Deserialize(tempFile);
    14851551      CollectionAssert.AreEqual(values, newValues);
    14861552    }
     
    14901556    [TestProperty("Time", "short")]
    14911557    public void TestStringEnumerable() {
    1492       var values = new List<string> {"", null, "s", "string", string.Empty, "123", "<![CDATA[nice]]>", "<![CDATA[nasty unterminated"};
     1558      var values = new List<string> { "", null, "s", "string", string.Empty, "123", "<![CDATA[nice]]>", "<![CDATA[nasty unterminated" };
    14931559      XmlGenerator.Serialize(values, tempFile);
    1494       var newValues = (List<String>) XmlParser.Deserialize(tempFile);
     1560      var newValues = (List<String>)XmlParser.Deserialize(tempFile);
    14951561      CollectionAssert.AreEqual(values, newValues);
    14961562    }
     
    15001566    [TestProperty("Time", "short")]
    15011567    public void TestUnicodeCharArray() {
    1502       var s = Encoding.UTF8.GetChars(new byte[] {0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb});
     1568      var s = Encoding.UTF8.GetChars(new byte[] { 0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb });
    15031569      XmlGenerator.Serialize(s, tempFile);
    15041570      var newS = (char[])XmlParser.Deserialize(tempFile);
     
    15101576    [TestProperty("Time", "short")]
    15111577    public void TestUnicode() {
    1512       var s = Encoding.UTF8.GetString(new byte[] {0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb});
     1578      var s = Encoding.UTF8.GetString(new byte[] { 0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb });
    15131579      XmlGenerator.Serialize(s, tempFile);
    15141580      var newS = XmlParser.Deserialize(tempFile);
     
    15201586    [TestProperty("Time", "short")]
    15211587    public void TestQueue() {
    1522       var q = new Queue<int>(new[] {1, 2, 3, 4, 0});
     1588      var q = new Queue<int>(new[] { 1, 2, 3, 4, 0 });
    15231589      XmlGenerator.Serialize(q, tempFile);
    15241590      var newQ = (Queue<int>)XmlParser.Deserialize(tempFile);
Note: See TracChangeset for help on using the changeset viewer.