22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
44import static org .junit .jupiter .api .Assertions .assertThrows ;
5+ import static org .junit .jupiter .api .Assertions .assertTrue ;
56
67import java .util .ArrayList ;
8+ import java .util .Arrays ;
9+ import java .util .Collections ;
710import java .util .List ;
11+ import java .util .stream .Collectors ;
812
913import org .junit .jupiter .api .BeforeEach ;
1014import org .junit .jupiter .api .RepeatedTest ;
@@ -62,6 +66,27 @@ void setUp(){
6266 */
6367 }
6468
69+ @ Test
70+ void startingNodeIsInRangeWhenRandomCtorUsed () {
71+ int n = 32 ;
72+ CentroidDecomposition rnd = new CentroidDecomposition (n );
73+ int s = rnd .getStartingNode ();
74+ assertTrue (0 <= s && s < n , "starting node must be in [0, n)" );
75+ }
76+
77+ @ Test
78+ void invalidStartingNodeThrows () {
79+ assertThrows (IllegalArgumentException .class , () -> new CentroidDecomposition (10 , -1 ));
80+ assertThrows (IllegalArgumentException .class , () -> new CentroidDecomposition (10 , 10 ));
81+ }
82+
83+ @ Test
84+ void IllegalArgumentThrows (){
85+ assertThrows (IllegalArgumentException .class , () -> {
86+ new CentroidDecomposition (10 , 99 );
87+ });
88+ }
89+
6590 @ Test
6691 void testFindSubtreeSizes (){
6792 // int[] subtreeSizes = new int[n];
@@ -73,25 +98,117 @@ void testFindSubtreeSizes(){
7398 }
7499
75100 @ Test
76- void IllegalArgumentThrows (){
77- assertThrows (IllegalArgumentException .class , () -> {
78- new CentroidDecomposition (10 , 99 );
79- });
101+ void getCentroidChildrenReturnsDirectedChildrenOnly () {
102+ cd .build ();
103+
104+ // children of 0 in centroid tree should be [1, 11, 8] in any order
105+ List <Integer > children0 = cd .getCentroidChildren (0 ).stream ().sorted ().collect (Collectors .toList ());
106+ assertEquals (Arrays .asList (1 , 8 , 11 ), children0 );
107+
108+ // children of 11 should be [2, 12, 14]
109+ List <Integer > children11 = cd .getCentroidChildren (11 ).stream ().sorted ().collect (Collectors .toList ());
110+ assertEquals (Arrays .asList (2 , 12 , 14 ), children11 );
111+
112+ // leaves have no children
113+ assertTrue (cd .getCentroidChildren (4 ).isEmpty ());
114+ assertTrue (cd .getCentroidChildren (9 ).isEmpty ());
115+ assertTrue (cd .getCentroidChildren (15 ).isEmpty ());
116+ }
117+ @ Test
118+ void buildSetsExpectedParentsForKeyNodes () {
119+ cd .build ();
120+ // parent(11) = 0, parent(1) = 0, parent(8) = 0
121+ assertEquals (0 , cd .getParent (11 ));
122+ assertEquals (0 , cd .getParent (1 ));
123+ assertEquals (0 , cd .getParent (8 ));
124+
125+ assertEquals (1 , cd .getParent (4 ));
126+ assertEquals (1 , cd .getParent (5 ));
127+
128+ assertEquals (11 , cd .getParent (2 ));
129+ assertEquals (11 , cd .getParent (12 ));
130+ assertEquals (11 , cd .getParent (14 ));
131+
132+ assertEquals (2 , cd .getParent (6 ));
133+ assertEquals (2 , cd .getParent (7 ));
134+
135+ assertEquals (14 , cd .getParent (13 ));
136+ assertEquals (14 , cd .getParent (15 ));
137+
138+ assertEquals (8 , cd .getParent (3 ));
139+ assertEquals (8 , cd .getParent (9 ));
140+ assertEquals (8 , cd .getParent (10 ));
141+ }
142+
143+ @ Test
144+ void centroidAdjacencyFor8And11MatchesIgnoringOrder () {
145+ cd .build ();
146+ ArrayList <Integer >[] cg = cd .getCentroidTree ();
147+
148+ List <Integer > actual8 = new ArrayList <>(cg [8 ]);
149+ List <Integer > actual11 = new ArrayList <>(cg [11 ]);
150+
151+ List <Integer > expected8 = Arrays .asList (0 , 3 , 9 , 10 );
152+ List <Integer > expected11 = Arrays .asList (0 , 2 , 12 , 14 );
153+
154+ Collections .sort (actual8 );
155+ Collections .sort (actual11 );
156+ List <Integer > e8 = expected8 .stream ().sorted ().collect (Collectors .toList ());
157+ List <Integer > e11 = expected11 .stream ().sorted ().collect (Collectors .toList ());
158+
159+ assertEquals (e8 , actual8 );
160+ assertEquals (e11 , actual11 );
80161 }
81162
82163 @ Test
83- void testGetParent () {
164+ void resetClearsCentroidData () {
84165 cd .build ();
85- int three = 8 ;
86- int Twelve = 11 ;
87- int Five = 1 ;
88- int Eleven = 0 ;
166+ cd .reset ();
89167
90- assertEquals (cd .getParent (3 ), three );
91- assertEquals (cd .getParent (12 ), Twelve );
92- assertEquals (cd .getParent (5 ), Five );
93- assertEquals (cd .getParent (11 ), Eleven );
168+ ArrayList <Integer >[] cg = cd .getCentroidTree ();
169+ for (int i = 0 ; i < cg .length ; i ++) {
170+ assertTrue (cg [i ].isEmpty (), "centroid adjacency must be empty after reset" );
171+ assertEquals (-1 , cd .getParent (i ), "parent must be -1 after reset" );
172+ }
173+
174+ cd .build ();
175+ assertEquals (0 , cd .getParent (11 ));
176+ }
177+
178+ @ Test
179+ void buildingFromDifferentStartNodesProducesSameParents () {
180+ // build from 0
181+ CentroidDecomposition a = new CentroidDecomposition (16 , 0 );
182+ copyEdges (cd , a );
183+ a .build ();
184+
185+ // build from 7
186+ CentroidDecomposition b = new CentroidDecomposition (16 , 7 );
187+ copyEdges (cd , b );
188+ b .build ();
189+
190+ // compare parent arrays node by node
191+ for (int v = 0 ; v < 16 ; v ++) {
192+ assertEquals (a .getParent (v ), b .getParent (v ), "parent mismatch at node " + v );
193+ }
194+ }
94195
196+ private static void copyEdges (CentroidDecomposition from , CentroidDecomposition to ) {
197+ to .addEdgeTree (0 , 1 );
198+ to .addEdgeTree (0 , 2 );
199+ to .addEdgeTree (0 , 3 );
200+ to .addEdgeTree (1 , 4 );
201+ to .addEdgeTree (1 , 5 );
202+ to .addEdgeTree (2 , 6 );
203+ to .addEdgeTree (2 , 7 );
204+ to .addEdgeTree (3 , 8 );
205+ to .addEdgeTree (8 , 9 );
206+ to .addEdgeTree (8 , 10 );
207+ to .addEdgeTree (6 , 11 );
208+ to .addEdgeTree (11 , 12 );
209+ to .addEdgeTree (11 , 13 );
210+ to .addEdgeTree (13 , 14 );
211+ to .addEdgeTree (14 , 15 );
95212 }
96213
97214 @ RepeatedTest (100 )
0 commit comments