From 7e7278549beebe19749b414589e071cec4c73801 Mon Sep 17 00:00:00 2001 From: Xanonymous Date: Wed, 31 Jul 2024 00:16:17 +0100 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20test=20cases,=20gr?= =?UTF-8?q?oup=201,=20for=20topdown=20matcher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/matchers/TopDownMatcherTest1.kt | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 core/src/test/kotlin/matchers/TopDownMatcherTest1.kt diff --git a/core/src/test/kotlin/matchers/TopDownMatcherTest1.kt b/core/src/test/kotlin/matchers/TopDownMatcherTest1.kt new file mode 100644 index 0000000..9973996 --- /dev/null +++ b/core/src/test/kotlin/matchers/TopDownMatcherTest1.kt @@ -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() to mutableListOf() + + // 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() to mutableListOf() + + // 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() to mutableListOf() + + // 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() to mutableListOf() + + // 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() to mutableListOf() + + // 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])) + } + } +} \ No newline at end of file