Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/src/main/java/com/coderabbit/app/calculator/Calculator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Comment on lines +9 to +11

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Missing division by zero validation.

The divide method will throw an ArithmeticException when b is 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:

fun divide(a: Int, b: Int): Int? {
    return if (b == 0) null else a / b
}
🤖 Prompt for AI Agents
In app/src/main/java/com/coderabbit/app/calculator/Calculator.kt around lines 9
to 11, the divide function lacks validation for division by zero, which can
cause an ArithmeticException and crash the app. Add a check to verify if the
divisor b is zero before performing the division. If b is zero, either throw a
custom exception, return null, or use a Result wrapper to safely handle this
edge case and prevent runtime crashes.

}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 assertThrows:

import org.junit.Assert.assertThrows
🤖 Prompt for AI Agents
In app/src/test/java/com/coderabbit/app/calculator/CalculatorTest.kt around
lines 21 to 25, the existing divide function test only covers the basic case. To
improve coverage, add tests for division by zero that assert an
IllegalArgumentException is thrown, tests for dividing negative numbers with
expected signed results, and tests for integer division truncation verifying
correct floor behavior. Also, add the import statement for assertThrows from
org.junit.Assert to support exception testing.

}