using System; using System.Collections.Generic; using System.Linq; using NUnit.Framework; using VocaluxeLib.Utils.Combinatorics; namespace Tests.VocaluxeLib.Utils.Combinatorics { [TestFixture] public class CCombinationsTest { [Test] public void TestCount() { 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)); Assert.That(() => CCombinations.Count(-1, -1), Throws.TypeOf()); Assert.That(() => CCombinations.Count(-1, 1), Throws.TypeOf()); Assert.That(() => CCombinations.Count(1, -1), Throws.TypeOf()); } [Test] public void TestResults([Range(0, 8, 1)] int n) { Random rand = new Random(); 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); } } // 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])); } } }