Skip to content

Commit

Permalink
can find pedigree under all basic conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
vextorspace committed Jul 15, 2024
1 parent c69878d commit e258dc6
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 11 deletions.
22 changes: 11 additions & 11 deletions composeApp/src/commonMain/kotlin/model/visitors/PedigreeFinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ package model.visitors
import model.Item

class PedigreeFinder(val rootItem: Item) {
private val familyTree = mutableListOf<Item>()

fun findPedigree(item: Item): List<Item> {
return if (item == rootItem) {
listOf(item)
} else if(rootItem.subItems.isNotEmpty()) {
familyTree.add(rootItem)
rootItem.subItems.map {
PedigreeFinder(it).findPedigree(item)
}.filter {
it.last() == item
}.flatten()
.also {
familyTree.addAll(it)
}
familyTree
listOf(rootItem) + findChildToItem(item)
} else {
emptyList()
}
}

fun findChildToItem(item: Item): List<Item> {
return rootItem.subItems.map {
PedigreeFinder(it).findPedigree(item)
}.filter {
lastItemIsItem(it, item)
}.flatten()
}

private fun lastItemIsItem(it: List<Item>, item: Item) = it.isNotEmpty() && it.last() == item
}
101 changes: 101 additions & 0 deletions composeApp/src/commonTest/kotlin/model/visitors/PedigreeFinderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,105 @@ class PedigreeFinderTest {
// Then
pedigree.shouldContainExactly(root, child)
}

@Test
fun `looking for pedigree works with multiple children`() {
// Given
val root = Item("Root")
val child1 = Item("Child1")
val child2 = Item("Child2")
root.add(child1)
root.add(child2)

val finder = PedigreeFinder(root)

// When
val pedigree = finder.findPedigree(child2)

// Then
pedigree.shouldContainExactly(root, child2)
}

@Test
fun `looking for pedigree works with multiple generations`() {
// Given
val root = Item("Root")
val child1 = Item("Child1")
val child2 = Item("Child2")
val grandchild = Item("Grandchild")
root.add(child1)
root.add(child2)
child2.add(grandchild)

val finder = PedigreeFinder(root)

// When
val pedigree = finder.findPedigree(grandchild)

// Then
pedigree.shouldContainExactly(root, child2, grandchild)
}

@Test
fun `looking for pedigree works with multiple generations and multiple children`() {
// Given
val root = Item("Root")
val child1 = Item("Child1")
val child2 = Item("Child2")
val grandchild1 = Item("Grandchild1")
val grandchild2 = Item("Grandchild2")
root.add(child1)
root.add(child2)
child2.add(grandchild1)
child2.add(grandchild2)

val finder = PedigreeFinder(root)

// When
val pedigree = finder.findPedigree(child1)

// Then
pedigree.shouldContainExactly(root, child1)
}

@Test
fun `findChildToItem works one level down`() {
// Given
val root = Item("Root")
val child = Item("Child")
val grandChild = Item("Grandchild")
root.add(child)
child.add(grandChild)

val finder = PedigreeFinder(root)

// When
val pedigree = finder.findChildToItem(grandChild)

// Then
pedigree.shouldContainExactly(child, grandChild)
}

@Test
fun `findChildToItem returns empty list if no path`() {
// Given
val root = Item("Root")
val child = Item("Child")
val child2 = Item("Child2")
val grandchild2 = Item("Grandchild2")
val grandChild = Item("Grandchild")
root.add(child)
root.add(child2)
child.add(grandChild)
child2.add(grandchild2)

val finder = PedigreeFinder(child)

// When
val pedigree = finder.findChildToItem(grandchild2)

// Then
pedigree.shouldBeEmpty()

}
}

0 comments on commit e258dc6

Please sign in to comment.