-
Notifications
You must be signed in to change notification settings - Fork 0
calculator tests #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,8 @@ class Calculator { | |
| fun add(a: Int, b: Int): Int { | ||
| return a + b | ||
| } | ||
|
|
||
| fun divide(a: Int, b: Int): Int { | ||
| return a/b | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,4 +17,10 @@ class CalculatorTest { | |
| val calculator = Calculator() | ||
| assertEquals(calculator.add(1,2) ,3) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test divide function equals`() { | ||
| val calculator = Calculator() | ||
| assertEquals(calculator.divide(4,2) ,2) | ||
| } | ||
|
Comment on lines
+21
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Test structure looks good, but coverage is incomplete. The basic divide test follows the established pattern and correctly validates the happy path. However, comprehensive testing for division requires additional edge cases. Add these additional test cases for comprehensive coverage: @Test
fun `test divide by zero throws exception`() {
val calculator = Calculator()
assertThrows(IllegalArgumentException::class.java) {
calculator.divide(4, 0)
}
}
@Test
fun `test divide negative numbers`() {
val calculator = Calculator()
assertEquals(calculator.divide(-4, 2), -2)
assertEquals(calculator.divide(4, -2), -2)
assertEquals(calculator.divide(-4, -2), 2)
}
@Test
fun `test integer division truncation`() {
val calculator = Calculator()
assertEquals(calculator.divide(5, 2), 2) // 5/2 = 2.5 → 2
assertEquals(calculator.divide(1, 2), 0) // 1/2 = 0.5 → 0
}Note: You'll also need to add this import for import org.junit.Assert.assertThrows🤖 Prompt for AI Agents |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Missing division by zero validation.
The
dividemethod will throw anArithmeticExceptionwhenbis zero, which could crash the application. Add proper validation to handle this edge case.Apply this diff to add division by zero validation:
fun divide(a: Int, b: Int): Int { + if (b == 0) { + throw IllegalArgumentException("Division by zero is not allowed") + } return a/b }Alternatively, consider returning a nullable result or using a Result wrapper:
🤖 Prompt for AI Agents