1 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Parameters;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Encodings.IntegerVectorEncoding_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 SinglePointCrossoverTest {
|
---|
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 SinglePointCrossoverCrossTest() {
|
---|
70 | SinglePointCrossover_Accessor target = new SinglePointCrossover_Accessor(new PrivateObject(typeof(SinglePointCrossover)));
|
---|
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 more than 2 parents
|
---|
75 | random.Reset();
|
---|
76 | parents = new ItemArray<IntegerVector>(new IntegerVector[] { new IntegerVector(5), new IntegerVector(6), new IntegerVector(4) });
|
---|
77 | exceptionFired = false;
|
---|
78 | try {
|
---|
79 | IntegerVector 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<IntegerVector>(new IntegerVector[] { new IntegerVector(4) });
|
---|
89 | exceptionFired = false;
|
---|
90 | try {
|
---|
91 | IntegerVector 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 | IntegerVector parent1, parent2, expected, actual;
|
---|
106 | bool exceptionFired;
|
---|
107 | // The following test is not based on published examples
|
---|
108 | random.Reset();
|
---|
109 | random.IntNumbers = new int[] { 3 };
|
---|
110 | parent1 = new IntegerVector(new int[] { 2, 2, 3, 5, 1 });
|
---|
111 | parent2 = new IntegerVector(new int[] { 4, 1, 3, 2, 8 });
|
---|
112 | expected = new IntegerVector(new int[] { 2, 2, 3, 2, 8 });
|
---|
113 | actual = SinglePointCrossover.Apply(random, parent1, parent2);
|
---|
114 | Assert.IsTrue(Auxiliary.IntegerVectorIsEqualByPosition(actual, expected));
|
---|
115 | // The following test is not based on published examples
|
---|
116 | random.Reset();
|
---|
117 | random.IntNumbers = new int[] { 2 };
|
---|
118 | parent1 = new IntegerVector(new int[] { 2, 2, 3, 5, 1, 9 }); // this parent is longer
|
---|
119 | parent2 = new IntegerVector(new int[] { 4, 1, 3, 2, 8 });
|
---|
120 | exceptionFired = false;
|
---|
121 | try {
|
---|
122 | actual = SinglePointCrossover.Apply(random, parent1, parent2);
|
---|
123 | } catch (System.ArgumentException) {
|
---|
124 | exceptionFired = true;
|
---|
125 | }
|
---|
126 | Assert.IsTrue(exceptionFired);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /// <summary>
|
---|
130 | ///A test for SinglePointCrossover Constructor
|
---|
131 | ///</summary>
|
---|
132 | [TestMethod()]
|
---|
133 | public void SinglePointCrossoverConstructorTest() {
|
---|
134 | SinglePointCrossover target = new SinglePointCrossover();
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|