summaryrefslogtreecommitdiff
path: root/solutions/18_iterators
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-07-08 14:40:35 +0200
committermo8it <mo8it@proton.me>2024-07-08 14:40:35 +0200
commita4091ade5cde7602acef0601cb7f69984fcad009 (patch)
tree3c9f3a89d4a88acf26c3f7ae56ab35b1d322e6bb /solutions/18_iterators
parenta7a881809f462688b26ba7b81de85753c3507c22 (diff)
iterators3: Add `IntegerOverflow` error variant
Diffstat (limited to 'solutions/18_iterators')
-rw-r--r--solutions/18_iterators/iterators3.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/solutions/18_iterators/iterators3.rs b/solutions/18_iterators/iterators3.rs
index d66d1ef..11aa1ec 100644
--- a/solutions/18_iterators/iterators3.rs
+++ b/solutions/18_iterators/iterators3.rs
@@ -1,6 +1,10 @@
#[derive(Debug, PartialEq, Eq)]
enum DivisionError {
+ // Example: 42 / 0
DivideByZero,
+ // Only case for `i64`: `i64::MIN / -1` because the result is `i64::MAX + 1`
+ IntegerOverflow,
+ // Example: 5 / 2 = 2.5
NotDivisible,
}
@@ -9,6 +13,10 @@ fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
return Err(DivisionError::DivideByZero);
}
+ if a == i64::MIN && b == -1 {
+ return Err(DivisionError::IntegerOverflow);
+ }
+
if a % b != 0 {
return Err(DivisionError::NotDivisible);
}
@@ -52,6 +60,11 @@ mod tests {
}
#[test]
+ fn test_integer_overflow() {
+ assert_eq!(divide(i64::MIN, -1), Err(DivisionError::IntegerOverflow));
+ }
+
+ #[test]
fn test_not_divisible() {
assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));
}