Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Tests/CyclicCrossover2Test.cs @ 3063

Last change on this file since 3063 was 3063, checked in by svonolfe, 14 years ago

Updated assembly reference in test cases (#889)

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