From 87a1da1e43c6757b56c3883b506cf9041ec40762 Mon Sep 17 00:00:00 2001 From: Slowth-KIM Date: Mon, 2 Dec 2024 22:07:14 +0900 Subject: [PATCH 1/8] add test cases. --- README.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d49f3cfc8..09d2353a0a 100644 --- a/README.md +++ b/README.md @@ -1 +1,24 @@ -# kotlin-racingcar \ No newline at end of file +# kotlin-racingcar + +## 문자열 계산기 테스트 케이스 목록 + +기본 연산 테스트 + +- [ ] 덧셈: "2 + 3" = 5 +- [ ] 뺄셈: "5 - 3" = 2 +- [ ] 곱셈: "4 * 3" = 12 +- [ ] 나눗셈: "8 / 2" = 4 + +예외 처리 테스트 + +- [ ] null 입력 처리 +- [ ] 빈 문자열 입력 처리 +- [ ] 잘못된 연산자 입력 처리 ("2 @ 3") +- [ ] 잘못된 형식의 입력 처리 ("2 + ") + +복합 연산 테스트 + +- [ ] 두 개의 연산: "2 + 3 * 4" = 20 +- [ ] 세 개의 연산: "2 + 3 * 4 / 2" = 10 +- [ ] 동일 우선순위 연산: "1 + 2 + 3" = 6 +- \ No newline at end of file From 07a14fb9e8b83142e5accb65b62f2073c15c7841 Mon Sep 17 00:00:00 2001 From: Slowth-KIM Date: Mon, 2 Dec 2024 22:10:06 +0900 Subject: [PATCH 2/8] =?UTF-8?q?test=20failed=20=EA=B8=B0=EB=B3=B8=20?= =?UTF-8?q?=EC=82=AC=EC=B9=99=20=EC=97=B0=EC=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/step2/StringCalculatorTest.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/test/kotlin/step2/StringCalculatorTest.kt diff --git a/src/test/kotlin/step2/StringCalculatorTest.kt b/src/test/kotlin/step2/StringCalculatorTest.kt new file mode 100644 index 0000000000..b629b52e87 --- /dev/null +++ b/src/test/kotlin/step2/StringCalculatorTest.kt @@ -0,0 +1,28 @@ +package step2 + +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe + + +class StringCalculatorTest : FunSpec({ + val calculator = StringCalculator() + + context("기본 사칙 연산") { + test("덧셈 연산을 수행한다") { + calculator.calculate("2 + 3") shouldBe 5 + } + + test("뺄셈 연산을 수행한다") { + calculator.calculate("5 - 3") shouldBe 2 + } + + test("곱셈 연산을 수행한다") { + calculator.calculate("4 * 3") shouldBe 12 + } + + test("나눗셈 연산을 수행한다") { + calculator.calculate("8 / 2") shouldBe 4 + } + } + +}) \ No newline at end of file From 2ed85e17641832b993bd9420775002b97b31ecf6 Mon Sep 17 00:00:00 2001 From: Slowth-KIM Date: Mon, 2 Dec 2024 22:15:14 +0900 Subject: [PATCH 3/8] =?UTF-8?q?test=20passed=20=EA=B8=B0=EB=B3=B8=20?= =?UTF-8?q?=EC=82=AC=EC=B9=99=20=EC=97=B0=EC=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/step2/StringCalculator.kt | 54 +++++++++++++++++++ src/test/kotlin/step2/StringCalculatorTest.kt | 3 +- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/step2/StringCalculator.kt diff --git a/src/main/kotlin/step2/StringCalculator.kt b/src/main/kotlin/step2/StringCalculator.kt new file mode 100644 index 0000000000..99e072fe5b --- /dev/null +++ b/src/main/kotlin/step2/StringCalculator.kt @@ -0,0 +1,54 @@ +package step2 + +class StringCalculator { + fun calculate(expression: String): Int { + validateInput(expression) + + val tokens = expression.trim().split(" ") + + var result = tokens[0].toInt() + + var i = 1 + while (i < tokens.size - 1) { + val operator = tokens[i] + val number = tokens[i + 1].toInt() + + result = performOperation(result, operator, number) + i += 2 + } + + return result + } + + private fun validateInput(expression: String) { + if (expression.isBlank()) { + throw IllegalArgumentException("입력값이 null이거나 빈 문자열입니다.") + } + + val tokens = expression.trim().split(" ") + if (tokens.size < 3 || tokens.size % 2 == 0) { + throw IllegalArgumentException("올바르지 않은 수식 형식입니다.") + } + + tokens.filterIndexed { index, _ -> index % 2 == 1 } + .forEach { operator -> + if (operator !in validOperators) { + throw IllegalArgumentException("사칙연산 기호가 아닙니다.") + } + } + } + + private fun performOperation(a: Int, operator: String, b: Int): Int { + return when (operator) { + "+" -> a + b + "-" -> a - b + "*" -> a * b + "/" -> if (b != 0) a / b else throw IllegalArgumentException("0으로 나눌 수 없습니다.") + else -> throw IllegalArgumentException("지원하지 않는 연산자입니다.") + } + } + + companion object { + private val validOperators = setOf("+", "-", "*", "/") + } +} diff --git a/src/test/kotlin/step2/StringCalculatorTest.kt b/src/test/kotlin/step2/StringCalculatorTest.kt index b629b52e87..01dac25622 100644 --- a/src/test/kotlin/step2/StringCalculatorTest.kt +++ b/src/test/kotlin/step2/StringCalculatorTest.kt @@ -24,5 +24,4 @@ class StringCalculatorTest : FunSpec({ calculator.calculate("8 / 2") shouldBe 4 } } - -}) \ No newline at end of file +}) From 7e2a0621b5af9d0f9863e59cb9e91372d206df33 Mon Sep 17 00:00:00 2001 From: Slowth-KIM Date: Mon, 2 Dec 2024 22:15:58 +0900 Subject: [PATCH 4/8] marked passed test case. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 09d2353a0a..4fae160598 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,10 @@ 기본 연산 테스트 -- [ ] 덧셈: "2 + 3" = 5 -- [ ] 뺄셈: "5 - 3" = 2 -- [ ] 곱셈: "4 * 3" = 12 -- [ ] 나눗셈: "8 / 2" = 4 +- [x] 덧셈: "2 + 3" = 5 +- [x] 뺄셈: "5 - 3" = 2 +- [x] 곱셈: "4 * 3" = 12 +- [x] 나눗셈: "8 / 2" = 4 예외 처리 테스트 From f584904b6d49c336634cf124435194348497d25b Mon Sep 17 00:00:00 2001 From: Slowth-KIM Date: Mon, 2 Dec 2024 22:18:01 +0900 Subject: [PATCH 5/8] =?UTF-8?q?test=20passed=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/step2/StringCalculatorTest.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/test/kotlin/step2/StringCalculatorTest.kt b/src/test/kotlin/step2/StringCalculatorTest.kt index 01dac25622..aff7e4ae00 100644 --- a/src/test/kotlin/step2/StringCalculatorTest.kt +++ b/src/test/kotlin/step2/StringCalculatorTest.kt @@ -1,6 +1,9 @@ package step2 +import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec +import io.kotest.data.forAll +import io.kotest.data.row import io.kotest.matchers.shouldBe @@ -24,4 +27,29 @@ class StringCalculatorTest : FunSpec({ calculator.calculate("8 / 2") shouldBe 4 } } + + + context("예외 상황 처리") { + test("null 또는 빈 문자열 입력시 예외가 발생한다") { + shouldThrow { + calculator.calculate("") + }.message shouldBe "입력값이 null이거나 빈 문자열입니다." + + shouldThrow { + calculator.calculate(" ") + }.message shouldBe "입력값이 null이거나 빈 문자열입니다." + } + + test("잘못된 연산자 입력시 예외가 발생한다") { + forAll( + row("2 @ 3"), + row("4 # 5"), + row("6 $ 7") + ) { expression -> + shouldThrow { + calculator.calculate(expression) + }.message shouldBe "사칙연산 기호가 아닙니다." + } + } + } }) From 59166e5a914675a5458f3589d233408d45becb55 Mon Sep 17 00:00:00 2001 From: Slowth-KIM Date: Mon, 2 Dec 2024 22:18:19 +0900 Subject: [PATCH 6/8] marked passed test case. --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4fae160598..5295f4e587 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,9 @@ 예외 처리 테스트 -- [ ] null 입력 처리 -- [ ] 빈 문자열 입력 처리 -- [ ] 잘못된 연산자 입력 처리 ("2 @ 3") -- [ ] 잘못된 형식의 입력 처리 ("2 + ") +- [x] null 입력 처리 +- [x] 빈 문자열 입력 처리 +- [x] 잘못된 연산자 입력 처리 ("2 @ 3") 복합 연산 테스트 From 98d5f7b0456e5342a2dc87fccf9eeb666bc07894 Mon Sep 17 00:00:00 2001 From: Slowth-KIM Date: Mon, 2 Dec 2024 22:19:11 +0900 Subject: [PATCH 7/8] =?UTF-8?q?test=20passed=20=EB=B3=B5=ED=95=A9=20?= =?UTF-8?q?=EC=97=B0=EC=82=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/step2/StringCalculatorTest.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/kotlin/step2/StringCalculatorTest.kt b/src/test/kotlin/step2/StringCalculatorTest.kt index aff7e4ae00..deafee24b9 100644 --- a/src/test/kotlin/step2/StringCalculatorTest.kt +++ b/src/test/kotlin/step2/StringCalculatorTest.kt @@ -52,4 +52,16 @@ class StringCalculatorTest : FunSpec({ } } } + + context("복합 연산") { + test("여러 개의 연산을 순서대로 처리한다") { + forAll( + row("2 + 3 * 4 / 2", 10), + row("1 + 2 + 3", 6), + row("10 - 2 * 3", 24) + ) { expression, expected -> + calculator.calculate(expression) shouldBe expected + } + } + } }) From 6d1f9dbbc9ec4b8f90b4efcabad2be7cc7b857fe Mon Sep 17 00:00:00 2001 From: Slowth-KIM Date: Mon, 2 Dec 2024 22:19:36 +0900 Subject: [PATCH 8/8] marked passed test case. --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5295f4e587..718a96a707 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ 복합 연산 테스트 -- [ ] 두 개의 연산: "2 + 3 * 4" = 20 -- [ ] 세 개의 연산: "2 + 3 * 4 / 2" = 10 -- [ ] 동일 우선순위 연산: "1 + 2 + 3" = 6 -- \ No newline at end of file +- [x] 두 개의 연산: "2 + 3 * 4" = 20 +- [x] 세 개의 연산: "2 + 3 * 4 / 2" = 10 +- [x] 동일 우선순위 연산: "1 + 2 + 3" = 6 \ No newline at end of file