Skip to content

Commit

Permalink
test: 💍 add test cases, group 1, for topdown matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Jul 30, 2024
1 parent 5c300b7 commit 7e72785
Showing 1 changed file with 169 additions and 0 deletions.
169 changes: 169 additions & 0 deletions core/src/test/kotlin/matchers/TopDownMatcherTest1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package matchers

import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.parallel.Execution
import org.junit.jupiter.api.parallel.ExecutionMode
import tw.xcc.gumtree.helper.gumTree
import tw.xcc.gumtree.matchers.GreedyTopDownMatcher
import tw.xcc.gumtree.model.GumTree
import tw.xcc.gumtree.model.MappingStorage
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@Execution(ExecutionMode.CONCURRENT)
internal class TopDownMatcherTest1 {
private lateinit var topDownMatcher: GreedyTopDownMatcher
private lateinit var storage: MappingStorage

@BeforeTest
fun setUp() {
topDownMatcher = GreedyTopDownMatcher(minHeight = 0)
storage = MappingStorage()
}

@Test
fun `test match with simple tree 1`() {
val answerGrid = mutableListOf<GumTree>() to mutableListOf<GumTree>()

// Given
val tree1 =
gumTree("root") {
child("child1").also { answerGrid.first.add(it) }
child("child2").also { answerGrid.first.add(it) }
}
val tree2 =
gumTree("root") {
child("child1").also { answerGrid.second.add(it) }
child("child2").also { answerGrid.second.add(it) }
}

runBlocking {
// When
topDownMatcher.match(tree1, tree2, storage)

// Then
assertTrue(storage.has(tree1 to tree2))
assertTrue(storage.has(answerGrid.first[0] to answerGrid.second[0]))
assertTrue(storage.has(answerGrid.first[1] to answerGrid.second[1]))
}
}

@Test
fun `test match with simple tree 2`() {
val answerGrid = mutableListOf<GumTree>() to mutableListOf<GumTree>()

// Given
val tree1 =
gumTree("root") {
child("child1") {
child("child2").also { answerGrid.first.add(it) }
}.also { answerGrid.first.add(it) }
}
val tree2 =
gumTree("root") {
child("child1") {
child("child2").also { answerGrid.second.add(it) }
}.also { answerGrid.second.add(it) }
}

runBlocking {
// When
topDownMatcher.match(tree1, tree2, storage)

// Then
assertTrue(storage.has(tree1 to tree2))
assertTrue(storage.has(answerGrid.first[0] to answerGrid.second[0]))
assertTrue(storage.has(answerGrid.first[1] to answerGrid.second[1]))
}
}

@Test
fun `test match with simple tree 3, different tree`() {
val answerGrid = mutableListOf<GumTree>() to mutableListOf<GumTree>()

// Given
val tree1 =
gumTree("root") {
child("child1").also { answerGrid.first.add(it) }
child("child2").also { answerGrid.first.add(it) }
}
val tree2 =
gumTree("root") {
child("child1").also { answerGrid.second.add(it) }
child("child3").also { answerGrid.second.add(it) }
}

runBlocking {
// When
topDownMatcher.match(tree1, tree2, storage)

// Then
assertFalse(storage.has(tree1 to tree2))
assertTrue(storage.has(answerGrid.first[0] to answerGrid.second[0]))
assertFalse(storage.has(answerGrid.first[1] to answerGrid.second[1]))
}
}

@Test
fun `test match with simple tree 4, different tree`() {
val answerGrid = mutableListOf<GumTree>() to mutableListOf<GumTree>()

// Given
val tree1 =
gumTree("root") {
child("child1") {
child("child2").also { answerGrid.first.add(it) }
}.also { answerGrid.first.add(it) }
}
val tree2 =
gumTree("root") {
child("child2") {
child("child3").also { answerGrid.second.add(it) }
}.also { answerGrid.second.add(it) }
}

runBlocking {
// When
topDownMatcher.match(tree1, tree2, storage)

// Then
assertFalse(storage.has(tree1 to tree2))
assertFalse(storage.has(answerGrid.first[0] to answerGrid.second[0]))
assertFalse(storage.has(answerGrid.first[1] to answerGrid.second[1]))
}
}

@Test
fun `test match with simple tree 5, different structure`() {
val answerGrid = mutableListOf<GumTree>() to mutableListOf<GumTree>()

// Given
val tree1 =
gumTree("root") {
child("child1").also { answerGrid.first.add(it) }
child("child2").also { answerGrid.first.add(it) }
}
val tree2 =
gumTree("root") {
child("child1") {
child("child2").also {
answerGrid.second.add(it) // index 0
}
}.also {
answerGrid.second.add(it) // index 1
}
}

runBlocking {
// When
topDownMatcher.match(tree1, tree2, storage)

// Then
assertFalse(storage.has(tree1 to tree2))
assertFalse(storage.has(answerGrid.first[0] to answerGrid.second[1]))
assertTrue(storage.has(answerGrid.first[1] to answerGrid.second[0]))
}
}
}

0 comments on commit 7e72785

Please sign in to comment.