|
| 1 | +using System; |
| 2 | +using DataStructures.ScapegoatTree; |
| 3 | +using NUnit.Framework; |
| 4 | + |
| 5 | +namespace DataStructures.Tests.ScapegoatTree |
| 6 | +{ |
| 7 | + [TestFixture] |
| 8 | + public class ScapegoatTreeNodeTests |
| 9 | + { |
| 10 | + [Test] |
| 11 | + [TestCase(2,1)] |
| 12 | + [TestCase("B", "A")] |
| 13 | + public void RightSetter_OtherKeyPrecedesRightKey_ThrowsException<TKey>(TKey a, TKey b) |
| 14 | + where TKey : IComparable |
| 15 | + { |
| 16 | + var instance = new Node<TKey>(a); |
| 17 | + var other = new Node<TKey>(b); |
| 18 | + |
| 19 | + Assert.Throws<ArgumentException>(() => instance.Right = other); |
| 20 | + } |
| 21 | + |
| 22 | + [Test] |
| 23 | + [TestCase(1,2)] |
| 24 | + [TestCase("A","B")] |
| 25 | + public void RightSetter_OtherKeyFollowsRightKey_AddsChild<TKey>(TKey a, TKey b) |
| 26 | + where TKey : IComparable |
| 27 | + { |
| 28 | + var instance = new Node<TKey>(a); |
| 29 | + var other = new Node<TKey>(b); |
| 30 | + |
| 31 | + Assert.DoesNotThrow(() => instance.Right = other); |
| 32 | + } |
| 33 | + |
| 34 | + [Test] |
| 35 | + [TestCase(1,2)] |
| 36 | + [TestCase("A","B")] |
| 37 | + public void LeftSetter_OtherKeyFollowsLeftKey_ThrowsException<TKey>(TKey a, TKey b) |
| 38 | + where TKey : IComparable |
| 39 | + { |
| 40 | + var instance = new Node<TKey>(a); |
| 41 | + var other = new Node<TKey>(b); |
| 42 | + |
| 43 | + Assert.Throws<ArgumentException>(() => instance.Left = other); |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + [TestCase(2,1)] |
| 48 | + [TestCase("B", "A")] |
| 49 | + public void LeftSetter_OtherKeyPrecedesLeftKey_AddsChild<TKey>(TKey a, TKey b) |
| 50 | + where TKey : IComparable |
| 51 | + { |
| 52 | + var instance = new Node<TKey>(a); |
| 53 | + var other = new Node<TKey>(b); |
| 54 | + |
| 55 | + Assert.DoesNotThrow(() => instance.Left = other); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + [TestCase(1,2)] |
| 60 | + [TestCase("A","B")] |
| 61 | + public void CompareTo_InstanceKeyPrecedesOtherKey_ReturnsMinusOne<TKey>(TKey a, TKey b) |
| 62 | + where TKey : IComparable |
| 63 | + { |
| 64 | + var instance = new Node<TKey>(a); |
| 65 | + var other = new Node<TKey>(b); |
| 66 | + |
| 67 | + var result = instance.Key.CompareTo(other.Key); |
| 68 | + |
| 69 | + Assert.AreEqual(result, -1); |
| 70 | + } |
| 71 | + |
| 72 | + [Test] |
| 73 | + [TestCase(2, 1)] |
| 74 | + [TestCase("B","A")] |
| 75 | + public void CompareTo_InstanceKeyFollowsOtherKey_ReturnsOne<TKey>(TKey a, TKey b) |
| 76 | + where TKey : IComparable |
| 77 | + { |
| 78 | + var instance = new Node<TKey>(a); |
| 79 | + var other = new Node<TKey>(b); |
| 80 | + |
| 81 | + var result = instance.Key.CompareTo(other.Key); |
| 82 | + |
| 83 | + Assert.AreEqual(result, 1); |
| 84 | + } |
| 85 | + |
| 86 | + [Test] |
| 87 | + [TestCase(1, 1)] |
| 88 | + [TestCase("A","A")] |
| 89 | + public void CompareTo_InstanceKeyEqualsOtherKey_ReturnsZero<TKey>(TKey a, TKey b) |
| 90 | + where TKey : IComparable |
| 91 | + { |
| 92 | + var instance = new Node<TKey>(a); |
| 93 | + var other = new Node<TKey>(b); |
| 94 | + |
| 95 | + var result = instance.Key.CompareTo(other.Key); |
| 96 | + |
| 97 | + Assert.AreEqual(result, 0); |
| 98 | + } |
| 99 | + |
| 100 | + [Test] |
| 101 | + public void GetSize_NodeHasNoChildren_ReturnsOne() |
| 102 | + { |
| 103 | + var node = new Node<int>(1); |
| 104 | + |
| 105 | + Assert.AreEqual(node.GetSize(), 1); |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public void GetSize_NodeHasChildren_ReturnsCorrectSize() |
| 110 | + { |
| 111 | + var node = new Node<int>(1, new Node<int>(2), new Node<int>(0)); |
| 112 | + |
| 113 | + Assert.AreEqual(node.GetSize(), 3); |
| 114 | + } |
| 115 | + |
| 116 | + [Test] |
| 117 | + public void GetSmallestKeyNode_NodeHasNoLeftChildren_ReturnsNode() |
| 118 | + { |
| 119 | + var node = new Node<int>(1); |
| 120 | + |
| 121 | + Assert.AreEqual(node.GetSmallestKeyNode(), node); |
| 122 | + } |
| 123 | + |
| 124 | + [Test] |
| 125 | + public void GetSmallestKeyNode_NodeHasSmallestChild_ReturnsChild() |
| 126 | + { |
| 127 | + var node = new Node<int>(1); |
| 128 | + var smaller = new Node<int>(0); |
| 129 | + var smallest = new Node<int>(-1); |
| 130 | + node.Left = smaller; |
| 131 | + smaller.Left = smallest; |
| 132 | + |
| 133 | + Assert.AreEqual(node.GetSmallestKeyNode(), smallest); |
| 134 | + } |
| 135 | + |
| 136 | + [Test] |
| 137 | + public void GetLargestKeyNode_NodeHasNoRightChildren_ReturnsNode() |
| 138 | + { |
| 139 | + var node = new Node<int>(1); |
| 140 | + |
| 141 | + Assert.AreEqual(node.GetLargestKeyNode(), node); |
| 142 | + } |
| 143 | + |
| 144 | + [Test] |
| 145 | + public void GetLargestKeyNode_NodeHasLargestChild_ReturnsChild() |
| 146 | + { |
| 147 | + var node = new Node<int>(1); |
| 148 | + var larger = new Node<int>(2); |
| 149 | + var largest = new Node<int>(3); |
| 150 | + node.Right = larger; |
| 151 | + larger.Right = largest; |
| 152 | + |
| 153 | + Assert.AreEqual(node.GetLargestKeyNode(), largest); |
| 154 | + } |
| 155 | + |
| 156 | + [Test] |
| 157 | + public void IsAlphaWeightBalanced_TreeIsUnbalanced_ReturnsFalse() |
| 158 | + { |
| 159 | + var root = new Node<int>(0); |
| 160 | + var a = new Node<int>(-1); |
| 161 | + var b = new Node<int>(-2); |
| 162 | + var c = new Node<int>(-3); |
| 163 | + var d = new Node<int>(1); |
| 164 | + |
| 165 | + root.Left = a; |
| 166 | + a.Left = b; |
| 167 | + b.Left = c; |
| 168 | + root.Right = d; |
| 169 | + |
| 170 | + Assert.IsFalse(root.IsAlphaWeightBalanced(0.5)); |
| 171 | + } |
| 172 | + |
| 173 | + [Test] |
| 174 | + public void IsAlphaWeightBalanced_TreeIsBalanced_ReturnsTrue() |
| 175 | + { |
| 176 | + var root = new Node<int>(0); |
| 177 | + var a = new Node<int>(-1); |
| 178 | + var b = new Node<int>(-2); |
| 179 | + var d = new Node<int>(1); |
| 180 | + |
| 181 | + root.Left = a; |
| 182 | + a.Left = b; |
| 183 | + root.Right = d; |
| 184 | + |
| 185 | + Assert.IsTrue(root.IsAlphaWeightBalanced(0.5)); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments