Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,134 @@ pub(crate) fn apply_arithmetic_op(
),
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::interpreter::core::Interpreter;
use crate::token::Span;

// Helper to run ops
fn run_op(a: Value, op: TokenKind, b: Value) -> Result<Value, EldritchError> {
let interp = Interpreter::new();
let span = Span::new(0, 0, 1);
apply_arithmetic_op(&interp, &a, &op, &b, span)
}

#[test]
fn test_int_arithmetic() {
assert_eq!(
run_op(Value::Int(1), TokenKind::Plus, Value::Int(2)).unwrap(),
Value::Int(3)
);
assert_eq!(
run_op(Value::Int(5), TokenKind::Minus, Value::Int(2)).unwrap(),
Value::Int(3)
);
assert_eq!(
run_op(Value::Int(3), TokenKind::Star, Value::Int(2)).unwrap(),
Value::Int(6)
);
assert_eq!(
run_op(Value::Int(6), TokenKind::Slash, Value::Int(2)).unwrap(),
Value::Float(3.0)
);
}

#[test]
fn test_float_arithmetic() {
assert_eq!(
run_op(Value::Float(1.5), TokenKind::Plus, Value::Float(2.5)).unwrap(),
Value::Float(4.0)
);
assert_eq!(
run_op(Value::Float(5.5), TokenKind::Minus, Value::Float(2.0)).unwrap(),
Value::Float(3.5)
);
}

#[test]
fn test_mixed_arithmetic() {
assert_eq!(
run_op(Value::Int(1), TokenKind::Plus, Value::Float(2.5)).unwrap(),
Value::Float(3.5)
);
assert_eq!(
run_op(Value::Float(1.5), TokenKind::Plus, Value::Int(2)).unwrap(),
Value::Float(3.5)
);
assert_eq!(
run_op(Value::Int(3), TokenKind::Slash, Value::Float(2.0)).unwrap(),
Value::Float(1.5)
);
}

#[test]
fn test_zero_division() {
let err = run_op(Value::Int(1), TokenKind::Slash, Value::Int(0));
assert!(matches!(
err.unwrap_err().kind,
EldritchErrorKind::ZeroDivisionError
));

let err = run_op(Value::Float(1.0), TokenKind::Slash, Value::Float(0.0));
assert!(matches!(
err.unwrap_err().kind,
EldritchErrorKind::ZeroDivisionError
));

let err = run_op(Value::Int(1), TokenKind::SlashSlash, Value::Int(0));
assert!(matches!(
err.unwrap_err().kind,
EldritchErrorKind::ZeroDivisionError
));
}

#[test]
fn test_modulo_python_behavior() {
// 5 % 3 = 2
assert_eq!(
run_op(Value::Int(5), TokenKind::Percent, Value::Int(3)).unwrap(),
Value::Int(2)
);
// -5 % 3 = 1
assert_eq!(
run_op(Value::Int(-5), TokenKind::Percent, Value::Int(3)).unwrap(),
Value::Int(1)
);
// 5 % -3 = -1
assert_eq!(
run_op(Value::Int(5), TokenKind::Percent, Value::Int(-3)).unwrap(),
Value::Int(-1)
);
// -5 % -3 = -2
assert_eq!(
run_op(Value::Int(-5), TokenKind::Percent, Value::Int(-3)).unwrap(),
Value::Int(-2)
);
}

#[test]
fn test_floor_div_python_behavior() {
// 5 // 2 = 2
assert_eq!(
run_op(Value::Int(5), TokenKind::SlashSlash, Value::Int(2)).unwrap(),
Value::Int(2)
);
// -5 // 2 = -3
assert_eq!(
run_op(Value::Int(-5), TokenKind::SlashSlash, Value::Int(2)).unwrap(),
Value::Int(-3)
);
// 5.0 // 2.0 = 2.0
assert_eq!(
run_op(Value::Float(5.0), TokenKind::SlashSlash, Value::Float(2.0)).unwrap(),
Value::Float(2.0)
);
// -5.0 // 2.0 = -3.0
assert_eq!(
run_op(Value::Float(-5.0), TokenKind::SlashSlash, Value::Float(2.0)).unwrap(),
Value::Float(-3.0)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,72 @@ pub fn adjust_slice_indices(

(start_val, stop_val)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_adjust_slice_indices_basic() {
// len=10, [:]
assert_eq!(adjust_slice_indices(10, &None, &None, 1), (0, 10));
// len=10, [2:5]
assert_eq!(adjust_slice_indices(10, &Some(2), &Some(5), 1), (2, 5));
// len=10, [:5]
assert_eq!(adjust_slice_indices(10, &None, &Some(5), 1), (0, 5));
// len=10, [2:]
assert_eq!(adjust_slice_indices(10, &Some(2), &None, 1), (2, 10));
}

#[test]
fn test_adjust_slice_indices_negative_step() {
// len=10, [::-1] -> start defaults to len-1 (9), stop defaults to -1
assert_eq!(adjust_slice_indices(10, &None, &None, -1), (9, -1));
// len=10, [5:2:-1]
assert_eq!(adjust_slice_indices(10, &Some(5), &Some(2), -1), (5, 2));
}

#[test]
fn test_adjust_slice_indices_negative_indices() {
// len=10, [-5:] -> start=-5 -> 5
assert_eq!(adjust_slice_indices(10, &Some(-5), &None, 1), (5, 10));
// len=10, [:-2] -> stop=-2 -> 8
assert_eq!(adjust_slice_indices(10, &None, &Some(-2), 1), (0, 8));
// len=10, [-5:-2] -> 5, 8
assert_eq!(adjust_slice_indices(10, &Some(-5), &Some(-2), 1), (5, 8));
}

#[test]
fn test_adjust_slice_indices_out_of_bounds_positive_step() {
// len=10, [-100:] -> start clamped to 0
assert_eq!(adjust_slice_indices(10, &Some(-100), &None, 1), (0, 10));
// len=10, [100:] -> start clamped to 10
assert_eq!(adjust_slice_indices(10, &Some(100), &None, 1), (10, 10));
// len=10, [:100] -> stop clamped to 10
assert_eq!(adjust_slice_indices(10, &None, &Some(100), 1), (0, 10));
// len=10, [:-100] -> stop clamped to 0
assert_eq!(adjust_slice_indices(10, &None, &Some(-100), 1), (0, 0));
}

#[test]
fn test_adjust_slice_indices_out_of_bounds_negative_step() {
// len=10, [100::-1] -> start clamped to len-1 (9)
assert_eq!(adjust_slice_indices(10, &Some(100), &None, -1), (9, -1));
// len=10, [-100::-1] -> start clamped to -1 (Wait, logic check)
// logic: if s < 0 { -1 }
assert_eq!(adjust_slice_indices(10, &Some(-100), &None, -1), (-1, -1));

// len=10, [: -100 : -1] -> stop clamped to -1
// logic: if s < -1 { -1 }
assert_eq!(adjust_slice_indices(10, &None, &Some(-100), -1), (9, -1));
}

#[test]
fn test_adjust_slice_indices_complex_cases() {
// Regression cases or complex combos
// len=5, [::2]
assert_eq!(adjust_slice_indices(5, &None, &None, 2), (0, 5));
// len=5, [::-2]
assert_eq!(adjust_slice_indices(5, &None, &None, -2), (4, -1));
}
}
Loading