Beispiel #1
0
 /// <summary> Gets whether the specified type can be assigned to any of the specified candidates. </summary>
 public static bool IsAnyOf(this Type item, params Type[] candidates)
 {
     return(item.IsAnyOf(InterfaceWraps.ToEqualityComparer <Type>((a, b) => b.IsAssignableFrom(a)), candidates));
 }
Beispiel #2
0
        public void TestIntervalCombinations()
        {
            var f0  = new Filling("0", new[] { true });
            var f01 = new Filling("01", new[] { true, true });
            var f1  = new Filling("1", new[] { false, true });
            var f12 = new Filling("12", new[] { false, true, true });
            var f02 = new Filling("02", new[] { true, false, true });
            var f2  = new Filling("2", new[] { false, false, true });

            Func <IReadOnlyList <Filling>, int, IEnumerable <Filling[]> > functionToTest1 = (options, length) => Combinatorics.AllFillingIntervalCombinations(options, length);
            Func <IReadOnlyList <Filling>, int, IEnumerable <Filling[]> > functionToTest2 = (options, length) => Combinatorics.AllFillingIntervalCombinations(options, length, (a, b) => false).Select(cwg => (Filling[])cwg.Combination);        //tests the method where expanding is always impossible, and hence should reprocude the results of the other testmethod

            foreach (var functionToTest in new[] { functionToTest1, functionToTest2 })
            {
                IEnumerable <Filling[]> result = Combinatorics.AllFillingIntervalCombinations(f0.ToSingletonReadOnlyList(), 1).ToList();
                IEnumerable <Filling[]> expectation = new Filling[][] { new Filling[] { f0 } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                Func <Filling[], int, IEnumerable <Filling[]> > getResult = (options, length) =>
                {
                    int maxLength          = options.Select(f => f.Occupation.Count).Max();
                    var equalLengthOptions = options.TruncateOrLengthenWithFalses(maxLength);

                    var initialFilling = Enumerable.Repeat(false, length)
                                         .Concat(Enumerable.Repeat(true, maxLength - length))
                                         .ToReadOnlyList();

                    return(Combinatorics.AllFillingIntervalCombinations(equalLengthOptions, maxLength, initialFilling));
                };
                result      = getResult(new[] { f0, f1 }, 1);
                expectation = new Filling[][] { new Filling[] { f0 } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f1 }, 2);
                expectation = new Filling[][] { new Filling[] { f0, f1 } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f1, f12 }, 1);
                expectation = new Filling[][] { new Filling[] { f0 } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f1, f12 }, 2);
                expectation = new Filling[][] { new Filling[] { f0, f1 } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f1, f12 }, 3);
                expectation = new Filling[][] { new Filling[] { f0, f12 } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f01, f12 }, 3);
                expectation = new Filling[][] { new Filling[] { f0, f12 } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f01, f02 }, 3);
                expectation = new Filling[][] { };
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f01, f02, f1, f12 }, 3);
                expectation = new Filling[][] { new Filling[] { f0, f12 }, new Filling[] { f1, f02 } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f01, f02, f1, f12, f2 }, 3);
                expectation = new Filling[][] { new Filling[] { f0, f12 }, new Filling[] { f1, f02 }, new Filling[] { f0, f1, f2 }, new Filling[] { f01, f2 } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f01, f02, f1, f12, f2 }, 2);
                expectation = new Filling[][] { new Filling[] { f0, f1 }, new Filling[] { f01 } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                var f0b = new Filling("0b", new[] { true });
                var f01b = new Filling("01b", new[] { true, true });
                var f1b = new Filling("1b", new[] { false, true });
                var f12b = new Filling("12b", new[] { false, true, true });
                var f02b = new Filling("02b", new[] { true, false, true });
                var f2b = new Filling("2b", new[] { false, false, true });
                var f123 = new Filling("123", new[] { true, true, true });

                result      = getResult(new[] { f0, f0b }, 1);
                expectation = new Filling[][] { new Filling[] { f0 }, new Filling[] { f0b } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f0b, f1, f1b }, 2);
                expectation = new Filling[][] { new Filling[] { f0, f1 }, new Filling[] { f0b, f1 }, new Filling[] { f0, f1b }, new Filling[] { f0b, f1b } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f0b, f1, f1b, f01, f01b }, 2);
                expectation = new Filling[][] { new Filling[] { f0, f1 }, new Filling[] { f0b, f1 }, new Filling[] { f0, f1b }, new Filling[] { f0b, f1b }, new Filling[] { f01 }, new Filling[] { f01b } }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));

                result      = getResult(new[] { f0, f0b, f1, f01, f01b, f2, f12b, f12, f123 }, 3);
                expectation = new Filling[][]
                {
                    new Filling[] { f0, f1, f2 },
                    new Filling[] { f0, f12 },
                    new Filling[] { f0, f12b },
                    new Filling[] { f0b, f1, f2 },
                    new Filling[] { f0b, f12 },
                    new Filling[] { f0b, f12b },
                    new Filling[] { f01, f2 },
                    new Filling[] { f01b, f2 },
                    new Filling[] { f123 }
                }.ToList();
                Contract.Assert(result.ContainsSameElements(expectation, InterfaceWraps.ToEqualityComparer <Filling[]>(Equals, getHashCode)));
            }
        }