|
1 | 1 | using NUnit.Framework;
|
2 | 2 | using Algorithms.MachineLearning;
|
3 |
| -using System; |
| 3 | +using System; |
4 | 4 |
|
5 | 5 | namespace Algorithms.Tests.MachineLearning;
|
6 | 6 |
|
@@ -118,16 +118,16 @@ public void BuildTree_ReturnsNodeWithSingleLabel_WhenAllLabelsZero()
|
118 | 118 | public void Entropy_ReturnsZero_WhenAllZeroOrAllOne()
|
119 | 119 | {
|
120 | 120 | var method = typeof(DecisionTree).GetMethod("Entropy", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
|
121 |
| - Assert.That(method!.Invoke(null, new [] { new int[] { 0, 0, 0 } }), Is.EqualTo(0d)); |
122 |
| - Assert.That(method!.Invoke(null, new [] { new int[] { 1, 1, 1 } }), Is.EqualTo(0d)); |
| 121 | + Assert.That(method!.Invoke(null, new[] { new int[] { 0, 0, 0 } }), Is.EqualTo(0d)); |
| 122 | + Assert.That(method!.Invoke(null, new[] { new int[] { 1, 1, 1 } }), Is.EqualTo(0d)); |
123 | 123 | }
|
124 | 124 |
|
125 | 125 | [Test]
|
126 | 126 | public void MostCommon_ReturnsCorrectLabel()
|
127 | 127 | {
|
128 | 128 | var method = typeof(DecisionTree).GetMethod("MostCommon", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
|
129 |
| - Assert.That(method!.Invoke(null, new [] { new int[] { 1, 0, 1, 1, 0, 0, 0 } }), Is.EqualTo(0)); |
130 |
| - Assert.That(method!.Invoke(null, new [] { new int[] { 1, 1, 1, 0 } }), Is.EqualTo(1)); |
| 129 | + Assert.That(method!.Invoke(null, new[] { new int[] { 1, 0, 1, 1, 0, 0, 0 } }), Is.EqualTo(0)); |
| 130 | + Assert.That(method!.Invoke(null, new[] { new int[] { 1, 1, 1, 0 } }), Is.EqualTo(1)); |
131 | 131 | }
|
132 | 132 |
|
133 | 133 | [Test]
|
@@ -175,4 +175,34 @@ public void BestFeature_SkipsEmptyIdxBranch()
|
175 | 175 | Assert.That(resultObj, Is.Not.Null);
|
176 | 176 | Assert.That((int)resultObj!, Is.EqualTo(0));
|
177 | 177 | }
|
| 178 | + |
| 179 | + [Test] |
| 180 | + public void BuildTree_MostCommonLabelBranch_IsCovered() |
| 181 | + { |
| 182 | + int[][] X = { new[] { 0 }, new[] { 1 } }; |
| 183 | + int[] y = { 0, 1 }; |
| 184 | + var tree = new DecisionTree(); |
| 185 | + tree.Fit(X, y); |
| 186 | + Assert.That(tree.Predict(new[] { 2 }), Is.EqualTo(0)); |
| 187 | + } |
| 188 | + |
| 189 | + [Test] |
| 190 | + public void BuildTree_ContinueBranch_IsCovered() |
| 191 | + { |
| 192 | + int[][] X = { new[] { 0 }, new[] { 1 } }; |
| 193 | + int[] y = { 0, 1 }; |
| 194 | + var method = typeof(DecisionTree).GetMethod("BuildTree", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); |
| 195 | + var features = new System.Collections.Generic.List<int> { 0 }; |
| 196 | + Assert.DoesNotThrow(() => method!.Invoke(null, new object[] { X, y, features })); |
| 197 | + } |
| 198 | + |
| 199 | + [Test] |
| 200 | + public void BestFeature_ContinueBranch_IsCovered() |
| 201 | + { |
| 202 | + int[][] X = { new[] { 0, 1 }, new[] { 1, 1 } }; |
| 203 | + int[] y = { 0, 1 }; |
| 204 | + var method = typeof(DecisionTree).GetMethod("BestFeature", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); |
| 205 | + var features = new System.Collections.Generic.List<int> { 0, 1 }; |
| 206 | + Assert.DoesNotThrow(() => method!.Invoke(null, new object[] { X, y, features })); |
| 207 | + } |
178 | 208 | }
|
0 commit comments