Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BinTreeLib;
- namespace TestProject1 {
- public class UnitTest1 {
- [Fact]
- public void CountIncreaseAfterAdding() {
- var tree = new AVLTree<int, int>();
- var a = new[] { 69, 153, 74, 34, 75, 3, 6, 1, 7, 4, 54, 25, 2 };
- int n = a.Length;
- for (int i = 0; i < n; i++)
- tree.Add(a[i], i);
- Assert.Equal(n, tree.Count);
- }
- [Fact]
- public void ExistingKeyException() {
- var tree = new AVLTree<int, int>();
- int n = 10;
- for (int i = 0; i < n; i++)
- tree.Add(i, i);
- Assert.Throws<ArgumentException>(() => tree.Add(n - 1, n - 1));
- }
- [Fact]
- public void MinException() {
- var tree = new AVLTree<int, int>();
- Assert.Throws<InvalidOperationException>(() => tree.Min());
- }
- [Fact]
- public void RemoveUnexistedElement() {
- var tree = new AVLTree<int, int>();
- int n = 10;
- for (int i = 0; i < n; i++)
- tree.Add(i, i);
- Assert.Throws<ArgumentNullException>(() => tree.Remove(2 * n));
- }
- [Fact]
- public void ItemsExistAfterAdding() {
- var tree = new AVLTree<int, int>();
- var a = new[] { 69, 153, 74, 34, 75, 3, 6, 1, 7, 4, 54, 25, 2 };
- int n = a.Length;
- for (int i = 0; i < n; i++)
- tree.Add(a[i], i);
- Assert.Equal(n, tree.Count);
- Array.Sort(a);
- int j = 0;
- foreach (var pair in tree)
- Assert.Equal(a[j++], pair.Key);
- }
- [Fact]
- public void ExistingElementContains() {
- var tree = new AVLTree<int, int>();
- var arrayInts = new[] { 69, 153, 74, 34, 75, 3, 6, 1, 7, 4, 54, 25, 2 };
- foreach (var number in arrayInts)
- tree.Add(number, 0);
- foreach (var number in arrayInts)
- Assert.True(tree.ContainsKey(number));
- }
- [Fact]
- public void UnexistedElementContains() {
- var tree = new AVLTree<int, int>();
- var a = new[] { 69, 153, 74, 34, 75, 3, 6, 1, 7, 4, 54, 25, 2 };
- for (int i = 0; i < a.Length; i++)
- tree.Add(a[i], 0);
- Assert.False(tree.ContainsKey(37));
- }
- [Fact]
- public void DictionaryConstructor() {
- Dictionary<string, string> dict = new Dictionary<string, string> () {
- { "abc", "def" },
- { "ghj", "hlm" },
- { "nop", "qrs" }
- };
- AVLTree<string, string> avl = new AVLTree<string, string>(dict, StringComparer.CurrentCultureIgnoreCase);
- Assert.True(dict.Count == avl.Count);
- foreach (var pair in dict) {
- Assert.True(avl.ContainsKey(pair.Key));
- Assert.True(avl.ContainsValue(pair.Value));
- }
- }
- [Fact]
- public void UnexistedKeyException() {
- var tree = new AVLTree<int, int>();
- int n = 10;
- for (int i = 0; i < n; i++)
- tree.Add(i, i);
- Assert.Throws<KeyNotFoundException>(() => tree[n + 1]);
- }
- [Fact]
- public void AddItemToNewIndex() {
- var tree = new AVLTree<int, int>();
- int n = 10;
- for (int i = 0; i < n; i++)
- tree.Add(i, i);
- tree[n] = n;
- Assert.True(tree.ContainsKey(n));
- Assert.True(tree.ContainsValue(n));
- }
- [Fact]
- public void KeyNullException() {
- var tree = new AVLTree<string, int>();
- Assert.Throws<ArgumentNullException>(() => tree[null] = 6);
- }
- [Fact]
- public void SearchMinElement() {
- var tree = new AVLTree<int, int>();
- int n = 10;
- for (int i = n; i < 5 * n; ++i)
- tree.Add(i, i);
- for (int i = 0; i < n; i++)
- tree.Add(i, i);
- for (int i = 5 * n; i < 10 * n; i++)
- tree.Add(i, i);
- Assert.True(tree.Min().Equals(new KeyValuePair<int, int>(0, 0)));
- }
- [Fact]
- public void CheckRemoveLeaf()
- {
- var tree = new AVLTree<int, int>();
- int n = 10;
- for (int i = 0; i < n; i++)
- tree.Add(i, i);
- int k = -10;
- for (int i = k; i < 0; i++)
- tree.Add(i, i);
- tree.Remove(n - 1);
- Assert.True(!tree.ContainsValue(n - 1));
- }
- [Fact]
- public void CheckRemoveStepByStep() {
- var tree = new AVLTree<int, int>();
- int n = 10;
- for (int i = 0; i < n; i++)
- tree.Add(i, i);
- for (int i = 0; i < n; i++)
- tree.Remove(i);
- Assert.True(tree.Count == 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement