Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Tests/NPointCrossoverTest.cs @ 3376

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

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

File size: 6.3 KB
Line 
1using HeuristicLab.Encodings.BinaryVectorEncoding;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Parameters;
7
8namespace HeuristicLab.Encodings.BinaryVectorEncoding_33.Tests {
9
10
11  /// <summary>
12  ///This is a test class for SinglePointCrossoverTest and is intended
13  ///to contain all SinglePointCrossoverTest Unit Tests
14  ///</summary>
15  [TestClass()]
16  public class NPointCrossoverTest {
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.BinaryVectorEncoding-3.3.dll")]
69    public void SinglePointCrossoverCrossTest() {
70      NPointCrossover_Accessor target = new NPointCrossover_Accessor(new PrivateObject(typeof(NPointCrossover)));
71      ItemArray<BinaryVector> parents;
72      TestRandom random = new TestRandom();
73      bool exceptionFired;
74      // The following test checks if there is an exception when there are more than 2 parents
75      random.Reset();
76      parents = new ItemArray<BinaryVector>(new BinaryVector[] { new BinaryVector(5), new BinaryVector(6), new BinaryVector(4) });
77      exceptionFired = false;
78      try {
79        BinaryVector actual;
80        actual = target.Cross(random, parents);
81      }
82      catch (System.ArgumentException) {
83        exceptionFired = true;
84      }
85      Assert.IsTrue(exceptionFired);
86      // The following test checks if there is an exception when there are less than 2 parents
87      random.Reset();
88      parents = new ItemArray<BinaryVector>(new BinaryVector[] { new BinaryVector(4) });
89      exceptionFired = false;
90      try {
91        BinaryVector actual;
92        actual = target.Cross(random, parents);
93      } catch (System.ArgumentException) {
94        exceptionFired = true;
95      }
96      Assert.IsTrue(exceptionFired);
97    }
98
99    /// <summary>
100    ///A test for Apply
101    ///</summary>
102    [TestMethod()]
103    public void SinglePointCrossoverApplyTest() {
104      TestRandom random = new TestRandom();
105      BinaryVector parent1, parent2, expected, actual;
106      IntValue n;
107      bool exceptionFired;
108      // The following test is based on Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 48
109      random.Reset();
110      n = new IntValue(1);
111      random.IntNumbers = new int[] { 4 };
112      parent1 = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
113      parent2 = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
114      expected = new BinaryVector(new bool[] { false, false, false, false, false, false, false, false, true });
115      actual = NPointCrossover.Apply(random, parent1, parent2, n);
116      Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));
117     
118      // The following test is based on Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 48
119      random.Reset();
120      n = new IntValue(2);
121      random.IntNumbers = new int[] { 4, 5 };
122      parent1 = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
123      parent2 = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
124      expected = new BinaryVector(new bool[] { false, false, false, false, false, false, false, false, false });
125      actual = NPointCrossover.Apply(random, parent1, parent2, n);
126      Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));
127
128      // The following test is based on Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 48
129      random.Reset();
130      n = new IntValue(2);
131      random.IntNumbers = new int[] { 4, 5 };
132      parent2 = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
133      parent1 = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
134      expected = new BinaryVector(new bool[] { true, true, false, true, true, false, false, false, true });
135      actual = NPointCrossover.Apply(random, parent1, parent2, n);
136      Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));
137
138      // The following test is not based on any published examples
139      random.Reset();
140      random.IntNumbers = new int[] { 2 };
141      parent1 = new BinaryVector(new bool[] { false, true, true, false, false }); // this parent is longer
142      parent2 = new BinaryVector(new bool[] { false, true, true, false });
143      exceptionFired = false;
144      try {
145        actual = NPointCrossover.Apply(random, parent1, parent2, n);
146      } catch (System.ArgumentException) {
147        exceptionFired = true;
148      }
149      Assert.IsTrue(exceptionFired);
150    }
151
152    /// <summary>
153    ///A test for SinglePointCrossover Constructor
154    ///</summary>
155    [TestMethod()]
156    public void SinglePointCrossoverConstructorTest() {
157      NPointCrossover target = new NPointCrossover();
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.