示例#1
0
        public void Test_BinarySearch_ShouldReturnTrue_WhenItemIsContainedInTheCollection()
        {
            collection.Sort(new QuickSorter <int>());
            var isContained = collection.BinarySearch(1);

            Assert.IsTrue(isContained, "BinarySearch should return true when element is contained in the collection.");
        }
        public void MethodShouldReturnFalseWhenItemDoesnotExists()
        {
            var collection = new SortableCollection<int>(new[] { 52, 111, 33, 33, 46, 1125, 1 });

            Assert.IsFalse(collection.BinarySearch(1111));
            Assert.IsFalse(collection.BinarySearch(-52));
            Assert.IsFalse(collection.BinarySearch(0));
        }
        public void MethodShouldReturnFalseWhenItemDoesnotExists()
        {
            var collection = new SortableCollection <int>(new[] { 52, 111, 33, 33, 46, 1125, 1 });

            Assert.IsFalse(collection.BinarySearch(1111));
            Assert.IsFalse(collection.BinarySearch(-52));
            Assert.IsFalse(collection.BinarySearch(0));
        }
示例#4
0
 public void TesBinarySearchtWithEmptyCollection()
 {
     SortableCollection<int> collection = new SortableCollection<int>();
     bool result = collection.BinarySearch(15);
     Assert.IsFalse(result);
     result = collection.BinarySearch(0);
     Assert.IsFalse(result);
     result = collection.BinarySearch(-1);
     Assert.IsFalse(result);
 }
示例#5
0
        public void TesBinarySearchtWithEmptyCollection()
        {
            SortableCollection <int> collection = new SortableCollection <int>();
            bool result = collection.BinarySearch(15);

            Assert.IsFalse(result);
            result = collection.BinarySearch(0);
            Assert.IsFalse(result);
            result = collection.BinarySearch(-1);
            Assert.IsFalse(result);
        }
 public void TestForSingleItem()
 {
     SortableCollection<string> collection = new SortableCollection<string>(
         new List<string>() { "1" });
     Assert.IsTrue(collection.LinearSearch("1"), "Linear search failed on single item collection");
     Assert.IsTrue(collection.BinarySearch("1"), "Binary search failed on single item collection");
     Assert.IsFalse(collection.LinearSearch("2"),
         "Linear search failed on single item collection - non-existing item");
     Assert.IsFalse(collection.BinarySearch("2"),
         "Binary search failed on single item collection - non-existing item");
 }
示例#7
0
        public void TesBinarySearchtWithOneMissingElement()
        {
            SortableCollection<int> collection = new SortableCollection<int>(
                new int[] { 3 });

            bool result = collection.BinarySearch(15);
            Assert.IsFalse(result);
            result = collection.BinarySearch(0);
            Assert.IsFalse(result);
            result = collection.BinarySearch(-1);
            Assert.IsFalse(result);
        }
 public void TestForOddItems()
 {
     SortableCollection<string> collection = new SortableCollection<string>(
         new List<string>() { "5", "1", "4", "1", "6" });
     Assert.IsTrue(collection.LinearSearch("1"), "Linear search failed on odd items collection");
     collection.Sort(new SelectionSorter<string>());
     Assert.IsTrue(collection.BinarySearch("1"), "Binary search failed on odd items collection");
     Assert.IsFalse(collection.LinearSearch("2"),
         "Linear search failed on odd items collection - non-existing item");
     Assert.IsFalse(collection.BinarySearch("2"),
         "Binary search failed on odd items collection - non-existing item");
 }
示例#9
0
        public void TesBinarySearchTrivialCaseMissingElement()
        {
            SortableCollection <int> collection = new SortableCollection <int>(
                new int[] { -33, -21, 0, 1, 2, 2, 4, 5, 6, 7, 12, 15 });

            bool result = collection.BinarySearch(8);

            Assert.IsFalse(result);
            result = collection.BinarySearch(3);
            Assert.IsFalse(result);
            result = collection.BinarySearch(11);
            Assert.IsFalse(result);
        }
示例#10
0
        public void TesBinarySearchtWithOneMissingElement()
        {
            SortableCollection <int> collection = new SortableCollection <int>(
                new int[] { 3 });

            bool result = collection.BinarySearch(15);

            Assert.IsFalse(result);
            result = collection.BinarySearch(0);
            Assert.IsFalse(result);
            result = collection.BinarySearch(-1);
            Assert.IsFalse(result);
        }
示例#11
0
        public void TesBinarySearchTrivialCase()
        {
            SortableCollection<int> collection = new SortableCollection<int>(
                new int[] { -33, -21, 0, 1, 2, 2, 4, 5, 6, 7, 12, 15 });

            bool result = collection.BinarySearch(2);
            Assert.IsTrue(result);
            result = collection.BinarySearch(4);
            Assert.IsTrue(result);
            result = collection.BinarySearch(5);
            Assert.IsTrue(result);
            result = collection.BinarySearch(6);
            Assert.IsTrue(result);
        }
示例#12
0
        public void TesBinarySearchTrivialCase()
        {
            SortableCollection <int> collection = new SortableCollection <int>(
                new int[] { -33, -21, 0, 1, 2, 2, 4, 5, 6, 7, 12, 15 });

            bool result = collection.BinarySearch(2);

            Assert.IsTrue(result);
            result = collection.BinarySearch(4);
            Assert.IsTrue(result);
            result = collection.BinarySearch(5);
            Assert.IsTrue(result);
            result = collection.BinarySearch(6);
            Assert.IsTrue(result);
        }
        public void TestBinarySearchWithExistingRepeatingElementInCollection()
        {
            var collection = new SortableCollection<int>(new[] { 101, 101, 33, 22, 11, 0 });
            var result = collection.BinarySearch(101);

            Assert.IsTrue(result, "Element not found");
        }
        public void TestBinarySearchWithExistingElementInMiddlePositionOddCountOfElements()
        {
            var collection = new SortableCollection<int>(new[] { 49, 101, 33, 22, 11, 0, 17 });
            var result = collection.BinarySearch(22);

            Assert.IsTrue(result, "Element not found");
        }
        public void TestBinarySearchWithExistingElementInFirstPositionEvenCountOfElements()
        {
            var collection = new SortableCollection<int>(new[] { 49, 101, 33, 22, 11, 0 });
            var result = collection.BinarySearch(49);

            Assert.IsTrue(result, "Element not found");
        }
        public void TestBinarySearchWithEmptyCollection()
        {
            var collection = new SortableCollection<int>();
            var result = collection.BinarySearch(101);

            Assert.IsFalse(result, "Element found in empty collection");
        }
        public void TestBinarySearchWithCollectionOfEqualElements()
        {
            var collection = new SortableCollection<int>(new[] { 101, 101, 101, 101, 101 });
            var result = collection.BinarySearch(101);

            Assert.IsTrue(result, "Element not found");
        }
        public void SearchValueNotFoundInSortedCollection()
        {
            var  collection = new SortableCollection <int>(new[] { -4, 1, 3, 5, 10, 14 });
            bool actual     = collection.BinarySearch(33);

            Assert.IsFalse(actual);
        }
        public void TestBinarySearchWithEmptyCollection()
        {
            var collection = new SortableCollection <int>();
            var result     = collection.BinarySearch(101);

            Assert.IsFalse(result, "Element found in empty collection");
        }
        public void TestWithMultipleMissingKeysLargerThanMaximum()
        {
            const int NumberOfChecks   = 10000;
            const int NumberOfElements = 1000;

            var elements = new int[NumberOfElements];

            for (int i = 0; i < NumberOfElements; i++)
            {
                elements[i] = Random.Next(int.MinValue / 2, int.MaxValue / 2);
            }

            Array.Sort(elements);

            var collection = new SortableCollection <int>(elements);

            for (int i = 0; i < NumberOfChecks; i++)
            {
                var item = Random.Next(collection.Items[collection.Count - 1], int.MaxValue);

                int result = collection.BinarySearch(item);

                Assert.AreEqual(-1, result);
            }
        }
示例#21
0
        public static void Main()
        {
            List<int> numbers = new List<int>() {1,3,4,6,7,8,9,10,23,42};

            SortableCollection<int> sortableCollection = new SortableCollection<int>(numbers);
            Console.WriteLine(sortableCollection.BinarySearch(-12));
        }
        public void TestBinarySearchWithCollectionOfEqualElements()
        {
            var collection = new SortableCollection <int>(new[] { 101, 101, 101, 101, 101 });
            var result     = collection.BinarySearch(101);

            Assert.IsTrue(result, "Element not found");
        }
        public void TestBinarySearchWithExistingRepeatingElementInCollection()
        {
            var collection = new SortableCollection <int>(new[] { 101, 101, 33, 22, 11, 0 });
            var result     = collection.BinarySearch(101);

            Assert.IsTrue(result, "Element not found");
        }
示例#24
0
    static void Main()
    {
        var collection = new SortableCollection<int>(new[] { 22, 11, 101, 33, 0, 101 });
        Console.WriteLine("All items before sorting:");
        Console.WriteLine(collection);

        Console.WriteLine("SelectionSorter result:");
        collection.Sort(new SelectionSorter<int>());
        Console.WriteLine(collection);

        Console.WriteLine("Quicksorter result:");
        collection.Sort(new Quicksorter<int>());
        Console.WriteLine(collection);

        Console.WriteLine("MergeSorter result:");
        collection.Sort(new MergeSorter<int>());
        Console.WriteLine(collection);

        Console.WriteLine("Linear search 101:");
        Console.WriteLine(collection.LinearSearch(101));

        Console.WriteLine("Binary search 101:");
        Console.WriteLine(collection.BinarySearch(101));

        Console.WriteLine("Shuffle:");
        collection.Shuffle();
        Console.WriteLine(collection);

        Console.WriteLine("Shuffle again:");
        collection.Shuffle();
        Console.WriteLine(collection);
    }
示例#25
0
    static void Main()
    {
        var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 });

        Console.WriteLine("All items before sorting:");
        Console.WriteLine(collection);

        Console.WriteLine("SelectionSorter result:");
        collection.Sort(new SelectionSorter <int>());
        Console.WriteLine(collection);

        Console.WriteLine("Quicksorter result:");
        collection.Sort(new Quicksorter <int>());
        Console.WriteLine(collection);

        Console.WriteLine("MergeSorter result:");
        collection.Sort(new MergeSorter <int>());
        Console.WriteLine(collection);

        Console.WriteLine("Linear search 101:");
        Console.WriteLine(collection.LinearSearch(101));

        Console.WriteLine("Binary search 101:");
        Console.WriteLine(collection.BinarySearch(101));

        Console.WriteLine("Shuffle:");
        collection.Shuffle();
        Console.WriteLine(collection);

        Console.WriteLine("Shuffle again:");
        collection.Shuffle();
        Console.WriteLine(collection);
    }
        public void BinarySearchShouldWorkCorrectlyWithEmptyCollection()
        {
            var collection = new SortableCollection <int>();
            var found      = collection.BinarySearch(21);

            Assert.IsFalse(found, "Element is found in empty collection!");
        }
示例#27
0
        internal static void Main(string[] args)
        {
            var collection = new SortableCollection<int>(new[] { 22, 11, 101, 33, 0, 101 });
            Console.WriteLine("All items before sorting:");
            collection.PrintAllItemsOnConsole();

            Console.WriteLine("SelectionSorter result:");
            collection.Sort(new SelectionSorter<int>());
            collection.PrintAllItemsOnConsole();

            collection = new SortableCollection<int>(new[] { 22, 11, 101, 33, 0, 101 });
            Console.WriteLine("Quicksorter result:");
            collection.Sort(new QuickSorter<int>());
            collection.PrintAllItemsOnConsole();

            collection = new SortableCollection<int>(new[] { 22, 11, 101, 33, 0, 101 });
            Console.WriteLine("MergeSorter result:");
            collection.Sort(new MergeSorter<int>());
            collection.PrintAllItemsOnConsole();

            Console.WriteLine("Linear search 101:");
            Console.WriteLine(collection.LinearSearch(101) + Environment.NewLine);

            Console.WriteLine("Binary search 101:");
            Console.WriteLine(collection.BinarySearch(101) + Environment.NewLine);

            Console.WriteLine("Shuffle:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();

            Console.WriteLine("Shuffle again:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
        }
        public void BinarySearchShouldWorkCorrectlyWithEmptyCollection()
        {
            var collection = new SortableCollection<int>();
            var found = collection.BinarySearch(21);

            Assert.IsFalse(found, "Element is found in empty collection!");
        }
示例#29
0
        public void BinarySearchNotFoundTest()
        {
            var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 });
            var actual     = collection.BinarySearch(5);

            Assert.AreEqual(false, actual);
        }
示例#30
0
        public void BinarySearchNullTest()
        {
            var    collection = new SortableCollection <string>(new[] { "da", "ne" });
            string word       = null;

            collection.BinarySearch(word);
        }
        public void TestBinarySearchWithExistingElementInLastPositionOddCountOfElements()
        {
            var collection = new SortableCollection <int>(new[] { 49, 101, 33, 22, 11, 0, 17 });
            var result     = collection.BinarySearch(17);

            Assert.IsTrue(result, "Element not found");
        }
示例#32
0
        public void BinarySearchFoundPositionAtEndTest()
        {
            var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 });
            var actual     = collection.BinarySearch(22);

            Assert.AreEqual(true, actual);
        }
        public void TestWithRepeatingItemShouldReturnFirstDiscoveredIndex()
        {
            var collection = new SortableCollection <int>(0, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7);
            var result     = collection.BinarySearch(3);

            Assert.AreEqual(2, result);
        }
示例#34
0
        public void TesBinarySearchtWithFirstMatchingElement()
        {
            SortableCollection<int> collection = new SortableCollection<int>(
                new int[] { -3, -1, 1, 15, 101, 333, 334, 444 });

            bool result = collection.BinarySearch(-3);
            Assert.IsTrue(result);
        }
示例#35
0
        public void BinarySearchTest()
        {
            var collection = new SortableCollection <int>(new[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });

            bool result = collection.BinarySearch(8);

            Assert.IsTrue(result);
        }
示例#36
0
        public void TesBinarySearchtWithOneElement()
        {
            SortableCollection<int> collection = new SortableCollection<int>(
                new int[] { 3 });

            bool result = collection.BinarySearch(3);
            Assert.IsTrue(result);
        }
示例#37
0
        public void BinarySearchNonExistingTest()
        {
            var collection = new SortableCollection <int>(new[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });

            bool result = collection.BinarySearch(10);

            Assert.IsFalse(result);
        }
示例#38
0
        public void TesBinarySearchtWithLastMatchingElement()
        {
            SortableCollection<int> collection = new SortableCollection<int>(
                new int[] { -33, -21, 0, 1, 2, 2, 4, 5, 6, 7, 12, 15 });

            bool result = collection.BinarySearch(15);
            Assert.IsTrue(result);
        }
        public void TestWithTwoItemsNotFound()
        {
            SortableCollection<int> collection = new SortableCollection<int>(new int[] { 4, 3 });
            collection.Sort(new MergeSorter<int>());

            bool isFound = collection.BinarySearch(0);
            Assert.IsFalse(isFound);
        }
示例#40
0
        public void TestBinarySearchWithSeveralMatchingElements()
        {
            SortableCollection <int> collection = new SortableCollection <int>(
                new int[] { -33, -21, -21, 1, 1, 2, 2, 3, 4, 7, 7, 7, 11, 12, 123, 156 });
            bool result = collection.BinarySearch(7);

            Assert.IsTrue(result);
        }
 public void TestBinarySearchResultIsNotContained()
 {
     int[] input = { -1, 5, 8, 3, 4, 67, -9, 3, 9, 4 };
     SortableCollection<int> sortableCol = new SortableCollection<int>(input);
     sortableCol.Sort(new MergeSorter<int>());
     bool containsItem = sortableCol.BinarySearch(0);
     Assert.IsFalse(containsItem);
 }
        public void BinarySearchItemNotFound()
        {
            var searchCollection = new SortableCollection<int>(new[] { 22, 11, 101, 33, 0, 122, 22, 33, 11, 3445667, 1233, 123, 101 });

            searchCollection.Sort(new SelectionSorter<int>()); 

            Assert.IsFalse(searchCollection.BinarySearch(66));
        }
        public void BinarySearchItemFound()
        {
            var searchCollection = new SortableCollection<int>(new[] { 22, 11, 101, 33, 0, 122, 22, 33, 11, 3445667, 1233, 123, 101 });

            searchCollection.Sort(new Quicksorter<int>()); 

            Assert.IsTrue(searchCollection.BinarySearch(11));
        }
示例#44
0
        public void TestBinarySearch_ItemEqualToMaximum()
        {
            int[] sortedNumbers = new int[] { 1, 5, 7, 9, 22 };
            SortableCollection <int> collection = new SortableCollection <int>(sortedNumbers);
            bool numberFound = collection.BinarySearch(22);

            Assert.AreEqual(true, numberFound);
        }
示例#45
0
        public void BinarySearch()
        {
            var collection = new SortableCollection<int>(new[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });

            bool result = collection.BinarySearch(8);

            Assert.IsTrue(result);
        }
        public void TestWithOddNumberOfItemsFoundAtMiddlePosition()
        {
            SortableCollection<int> collection = new SortableCollection<int>(new int[] { 4, 3, -2, 11, 16, 0, 1 });
            collection.Sort(new MergeSorter<int>());

            bool isFound = collection.BinarySearch(3);
            Assert.IsTrue(isFound);
        }
        public void BinarySearchShouldWorkCorrectlyWhenSearchedElementIsLast()
        {
            var collection = new SortableCollection<int>(new int[] { 6, 1, 4, 2, 3, 21, -5, 8 });
            collection.Sort(new MergeSorter<int>());
            var found = collection.BinarySearch(8);

            Assert.IsTrue(found, "Element is not found in collection!");
        }
示例#48
0
        public void TestBinarySearch_ItemGreaterThanMaximum()
        {
            int[] sortedNumbers = new int[] { 1, 5, 7, 9, 22 };
            SortableCollection <int> collection = new SortableCollection <int>(sortedNumbers);
            bool numberFound = collection.BinarySearch(30);

            Assert.AreEqual(false, numberFound);
        }
        public void BinarySearchShouldWorkCorrectlyWhenSearchedElementIsNotFound()
        {
            var collection = new SortableCollection<int>(new int[] { 6, 1, 4, 2, 3, -5, 8 });
            collection.Sort(new MergeSorter<int>());
            var found = collection.BinarySearch(21);

            Assert.IsFalse(found, "Element is found in collection!");
        }
示例#50
0
        public void BinarySearchShouldReturnFalseInSearchingInEmptyCollection()
        {
            int[] data = new int[] { };
            SortableCollection <int> sortable = new SortableCollection <int>(data);
            bool found = sortable.BinarySearch(4);

            Assert.IsFalse(found);
        }
示例#51
0
        public void BinarySearchNonExisting()
        {
            var collection = new SortableCollection<int>(new[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });

            bool result = collection.BinarySearch(10);

            Assert.IsFalse(result);
        }
 public void TestBinarySearchResultIsLast()
 {
     int[] input = { 8, 5, 8, 3, 4, 67, 9, 3, 9, -4 };
     SortableCollection<int> sortableCol = new SortableCollection<int>(input);
     sortableCol.Sort(new MergeSorter<int>());
     bool containsItem = sortableCol.BinarySearch(-4);
     Assert.IsTrue(containsItem);
 }
        public void TestBinarySearch_ItemEqualToMaximum()
        {
            int[] sortedNumbers = new int[] { 1, 5, 7, 9, 22 };
            SortableCollection<int> collection = new SortableCollection<int>(sortedNumbers);
            bool numberFound = collection.BinarySearch(22);

            Assert.AreEqual(true, numberFound);
        }
        public void TestBinarySearch_ItemGreaterThanMaximum()
        {
            int[] sortedNumbers = new int[] { 1, 5, 7, 9, 22 };
            SortableCollection<int> collection = new SortableCollection<int>(sortedNumbers);
            bool numberFound = collection.BinarySearch(30);

            Assert.AreEqual(false, numberFound);
        }
示例#55
0
        public void ReturnTrue_WhenElementIsFound <T>(IList <T> items, T itemToSearchFor)
            where T : IComparable <T>
        {
            var sut = new SortableCollection <T>(items);

            var result = sut.BinarySearch(itemToSearchFor);

            Assert.IsTrue(result);
        }
        public void TestWithItemToTheRightOfMidpoint()
        {
            var collection = new SortableCollection <int>(1, 2, 3, 4, 5);

            var result   = collection.BinarySearch(4);
            var expected = Array.BinarySearch(collection.ToArray(), 4);

            Assert.AreEqual(expected, result);
        }
示例#57
0
        public void BinarySearchShouldReturnTrueIfElementExists()
        {
            int[] data     = new int[] { 1, 2, 5, 100, 230 };
            var   sortable = new SortableCollection <int>(data);

            bool found = sortable.BinarySearch(5);

            Assert.IsTrue(found);
        }
示例#58
0
        public void BinarySearchShouldReturnFalseIfElementDoesntExists()
        {
            int[] data     = new int[] { 1, 5, 10, 25, 100 };
            var   sortable = new SortableCollection <int>(data);

            bool found = sortable.BinarySearch(19);

            Assert.IsFalse(found);
        }
示例#59
0
        public void BinarySearchShouldReturnTrueIFElementExistsInCollectionWithSizeOne()
        {
            int[] data     = new int[] { 5 };
            var   sortable = new SortableCollection <int>(data);

            bool found = sortable.BinarySearch(5);

            Assert.IsTrue(found);
        }
示例#60
0
        public void BinarySearchSearch_LastItemFound()
        {
            var collection = new SortableCollection <int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });

            int expected = 0;
            int actual   = collection.BinarySearch(1);

            Assert.AreEqual(expected, actual);
        }