Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • renamed HeuristicLab.Permutation to HeuristicLab.Encodings.Permutation
File size: 3.8 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 OrderCrossover2Test and is intended
7    ///to contain all OrderCrossover2Test Unit Tests
8    ///</summary>
9  [TestClass()]
10  public class OrderCrossover2Test {
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 OrderCrossover2CrossTest() {
65      TestRandom random = new TestRandom();
66      OrderCrossover2_Accessor target = new OrderCrossover2_Accessor(new PrivateObject(typeof(OrderCrossover2)));
67      random.Reset();
68      bool exceptionFired = false;
69      try {
70        target.Cross(random, new ItemArray<Permutation>(new Permutation[] {
71          new Permutation(4), new Permutation(4), new Permutation(4)}));
72      } catch (System.InvalidOperationException) {
73        exceptionFired = true;
74      }
75      Assert.IsTrue(exceptionFired);
76    }
77
78    /// <summary>
79    ///A test for Apply
80    ///</summary>
81    [TestMethod()]
82    public void OrderCrossover2ApplyTest() {
83      TestRandom random = new TestRandom();
84      Permutation parent1, parent2, expected, actual;
85      // 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. p. 135.
86      random.Reset();
87      random.IntNumbers = new int[] { 5, 7 };
88      parent1 = new Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
89      Assert.IsTrue(parent1.Validate());
90      parent2 = new Permutation(new int[] { 2, 5, 6, 0, 9, 1, 3, 8, 4, 7 });
91      Assert.IsTrue(parent2.Validate());
92      expected = new Permutation(new int[] { 2, 0, 9, 1, 3, 5, 6, 7, 8, 4 });
93      Assert.IsTrue(expected.Validate());
94      actual = OrderCrossover2.Apply(random, parent1, parent2);
95      Assert.IsTrue(actual.Validate());
96      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
97
98      // perform a test when the two permutations are of unequal length
99      random.Reset();
100      bool exceptionFired = false;
101      try {
102        OrderCrossover.Apply(random, new Permutation(8), new Permutation(6));
103      } catch (System.ArgumentException) {
104        exceptionFired = true;
105      }
106      Assert.IsTrue(exceptionFired);
107    }
108
109    /// <summary>
110    ///A test for OrderCrossover2 Constructor
111    ///</summary>
112    [TestMethod()]
113    public void OrderCrossover2ConstructorTest() {
114      OrderCrossover2 target = new OrderCrossover2();
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.