Пример #1
0
        public void TestCardinalityOnePermutation()
        {
            var set = new[] { 42 };
            var permutations = set.Permutations();

            // should contain a single result: the set itself
            Assert.IsTrue(permutations.Single().SequenceEqual(set));
        }
Пример #2
0
        public void TestCardinalityTwoPermutation()
        {
            var set = new[] { 42, 37 };
            var permutations = set.Permutations();

            // should contain two results: the set itself and its reverse
            Assert.IsTrue(permutations.Count() == 2);
            Assert.IsTrue(permutations.First().SequenceEqual(set));
            Assert.IsTrue(permutations.Last().SequenceEqual(set.Reverse()));
        }
Пример #3
0
        public void TestCardinalityThreePermutation()
        {
            var set = new[] { 42, 11, 100 };
            var permutations = set.Permutations();

            var expectedPermutations = new[]
                                           {
                                               new[] {42, 11, 100},
                                               new[] {42, 100, 11},
                                               new[] {11, 100, 42},
                                               new[] {11, 42, 100},
                                               new[] {100, 11, 42},
                                               new[] {100, 42, 11},
                                           };

            // should contain six permutations (as defined above)
            Assert.AreEqual(expectedPermutations.Count(), permutations.Count());
            Assert.IsTrue(permutations.All(p => expectedPermutations.Contains(p, EqualityComparerFunc<IList<int>>.As((x, y) => x.SequenceEqual(y)))));
        }
Пример #4
0
        public void TestPermutationsAreIndependent()
        {
            var set = new[] { 10, 20, 30, 40, };
            var permutedSets = set.Permutations();

            var listPermutations = new List<IList<int>>();
            listPermutations.AddRange(permutedSets);
            Assert.IsNotEmpty(listPermutations);

            for (var i = 0; i < listPermutations.Count; i++)
            {
                for (var j = 1; j < listPermutations.Count; j++)
                {
                    if (j == i) continue;
                    Assert.AreNotSame(listPermutations[i], listPermutations[j]);
                }
            }
        }
        public void TestPermutations()
        {
            Assert.Throws<ArgumentNullException>(() => { IEnumerableExtensions.Permutations<string>(null); });

            var input = new[] { 1, 2, 3 };
            var result = input.Permutations().ToArray();

            Assert.AreEqual(6, result.Length);
            Assert.IsTrue(result.Any(r => r.SequenceEqual(new[] { 1, 2, 3 })));
            Assert.IsTrue(result.Any(r => r.SequenceEqual(new[] { 1, 3, 2 })));
            Assert.IsTrue(result.Any(r => r.SequenceEqual(new[] { 2, 1, 3 })));
            Assert.IsTrue(result.Any(r => r.SequenceEqual(new[] { 2, 3, 1 })));
            Assert.IsTrue(result.Any(r => r.SequenceEqual(new[] { 3, 1, 2 })));
            Assert.IsTrue(result.Any(r => r.SequenceEqual(new[] { 3, 2, 1 })));
            Assert.IsFalse(result.Any(r => r.SequenceEqual(new[] { 1, 2, 3, 4 })));
        }