Skip to content

Introduce error types #709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2024
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
46 changes: 38 additions & 8 deletions src/data_structures/postfix_evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
pub fn evaluate_postfix(expression: &str) -> Result<i32, &'static str> {
#[derive(Debug, PartialEq)]
pub enum PostfixError {
DivisionByZero,
InvalidOperator,
InsufficientOperands,
InvalidExpression,
}

pub fn evaluate_postfix(expression: &str) -> Result<i32, PostfixError> {
let mut stack: Vec<i32> = Vec::new();

for token in expression.split_whitespace() {
Expand All @@ -15,23 +23,22 @@ pub fn evaluate_postfix(expression: &str) -> Result<i32, &'static str> {
"*" => stack.push(a * b),
"/" => {
if b == 0 {
return Err("Division by zero");
return Err(PostfixError::DivisionByZero);
}
stack.push(a / b);
}
_ => return Err("Invalid operator"),
_ => return Err(PostfixError::InvalidOperator),
}
} else {
return Err("Insufficient operands");
return Err(PostfixError::InsufficientOperands);
}
}
}

// The final result should be the only element on the stack.
if stack.len() == 1 {
Ok(stack[0])
} else {
Err("Invalid expression")
Err(PostfixError::InvalidExpression)
}
}

Expand All @@ -48,11 +55,34 @@ mod tests {

#[test]
fn test_insufficient_operands() {
assert_eq!(evaluate_postfix("+"), Err("Insufficient operands"));
assert_eq!(
evaluate_postfix("+"),
Err(PostfixError::InsufficientOperands)
);
}

#[test]
fn test_division_by_zero() {
assert_eq!(evaluate_postfix("5 0 /"), Err("Division by zero"));
assert_eq!(evaluate_postfix("5 0 /"), Err(PostfixError::DivisionByZero));
}

#[test]
fn test_invalid_operator() {
assert_eq!(
evaluate_postfix("2 3 #"),
Err(PostfixError::InvalidOperator)
);
}

#[test]
fn test_invalid_expression() {
assert_eq!(
evaluate_postfix("2 3"),
Err(PostfixError::InvalidExpression)
);
assert_eq!(
evaluate_postfix("2 3 4 +"),
Err(PostfixError::InvalidExpression)
);
}
}
28 changes: 24 additions & 4 deletions src/data_structures/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod tests {
fn test_enqueue() {
let mut queue: Queue<u8> = Queue::new();
queue.enqueue(64);
assert!(!queue.is_empty());
assert!(!queue.is_empty(), "Queue should not be empty after enqueue");
}

#[test]
Expand All @@ -56,7 +56,11 @@ mod tests {
queue.enqueue(32);
queue.enqueue(64);
let retrieved_dequeue = queue.dequeue();
assert_eq!(retrieved_dequeue, Some(32));
assert_eq!(
retrieved_dequeue,
Some(32),
"Dequeue should return the first element"
);
}

#[test]
Expand All @@ -65,14 +69,30 @@ mod tests {
queue.enqueue(8);
queue.enqueue(16);
let retrieved_peek = queue.peek_front();
assert_eq!(retrieved_peek, Some(&8));
assert_eq!(
retrieved_peek,
Some(&8),
"Peek should return a reference to the first element"
);
}

#[test]
fn test_size() {
let mut queue: Queue<u8> = Queue::new();
queue.enqueue(8);
queue.enqueue(16);
assert_eq!(2, queue.len());
assert_eq!(
2,
queue.len(),
"Queue length should be equal to the number of enqueued elements"
);
}

#[test]
fn test_is_empty() {
let mut queue: Queue<u8> = Queue::new();
assert!(queue.is_empty(), "Newly created queue should be empty");
queue.enqueue(8);
assert!(!queue.is_empty(), "Queue should not be empty after enqueue");
}
}
15 changes: 13 additions & 2 deletions src/data_structures/range_minimum_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
*/

use std::cmp::PartialOrd;
use std::fmt;

/// Custom error for invalid range
#[derive(Debug, PartialEq)]
pub struct RangeError;

impl fmt::Display for RangeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Invalid range")
}
}

pub struct RangeMinimumQuery<T: PartialOrd + Copy> {
// the current version makes a copy of the input array, but this could be changed
Expand All @@ -26,9 +37,9 @@ impl<T: PartialOrd + Copy> RangeMinimumQuery<T> {
}
}

pub fn get_range_min(&self, start: usize, end: usize) -> Result<T, &str> {
pub fn get_range_min(&self, start: usize, end: usize) -> Result<T, RangeError> {
if start >= end || start >= self.array.len() || end > self.array.len() {
return Err("invalid range");
return Err(RangeError);
}
let loglen = (end - start).ilog2() as usize;
let idx: usize = end - (1 << loglen);
Expand Down