Ejemplo n.º 1
0
        public void Bind_ReturnsCorrectStructureAndValues()
        {
            var filter5To10 = new NumericRangeFilter(5, 10);
            var filter8To15 = new NumericRangeFilter(8, 15);
            var filter5To10Or8To15 = new[] { filter5To10, filter8To15 }.Combine(CombinationOperator.Any);
            var filter9To12 = new NumericRangeFilter(9, 12);
            var filter = new[] { filter5To10Or8To15, filter9To12.ToLeafFilterNode() }.Combine(CombinationOperator.All);

            var result = filter.Bind <NumericRangeFilter>(
                f =>
            {
                if (ReferenceEquals(f, filter5To10))
                {
                    return(new CombinationFilterNode <NumericRangeFilter>(new [] { filter5To10, filter8To15 }, CombinationOperator.Any));
                }

                if (ReferenceEquals(f, filter8To15))
                {
                    return(FilterNode <NumericRangeFilter> .False);
                }

                if (ReferenceEquals(f, filter9To12))
                {
                    return(new InvertedFilterNode <NumericRangeFilter>(f));
                }

                return(f.ToLeafFilterNode());
            });

            var expectedFilter = new CombinationFilterNode <NumericRangeFilter>(
                new IFilterNode <NumericRangeFilter>[]
            {
                new CombinationFilterNode <NumericRangeFilter>(
                    new IFilterNode <NumericRangeFilter>[]
                {
                    filter5To10Or8To15,
                    FilterNode <NumericRangeFilter> .False
                }, CombinationOperator.Any),
                new InvertedFilterNode <NumericRangeFilter>(filter9To12),
            }, CombinationOperator.All);

            Assert.Equal(expectedFilter, result);
        }
        public void Aggregate_ReturnsExpectedResult_WhenComputingLengthOfLongestInterval()
        {
            var filter5To10        = new NumericRangeFilter(5, 10);
            var filter8To15        = new NumericRangeFilter(8, 15);
            var filter5To10Or8To15 = new CombinationFilterNode <NumericRangeFilter>(new[] { filter5To10, filter8To15 }, CombinationOperator.Any);
            var filter9To12        = new NumericRangeFilter(9, 12);
            var filter             = new CombinationFilterNode <NumericRangeFilter>(new IFilterNode <NumericRangeFilter>[] { filter5To10Or8To15, filter9To12.ToLeafFilterNode() }, CombinationOperator.All);

            // Reduce the filter to get the length of the longest interval
            var result = filter.Aggregate <double>(
                (lengths, _) => lengths.Max(),
                length => double.PositiveInfinity,
                leafFilterNode => leafFilterNode.Filter.UpperBound - leafFilterNode.Filter.LowerBound);

            // Max is 15 - 8
            Assert.Equal(7, result);
        }
Ejemplo n.º 3
0
        public void ComplexFilter_IsMatch_FiltersToExpectedResults()
        {
            var filter5To10        = new NumericRangeFilter(5, 10);
            var filter8To15        = new NumericRangeFilter(8, 15);
            var filter5To10Or8To15 = new CombinationFilterNode <NumericRangeFilter>(new[] { filter5To10, filter8To15 }, CombinationOperator.Any);
            var filter9To12        = new NumericRangeFilter(9, 12);
            var filter             = new CombinationFilterNode <NumericRangeFilter>(new IFilterNode <NumericRangeFilter>[] { filter5To10Or8To15, filter9To12.ToLeafFilterNode() }, CombinationOperator.All);

            var values = new[] { 1, 3, 5, 9, 11 };
            var expectedFilteredValues = new[] { 9, 11 };

            var filteredValues = values.Where(filter.IsMatch);

            Assert.Equal(expectedFilteredValues, filteredValues);
        }
Ejemplo n.º 4
0
        public void Map_ReturnsCorrectStructureAndValues()
        {
            var filter5To10        = new NumericRangeFilter(5, 10);
            var filter8To15        = new NumericRangeFilter(8, 15);
            var filter5To10Or8To15 = new CombinationFilterNode <NumericRangeFilter>(new[] { filter5To10, filter8To15 }, CombinationOperator.Any);
            var filter9To12        = new NumericRangeFilter(9, 12);
            var filter             = new CombinationFilterNode <NumericRangeFilter>(new IFilterNode <NumericRangeFilter>[] { filter5To10Or8To15, filter9To12.ToLeafFilterNode() }, CombinationOperator.All);

            var result = filter.Map(
                f =>
            {
                var newLowerBound = f.LowerBound + 1;
                var newUpperBound = f.UpperBound - 1;
                return(new NumericRangeFilter(newLowerBound, newUpperBound));
            });

            var filter6To9        = new NumericRangeFilter(6, 9);
            var filter9To14       = new NumericRangeFilter(9, 14);
            var filter6To9Or9To14 = new CombinationFilterNode <NumericRangeFilter>(new[] { filter6To9, filter9To14 }, CombinationOperator.Any);
            var filter10To11      = new NumericRangeFilter(10, 11);
            var expectedFilter    = new CombinationFilterNode <NumericRangeFilter>(new IFilterNode <NumericRangeFilter>[] { filter6To9Or9To14, filter10To11.ToLeafFilterNode() }, CombinationOperator.All);

            Assert.Equal(expectedFilter, result);
        }