public void Task_03_Single_Priority()
        {
            var pc = new PriorityComparer <Item>("Z");
            var dc = new DelegateComparer <Item>(CompareFirstName);
            var c  = new CompositeComparer <Item>(new IComparer <Item>[] { pc, dc });
            var z  = new Item("Z");

            c.Compare(A, z).Should().BeGreaterThan(0);
            c.Compare(z, A).Should().BeLessThan(0);
        }
        public void Task_03_Single_Descending(Item x, Item y, bool?result)
        {
            var dc = new DelegateComparer <Item>(CompareFirstNameDescending);
            var c  = new CompositeComparer <Item>(new[] { dc });
            var v  = c.Compare(x, y);

            if (v == 0)
            {
                result.Should().BeNull();
                return;
            }

            if (v < 0)
            {
                result.Should().BeTrue();
                return;
            }

            result.Should().BeFalse();
        }
Exemple #3
0
        public void Compare_IfOneComparerReturnNotZero_TheNextComparerShouldNotBeCalled()
        {
            var returnValue       = -1;
            var compositeComparer = new CompositeComparer <object>(this.CreateComparer <object>(returnValue), this.CreateComparer <object>(Times.Never()));

            Assert.AreEqual(returnValue, compositeComparer.Compare(new object(), new object()));

            returnValue       = -10;
            compositeComparer = new CompositeComparer <object>(this.CreateComparer <object>(returnValue), this.CreateComparer <object>(Times.Never()));

            Assert.AreEqual(returnValue, compositeComparer.Compare(new object(), new object()));

            returnValue       = 1;
            compositeComparer = new CompositeComparer <object>(this.CreateComparer <object>(returnValue), this.CreateComparer <object>(Times.Never()));

            Assert.AreEqual(returnValue, compositeComparer.Compare(new object(), new object()));

            returnValue       = 70;
            compositeComparer = new CompositeComparer <object>(this.CreateComparer <object>(returnValue), this.CreateComparer <object>(Times.Never()));

            Assert.AreEqual(returnValue, compositeComparer.Compare(new object(), new object()));
        }
        private int InternalBinarySearch(int index, int count, T item, bool checkForEquality)
        {
            int lo = index;
            int hi = index + count - 1;

            while (lo <= hi)
            {
                int mid = lo + ((hi - lo) >> 1);
                // Ignore items that need to be resorted
                int usableMid = mid;
                while (usableMid <= hi && LiveShapingItems[BackingList[usableMid]].IsSortDirty)
                {
                    if (checkForEquality && EqualityComparer.Equals(BackingList[usableMid], item))
                    {
                        return(usableMid);
                    }

                    usableMid++;
                }

                if (usableMid > hi)
                {
                    //everything in [mid,hi] had a dirty sort
                    usableMid = mid - 1;

                    while (usableMid >= lo && LiveShapingItems[BackingList[usableMid]].IsSortDirty)
                    {
                        if (checkForEquality && EqualityComparer.Equals(BackingList[usableMid], item))
                        {
                            return(usableMid);
                        }

                        usableMid--;
                    }

                    if (usableMid < lo)
                    {
                        //everything in [lo,hi] was dirty, so place it after that interval
                        return(~(hi + 1));
                    }

                    var usableOrder = CompositeComparer.Compare(item, BackingList[usableMid]);

                    if (usableOrder == 0)
                    {
                        return(checkForEquality && !EqualityComparer.Equals(BackingList[usableMid], item) ? ~usableMid : usableMid);
                    }

                    if (usableOrder < 0)
                    {
                        hi = usableMid - 1;
                    }
                    else
                    {
                        //we already checked that [mid,hi] is dirty, so instead of setting lo = mid + 1 we can just end here
                        return(~(hi + 1));
                    }
                }
                else
                {
                    var usableOrder = CompositeComparer.Compare(item, BackingList[usableMid]);
                    if (usableOrder == 0)
                    {
                        return(checkForEquality && !EqualityComparer.Equals(BackingList[usableMid], item) ? ~usableMid : usableMid);
                    }
                    // we know that [mid,usableMid) are dirty and should be ignored
                    if (usableOrder < 0)
                    {
                        hi = mid - 1;
                    }
                    else
                    {
                        lo = usableMid + 1;
                    }
                }
            }

            return(~lo);
        }