2016-04-19 20:56:46 +01:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2024-05-22 14:04:12 +01:00
|
|
|
enum DivisionError {
|
2024-07-08 13:40:35 +01:00
|
|
|
// Example: 42 / 0
|
2016-04-19 20:56:46 +01:00
|
|
|
DivideByZero,
|
2024-07-08 13:40:35 +01:00
|
|
|
// Only case for `i64`: `i64::MIN / -1` because the result is `i64::MAX + 1`
|
|
|
|
IntegerOverflow,
|
|
|
|
// Example: 5 / 2 = 2.5
|
2024-06-28 14:00:13 +01:00
|
|
|
NotDivisible,
|
2016-04-19 20:56:46 +01:00
|
|
|
}
|
|
|
|
|
2024-06-28 14:00:13 +01:00
|
|
|
// TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
2021-02-12 20:36:53 +00:00
|
|
|
// Otherwise, return a suitable error.
|
2024-07-08 13:40:35 +01:00
|
|
|
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
|
2022-01-13 15:26:46 +00:00
|
|
|
todo!();
|
|
|
|
}
|
2016-04-19 20:56:46 +01:00
|
|
|
|
2024-06-28 14:00:13 +01:00
|
|
|
// TODO: Add the correct return type and complete the function body.
|
|
|
|
// Desired output: `Ok([1, 11, 1426, 3])`
|
|
|
|
fn result_with_list() {
|
|
|
|
let numbers = [27, 297, 38502, 81];
|
2021-02-12 20:36:53 +00:00
|
|
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
|
|
|
}
|
|
|
|
|
2024-06-28 14:00:13 +01:00
|
|
|
// TODO: Add the correct return type and complete the function body.
|
|
|
|
// Desired output: `[Ok(1), Ok(11), Ok(1426), Ok(3)]`
|
|
|
|
fn list_of_results() {
|
|
|
|
let numbers = [27, 297, 38502, 81];
|
2021-02-12 20:36:53 +00:00
|
|
|
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
|
|
|
}
|
|
|
|
|
2024-04-17 21:46:21 +01:00
|
|
|
fn main() {
|
|
|
|
// You can optionally experiment here.
|
|
|
|
}
|
|
|
|
|
2016-04-19 20:56:46 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_success() {
|
|
|
|
assert_eq!(divide(81, 9), Ok(9));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-06-28 14:00:13 +01:00
|
|
|
fn test_divide_by_0() {
|
|
|
|
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
|
2016-04-19 20:56:46 +01:00
|
|
|
}
|
|
|
|
|
2024-07-08 13:40:35 +01:00
|
|
|
#[test]
|
|
|
|
fn test_integer_overflow() {
|
|
|
|
assert_eq!(divide(i64::MIN, -1), Err(DivisionError::IntegerOverflow));
|
|
|
|
}
|
|
|
|
|
2016-04-19 20:56:46 +01:00
|
|
|
#[test]
|
2024-06-28 14:00:13 +01:00
|
|
|
fn test_not_divisible() {
|
|
|
|
assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));
|
2016-04-19 20:56:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_divide_0_by_something() {
|
|
|
|
assert_eq!(divide(0, 81), Ok(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-12 20:36:53 +00:00
|
|
|
fn test_result_with_list() {
|
2024-07-04 06:20:59 +01:00
|
|
|
assert_eq!(result_with_list().unwrap(), [1, 11, 1426, 3]);
|
2016-04-19 20:56:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-12 20:36:53 +00:00
|
|
|
fn test_list_of_results() {
|
2024-06-28 14:00:13 +01:00
|
|
|
assert_eq!(list_of_results(), [Ok(1), Ok(11), Ok(1426), Ok(3)]);
|
2016-04-19 20:56:46 +01:00
|
|
|
}
|
|
|
|
}
|