#[cfg(test)] mod cpu_evaluator_tests { use vtl_rs::cpu::CPU; use vtl_rs::interpreter::evaluate_expression; use vtl_rs::memory::Memory; #[test] fn test_evaluator_addition() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); let result = evaluate_expression("?=2+2", &mut cpu, &mut memory).unwrap(); assert_eq!(result, 4); } #[test] fn test_evaluator_large_numbers() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); let result = evaluate_expression("?=999999999999999999999999", &mut cpu, &mut memory); assert!(result.is_err(), "Expected overflow for large numbers"); } #[test] fn test_division_by_zero() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); // Division by zero should not panic and should set the division error flag let result = evaluate_expression("?=10/0", &mut cpu, &mut memory); // Expect the result to be an error assert!(result.is_err(), "Expected a division by zero error"); // Check the exact error message let error_msg = result.unwrap_err(); assert_eq!( error_msg, "CPU Error: Evaluation Error (Divide): Division by zero", "Unexpected error message: {}", error_msg ); } #[test] fn test_basic_division() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); let result = evaluate_expression("?=6/3", &mut cpu, &mut memory); assert!(result.is_ok(), "Basic division failed: {:?}", result); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 2, "Expected 6 / 3 to equal 2, but got {}", result_value ); } #[test] fn test_zero_as_numerator() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); let result = evaluate_expression("?=0/3", &mut cpu, &mut memory); assert!( result.is_ok(), "Division with zero numerator failed: {:?}", result ); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 0, "Expected 0 / 3 to equal 0, but got {}", result_value ); } #[test] fn test_negative_numerator() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); let result = evaluate_expression("?=-6/3", &mut cpu, &mut memory); assert!( result.is_ok(), "Division with negative numerator failed: {:?}", result ); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, -2, "Expected -6 / 3 to equal -2, but got {}", result_value ); } #[test] fn test_negative_denominator() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); let result = evaluate_expression("?=6/-3", &mut cpu, &mut memory); assert!( result.is_ok(), "Division with negative denominator failed: {:?}", result ); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, -2, "Expected 6 / -3 to equal -2, but got {}", result_value ); } #[test] fn test_both_negative_numbers() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); let result = evaluate_expression("?=-6/-3", &mut cpu, &mut memory); assert!( result.is_ok(), "Division with both negative numbers failed: {:?}", result ); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 2, "Expected -6 / -3 to equal 2, but got {}", result_value ); } #[test] fn test_integer_truncation() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); let result = evaluate_expression("?=7/3", &mut cpu, &mut memory); assert!( result.is_ok(), "Division resulting in fraction failed: {:?}", result ); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 2, "Expected 7 / 3 to equal 2 (integer truncation), but got {}", result_value ); } #[test] fn test_remainder_as_variable() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); let result = evaluate_expression("?=7/3", &mut cpu, &mut memory); assert!(result.is_ok(), "Division failed: {:?}", result); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 2, "Expected 7 / 3 to equal 2, but got {}", result_value ); // Test remainder retrieval let result = evaluate_expression("?=%", &mut cpu, &mut memory); assert!(result.is_ok(), "Fetching remainder failed: {:?}", result); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 1, "Expected remainder 7 % 3 to be 1, but got {}", result_value ); } // Demo sequence from README #[test] fn test_mixed_operations() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); // Test case 1: 22 / 7 let result = evaluate_expression("?=22/7", &mut cpu, &mut memory); assert!(result.is_ok(), "Division failed: {:?}", result); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 3, "Expected 22 / 7 to equal 3, but got {}", result_value ); // Test case 2: % let result = evaluate_expression("?=%", &mut cpu, &mut memory); assert!(result.is_ok(), "Fetching remainder failed: {:?}", result); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 1, "Expected remainder 22 % 7 to be 1, but got {}", result_value ); // Test case 3: 6 / 1 let result = evaluate_expression("6/1", &mut cpu, &mut memory); assert!(result.is_ok(), "Division failed: {:?}", result); assert_eq!(result.unwrap(), 6, "Expected 6 / 1 to equal 6"); // Test case 4: % let result = evaluate_expression("?=%", &mut cpu, &mut memory); assert!(result.is_ok(), "Fetching remainder failed: {:?}", result); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 0, "Expected remainder 6 % 1 to be 0, but got {}", result_value ); // Test case 5: 17 / -3 let result = evaluate_expression("?=17/-3", &mut cpu, &mut memory); assert!(result.is_ok(), "Division failed: {:?}", result); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, -5, "Expected 17 / -3 to equal -5, but got {}", result_value ); // Test case 6: % let result = evaluate_expression("?=%", &mut cpu, &mut memory); assert!(result.is_ok(), "Fetching remainder failed: {:?}", result); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 2, "Expected remainder 17 % -3 to be 2, but got {}", result_value ); // Test case 7: 2 + 2 let result = evaluate_expression("?=2+2", &mut cpu, &mut memory); assert!(result.is_ok(), "Addition failed: {:?}", result); let result_value = cpu .get_variable('?') .expect("Failed to retrieve '?' variable"); assert_eq!( result_value, 4, "Expected 2 + 2 to equal 4, but got {}", result_value ); } #[test] fn test_variable_assignment() { let mut cpu = CPU::new(); let mut memory = Memory::new(1024); // Test assigning a value to variable A let result = evaluate_expression("A=10", &mut cpu, &mut memory); assert!(result.is_ok(), "Assignment to A failed: {:?}", result); assert_eq!(cpu.get_variable('A'), Some(10), "Expected A to equal 10"); // Test assigning a value to variable Z let result = evaluate_expression("Z=100", &mut cpu, &mut memory); assert!(result.is_ok(), "Assignment to Z failed: {:?}", result); assert_eq!(cpu.get_variable('Z'), Some(100), "Expected Z to equal 100"); } }