Exemplo n.º 1
0
        public bool Equals(NumericRangeFilter other)
        {
            if (other is null)
            {
                return(false);
            }

            return(LowerBound == other.LowerBound &&
                   UpperBound == other.UpperBound);
        }
        public void LeafItemFilter_GetPredicate_FiltersToExpectedResults()
        {
            var filter = new NumericRangeFilter(5, 10);
            var values = new[] { 1, 3, 5, 9, 11 };
            var expectedFilteredValues = new[] { 5, 9 };

            var filterPredicate = filter.GetPredicate <NumericRangeFilter, int>();
            var filteredValues  = values.Where(filterPredicate);

            Assert.Equal(expectedFilteredValues, filteredValues);
        }
        public void CombinationFilter_OrOperator_GetPredicate_FiltersToExpectedResults()
        {
            var filter1 = new NumericRangeFilter(5, 10);
            var filter2 = new NumericRangeFilter(8, 15);
            var filter  = new CombinationFilter <NumericRangeFilter>(new[] { filter1, filter2 }, CombinationOperator.Any);
            var values  = new[] { 1, 3, 5, 9, 11 };
            var expectedFilteredValues = new[] { 5, 9, 11 };

            var filterPredicate = filter.GetPredicate <NumericRangeFilter, int>();
            var filteredValues  = values.Where(filterPredicate);

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

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

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

            Assert.Equal(expectedFilteredValues, filteredValues);
        }
        public void ComplexFilter_GetPredicate_FiltersToExpectedResults()
        {
            var filter5To10         = new NumericRangeFilter(5, 10);
            var filter8To15         = new NumericRangeFilter(8, 15);
            var filter5To10And8To15 = new CombinationFilter <NumericRangeFilter>(new[] { filter5To10, filter8To15 }, CombinationOperator.Any);
            var filter9To12         = new NumericRangeFilter(9, 12);
            var filter = new CombinationFilter <NumericRangeFilter>(new IFilterNode <NumericRangeFilter>[] { filter5To10And8To15, filter9To12 }, CombinationOperator.All);
            var values = new[] { 1, 3, 5, 9, 11 };
            var expectedFilteredValues = new[] { 9, 11 };

            var filterPredicate = filter.GetPredicate <NumericRangeFilter, int>();
            var filteredValues  = values.Where(filterPredicate);

            Assert.Equal(expectedFilteredValues, filteredValues);
        }
        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);
        }
Exemplo n.º 7
0
        public int CompareTo(NumericRangeFilter other)
        {
            if (ReferenceEquals(this, other))
            {
                return(0);
            }

            if (ReferenceEquals(null, other))
            {
                return(1);
            }

            var lowerBoundComparison = LowerBound.CompareTo(other.LowerBound);

            return(lowerBoundComparison != 0
                ? lowerBoundComparison
                : UpperBound.CompareTo(other.UpperBound));
        }
Exemplo n.º 8
0
        public void Aggregate_ReturnsExpectedResult_WhenComputingMinimumLowerBound()
        {
            var filter5To10 = new NumericRangeFilter(5, 10);
            var filter      = filter5To10.Invert();

            double GetLowerBound(NumericRangeFilter f)
            {
                return(f.LowerBound);
            }

            // Reduce the filter to get the length of the longest interval
            var result = filter.Aggregate(
                (lowerBounds, _) => lowerBounds.Min(),
                length => double.NegativeInfinity,
                GetLowerBound);

            // Max is 15 - 8
            Assert.Equal(double.NegativeInfinity, result);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
        public void Map_ReturnsCorrectStructureAndValues()
        {
            var filter5To10        = new NumericRangeFilter(5, 10);
            var filter8To15        = new NumericRangeFilter(8, 15);
            var filter5To10Or8To15 = new CombinationFilter <NumericRangeFilter>(new[] { filter5To10, filter8To15 }, CombinationOperator.Any);
            var filter9To12        = new NumericRangeFilter(9, 12);
            var filter             = new CombinationFilter <NumericRangeFilter>(new IFilterNode <NumericRangeFilter>[] { filter5To10Or8To15, filter9To12 }, 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 CombinationFilter <NumericRangeFilter>(new[] { filter6To9, filter9To14 }, CombinationOperator.Any);
            var filter10To11      = new NumericRangeFilter(10, 11);
            var expectedFilter    = new CombinationFilter <NumericRangeFilter>(new IFilterNode <NumericRangeFilter>[] { filter6To9Or9To14, filter10To11 }, CombinationOperator.All);

            Assert.Equal(expectedFilter, result, EqualityComparer <IFilterNode> .Default);
        }