1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Persistence.Default.Xml;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Tests {
|
---|
33 | /// <summary>
|
---|
34 | /// Summary description for DeepCloneableCloningTest
|
---|
35 | /// </summary>
|
---|
36 | [TestClass]
|
---|
37 | public class DeepCloneableCloningTest {
|
---|
38 |
|
---|
39 | [ClassInitialize]
|
---|
40 | public static void MyClassInitialize(TestContext testContext) {
|
---|
41 | PluginLoader.Assemblies.Any();
|
---|
42 | }
|
---|
43 |
|
---|
44 | private TestContext testContextInstance;
|
---|
45 | public TestContext TestContext {
|
---|
46 | get { return testContextInstance; }
|
---|
47 | set { testContextInstance = value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public DeepCloneableCloningTest() {
|
---|
51 | excludedTypes = new HashSet<Type>();
|
---|
52 | excludedTypes.Add(typeof(HeuristicLab.Problems.DataAnalysis.Dataset));
|
---|
53 | excludedTypes.Add(typeof(HeuristicLab.Problems.TravelingSalesman.DistanceMatrix));
|
---|
54 | excludedTypes.Add(typeof(HeuristicLab.Problems.DataAnalysis.ClassificationEnsembleSolution));
|
---|
55 | excludedTypes.Add(typeof(HeuristicLab.Problems.DataAnalysis.RegressionEnsembleSolution));
|
---|
56 |
|
---|
57 | foreach (var symbolType in ApplicationManager.Manager.GetTypes(typeof(HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbol)))
|
---|
58 | excludedTypes.Add(symbolType);
|
---|
59 | foreach (var grammarType in ApplicationManager.Manager.GetTypes(typeof(HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.SymbolicExpressionGrammarBase)))
|
---|
60 | excludedTypes.Add(grammarType);
|
---|
61 | }
|
---|
62 |
|
---|
63 | private readonly HashSet<Type> excludedTypes;
|
---|
64 |
|
---|
65 | [TestMethod]
|
---|
66 | [TestCategory("General")]
|
---|
67 | [TestCategory("Essential")]
|
---|
68 | [TestProperty("Time", "long")]
|
---|
69 | public void TestCloningFinishedExperiment() {
|
---|
70 | Experiment experiment = (Experiment)XmlParser.Deserialize(@"Test Resources\SamplesExperimentFinished.hl");
|
---|
71 |
|
---|
72 | Experiment clone = (Experiment)experiment.Clone(new Cloner());
|
---|
73 | var intersections = CheckTotalInequality(experiment, clone).Where(x => x.GetType().FullName.StartsWith("HeuristicLab"));
|
---|
74 |
|
---|
75 | Assert.IsTrue(ProcessEqualObjects(experiment, intersections));
|
---|
76 | }
|
---|
77 |
|
---|
78 | [TestMethod]
|
---|
79 | [TestCategory("General")]
|
---|
80 | [TestCategory("Essential")]
|
---|
81 | [TestProperty("Time", "long")]
|
---|
82 | public void TestCloningAllDeepCloneables() {
|
---|
83 | PluginLoader.Assemblies.ToArray();
|
---|
84 | bool success = true;
|
---|
85 | foreach (Type deepCloneableType in ApplicationManager.Manager.GetTypes(typeof(IDeepCloneable))) {
|
---|
86 | // skip types that explicitely choose not to deep-clone every member
|
---|
87 | if (excludedTypes.Contains(deepCloneableType)) continue;
|
---|
88 | // test only types contained in HL plugin assemblies
|
---|
89 | if (!PluginLoader.Assemblies.Contains(deepCloneableType.Assembly)) continue;
|
---|
90 | // test only instantiable types
|
---|
91 | if (deepCloneableType.IsAbstract || !deepCloneableType.IsClass) continue;
|
---|
92 |
|
---|
93 | IDeepCloneable item = null;
|
---|
94 | try {
|
---|
95 | item = (IDeepCloneable)Activator.CreateInstance(deepCloneableType, nonPublic: false);
|
---|
96 | }
|
---|
97 | catch { continue; } // no default constructor
|
---|
98 |
|
---|
99 | IDeepCloneable clone = null;
|
---|
100 | try {
|
---|
101 | clone = (IDeepCloneable)item.Clone(new Cloner());
|
---|
102 | }
|
---|
103 | catch (Exception e) {
|
---|
104 | TestContext.WriteLine(Environment.NewLine + deepCloneableType.FullName + ":");
|
---|
105 | TestContext.WriteLine("ERROR! " + e.GetType().Name + @" was thrown during cloning.
|
---|
106 | All IDeepCloneable items with a default constructor should be cloneable when using that constructor!");
|
---|
107 | success = false;
|
---|
108 | continue;
|
---|
109 | }
|
---|
110 | var intersections = CheckTotalInequality(item, clone).Where(x => x.GetType().FullName.StartsWith("HeuristicLab"));
|
---|
111 | if (!intersections.Any()) continue;
|
---|
112 |
|
---|
113 | if (!ProcessEqualObjects(item, intersections))
|
---|
114 | success = false;
|
---|
115 | }
|
---|
116 | Assert.IsTrue(success, "There are potential errors in deep cloning objects.");
|
---|
117 | }
|
---|
118 |
|
---|
119 | private IEnumerable<object> CheckTotalInequality(object original, object clone) {
|
---|
120 | var originalObjects = new HashSet<object>(original.GetObjectGraphObjects(excludeStaticMembers: true).Where(x => !x.GetType().IsValueType), new ReferenceEqualityComparer());
|
---|
121 | var clonedObjects = new HashSet<object>(clone.GetObjectGraphObjects(excludeStaticMembers: true).Where(x => !x.GetType().IsValueType), new ReferenceEqualityComparer());
|
---|
122 |
|
---|
123 | return originalObjects.Intersect(clonedObjects, new ReferenceEqualityComparer());
|
---|
124 | }
|
---|
125 |
|
---|
126 | private bool ProcessEqualObjects(IDeepCloneable item, IEnumerable<object> intersections) {
|
---|
127 | bool success = true;
|
---|
128 | bool headerWritten = false;
|
---|
129 |
|
---|
130 | foreach (object o in intersections) {
|
---|
131 | string typeName = o.GetType().FullName;
|
---|
132 | if (excludedTypes.Contains(o.GetType())) {
|
---|
133 | //TestContext.WriteLine("Skipping excluded type " + typeName);
|
---|
134 | } else if (o is IDeepCloneable) {
|
---|
135 | string info = (o is IItem) ? ((IItem)o).ItemName + ((o is INamedItem) ? ", " + ((INamedItem)o).Name : String.Empty) : String.Empty;
|
---|
136 | if (!headerWritten) {
|
---|
137 | TestContext.WriteLine(Environment.NewLine + item.GetType().FullName + ":");
|
---|
138 | headerWritten = true;
|
---|
139 | }
|
---|
140 | TestContext.WriteLine("POTENTIAL ERROR! A DEEPCLONEABLE WAS NOT DEEP CLONED (" + info + "): " + typeName);
|
---|
141 | success = false;
|
---|
142 | } else {
|
---|
143 | Array array = o as Array;
|
---|
144 | if (array != null && array.Length == 0) continue; //arrays of length 0 are used inside empty collections
|
---|
145 | if (!headerWritten) {
|
---|
146 | TestContext.WriteLine(Environment.NewLine + item.GetType().FullName + ":");
|
---|
147 | headerWritten = true;
|
---|
148 | }
|
---|
149 | TestContext.WriteLine("WARNING: An object of type " + typeName + " is referenced in the original and in the clone.");
|
---|
150 | }
|
---|
151 | }
|
---|
152 | return success;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|