#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.Xml;
using HeuristicLab.PluginInfrastructure;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace HeuristicLab.Tests {
///
/// Summary description for DeepCloneableCloningTest
///
[TestClass]
public class DeepCloneableCloningTest {
[ClassInitialize]
public static void MyClassInitialize(TestContext testContext) {
PluginLoader.Assemblies.Any();
}
private TestContext testContextInstance;
public TestContext TestContext {
get { return testContextInstance; }
set { testContextInstance = value; }
}
public DeepCloneableCloningTest() {
excludedTypes = new HashSet();
excludedTypes.Add(typeof(HeuristicLab.Problems.DataAnalysis.Dataset));
excludedTypes.Add(typeof(HeuristicLab.Problems.TravelingSalesman.DistanceMatrix));
excludedTypes.Add(typeof(HeuristicLab.Problems.DataAnalysis.ClassificationEnsembleSolution));
excludedTypes.Add(typeof(HeuristicLab.Problems.DataAnalysis.RegressionEnsembleSolution));
foreach (var symbolType in ApplicationManager.Manager.GetTypes(typeof(HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbol)))
excludedTypes.Add(symbolType);
foreach (var grammarType in ApplicationManager.Manager.GetTypes(typeof(HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.SymbolicExpressionGrammarBase)))
excludedTypes.Add(grammarType);
}
private readonly HashSet excludedTypes;
[TestMethod]
[TestCategory("General")]
[TestCategory("Essential")]
[TestProperty("Time", "long")]
public void TestCloningFinishedExperiment() {
Experiment experiment = (Experiment)XmlParser.Deserialize(@"Test Resources\SamplesExperimentFinished.hl");
Experiment clone = (Experiment)experiment.Clone(new Cloner());
var intersections = CheckTotalInequality(experiment, clone).Where(x => x.GetType().FullName.StartsWith("HeuristicLab"));
Assert.IsTrue(ProcessEqualObjects(experiment, intersections));
}
[TestMethod]
[TestCategory("General")]
[TestCategory("Essential")]
[TestProperty("Time", "long")]
public void TestCloningAllDeepCloneables() {
PluginLoader.Assemblies.ToArray();
bool success = true;
foreach (Type deepCloneableType in ApplicationManager.Manager.GetTypes(typeof(IDeepCloneable))) {
// skip types that explicitely choose not to deep-clone every member
if (excludedTypes.Contains(deepCloneableType)) continue;
// test only types contained in HL plugin assemblies
if (!PluginLoader.Assemblies.Contains(deepCloneableType.Assembly)) continue;
// test only instantiable types
if (deepCloneableType.IsAbstract || !deepCloneableType.IsClass) continue;
IDeepCloneable item = null;
try {
item = (IDeepCloneable)Activator.CreateInstance(deepCloneableType, nonPublic: false);
}
catch { continue; } // no default constructor
IDeepCloneable clone = null;
try {
clone = (IDeepCloneable)item.Clone(new Cloner());
}
catch (Exception e) {
TestContext.WriteLine(Environment.NewLine + deepCloneableType.FullName + ":");
TestContext.WriteLine("ERROR! " + e.GetType().Name + @" was thrown during cloning.
All IDeepCloneable items with a default constructor should be cloneable when using that constructor!");
success = false;
continue;
}
var intersections = CheckTotalInequality(item, clone).Where(x => x.GetType().FullName.StartsWith("HeuristicLab"));
if (!intersections.Any()) continue;
if (!ProcessEqualObjects(item, intersections))
success = false;
}
Assert.IsTrue(success, "There are potential errors in deep cloning objects.");
}
private IEnumerable