|
1 | 1 | package com.schmoczer.leetcode._0008
|
2 | 2 |
|
3 | 3 | 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 |
5 | 7 | import kotlin.test.assertEquals
|
6 | 8 |
|
7 | 9 | 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 | + |
8 | 28 | private lateinit var sut: StringToInteger
|
9 | 29 |
|
10 | 30 | @BeforeEach
|
11 | 31 | fun setUp() {
|
12 | 32 | sut = StringToInteger()
|
13 | 33 | }
|
14 | 34 |
|
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) |
69 | 39 |
|
70 |
| -@Test |
71 |
| -fun `multiple whitespaces are 0`() { |
72 |
| -assertEquals(0, sut.myAtoi(" ")) |
| 40 | +assertEquals(expected, result) |
73 | 41 | }
|
74 | 42 | }
|
0 commit comments