Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9765 was 9765, checked in by mkommend, 9 years ago

#2088: Removed useless brackets in the TestMethod attribute and added test categories and properties for SymExpTreeEncoding unit test.

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Tests;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26
27namespace HeuristicLab.Encodings.BinaryVectorEncoding.Tests {
28
29
30  /// <summary>
31  ///This is a test class for SinglePointCrossoverTest and is intended
32  ///to contain all SinglePointCrossoverTest Unit Tests
33  ///</summary>
34  [TestClass()]
35  public class NPointCrossoverTest {
36
37
38    private TestContext testContextInstance;
39
40    /// <summary>
41    ///Gets or sets the test context which provides
42    ///information about and functionality for the current test run.
43    ///</summary>
44    public TestContext TestContext {
45      get {
46        return testContextInstance;
47      }
48      set {
49        testContextInstance = value;
50      }
51    }
52
53    #region Additional test attributes
54    //
55    //You can use the following additional attributes as you write your tests:
56    //
57    //Use ClassInitialize to run code before running the first test in the class
58    //[ClassInitialize()]
59    //public static void MyClassInitialize(TestContext testContext)
60    //{
61    //}
62    //
63    //Use ClassCleanup to run code after all tests in a class have run
64    //[ClassCleanup()]
65    //public static void MyClassCleanup()
66    //{
67    //}
68    //
69    //Use TestInitialize to run code before running each test
70    //[TestInitialize()]
71    //public void MyTestInitialize()
72    //{
73    //}
74    //
75    //Use TestCleanup to run code after each test has run
76    //[TestCleanup()]
77    //public void MyTestCleanup()
78    //{
79    //}
80    //
81    #endregion
82
83    /// <summary>
84    ///A test for Cross
85    ///</summary>
86    [TestMethod]
87    [DeploymentItem("HeuristicLab.Encodings.BinaryVectorEncoding-3.3.dll")]
88    public void NPointCrossoverCrossTest() {
89      NPointCrossover_Accessor target = new NPointCrossover_Accessor(new PrivateObject(typeof(NPointCrossover)));
90      ItemArray<BinaryVector> parents;
91      TestRandom random = new TestRandom();
92      bool exceptionFired;
93      // The following test checks if there is an exception when there are more than 2 parents
94      random.Reset();
95      parents = new ItemArray<BinaryVector>(new BinaryVector[] { new BinaryVector(5), new BinaryVector(6), new BinaryVector(4) });
96      exceptionFired = false;
97      try {
98        BinaryVector actual;
99        actual = target.Cross(random, parents);
100      } catch (System.ArgumentException) {
101        exceptionFired = true;
102      }
103      Assert.IsTrue(exceptionFired);
104      // The following test checks if there is an exception when there are less than 2 parents
105      random.Reset();
106      parents = new ItemArray<BinaryVector>(new BinaryVector[] { new BinaryVector(4) });
107      exceptionFired = false;
108      try {
109        BinaryVector actual;
110        actual = target.Cross(random, parents);
111      } catch (System.ArgumentException) {
112        exceptionFired = true;
113      }
114      Assert.IsTrue(exceptionFired);
115    }
116
117    /// <summary>
118    ///A test for Apply
119    ///</summary>
120    [TestMethod]
121    public void NPointCrossoverApplyTest() {
122      TestRandom random = new TestRandom();
123      BinaryVector parent1, parent2, expected, actual;
124      IntValue n;
125      bool exceptionFired;
126      // 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
127      random.Reset();
128      n = new IntValue(1);
129      random.IntNumbers = new int[] { 4 };
130      parent1 = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
131      parent2 = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
132      expected = new BinaryVector(new bool[] { false, false, false, false, false, false, false, false, true });
133      actual = NPointCrossover.Apply(random, parent1, parent2, n);
134      Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));
135
136      // 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
137      random.Reset();
138      n = new IntValue(2);
139      random.IntNumbers = new int[] { 4, 5 };
140      parent1 = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
141      parent2 = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
142      expected = new BinaryVector(new bool[] { false, false, false, false, false, false, false, false, false });
143      actual = NPointCrossover.Apply(random, parent1, parent2, n);
144      Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));
145
146      // 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
147      random.Reset();
148      n = new IntValue(2);
149      random.IntNumbers = new int[] { 4, 5 };
150      parent2 = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
151      parent1 = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
152      expected = new BinaryVector(new bool[] { true, true, false, true, true, false, false, false, true });
153      actual = NPointCrossover.Apply(random, parent1, parent2, n);
154      Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));
155
156      // The following test is not based on any published examples
157      random.Reset();
158      random.IntNumbers = new int[] { 2 };
159      parent1 = new BinaryVector(new bool[] { false, true, true, false, false }); // this parent is longer
160      parent2 = new BinaryVector(new bool[] { false, true, true, false });
161      exceptionFired = false;
162      try {
163        actual = NPointCrossover.Apply(random, parent1, parent2, n);
164      } catch (System.ArgumentException) {
165        exceptionFired = true;
166      }
167      Assert.IsTrue(exceptionFired);
168    }
169
170    /// <summary>
171    ///A test for SinglePointCrossover Constructor
172    ///</summary>
173    [TestMethod]
174    public void NPointCrossoverConstructorTest() {
175      NPointCrossover target = new NPointCrossover();
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.