Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/13/11 13:01:28 (14 years ago)
Author:
epitzer
Message:

Added custom serializers for generic HashSets and Dictionaries that include the Comparer property (#1375)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs

    r4068 r5290  
    11021102      }
    11031103    }
     1104    private class IdentityComparer<T> : IEqualityComparer<T> {
     1105
     1106      public bool Equals(T x, T y) {
     1107        return x.Equals(y);
     1108      }
     1109
     1110      public int GetHashCode(T obj) {
     1111        return obj.GetHashCode();
     1112      }
     1113    }
     1114
     1115    [TestMethod]
     1116    public void TestHashSetSerializer() {
     1117      var hashSets = new List<HashSet<int>>() {
     1118        new HashSet<int>(new[] { 1, 2, 3 }),
     1119        new HashSet<int>(new[] { 4, 5, 6 }, new IdentityComparer<int>()),
     1120      };
     1121      XmlGenerator.Serialize(hashSets, tempFile);
     1122      var newHashSets = XmlParser.Deserialize<List<HashSet<int>>>(tempFile);
     1123      Assert.IsTrue(newHashSets[0].Contains(1));
     1124      Assert.IsTrue(newHashSets[0].Contains(2));
     1125      Assert.IsTrue(newHashSets[0].Contains(3));
     1126      Assert.IsTrue(newHashSets[1].Contains(4));
     1127      Assert.IsTrue(newHashSets[1].Contains(5));
     1128      Assert.IsTrue(newHashSets[1].Contains(6));
     1129      Assert.AreEqual(newHashSets[0].Comparer.GetType(), new HashSet<int>().Comparer.GetType());
     1130      Assert.AreEqual(newHashSets[1].Comparer.GetType(), typeof(IdentityComparer<int>));
     1131    }
     1132
     1133    [TestMethod]
     1134    public void TestConcreteDictionarySerializer() {
     1135      var dictionaries = new List<Dictionary<int, int>>() {
     1136        new Dictionary<int, int>(),
     1137        new Dictionary<int, int>(new IdentityComparer<int>()),
     1138      };
     1139      dictionaries[0].Add(1, 1);
     1140      dictionaries[0].Add(2, 2);
     1141      dictionaries[0].Add(3, 3);
     1142      dictionaries[1].Add(4, 4);
     1143      dictionaries[1].Add(5, 5);
     1144      dictionaries[1].Add(6, 6);
     1145      XmlGenerator.Serialize(dictionaries, tempFile);
     1146      var newDictionaries = XmlParser.Deserialize<List<Dictionary<int, int>>>(tempFile);
     1147      Assert.IsTrue(newDictionaries[0].ContainsKey(1));
     1148      Assert.IsTrue(newDictionaries[0].ContainsKey(2));
     1149      Assert.IsTrue(newDictionaries[0].ContainsKey(3));
     1150      Assert.IsTrue(newDictionaries[1].ContainsKey(4));
     1151      Assert.IsTrue(newDictionaries[1].ContainsKey(5));
     1152      Assert.IsTrue(newDictionaries[1].ContainsKey(6));
     1153      Assert.IsTrue(newDictionaries[0].ContainsValue(1));
     1154      Assert.IsTrue(newDictionaries[0].ContainsValue(2));
     1155      Assert.IsTrue(newDictionaries[0].ContainsValue(3));
     1156      Assert.IsTrue(newDictionaries[1].ContainsValue(4));
     1157      Assert.IsTrue(newDictionaries[1].ContainsValue(5));
     1158      Assert.IsTrue(newDictionaries[1].ContainsValue(6));
     1159      Assert.AreEqual(newDictionaries[0].Comparer.GetType(), new Dictionary<int, int>().Comparer.GetType());
     1160      Assert.AreEqual(newDictionaries[1].Comparer.GetType(), typeof(IdentityComparer<int>));
     1161    }
    11041162
    11051163    [ClassInitialize]
Note: See TracChangeset for help on using the changeset viewer.