using System; using System.Collections.Generic; using HeuristicLab.Algorithms.NSGA3; using HeuristicLab.Encodings.RealVectorEncoding; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Algorithms.NSGA3_3._3.Test { [TestClass] public class UtilityTests { [DataTestMethod] [DataRow(0, 1)] [DataRow(1, 1)] [DataRow(3, 6)] [DataRow(4, 24)] [DataRow(10, 3628800)] [DataRow(12, 479001600)] [DataRow(13, 6227020800)] [DataRow(14, 87178291200)] public void TestFactorial(int input, long result) { Assert.AreEqual(result, Utility.Factorial(input)); } [DataTestMethod] [DataRow(-1)] [DataRow(31)] public void TestFactorialConstraint(int input) { Assert.ThrowsException(() => { Utility.Factorial(input); }); } [DataTestMethod] [DataRow(14, 12, 91)] public void TestNCR(int n, int r, int result) { Assert.AreEqual(result, Utility.NCR(n, r)); } [DataTestMethod] public void TestToFitnessMatrix() { Solution solution1 = new Solution(new RealVector()); Solution solution2 = new Solution(new RealVector()); Solution solution3 = new Solution(new RealVector()); Solution solution4 = new Solution(new RealVector()); solution1.Fitness = new double[] { 1.0, 2.0, 3.0 }; solution2.Fitness = new double[] { 4.0, 5.0, 6.0 }; solution3.Fitness = new double[] { 7.0, 8.0, 9.0 }; solution4.Fitness = new double[] { 10.0, 11.0, 12.0 }; var fitnessMatrix = Utility.ToFitnessMatrix(new List() { solution1, solution2, solution3, solution4 }); Assert.AreEqual(1.0, fitnessMatrix[0][0]); Assert.AreEqual(5.0, fitnessMatrix[1][1]); Assert.AreEqual(9.0, fitnessMatrix[2][2]); Assert.AreEqual(2.0, fitnessMatrix[0][1]); Assert.AreEqual(4.0, fitnessMatrix[1][0]); Assert.AreEqual(10.0, fitnessMatrix[3][0]); Assert.AreEqual(12.0, fitnessMatrix[3][2]); } [TestMethod] public void TestConcat() { List ints1 = new List() { 1, 2, 3, 4 }; List ints2 = new List() { 5, 6, 7, 8 }; List concatList = Utility.Concat(ints1, ints2); Assert.AreEqual(8, concatList.Count); List ints3 = new List() { 1, 2, 3, 4, 5, 6, 7, 8 }; for (int i = 0; i < ints3.Count; i++) { Assert.AreEqual(ints3[i], concatList[i]); } } [DataTestMethod] public void TestMinArgMin() { List ints = new List() { 3, 4, 5, 1, 2 }; var result = Utility.MinArgMin(i => 2 * i, ints); Assert.AreEqual(1, result.Item1); Assert.AreEqual(2, result.Item2); } [DataTestMethod] public void TestMaxArgMax() { List ints = new List() { 3, 4, 5, 1, 2 }; var result = Utility.MaxArgMax(i => 2 * i, ints); Assert.AreEqual(5, result.Item1); Assert.AreEqual(10, result.Item2); } [DataTestMethod] public void TestMin() { List ints = new List() { 3, 4, 5, 1, 2 }; Assert.AreEqual(2, Utility.Min(i => 2 * i, ints)); } [DataTestMethod] public void TestMax() { List ints = new List() { 3, 4, 5, 1, 2 }; Assert.AreEqual(10, Utility.Max(i => 2 * i, ints)); } [DataTestMethod] public void TestArgMin() { List ints = new List() { 3, 4, 5, 1, 2 }; Assert.AreEqual(1, Utility.ArgMin(i => 2 * i, ints)); } [DataTestMethod] public void TestArgMax() { List ints = new List() { 3, 4, 5, 1, 2 }; Assert.AreEqual(5, Utility.ArgMax(i => 2 * i, ints)); } } }