コード例 #1
0
ファイル: Tests.cs プロジェクト: JeroenBos/JBSnorro
        public void TestExpandableIntervalCombinations()
        {
            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 });

            CanExpandFillingDelegate <Filling> canExpand = (filling, gap) => filling.Occupation.Count >= 2 &&
                                                           filling.Occupation[1] &&
                                                           !filling.Occupation[0] &&
                                                           gap == new Gap(0, 1);

            IEnumerable <CombinationWithGaps <Filling> > result = Combinatorics.AllFillingIntervalCombinations(f0.ToSingletonReadOnlyList(), 1, canExpand).ToList();
            IEnumerable <Filling[]> expectation = new Filling[][] { new Filling[] { f0 } }.ToList();

            Contract.Assert(result.ContainsSameElements(expectation, Equals));

            result      = Combinatorics.AllFillingIntervalCombinations(f1.ToSingletonReadOnlyList(), 2, canExpand).ToList();
            expectation = new Filling[][] { new Filling[] { f1 } }.ToList();            //even though f1 doesn't fill position 0
            Contract.Assert(result.ContainsSameElements(expectation, Equals));

            result      = Combinatorics.AllFillingIntervalCombinations(f1.ToSingletonReadOnlyList(), 3, canExpand).ToList();
            expectation = new Filling[0][].ToList();            //f1 may expand to position 0, but not to position 2 (which wasn't available in the case above
            Contract.Assert(result.ContainsSameElements(expectation, Equals));

            result      = Combinatorics.AllFillingIntervalCombinations(f12.ToSingletonReadOnlyList(), 3, canExpand).ToList();
            expectation = new Filling[][] { new Filling[] { f12 } }.ToList();            //even though f12 doesn't fill position 0
            Contract.Assert(result.ContainsSameElements(expectation, Equals));

            result      = Combinatorics.AllFillingIntervalCombinations(new[] { f1, f2 }, 3, canExpand).ToList();
            expectation = new Filling[][] { new Filling[] { f1, f2 } }.ToList();            //f1 fills position 0
            Contract.Assert(result.ContainsSameElements(expectation, Equals));

            result      = Combinatorics.AllFillingIntervalCombinations(new[] { f0, f2 }, 3, canExpand).ToList();
            expectation = new Filling[0][].ToList();            //position 1 is unfilled
            Contract.Assert(result.ContainsSameElements(expectation, Equals));

            result      = Combinatorics.AllFillingIntervalCombinations(new Filling[0], 1, canExpand).ToList();
            expectation = new Filling[0][].ToList();
            Contract.Assert(result.ContainsSameElements(expectation, Equals));

            result      = Combinatorics.AllFillingIntervalCombinations(new Filling[0], 3, canExpand).ToList();
            expectation = new Filling[0][].ToList();
            Contract.Assert(result.ContainsSameElements(expectation, Equals));

            result      = Combinatorics.AllFillingIntervalCombinations(new[] { f1, f12 }, 3, canExpand).ToList();
            expectation = new Filling[][] { new Filling[] { f12 } }.ToList();            //f12 fills position 0, f1 remains unused
            Contract.Assert(result.ContainsSameElements(expectation, Equals));

            //realization: an expansion shouldn't be applied when it could be omitted. Or at least, those without the expansion should be yielded first.
            //So the output of AllFillingIntervalCombinations should be ordered by the number of expansions used
        }
コード例 #2
0
ファイル: Tests.cs プロジェクト: JeroenBos/JBSnorro
 public bool Equals(Filling other)
 {
     return(other != null && other.Tag == this.Tag && other.Occupation.SequenceEqual(this.Occupation));
 }
コード例 #3
0
ファイル: Tests.cs プロジェクト: JeroenBos/JBSnorro
        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)));
            }
        }