Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Tests/BlendAlphaCrossoverTest.cs @ 2994

Last change on this file since 2994 was 2929, checked in by abeham, 15 years ago

Changed namespace of unit tests to add version number as discussed with swagner #95

File size: 6.2 KB
Line 
1using HeuristicLab.Encodings.RealVector;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Parameters;
6
7namespace HeuristicLab.Encodings.RealVector_33.Tests {
8
9
10  /// <summary>
11  ///This is a test class for BlendAlphaCrossoverTest and is intended
12  ///to contain all BlendAlphaCrossoverTest Unit Tests
13  ///</summary>
14  [TestClass()]
15  public class BlendAlphaCrossoverTest {
16
17
18    private TestContext testContextInstance;
19
20    /// <summary>
21    ///Gets or sets the test context which provides
22    ///information about and functionality for the current test run.
23    ///</summary>
24    public TestContext TestContext {
25      get {
26        return testContextInstance;
27      }
28      set {
29        testContextInstance = value;
30      }
31    }
32
33    #region Additional test attributes
34    //
35    //You can use the following additional attributes as you write your tests:
36    //
37    //Use ClassInitialize to run code before running the first test in the class
38    //[ClassInitialize()]
39    //public static void MyClassInitialize(TestContext testContext)
40    //{
41    //}
42    //
43    //Use ClassCleanup to run code after all tests in a class have run
44    //[ClassCleanup()]
45    //public static void MyClassCleanup()
46    //{
47    //}
48    //
49    //Use TestInitialize to run code before running each test
50    //[TestInitialize()]
51    //public void MyTestInitialize()
52    //{
53    //}
54    //
55    //Use TestCleanup to run code after each test has run
56    //[TestCleanup()]
57    //public void MyTestCleanup()
58    //{
59    //}
60    //
61    #endregion
62
63    /// <summary>
64    ///A test for Cross
65    ///</summary>
66    [TestMethod()]
67    [DeploymentItem("HeuristicLab.Encodings.RealVector-3.3.dll")]
68    public void BlendAlphaCrossoverCrossTest() {
69      BlendAlphaCrossover_Accessor target = new BlendAlphaCrossover_Accessor(new PrivateObject(typeof(BlendAlphaCrossover)));
70      ItemArray<DoubleArrayData> parents;
71      TestRandom random = new TestRandom();
72      bool exceptionFired;
73      // The following test checks if there is an exception when there are more than 2 parents
74      random.Reset();
75      parents = new ItemArray<DoubleArrayData>(new DoubleArrayData[] { new DoubleArrayData(5), new DoubleArrayData(6), new DoubleArrayData(4) });
76      exceptionFired = false;
77      try {
78        DoubleArrayData actual;
79        actual = target.Cross(random, parents);
80      } catch (System.ArgumentException) {
81        exceptionFired = true;
82      }
83      Assert.IsTrue(exceptionFired);
84      // The following test checks if there is an exception when there are less than 2 parents
85      random.Reset();
86      parents = new ItemArray<DoubleArrayData>(new DoubleArrayData[] { new DoubleArrayData(4) });
87      exceptionFired = false;
88      try {
89        DoubleArrayData actual;
90        actual = target.Cross(random, parents);
91      } catch (System.ArgumentException) {
92        exceptionFired = true;
93      }
94      Assert.IsTrue(exceptionFired);
95    }
96
97    /// <summary>
98    ///A test for Apply
99    ///</summary>
100    [TestMethod()]
101    public void BlendAlphaCrossoverApplyTest() {
102      TestRandom random = new TestRandom();
103      DoubleArrayData parent1, parent2, expected, actual;
104      DoubleData alpha;
105      bool exceptionFired;
106      // The following test is not based on published examples
107      random.Reset();
108      random.DoubleNumbers = new double[] { 0.5, 0.5, 0.5, 0.5, 0.5 };
109      alpha = new DoubleData(0.5);
110      parent1 = new DoubleArrayData(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
111      parent2 = new DoubleArrayData(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
112      expected = new DoubleArrayData(new double[] { 0.3, 0.15, 0.3, 0.35, 0.45 });
113      actual = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
114      Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
115      // The following test is not based on published examples
116      random.Reset();
117      random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25 };
118      alpha = new DoubleData(0.25);
119      parent1 = new DoubleArrayData(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
120      parent2 = new DoubleArrayData(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
121      expected = new DoubleArrayData(new double[] { 0.225, 0.1875, 0.3, 0.4625, 0.1875 });
122      actual = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
123      Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
124      // The following test is not based on published examples
125      random.Reset();
126      random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25 };
127      alpha = new DoubleData(-0.25); // negative values for alpha are not allowed
128      parent1 = new DoubleArrayData(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
129      parent2 = new DoubleArrayData(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
130      expected = new DoubleArrayData(new double[] { 0.225, 0.1875, 0.3, 0.4625, 0.1875 });
131      exceptionFired = false;
132      try {
133        actual = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
134      } catch (System.ArgumentException) {
135        exceptionFired = true;
136      }
137      Assert.IsTrue(exceptionFired);
138      // The following test is not based on published examples
139      random.Reset();
140      random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25, .75 };
141      alpha = new DoubleData(0.25);
142      parent1 = new DoubleArrayData(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1, 0.9 }); // this parent is longer
143      parent2 = new DoubleArrayData(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 });
144      expected = new DoubleArrayData(new double[] { 0.225, 0.1875, 0.3, 0.4625, 0.1875 });
145      exceptionFired = false;
146      try {
147        actual = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
148      } catch (System.ArgumentException) {
149        exceptionFired = true;
150      }
151      Assert.IsTrue(exceptionFired);
152    }
153
154    /// <summary>
155    ///A test for BlendAlphaCrossover Constructor
156    ///</summary>
157    [TestMethod()]
158    public void BlendAlphaCrossoverConstructorTest() {
159      BlendAlphaCrossover target = new BlendAlphaCrossover();
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.