From 800e835de133b087e384bc678f7658543b107213 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Fri, 28 Nov 2025 08:08:51 +0700 Subject: [PATCH] Added tests for tasks 101-2929 --- .../s0101_symmetric_tree/SolutionTest.kt | 30 +++++++++ .../s0198_house_robber/SolutionTest.kt | 30 +++++++++ .../MyQueueTest.kt | 64 +++++++++++++++++++ .../SolutionTest.kt | 28 ++++++++ .../SolutionTest.kt | 35 ++++++++++ 5 files changed, 187 insertions(+) diff --git a/src/test/kotlin/g0101_0200/s0101_symmetric_tree/SolutionTest.kt b/src/test/kotlin/g0101_0200/s0101_symmetric_tree/SolutionTest.kt index e041e1ef7..71c918bf7 100644 --- a/src/test/kotlin/g0101_0200/s0101_symmetric_tree/SolutionTest.kt +++ b/src/test/kotlin/g0101_0200/s0101_symmetric_tree/SolutionTest.kt @@ -1,6 +1,7 @@ package g0101_0200.s0101_symmetric_tree import com_github_leetcode.TreeNode +import com_github_leetcode.TreeUtils import org.hamcrest.CoreMatchers.equalTo import org.hamcrest.MatcherAssert.assertThat import org.junit.jupiter.api.Test @@ -25,4 +26,33 @@ internal class SolutionTest { fun isSymmetric4() { assertThat(Solution().isSymmetric(TreeNode.create(listOf(1, 2, 2, 3, 4, 4, 5))), equalTo(false)) } + + @Test + fun symmetricTree3() { + assertThat(Solution().isSymmetric(null), equalTo(true)) + } + + @Test + fun symmetricTree4() { + val root = TreeNode(1) + assertThat(Solution().isSymmetric(root), equalTo(true)) + } + + @Test + fun symmetricTree5() { + val root = TreeNode(1, TreeNode(2), null) + assertThat(Solution().isSymmetric(root), equalTo(false)) + } + + @Test + fun symmetricTree6() { + val root = TreeNode(1, null, TreeNode(2)) + assertThat(Solution().isSymmetric(root), equalTo(false)) + } + + @Test + fun symmetricTree7() { + val root = TreeUtils.constructBinaryTree(listOf(1, 2, 2, 3, 4, 5, 3)) + assertThat(Solution().isSymmetric(root), equalTo(false)) + } } diff --git a/src/test/kotlin/g0101_0200/s0198_house_robber/SolutionTest.kt b/src/test/kotlin/g0101_0200/s0198_house_robber/SolutionTest.kt index 5b471b801..b28f0cb0a 100644 --- a/src/test/kotlin/g0101_0200/s0198_house_robber/SolutionTest.kt +++ b/src/test/kotlin/g0101_0200/s0198_house_robber/SolutionTest.kt @@ -29,4 +29,34 @@ internal class SolutionTest { fun rob5() { assertThat(Solution().rob(intArrayOf(1, 2)), equalTo(2)) } + + @Test + fun rob6() { + assertThat(Solution().rob(intArrayOf(5)), equalTo(5)) + } + + @Test + fun rob7() { + assertThat(Solution().rob(intArrayOf(5, 10)), equalTo(10)) + } + + @Test + fun rob8() { + assertThat(Solution().rob(intArrayOf(2, 1, 1, 2)), equalTo(4)) + } + + @Test + fun rob9() { + assertThat(Solution().rob(intArrayOf(3, 3, 3, 3, 3)), equalTo(9)) + } + + @Test + fun rob10() { + assertThat(Solution().rob(intArrayOf(100, 1, 100, 1, 100)), equalTo(300)) + } + + @Test + fun rob11() { + assertThat(Solution().rob(intArrayOf()), equalTo(0)) + } } diff --git a/src/test/kotlin/g0201_0300/s0232_implement_queue_using_stacks/MyQueueTest.kt b/src/test/kotlin/g0201_0300/s0232_implement_queue_using_stacks/MyQueueTest.kt index 5c797f70f..b282edf78 100644 --- a/src/test/kotlin/g0201_0300/s0232_implement_queue_using_stacks/MyQueueTest.kt +++ b/src/test/kotlin/g0201_0300/s0232_implement_queue_using_stacks/MyQueueTest.kt @@ -14,4 +14,68 @@ internal class MyQueueTest { assertThat(myQueue.pop(), equalTo(1)) assertThat(myQueue.empty(), equalTo(false)) } + + @Test + fun queuePushPopPeekMultiple() { + val myQueue = MyQueue() + myQueue.push(10) + myQueue.push(20) + myQueue.push(30) + + assertThat(myQueue.peek(), equalTo(10)) + assertThat(myQueue.pop(), equalTo(10)) + assertThat(myQueue.peek(), equalTo(20)) + assertThat(myQueue.pop(), equalTo(20)) + assertThat(myQueue.peek(), equalTo(30)) + assertThat(myQueue.pop(), equalTo(30)) + assertThat(myQueue.empty(), equalTo(true)) + } + + @Test + fun queueEmptyInitially() { + val myQueue = MyQueue() + assertThat(myQueue.empty(), equalTo(true)) + } + + @Test + fun queuePushAfterPopAll() { + val myQueue = MyQueue() + myQueue.push(1) + myQueue.push(2) + + assertThat(myQueue.pop(), equalTo(1)) + assertThat(myQueue.pop(), equalTo(2)) + assertThat(myQueue.empty(), equalTo(true)) + + myQueue.push(3) + assertThat(myQueue.peek(), equalTo(3)) + assertThat(myQueue.empty(), equalTo(false)) + } + + @Test + fun queuePeekDoesNotRemove() { + val myQueue = MyQueue() + myQueue.push(5) + myQueue.push(6) + + assertThat(myQueue.peek(), equalTo(5)) + assertThat(myQueue.peek(), equalTo(5)) + assertThat(myQueue.pop(), equalTo(5)) + assertThat(myQueue.peek(), equalTo(6)) + } + + @Test + fun pushAfterPopTriggersRightToLeft() { + val myQueue = MyQueue() + myQueue.push(1) + myQueue.push(2) + + assertThat(myQueue.pop(), equalTo(1)) + + myQueue.push(3) + + assertThat(myQueue.pop(), equalTo(2)) + assertThat(myQueue.pop(), equalTo(3)) + assertThat(myQueue.empty(), equalTo(true)) + } } diff --git a/src/test/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/SolutionTest.kt b/src/test/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/SolutionTest.kt index 257ce8ed8..81fe2609f 100644 --- a/src/test/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/SolutionTest.kt +++ b/src/test/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/SolutionTest.kt @@ -57,4 +57,32 @@ internal class SolutionTest { equalTo(4), ) } + + @Test + fun lowestCommonAncestor5() { + val root = TreeNode(1) + val result = Solution().lowestCommonAncestor(root, TreeNode(1), TreeNode(1)) + assertThat(result!!.`val`, equalTo(1)) + } + + @Test + fun lowestCommonAncestor6() { + val root = TreeNode(3, TreeNode(1), TreeNode(4)) + val result = Solution().lowestCommonAncestor(root, TreeNode(1), TreeNode(1)) + assertThat(result!!.`val`, equalTo(1)) + } + + @Test + fun lowestCommonAncestor7() { + val root = TreeNode(3, TreeNode(1), TreeNode(4)) + val result = Solution().lowestCommonAncestor(root, TreeNode(4), TreeNode(4)) + assertThat(result!!.`val`, equalTo(4)) + } + + @Test + fun lowestCommonAncestor8() { + val root = TreeNode(5, TreeNode(3), TreeNode(8)) + val result = Solution().lowestCommonAncestor(root, TreeNode(3), TreeNode(8)) + assertThat(result!!.`val`, equalTo(5)) + } } diff --git a/src/test/kotlin/g2901_3000/s2929_distribute_candies_among_children_ii/SolutionTest.kt b/src/test/kotlin/g2901_3000/s2929_distribute_candies_among_children_ii/SolutionTest.kt index 3518e2ff0..39a75dcf8 100644 --- a/src/test/kotlin/g2901_3000/s2929_distribute_candies_among_children_ii/SolutionTest.kt +++ b/src/test/kotlin/g2901_3000/s2929_distribute_candies_among_children_ii/SolutionTest.kt @@ -14,4 +14,39 @@ internal class SolutionTest { fun distributeCandies2() { assertThat(Solution().distributeCandies(3, 3), equalTo(10L)) } + + @Test + fun distributeCandies3() { + assertThat(Solution().distributeCandies(2, 5), equalTo(6L)) + } + + @Test + fun distributeCandies4() { + assertThat(Solution().distributeCandies(4, 4), equalTo(15L)) + } + + @Test + fun distributeCandies5() { + assertThat(Solution().distributeCandies(7, 5), equalTo(27L)) + } + + @Test + fun distributeCandies6() { + assertThat(Solution().distributeCandies(12, 5), equalTo(10L)) + } + + @Test + fun distributeCandies7() { + assertThat(Solution().distributeCandies(20, 5), equalTo(0L)) + } + + @Test + fun distributeCandies8() { + assertThat(Solution().distributeCandies(10, 5), equalTo(21L)) + } + + @Test + fun distributeCandies9() { + assertThat(Solution().distributeCandies(15, 5), equalTo(1L)) + } }