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

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

fn dummy_span() -> Span {
Span::new(0, 0, 1)
}

#[test]
fn test_apply_arithmetic_op_int_basics() {
let interp = Interpreter::new();
let span = dummy_span();

let a = Value::Int(10);
let b = Value::Int(3);

// +
assert_eq!(
apply_arithmetic_op(&interp, &a, &TokenKind::Plus, &b, span).unwrap(),
Value::Int(13)
);
// -
assert_eq!(
apply_arithmetic_op(&interp, &a, &TokenKind::Minus, &b, span).unwrap(),
Value::Int(7)
);
// *
assert_eq!(
apply_arithmetic_op(&interp, &a, &TokenKind::Star, &b, span).unwrap(),
Value::Int(30)
);
// / -> Float
assert!(matches!(
apply_arithmetic_op(&interp, &a, &TokenKind::Slash, &b, span).unwrap(),
Value::Float(v) if (v - 3.3333333333).abs() < 1e-9
));
}

#[test]
fn test_apply_arithmetic_op_int_div_zero() {
let interp = Interpreter::new();
let span = dummy_span();

let a = Value::Int(10);
let b = Value::Int(0);

let res = apply_arithmetic_op(&interp, &a, &TokenKind::Slash, &b, span);
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, EldritchErrorKind::ZeroDivisionError);

let res = apply_arithmetic_op(&interp, &a, &TokenKind::SlashSlash, &b, span);
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, EldritchErrorKind::ZeroDivisionError);

let res = apply_arithmetic_op(&interp, &a, &TokenKind::Percent, &b, span);
assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, EldritchErrorKind::ZeroDivisionError);
}

#[test]
fn test_apply_arithmetic_op_floor_div_python_semantics() {
let interp = Interpreter::new();
let span = dummy_span();

// 10 // 3 = 3
assert_eq!(
apply_arithmetic_op(
&interp,
&Value::Int(10),
&TokenKind::SlashSlash,
&Value::Int(3),
span
)
.unwrap(),
Value::Int(3)
);
// -10 // 3 = -4
assert_eq!(
apply_arithmetic_op(
&interp,
&Value::Int(-10),
&TokenKind::SlashSlash,
&Value::Int(3),
span
)
.unwrap(),
Value::Int(-4)
);
// 10 // -3 = -4
assert_eq!(
apply_arithmetic_op(
&interp,
&Value::Int(10),
&TokenKind::SlashSlash,
&Value::Int(-3),
span
)
.unwrap(),
Value::Int(-4)
);
// -10 // -3 = 3
assert_eq!(
apply_arithmetic_op(
&interp,
&Value::Int(-10),
&TokenKind::SlashSlash,
&Value::Int(-3),
span
)
.unwrap(),
Value::Int(3)
);
}

#[test]
fn test_apply_arithmetic_op_modulo_python_semantics() {
let interp = Interpreter::new();
let span = dummy_span();

// 10 % 3 = 1
assert_eq!(
apply_arithmetic_op(
&interp,
&Value::Int(10),
&TokenKind::Percent,
&Value::Int(3),
span
)
.unwrap(),
Value::Int(1)
);
// -10 % 3 = 2 (Python: -10 % 3 = 2, Rust: -10 % 3 = -1)
assert_eq!(
apply_arithmetic_op(
&interp,
&Value::Int(-10),
&TokenKind::Percent,
&Value::Int(3),
span
)
.unwrap(),
Value::Int(2)
);
// 10 % -3 = -2 (Python: 10 % -3 = -2)
assert_eq!(
apply_arithmetic_op(
&interp,
&Value::Int(10),
&TokenKind::Percent,
&Value::Int(-3),
span
)
.unwrap(),
Value::Int(-2)
);
}

#[test]
fn test_apply_arithmetic_op_mixed_types() {
let interp = Interpreter::new();
let span = dummy_span();

// Int + Float
match apply_arithmetic_op(
&interp,
&Value::Int(1),
&TokenKind::Plus,
&Value::Float(2.5),
span,
)
.unwrap()
{
Value::Float(v) => assert!((v - 3.5).abs() < f64::EPSILON),
_ => panic!("Expected Float"),
}

// Float + Int
match apply_arithmetic_op(
&interp,
&Value::Float(2.5),
&TokenKind::Plus,
&Value::Int(1),
span,
)
.unwrap()
{
Value::Float(v) => assert!((v - 3.5).abs() < f64::EPSILON),
_ => panic!("Expected Float"),
}
}

#[test]
fn test_apply_arithmetic_op_mixed_div_zero() {
let interp = Interpreter::new();
let span = dummy_span();

// Int / Float(0.0)
let res = apply_arithmetic_op(
&interp,
&Value::Int(1),
&TokenKind::Slash,
&Value::Float(0.0),
span,
);
assert!(res.is_err());

// Float / Int(0)
let res = apply_arithmetic_op(
&interp,
&Value::Float(1.0),
&TokenKind::Slash,
&Value::Int(0),
span,
);
assert!(res.is_err());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,71 @@ 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 (empty slice)
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