Advertisement
Pearlfromsu

avl tests

Mar 14th, 2024
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.02 KB | None | 0 0
  1. using BinTreeLib;
  2.  
  3. namespace TestProject1 {
  4.     public class UnitTest1 {
  5.         [Fact]
  6.         public void CountIncreaseAfterAdding() {
  7.             var tree = new AVLTree<int, int>();
  8.             var a = new[] { 69, 153, 74, 34, 75, 3, 6, 1, 7, 4, 54, 25, 2 };
  9.             int n = a.Length;
  10.             for (int i = 0; i < n; i++)
  11.                 tree.Add(a[i], i);
  12.             Assert.Equal(n, tree.Count);
  13.         }
  14.        
  15.         [Fact]
  16.         public void ExistingKeyException() {
  17.             var tree = new AVLTree<int, int>();
  18.             int n = 10;
  19.             for (int i = 0; i < n; i++)
  20.                 tree.Add(i, i);
  21.             Assert.Throws<ArgumentException>(() => tree.Add(n - 1, n - 1));
  22.         }
  23.        
  24.         [Fact]
  25.         public void MinException() {
  26.             var tree = new AVLTree<int, int>();
  27.             Assert.Throws<InvalidOperationException>(() => tree.Min());
  28.         }
  29.         [Fact]
  30.         public void RemoveUnexistedElement() {
  31.             var tree = new AVLTree<int, int>();
  32.             int n = 10;
  33.             for (int i = 0; i < n; i++)
  34.                 tree.Add(i, i);
  35.             Assert.Throws<ArgumentNullException>(() => tree.Remove(2 * n));
  36.         }
  37.  
  38.         [Fact]
  39.         public void ItemsExistAfterAdding() {
  40.             var tree = new AVLTree<int, int>();
  41.             var a = new[] { 69, 153, 74, 34, 75, 3, 6, 1, 7, 4, 54, 25, 2 };
  42.             int n = a.Length;
  43.             for (int i = 0; i < n; i++)
  44.                 tree.Add(a[i], i);
  45.             Assert.Equal(n, tree.Count);
  46.             Array.Sort(a);
  47.             int j = 0;
  48.             foreach (var pair in tree)
  49.                 Assert.Equal(a[j++], pair.Key);
  50.         }
  51.  
  52.         [Fact]
  53.         public void ExistingElementContains() {
  54.             var tree = new AVLTree<int, int>();
  55.             var arrayInts = new[] { 69, 153, 74, 34, 75, 3, 6, 1, 7, 4, 54, 25, 2 };
  56.             foreach (var number in arrayInts)
  57.                 tree.Add(number, 0);
  58.  
  59.             foreach (var number in arrayInts)
  60.                 Assert.True(tree.ContainsKey(number));
  61.         }
  62.  
  63.         [Fact]
  64.         public void UnexistedElementContains() {
  65.             var tree = new AVLTree<int, int>();
  66.             var a = new[] { 69, 153, 74, 34, 75, 3, 6, 1, 7, 4, 54, 25, 2 };
  67.             for (int i = 0; i < a.Length; i++)
  68.                 tree.Add(a[i], 0);
  69.             Assert.False(tree.ContainsKey(37));
  70.         }
  71.  
  72.         [Fact]
  73.         public void DictionaryConstructor() {
  74.             Dictionary<string, string> dict = new Dictionary<string, string> () {
  75.                 { "abc", "def" },
  76.                 { "ghj", "hlm" },
  77.                 { "nop", "qrs" }
  78.             };
  79.             AVLTree<string, string> avl = new AVLTree<string, string>(dict, StringComparer.CurrentCultureIgnoreCase);
  80.             Assert.True(dict.Count == avl.Count);
  81.             foreach (var pair in dict) {
  82.                 Assert.True(avl.ContainsKey(pair.Key));
  83.                 Assert.True(avl.ContainsValue(pair.Value));
  84.             }
  85.         }
  86.         [Fact]
  87.         public void UnexistedKeyException() {
  88.             var tree = new AVLTree<int, int>();
  89.             int n = 10;
  90.             for (int i = 0; i < n; i++)
  91.                 tree.Add(i, i);
  92.             Assert.Throws<KeyNotFoundException>(() => tree[n + 1]);
  93.         }
  94.  
  95.         [Fact]
  96.         public void AddItemToNewIndex() {
  97.             var tree = new AVLTree<int, int>();
  98.             int n = 10;
  99.             for (int i = 0; i < n; i++)
  100.                 tree.Add(i, i);
  101.             tree[n] = n;
  102.             Assert.True(tree.ContainsKey(n));
  103.             Assert.True(tree.ContainsValue(n));
  104.         }
  105.         [Fact]
  106.         public void KeyNullException() {
  107.             var tree = new AVLTree<string, int>();
  108.             Assert.Throws<ArgumentNullException>(() => tree[null] = 6);
  109.         }
  110.         [Fact]
  111.         public void SearchMinElement() {
  112.             var tree = new AVLTree<int, int>();
  113.             int n = 10;
  114.             for (int i = n; i < 5 * n; ++i)
  115.                 tree.Add(i, i);
  116.             for (int i = 0; i < n; i++)
  117.                 tree.Add(i, i);
  118.             for (int i = 5 * n; i < 10 * n; i++)
  119.                 tree.Add(i, i);
  120.  
  121.             Assert.True(tree.Min().Equals(new KeyValuePair<int, int>(0, 0)));
  122.         }
  123.         [Fact]
  124.         public void CheckRemoveLeaf()
  125.         {
  126.             var tree = new AVLTree<int, int>();
  127.             int n = 10;
  128.             for (int i = 0; i < n; i++)
  129.                 tree.Add(i, i);
  130.             int k = -10;
  131.             for (int i = k; i < 0; i++)
  132.                 tree.Add(i, i);
  133.             tree.Remove(n - 1);
  134.             Assert.True(!tree.ContainsValue(n - 1));
  135.         }
  136.         [Fact]
  137.         public void CheckRemoveStepByStep() {
  138.             var tree = new AVLTree<int, int>();
  139.             int n = 10;
  140.             for (int i = 0; i < n; i++)
  141.                 tree.Add(i, i);
  142.             for (int i = 0; i < n; i++)
  143.                 tree.Remove(i);
  144.             Assert.True(tree.Count == 0);
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement