Пример #1
0
        public void Should_Check_BinarySearch_Ranges()
        {
            //arrange
            var list = new MyArrayList();

            list.Add(1);
            list.Add(2);

            //act
            Action actLower         = () => list.BinarySearch(1, -1, 1);
            Action actHigher        = () => list.BinarySearch(1, 1, 3);
            Action actLowerIsHigher = () => list.BinarySearch(1, 1, 0);

            //assert
            actLower.ShouldThrow <ArgumentOutOfRangeException>();
            actHigher.ShouldThrow <ArgumentOutOfRangeException>();
            actLowerIsHigher.ShouldThrow <ArgumentOutOfRangeException>();

            list.Capacity.ShouldBeEquivalentTo(4);
            list.Count.ShouldBeEquivalentTo(2);
        }
Пример #2
0
        public void Should_Check_BinarySearch_Throw_If_Null()
        {
            //arrange
            var list = new MyArrayList();

            list.Add(1);
            list.Add(2);

            //act
            Action act = () => list.BinarySearch(null);

            //assert
            act.ShouldThrow <ArgumentNullException>();
            list.Capacity.ShouldBeEquivalentTo(4);
            list.Count.ShouldBeEquivalentTo(2);
        }
Пример #3
0
        public void Should_Check_BinarySearch_Even_Length()
        {
            //arrange
            var list = new MyArrayList();

            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);

            //act
            var result = list.BinarySearch(3);

            //assert
            result.ShouldBeEquivalentTo(2);
            list.Capacity.ShouldBeEquivalentTo(4);
            list.Count.ShouldBeEquivalentTo(4);
        }
Пример #4
0
        public void Should_Check_BinarySearch_Not_Find_Middle()
        {
            //arrange
            var list = new MyArrayList();

            list.Add(1);
            list.Add(2);
            list.Add(4);
            list.Add(6);
            list.Add(7);

            //act
            var result = list.BinarySearch(3);

            //assert
            result.ShouldBeEquivalentTo(-1);
            list.Capacity.ShouldBeEquivalentTo(8);
            list.Count.ShouldBeEquivalentTo(5);
        }
Пример #5
0
        public void Should_Check_BinarySearch_With_Range_False()
        {
            //arrange
            var list = new MyArrayList();

            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);
            list.Add(5);
            list.Add(6);
            list.Add(7);

            //act
            var result = list.BinarySearch(1, 1, 6);

            //assert
            result.ShouldBeEquivalentTo(-1);
            list.Capacity.ShouldBeEquivalentTo(8);
            list.Count.ShouldBeEquivalentTo(7);
        }
Пример #6
0
    public static void Main()
    {
        // Creates and initializes a new ArrayList.
        MyArrayList coloredAnimals = new MyArrayList();

        coloredAnimals.Add("White Tiger");
        coloredAnimals.Add("Pink Bunny");
        coloredAnimals.Add("Red Dragon");
        coloredAnimals.Add("Green Frog");
        coloredAnimals.Add("Blue Whale");
        coloredAnimals.Add("Black Cat");
        coloredAnimals.Add("Yellow Lion");

        // BinarySearch requires a sorted ArrayList.
        coloredAnimals.Sort();

        // Compare results of an iterative search with a binary search
        int index = coloredAnimals.IterativeSearch("White Tiger");

        Console.WriteLine("Iterative search, item found at index: {0}", index);

        index = coloredAnimals.BinarySearch("White Tiger", new SimpleStringComparer());
        Console.WriteLine("Binary search, item found at index:    {0}", index);
    }