Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3-3.3.Test/UtilityTests.cs @ 18088

Last change on this file since 18088 was 17691, checked in by dleko, 4 years ago

#2825 Add Unit tests for Utility methods.

File size: 4.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Algorithms.NSGA3;
4using HeuristicLab.Encodings.RealVectorEncoding;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6
7namespace HeuristicLab.Algorithms.NSGA3_3._3.Test
8{
9    [TestClass]
10    public class UtilityTests
11    {
12        [DataTestMethod]
13        [DataRow(0, 1)]
14        [DataRow(1, 1)]
15        [DataRow(3, 6)]
16        [DataRow(4, 24)]
17        [DataRow(10, 3628800)]
18        [DataRow(12, 479001600)]
19        [DataRow(13, 6227020800)]
20        [DataRow(14, 87178291200)]
21        public void TestFactorial(int input, long result)
22        {
23            Assert.AreEqual(result, Utility.Factorial(input));
24        }
25
26        [DataTestMethod]
27        [DataRow(-1)]
28        [DataRow(31)]
29        public void TestFactorialConstraint(int input)
30        {
31            Assert.ThrowsException<InvalidOperationException>(() => { Utility.Factorial(input); });
32        }
33
34        [DataTestMethod]
35        [DataRow(14, 12, 91)]
36        public void TestNCR(int n, int r, int result)
37        {
38            Assert.AreEqual(result, Utility.NCR(n, r));
39        }
40
41        [DataTestMethod]
42        public void TestToFitnessMatrix()
43        {
44            Solution solution1 = new Solution(new RealVector());
45            Solution solution2 = new Solution(new RealVector());
46            Solution solution3 = new Solution(new RealVector());
47            Solution solution4 = new Solution(new RealVector());
48
49            solution1.Fitness = new double[] { 1.0, 2.0, 3.0 };
50            solution2.Fitness = new double[] { 4.0, 5.0, 6.0 };
51            solution3.Fitness = new double[] { 7.0, 8.0, 9.0 };
52            solution4.Fitness = new double[] { 10.0, 11.0, 12.0 };
53
54            var fitnessMatrix = Utility.ToFitnessMatrix(new List<Solution>() { solution1, solution2, solution3, solution4 });
55
56            Assert.AreEqual(1.0, fitnessMatrix[0][0]);
57            Assert.AreEqual(5.0, fitnessMatrix[1][1]);
58            Assert.AreEqual(9.0, fitnessMatrix[2][2]);
59            Assert.AreEqual(2.0, fitnessMatrix[0][1]);
60            Assert.AreEqual(4.0, fitnessMatrix[1][0]);
61            Assert.AreEqual(10.0, fitnessMatrix[3][0]);
62            Assert.AreEqual(12.0, fitnessMatrix[3][2]);
63        }
64
65        [TestMethod]
66        public void TestConcat()
67        {
68            List<int> ints1 = new List<int>() { 1, 2, 3, 4 };
69            List<int> ints2 = new List<int>() { 5, 6, 7, 8 };
70
71            List<int> concatList = Utility.Concat(ints1, ints2);
72            Assert.AreEqual(8, concatList.Count);
73            List<int> ints3 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
74            for (int i = 0; i < ints3.Count; i++)
75            {
76                Assert.AreEqual(ints3[i], concatList[i]);
77            }
78        }
79
80        [DataTestMethod]
81        public void TestMinArgMin()
82        {
83            List<int> ints = new List<int>() { 3, 4, 5, 1, 2 };
84            var result = Utility.MinArgMin(i => 2 * i, ints);
85            Assert.AreEqual(1, result.Item1);
86            Assert.AreEqual(2, result.Item2);
87        }
88
89        [DataTestMethod]
90        public void TestMaxArgMax()
91        {
92            List<int> ints = new List<int>() { 3, 4, 5, 1, 2 };
93            var result = Utility.MaxArgMax(i => 2 * i, ints);
94            Assert.AreEqual(5, result.Item1);
95            Assert.AreEqual(10, result.Item2);
96        }
97
98        [DataTestMethod]
99        public void TestMin()
100        {
101            List<int> ints = new List<int>() { 3, 4, 5, 1, 2 };
102            Assert.AreEqual(2, Utility.Min(i => 2 * i, ints));
103        }
104
105        [DataTestMethod]
106        public void TestMax()
107        {
108            List<int> ints = new List<int>() { 3, 4, 5, 1, 2 };
109            Assert.AreEqual(10, Utility.Max(i => 2 * i, ints));
110        }
111
112        [DataTestMethod]
113        public void TestArgMin()
114        {
115            List<int> ints = new List<int>() { 3, 4, 5, 1, 2 };
116            Assert.AreEqual(1, Utility.ArgMin(i => 2 * i, ints));
117        }
118
119        [DataTestMethod]
120        public void TestArgMax()
121        {
122            List<int> ints = new List<int>() { 3, 4, 5, 1, 2 };
123            Assert.AreEqual(5, Utility.ArgMax(i => 2 * i, ints));
124        }
125    }
126}
Note: See TracBrowser for help on using the repository browser.