Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.Permutation/3.3/Tests/CyclicCrossover2Test.cs @ 2906

Last change on this file since 2906 was 2906, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • renamed HeuristicLab.Permutation to HeuristicLab.Encodings.Permutation
File size: 3.9 KB
Line 
1using HeuristicLab.Core;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3
4namespace HeuristicLab.Encodings.Permutation.Tests {
5    /// <summary>
6    ///This is a test class for CyclicCrossover2Test and is intended
7    ///to contain all CyclicCrossover2Test Unit Tests
8    ///</summary>
9  [TestClass()]
10  public class CyclicCrossover2Test {
11
12
13    private TestContext testContextInstance;
14
15    /// <summary>
16    ///Gets or sets the test context which provides
17    ///information about and functionality for the current test run.
18    ///</summary>
19    public TestContext TestContext {
20      get {
21        return testContextInstance;
22      }
23      set {
24        testContextInstance = value;
25      }
26    }
27
28    #region Additional test attributes
29    //
30    //You can use the following additional attributes as you write your tests:
31    //
32    //Use ClassInitialize to run code before running the first test in the class
33    //[ClassInitialize()]
34    //public static void MyClassInitialize(TestContext testContext)
35    //{
36    //}
37    //
38    //Use ClassCleanup to run code after all tests in a class have run
39    //[ClassCleanup()]
40    //public static void MyClassCleanup()
41    //{
42    //}
43    //
44    //Use TestInitialize to run code before running each test
45    //[TestInitialize()]
46    //public void MyTestInitialize()
47    //{
48    //}
49    //
50    //Use TestCleanup to run code after each test has run
51    //[TestCleanup()]
52    //public void MyTestCleanup()
53    //{
54    //}
55    //
56    #endregion
57
58
59    /// <summary>
60    ///A test for Cross
61    ///</summary>
62    [TestMethod()]
63    [DeploymentItem("HeuristicLab.Encodings.Permutation-3.3.dll")]
64    public void CyclicCrossover2CrossTest() {
65      TestRandom random = new TestRandom();
66      CyclicCrossover2_Accessor target = new CyclicCrossover2_Accessor(new PrivateObject(typeof(CyclicCrossover2)));
67      // perform a test with more than two parents
68      random.Reset();
69      bool exceptionFired = false;
70      try {
71        target.Cross(random, new ItemArray<Permutation>(new Permutation[] {
72          new Permutation(4), new Permutation(4), new Permutation(4)}));
73      } catch (System.InvalidOperationException) {
74        exceptionFired = true;
75      }
76      Assert.IsTrue(exceptionFired);
77    }
78
79    /// <summary>
80    ///A test for Apply
81    ///</summary>
82    [TestMethod()]
83    public void CyclicCrossover2ApplyTest() {
84      TestRandom random = new TestRandom();
85      Permutation parent1, parent2, expected, actual;
86      // The following test is based on an example from Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press. pp. 134.
87      random.Reset();
88      random.IntNumbers = new int[] { 0 };
89      parent1 = new Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
90      Assert.IsTrue(parent1.Validate());
91      parent2 = new Permutation(new int[] { 2, 5, 6, 0, 7, 1, 3, 8, 4, 9 });
92      Assert.IsTrue(parent2.Validate());
93      expected = new Permutation(new int[] { 0, 5, 2, 3, 7, 1, 6, 8, 4, 9 });
94      Assert.IsTrue(expected.Validate());
95      actual = CyclicCrossover2.Apply(random, parent1, parent2);
96      Assert.IsTrue(actual.Validate());
97      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
98
99      // perform a test when the two permutations are of unequal length
100      random.Reset();
101      bool exceptionFired = false;
102      try {
103        CyclicCrossover.Apply(random, new Permutation(8), new Permutation(6));
104      } catch (System.ArgumentException) {
105        exceptionFired = true;
106      }
107      Assert.IsTrue(exceptionFired);
108    }
109
110    /// <summary>
111    ///A test for CyclicCrossover2 Constructor
112    ///</summary>
113    [TestMethod()]
114    public void CyclicCrossover2ConstructorTest() {
115      CyclicCrossover2 target = new CyclicCrossover2();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.