Hone logo
Hone
Problems

Testing a Simple Calculator in Rust

This challenge focuses on writing unit tests for a basic calculator function in Rust. Unit testing is a crucial part of software development, ensuring individual components of your code function as expected and preventing regressions as you make changes. You'll be testing a function that performs simple arithmetic operations.

Problem Description

You are given a Rust function calculate that takes two integers and an operation character as input and returns the result of the operation. The function supports addition (+), subtraction (-), multiplication (*), and division (/). Your task is to write a comprehensive suite of unit tests using Rust's built-in testing framework to verify the correctness of the calculate function.

What needs to be achieved:

  • Write unit tests that cover all supported operations (+, -, *, /).
  • Test with positive, negative, and zero inputs.
  • Handle division by zero gracefully (return an Option<i32> to indicate failure).
  • Ensure the tests are well-structured and easy to understand.

Key Requirements:

  • The calculate function is provided below. Do not modify it.
  • Your tests should use the assert_eq! macro to compare the expected and actual results.
  • Use the #[test] attribute to mark your test functions.
  • Consider edge cases like zero inputs and division by zero.

Expected Behavior:

  • The tests should pass if the calculate function behaves as described.
  • Division by zero should result in None.
  • All other operations should return Some(result).

Edge Cases to Consider:

  • Division by zero.
  • Zero as one of the operands.
  • Negative numbers as operands.
  • Large numbers that might cause overflow (though overflow is not explicitly handled in the provided function, consider how your tests might reveal potential overflow issues).

Examples

Example 1:

Input: calculate(2, 3, '+')
Output: Some(5)
Explanation: 2 + 3 = 5

Example 2:

Input: calculate(10, 5, '-')
Output: Some(5)
Explanation: 10 - 5 = 5

Example 3:

Input: calculate(4, 2, '*')
Output: Some(8)
Explanation: 4 * 2 = 8

Example 4:

Input: calculate(10, 2, '/')
Output: Some(5)
Explanation: 10 / 2 = 5

Example 5:

Input: calculate(5, 0, '/')
Output: None
Explanation: Division by zero is undefined.

Example 6:

Input: calculate(-5, 3, '+')
Output: Some(-2)
Explanation: -5 + 3 = -2

Constraints

  • The calculate function will always receive two integers and a single character representing the operation.
  • The operation character will always be one of '+', '-', '*', or '/'.
  • The tests should be written in Rust and use the standard testing library.
  • The tests should be reasonably efficient; avoid unnecessary computations within the tests.

Notes

  • Think about how to structure your tests to maximize coverage and readability.
  • Consider using multiple assertions within a single test function to verify different aspects of the calculate function's behavior.
  • Rust's testing framework provides helpful attributes like should_panic for testing error handling. While not strictly required here, consider how it could be used.
  • The provided calculate function is intentionally simple. The focus is on writing the tests, not on implementing the calculation logic itself.
fn calculate(x: i32, y: i32, op: char) -> Option<i32> {
    match op {
        '+' => Some(x + y),
        '-' => Some(x - y),
        '*' => Some(x * y),
        '/' => {
            if y == 0 {
                None
            } else {
                Some(x / y)
            }
        }
        _ => None, // Handle invalid operations (optional)
    }
}
Loading editor...
rust