Esempio n. 1
0
 public void TestCopyTo()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     int[] array = new int[0];
     tree.CopyTo(array, 0);
     Assert.IsTrue(array.Length == 0);
     //basic copy
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     array = new int[10];
     int startIndex = 0;
     tree.CopyTo(array, startIndex);
     foreach (int curr in tree)
     {
         Assert.IsTrue(curr == array[startIndex]);
         startIndex++;
     }
     //copy at later position in middle
     array = new int[25];
     startIndex = 10;
     tree.CopyTo(array, startIndex);
     foreach (int curr in tree)
     {
         Assert.IsTrue(curr == array[startIndex]);
         startIndex++;
     }
     //copy to final positions of array.
     array = new int[25];
     startIndex = 15;
     tree.CopyTo(array, startIndex);
     foreach (int curr in tree)
     {
         Assert.IsTrue(curr == array[startIndex]);
         startIndex++;
     }
 }
        public void TestBSTCopyTo()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };

            int[] actual = new int[tree.Count];
            int[] expected = new int[] { 40, 11, 10, 34, 16, 62, 43, 63 };

            int[] actual2 = new int[tree.Count + 2];
            int[] expected2 = new int[] { 0, 0, 40, 11, 10, 34, 16, 62, 43, 63 };

            int[] actual3 = new int[tree.Count];
            int[] expected3 = new int[] { 0, 0, 40, 11, 10, 34, 16, 62 };

            tree.CopyTo(actual);
            tree.CopyTo(actual2, 2);
            tree.CopyTo(actual3, 2, 6);

            for (int i = 0; i < expected.Length; i++)
                Assert.AreEqual(expected[i], actual[i], "\nFailed at [" + i + "]");

            for (int i = 0; i < expected2.Length; i++)
                Assert.AreEqual(expected2[i], actual2[i], "\nFailed at [" + i + "]");

            for (int i = 0; i < expected3.Length; i++)
                Assert.AreEqual(expected3[i], actual3[i], "\nFailed at [" + i + "]");
        }
Esempio n. 3
0
 public void TestCopyToNullArray()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     int[] array  = null;
     tree.CopyTo(array, 0);
 }
Esempio n. 4
0
 public void TestCopyToSmallArray()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     int[] array = new int[5];
     //try copying to array that is too small
     try
     {
         tree.CopyTo(array, 0);
         Assert.Fail("Should fail, array too small");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentException);
     }
     //try copying too close to the end of the array.
     array = new int[25];
     try
     {
         tree.CopyTo(array, 20);
         Assert.Fail("Should fail, Not enough room to copy");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentException);
     }
 }
Esempio n. 5
0
 public void TestCopyToInvalidIndex()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     int[] array = new int[10];
     //Try to set start index outside of max size of array.
     try
     {
         tree.CopyTo(array, 11);
         Assert.Fail("Should fail, index is larger than size of array.");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentOutOfRangeException);
     }
     //try to copy to start index below 0
     try
     {
         tree.CopyTo(array, -2);
         Assert.Fail("Should fail, index is below 0");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentOutOfRangeException);
     }
 }