Files
Tree-Data-Structure/src/commonTest/kotlin/com.github.adriankuta/datastructure.tree/TreeNodeUtilitiesTest.kt
Adrian Kuta 69d19f89e3 feat!: v4.0 breaking API cleanup + explicitApi
BREAKING changes to the core:
- treeIterator is now a read-only `val`; added `iterator(order)` and use `asSequence(order)`.
- removeChild() only removes a direct child of the receiver; added `detach()` to unhook a node.
- addChild() rejects re-parenting and cycles (throws TreeNodeException); detach() first to move.
- clear() no longer nulls the receiver's own parent; only removes descendants.
- path() returns List<TreeNode<T>>? (null) instead of throwing.

Also:
- Enable strict explicitApi() across core + both modules; add explicit `public` modifiers.
- Update tests for the new contracts + add TreeNodeV4Test; refresh .api baselines.
- README + CHANGELOG (with migration notes); bump version to 4.0.0.

47 JVM tests green.
2026-06-07 18:47:40 +02:00

54 lines
1.2 KiB
Kotlin

package com.github.adriankuta.datastructure.tree
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertNull
class TreeNodeUtilitiesTest {
private val root = TreeNode(1)
private val a = TreeNode(2)
private val b = TreeNode(3)
init {
root.addChild(a)
a.addChild(b)
}
@Test
fun nodeCountCountsDescendantsExcludingRoot() {
assertEquals(0, TreeNode("solo").nodeCount())
assertEquals(2, root.nodeCount())
}
@Test
fun heightIsLongestEdgePathToLeaf() {
assertEquals(0, TreeNode("solo").height())
assertEquals(2, root.height())
assertEquals(1, a.height())
}
@Test
fun depthIsDistanceToRoot() {
assertEquals(0, root.depth())
assertEquals(1, a.depth())
assertEquals(2, b.depth())
}
@Test
fun pathReturnsDescendantToReceiverChain() {
assertContentEquals(listOf(b, a, root), root.path(b))
}
@Test
fun pathReturnsNullWhenNotADescendant() {
assertNull(root.path(TreeNode(99)))
}
@Test
fun pathReturnsNullWhenDescendantIsRootItself() {
assertNull(root.path(root))
}
}