40 files changed

+847
-902
lines changed
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ repositories {
1111

1212
dependencies {
1313
testImplementation(kotlin("test"))
14+
testImplementation("org.junit.jupiter:junit-jupiter-params:5.12.2")
1415
}
1516

1617
tasks.test {
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,33 @@
11
package com.schmoczer.leetcode._0001
22

33
import org.junit.jupiter.api.BeforeEach
4-
import kotlin.test.Test
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
57
import kotlin.test.assertContentEquals
68

79
class TwoSumTest {
10+
private companion object {
11+
@JvmStatic
12+
fun twoSumInputs() = listOf(
13+
Arguments.of(intArrayOf(2, 7, 11, 15), 9, intArrayOf(0, 1)),
14+
Arguments.of(intArrayOf(3, 2, 4), 6, intArrayOf(1, 2)),
15+
Arguments.of(intArrayOf(3, 3), 6, intArrayOf(0, 1)),
16+
)
17+
}
18+
819
private lateinit var sut: TwoSum
920

1021
@BeforeEach
1122
fun setUp() {
1223
sut = TwoSum()
1324
}
1425

15-
@Test
16-
fun `first and second index of 2,7,11,15 lead for target of 9`() {
17-
val input = intArrayOf(2, 7, 11, 15)
18-
val expected = intArrayOf(0, 1)
19-
20-
assertContentEquals(expected, sut.twoSum(input, 9))
21-
}
22-
23-
@Test
24-
fun `second and third index of 3,2,4 for target of 6`() {
25-
val input = intArrayOf(3, 2, 4)
26-
val expected = intArrayOf(1, 2)
27-
28-
assertContentEquals(expected, sut.twoSum(input, 6))
29-
}
30-
31-
@Test
32-
fun `first and second index of 3,3 for target of 6`() {
33-
val input = intArrayOf(3, 3)
34-
val expected = intArrayOf(0, 1)
26+
@ParameterizedTest(name = "two sum for input {0} with target {1} is {2}")
27+
@MethodSource("twoSumInputs")
28+
fun `returns the correct indices for two sum`(input: IntArray, target: Int, expected: IntArray) {
29+
val result = sut.twoSum(input, target)
3530

36-
assertContentEquals(expected, sut.twoSum(input, 6))
31+
assertContentEquals(expected, result)
3732
}
3833
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
package com.schmoczer.leetcode._0005
22

33
import org.junit.jupiter.api.BeforeEach
4-
import kotlin.test.Test
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
57
import kotlin.test.assertEquals
68

79
class LongestPalindromicSubstringTest {
10+
private companion object {
11+
@JvmStatic
12+
fun palindromeInputs() = listOf(
13+
Arguments.of("babad", "bab"),
14+
Arguments.of("cbbd", "bb"),
15+
Arguments.of("a", "a"),
16+
Arguments.of(
17+
"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
18+
"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
19+
),
20+
)
21+
}
22+
823
private lateinit var sut: LongestPalindromicSubstring
924

1025
@BeforeEach
1126
fun setUp() {
1227
sut = LongestPalindromicSubstring()
1328
}
1429

15-
@Test
16-
fun `bab is a palindrom babad`() {
17-
assertEquals("bab", sut.longestPalindrome("babad"))
18-
}
19-
20-
@Test
21-
fun `bb is a palindrome in cbbd`() {
22-
assertEquals("bb", sut.longestPalindrome("cbbd"))
23-
}
24-
25-
@Test
26-
fun `a is a palindrome in a`() {
27-
assertEquals("a", sut.longestPalindrome("a"))
28-
}
30+
@ParameterizedTest(name = "longest palindrome in \"{0}\" is \"{1}\"")
31+
@MethodSource("palindromeInputs")
32+
fun `returns the correct longest palindromic substring`(input: String, expected: String) {
33+
val result = sut.longestPalindrome(input)
2934

30-
@Test
31-
fun `long String containing only the same letter`() {
32-
val input =
33-
"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
34-
assertEquals(input, sut.longestPalindrome(input))
35+
assertEquals(expected, result)
3536
}
3637
}
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,42 @@
11
package com.schmoczer.leetcode._0008
22

33
import org.junit.jupiter.api.BeforeEach
4-
import kotlin.test.Test
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
57
import kotlin.test.assertEquals
68

79
class StringToIntegerTests {
10+
private companion object {
11+
@JvmStatic
12+
fun stringToIntegerInputs() = listOf(
13+
Arguments.of("42", 42),
14+
Arguments.of(" -042", -42),
15+
Arguments.of("1337c0d3", 1337),
16+
Arguments.of("0-1", 0),
17+
Arguments.of("words and 987", 0),
18+
Arguments.of("-2147483648", Int.MIN_VALUE),
19+
Arguments.of("2147483647", Int.MAX_VALUE),
20+
Arguments.of("-91283472332", Int.MIN_VALUE),
21+
Arguments.of("-2147483649", Int.MIN_VALUE),
22+
Arguments.of("2147483648", Int.MAX_VALUE),
23+
Arguments.of("", 0),
24+
Arguments.of(" ", 0),
25+
)
26+
}
27+
828
private lateinit var sut: StringToInteger
929

1030
@BeforeEach
1131
fun setUp() {
1232
sut = StringToInteger()
1333
}
1434

15-
@Test
16-
fun `"42" as String is 42 as Int`() {
17-
assertEquals(42, sut.myAtoi("42"))
18-
}
19-
20-
@Test
21-
fun `" -042" as String is -42 as Int`() {
22-
assertEquals(-42, sut.myAtoi(" -042"))
23-
}
24-
25-
@Test
26-
fun `"1337c0d3" as String is 1337 as Int`() {
27-
assertEquals(1337, sut.myAtoi("1337c0d3"))
28-
}
29-
30-
@Test
31-
fun `"0-1" as String is 0 as Int`() {
32-
assertEquals(0, sut.myAtoi("0-1"))
33-
}
34-
35-
@Test
36-
fun `"words and 987" as String is 0 as Int`() {
37-
assertEquals(0, sut.myAtoi("words and 987"))
38-
}
39-
40-
@Test
41-
fun `"-2147483648" as String is -2147483648`() {
42-
assertEquals(Int.MIN_VALUE, sut.myAtoi("-2147483648"))
43-
}
44-
45-
@Test
46-
fun `"2147483647" as String is 2147483647`() {
47-
assertEquals(Int.MAX_VALUE, sut.myAtoi("2147483647"))
48-
}
49-
50-
@Test
51-
fun `"-91283472332" as String is -2147483648 because it is lower than Int MIN_VALUE`() {
52-
assertEquals(Int.MIN_VALUE, sut.myAtoi("-91283472332"))
53-
}
54-
55-
@Test
56-
fun `"-2147483649" as String is -2147483648 because it is lower than Int MIN_VALUE`() {
57-
assertEquals(Int.MIN_VALUE, sut.myAtoi("-2147483649"))
58-
}
59-
60-
@Test
61-
fun `"2147483648" as String is 2147483647 because it is higher than Int MAX_VALUE`() {
62-
assertEquals(Int.MAX_VALUE, sut.myAtoi("2147483648"))
63-
}
64-
65-
@Test
66-
fun `empty String is 0`() {
67-
assertEquals(0, sut.myAtoi(""))
68-
}
35+
@ParameterizedTest(name = "myAtoi(\"{0}\") = {1}")
36+
@MethodSource("stringToIntegerInputs")
37+
fun `converts string to integer correctly`(input: String, expected: Int) {
38+
val result = sut.myAtoi(input)
6939

70-
@Test
71-
fun `multiple whitespaces are 0`() {
72-
assertEquals(0, sut.myAtoi(" "))
40+
assertEquals(expected, result)
7341
}
7442
}
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,36 @@
11
package com.schmoczer.leetcode._0020
22

33
import org.junit.jupiter.api.BeforeEach
4-
import kotlin.test.Test
5-
import kotlin.test.assertFalse
6-
import kotlin.test.assertTrue
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
7+
import kotlin.test.assertEquals
78

89
class ValidParenthesesTest {
10+
private companion object {
11+
@JvmStatic
12+
fun parenthesesInputs() = listOf(
13+
Arguments.of("()", true),
14+
Arguments.of("()[]{}", true),
15+
Arguments.of("(]", false),
16+
Arguments.of("([])", true),
17+
Arguments.of("(", false),
18+
Arguments.of(")", false),
19+
)
20+
}
21+
922
private lateinit var sut: ValidParentheses
1023

1124
@BeforeEach
1225
fun setUp() {
1326
sut = ValidParentheses()
1427
}
1528

16-
@Test
17-
fun `() is valid`() {
18-
assertTrue(sut.isValid("()"))
19-
}
20-
21-
@Test
22-
fun `all 3 brackets opening and closing after each other is valid`() {
23-
assertTrue(sut.isValid("()[]{}"))
24-
}
25-
26-
@Test
27-
fun `closing bracket different from opening bracket is not valid`() {
28-
assertFalse(sut.isValid("(]"))
29-
}
30-
31-
@Test
32-
fun `square brackets inside round brackets is valid`() {
33-
assertTrue(sut.isValid("([])"))
34-
}
35-
36-
@Test
37-
fun `only 1 opening bracket is not valid`() {
38-
assertFalse(sut.isValid("("))
39-
}
29+
@ParameterizedTest(name = "isValid(\"{0}\") = {1}")
30+
@MethodSource("parenthesesInputs")
31+
fun `validates parentheses correctly`(input: String, expected: Boolean) {
32+
val result = sut.isValid(input)
4033

41-
@Test
42-
fun `only closing bracket is not valid`() {
43-
assertFalse(sut.isValid(")"))
34+
assertEquals(expected, result)
4435
}
4536
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
package com.schmoczer.leetcode._0042
22

33
import org.junit.jupiter.api.BeforeEach
4-
import kotlin.test.Test
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
57
import kotlin.test.assertEquals
68

79
class TrappingRainWaterTests {
10+
private companion object {
11+
@JvmStatic
12+
fun waterInputs() = listOf(
13+
Arguments.of(intArrayOf(0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1), 6),
14+
Arguments.of(intArrayOf(4, 2, 0, 3, 2, 5), 9),
15+
Arguments.of(intArrayOf(0, 0, 0, 0), 0),
16+
Arguments.of(intArrayOf(2, 0, 2), 2),
17+
)
18+
}
19+
820
private lateinit var sut: TrappingRainWater
921

1022
@BeforeEach
1123
fun setUp() {
1224
sut = TrappingRainWater()
1325
}
1426

15-
@Test
16-
fun `0,1,0,2,1,0,1,3,2,1,2,1 traps 6 water`() {
17-
val input = intArrayOf(0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1)
18-
19-
assertEquals(6, sut.trap(input))
20-
}
21-
22-
@Test
23-
fun `4,2,0,3,2,5 traps 9 water`() {
24-
val input = intArrayOf(4, 2, 0, 3, 2, 5)
27+
@ParameterizedTest(name = "trapped water for heights {0} is {1}")
28+
@MethodSource("waterInputs")
29+
fun `returns the correct amount of trapped water`(heights: IntArray, expected: Int) {
30+
val result = sut.trap(heights)
2531

26-
assertEquals(9, sut.trap(input))
32+
assertEquals(expected, result)
2733
}
2834
}

0 commit comments

Comments
 (0)