Testing a Simple Calculator with Integration Tests in Rust
Integration tests verify that different parts of your system work together correctly. This challenge focuses on writing integration tests for a simple calculator crate to ensure its core functionality operates as expected when combined. Successfully completing this challenge demonstrates your ability to write robust tests that validate the interaction of multiple components.
Problem Description
You are provided with a basic calculator crate named simple_calculator. This crate contains a Calculator struct with methods for addition, subtraction, multiplication, and division. Your task is to write integration tests for this crate to ensure that the calculator functions correctly across various operations and input combinations.
Specifically, you need to create a new test module within the simple_calculator crate and implement tests that:
- Verify that addition, subtraction, multiplication, and division methods return the correct results for positive and negative numbers.
- Handle division by zero gracefully (expecting a
panic!or a specific error result, as defined in the crate). - Test the calculator with zero as an operand.
- Test with a mix of positive and negative numbers in a single calculation.
The integration tests should focus on testing the Calculator struct as a whole, rather than individual methods in isolation (unit tests). This means you should create instances of the Calculator struct and call its methods to verify the overall behavior.
Examples
Example 1:
Input: Calculator {}.add(5, 3)
Output: 8
Explanation: The add method should return the sum of the two operands.
Example 2:
Input: Calculator {}.subtract(10, 4)
Output: 6
Explanation: The subtract method should return the difference between the two operands.
Example 3:
Input: Calculator {}.divide(10, 0)
Output: panic! (or specific error handling as defined in the crate)
Explanation: Division by zero should result in a panic or a defined error.
Example 4:
Input: Calculator {}.multiply(-2, 4)
Output: -8
Explanation: Multiplication with negative numbers should be handled correctly.
Constraints
- The tests should be written within the
src/lib.rsfile of thesimple_calculatorcrate, in a new module namedintegration_tests. - The tests should not take more than 50ms to execute each.
- The crate should compile and all integration tests should pass.
- The
simple_calculatorcrate is provided below. Do not modify thesimple_calculatorcrate itself, only add the integration tests.
Notes
- Consider using the
#[test]attribute to mark your test functions. - Use
assert_eq!or similar assertions to verify the expected results. - Think about how to structure your tests to cover a wide range of scenarios efficiently.
- The
simple_calculatorcrate is designed to panic on division by zero. Your tests should account for this.
// simple_calculator/src/lib.rs
pub struct Calculator {}
impl Calculator {
pub fn add(&self, x: i32, y: i32) -> i32 {
x + y
}
pub fn subtract(&self, x: i32, y: i32) -> i32 {
x - y
}
pub fn multiply(&self, x: i32, y: i32) -> i32 {
x * y
}
pub fn divide(&self, x: i32, y: i32) -> i32 {
if y == 0 {
panic!("Division by zero!");
}
x / y
}
}