Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Tests/DiscreteCrossoverTest.cs @ 3714

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

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 4.1 KB
Line 
1using HeuristicLab.Encodings.IntegerVectorEncoding;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Parameters;
7
8namespace HeuristicLab.Encodings.IntegerVectorEncoding_33.Tests {
9
10
11  /// <summary>
12  ///This is a test class for DiscreteCrossoverTest and is intended
13  ///to contain all DiscreteCrossoverTest Unit Tests
14  ///</summary>
15  [TestClass()]
16  public class DiscreteCrossoverTest {
17
18
19    private TestContext testContextInstance;
20
21    /// <summary>
22    ///Gets or sets the test context which provides
23    ///information about and functionality for the current test run.
24    ///</summary>
25    public TestContext TestContext {
26      get {
27        return testContextInstance;
28      }
29      set {
30        testContextInstance = value;
31      }
32    }
33
34    #region Additional test attributes
35    //
36    //You can use the following additional attributes as you write your tests:
37    //
38    //Use ClassInitialize to run code before running the first test in the class
39    //[ClassInitialize()]
40    //public static void MyClassInitialize(TestContext testContext)
41    //{
42    //}
43    //
44    //Use ClassCleanup to run code after all tests in a class have run
45    //[ClassCleanup()]
46    //public static void MyClassCleanup()
47    //{
48    //}
49    //
50    //Use TestInitialize to run code before running each test
51    //[TestInitialize()]
52    //public void MyTestInitialize()
53    //{
54    //}
55    //
56    //Use TestCleanup to run code after each test has run
57    //[TestCleanup()]
58    //public void MyTestCleanup()
59    //{
60    //}
61    //
62    #endregion
63
64    /// <summary>
65    ///A test for Cross
66    ///</summary>
67    [TestMethod()]
68    [DeploymentItem("HeuristicLab.Encodings.IntegerVectorEncoding-3.3.dll")]
69    public void DiscreteCrossoverCrossTest() {
70      DiscreteCrossover_Accessor target = new DiscreteCrossover_Accessor(new PrivateObject(typeof(DiscreteCrossover)));
71      ItemArray<IntegerVector> parents;
72      TestRandom random = new TestRandom();
73      bool exceptionFired;
74      // The following test checks if there is an exception when there are less than 2 parents
75      random.Reset();
76      parents = new ItemArray<IntegerVector>(new IntegerVector[] { new IntegerVector(4) });
77      exceptionFired = false;
78      try {
79        IntegerVector actual;
80        actual = target.Cross(random, parents);
81      } catch (System.ArgumentException) {
82        exceptionFired = true;
83      }
84      Assert.IsTrue(exceptionFired);
85    }
86
87    /// <summary>
88    ///A test for Apply
89    ///</summary>
90    [TestMethod()]
91    public void DiscreteCrossoverApplyTest() {
92      TestRandom random = new TestRandom();
93      IntegerVector parent1, parent2, expected, actual;
94      bool exceptionFired;
95      // The following test is not based on published examples
96      random.Reset();
97      random.DoubleNumbers = new double[] { 0, 0, 0.9, 0, 0.9 };
98      parent1 = new IntegerVector(new int[] { 2, 2, 3, 5, 1 });
99      parent2 = new IntegerVector(new int[] { 4, 1, 3, 2, 8 });
100      expected = new IntegerVector(new int[] { 2, 2, 3, 5, 8 });
101      actual = DiscreteCrossover.Apply(random, parent1, parent2);
102      Assert.IsTrue(Auxiliary.IntegerVectorIsEqualByPosition(actual, expected));
103
104      // The following test is not based on published examples
105      random.Reset();
106      random.DoubleNumbers = new double[] { 0, 0, 0.9, 0, 0.9 };
107      parent1 = new IntegerVector(new int[] { 2, 2, 3, 5, 1, 9 }); // this parent is longer
108      parent2 = new IntegerVector(new int[] { 4, 1, 3, 2, 8 });
109      exceptionFired = false;
110      try {
111        actual = DiscreteCrossover.Apply(random, parent1, parent2);
112      } catch (System.ArgumentException) {
113        exceptionFired = true;
114      }
115      Assert.IsTrue(exceptionFired);
116    }
117
118    /// <summary>
119    ///A test for DiscreteCrossover Constructor
120    ///</summary>
121    [TestMethod()]
122    public void DiscreteCrossoverConstructorTest() {
123      DiscreteCrossover target = new DiscreteCrossover();
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.