@@ -80,4 +80,48 @@ public void Predict_FallbacksToZeroForUnseenValue()
80
80
// Value 2 is unseen in feature 0
81
81
Assert . That ( tree . Predict ( new [ ] { 2 , 0 } ) , Is . EqualTo ( 0 ) ) ;
82
82
}
83
+
84
+ [ Test ]
85
+ public void BuildTree_ReturnsNodeWithMostCommonLabel_WhenNoFeaturesLeft ( )
86
+ {
87
+ int [ ] [ ] X = { new [ ] { 0 } , new [ ] { 1 } , new [ ] { 2 } } ;
88
+ int [ ] y = { 1 , 0 , 1 } ;
89
+ var tree = new DecisionTree ( ) ;
90
+ tree . Fit ( X , y ) ;
91
+ // All features used, fallback to most common label (0)
92
+ Assert . That ( tree . Predict ( new [ ] { 3 } ) , Is . EqualTo ( 0 ) ) ;
93
+ }
94
+
95
+ [ Test ]
96
+ public void BuildTree_ReturnsNodeWithSingleLabel_WhenAllLabelsSame ( )
97
+ {
98
+ int [ ] [ ] X = { new [ ] { 0 } , new [ ] { 1 } , new [ ] { 2 } } ;
99
+ int [ ] y = { 1 , 1 , 1 } ;
100
+ var tree = new DecisionTree ( ) ;
101
+ tree . Fit ( X , y ) ;
102
+ Assert . That ( tree . Predict ( new [ ] { 0 } ) , Is . EqualTo ( 1 ) ) ;
103
+ Assert . That ( tree . Predict ( new [ ] { 1 } ) , Is . EqualTo ( 1 ) ) ;
104
+ Assert . That ( tree . Predict ( new [ ] { 2 } ) , Is . EqualTo ( 1 ) ) ;
105
+ }
106
+
107
+ [ Test ]
108
+ public void Entropy_ReturnsZero_WhenEmptyLabels ( )
109
+ {
110
+ // Use reflection to call private static Entropy
111
+ var method = typeof ( DecisionTree ) . GetMethod ( "Entropy" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Static ) ;
112
+ Assert . That ( method ! . Invoke ( null , new object [ ] { Array . Empty < int > ( ) } ) , Is . EqualTo ( 0d ) ) ;
113
+ }
114
+
115
+ [ Test ]
116
+ public void BestFeature_SkipsEmptyIdxBranch ( )
117
+ {
118
+ // Feature 1 has value 2 which is never present, triggers idx.Length == 0 branch
119
+ int [ ] [ ] X = { new [ ] { 0 , 1 } , new [ ] { 1 , 1 } } ;
120
+ int [ ] y = { 0 , 1 } ;
121
+ var method = typeof ( DecisionTree ) . GetMethod ( "BestFeature" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Static ) ;
122
+ var features = new System . Collections . Generic . List < int > { 0 , 1 } ;
123
+ var resultObj = method ! . Invoke ( null , new object [ ] { X , y , features } ) ;
124
+ Assert . That ( resultObj , Is . Not . Null ) ;
125
+ Assert . That ( ( int ) resultObj ! , Is . EqualTo ( 0 ) ) ;
126
+ }
83
127
}
0 commit comments