using System; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using VocaluxeLib.Utils.Combinatorics; namespace VocaluxeTests { [TestClass] public class CCombinatoricsTest { [TestMethod] public void TestCounts() { Assert.AreEqual(4, CCombinations.Count(4, 3)); Assert.AreEqual(6, CCombinations.Count(4, 2)); Assert.AreEqual(66, CCombinations.Count(12, 2)); // Corner cases for (int i = 1; i < 12; i++) { Assert.AreEqual(1, CCombinations.Count(i, 0)); Assert.AreEqual(1, CCombinations.Count(i, i)); Assert.AreEqual(0, CCombinations.Count(i, i + 1)); } // Check identity (n k) = (n n-k) for (int i = 0; i <= 12; i++) Assert.AreEqual(CCombinations.Count(12, i), CCombinations.Count(12, 12 - i)); CTestHelpers.AssertFail(() => CCombinations.Count(-1, -1)); CTestHelpers.AssertFail(() => CCombinations.Count(-1, 1)); CTestHelpers.AssertFail(() => CCombinations.Count(1, -1)); } // ReSharper disable UnusedParameter.Local private static void _CheckResults(CCombinations combs, List input) // ReSharper restore UnusedParameter.Local { List> results = new List>(); foreach (List comb in combs) { foreach (List other in results) Assert.IsFalse(other.SequenceEqual(comb), "Result must be unique"); for (int i = 0; i < comb.Count - 1; i++) { Assert.IsTrue(comb.IndexOf(comb[i], i + 1) < 0, "Repetition not allowed"); Assert.IsTrue(input.Contains(comb[i]), "Output must be in input sequence"); } results.Add(comb); } Assert.AreEqual(combs.Count(), results.Count); List> results2 = combs.GetAll(); Assert.AreEqual(results.Count, results2.Count); for (int i = 0; i < results2.Count; i++) Assert.IsTrue(results[i].SequenceEqual(results2[i])); } [TestMethod] public void TestResults() { Random rand = new Random(); for (int n = 0; n < 8; n++) { for (int k = 0; k <= n; k++) { List collection = new List(n); int r = rand.Next(1000); for (int i = 0; i < n; i++) collection.Add(i + r); CCombinations combs = new CCombinations(collection, k); Assert.AreEqual(CCombinations.Count(n, k), combs.Count()); _CheckResults(combs, collection); } } } } }