//! Unit tests for the value module use std::collections::HashSet; // Import from the library crate use morphology::value::*; use morphology::Value; // ======================================================================== // VALUE TESTS // ======================================================================== #[test] fn test_value_creation() { let v1 = Value::new(0, 0); assert_eq!(v1.outer, 0); assert_eq!(v1.inner, 0); let v2 = Value::new(5, -3); assert_eq!(v2.outer, 5); assert_eq!(v2.inner, -3); let v3 = Value::new(-10, 20); assert_eq!(v3.outer, -10); assert_eq!(v3.inner, 20); } #[test] fn test_value_equality() { let v1 = Value::new(2, 3); let v2 = Value::new(2, 3); let v3 = Value::new(2, 4); assert_eq!(v1, v2); assert_ne!(v1, v3); // Test in sets (uniqueness) let mut set = HashSet::new(); set.insert(v1); set.insert(v2); set.insert(v3); assert_eq!(set.len(), 2); } #[test] fn test_value_to_string() { assert_eq!(format!("{}", Value::new(0, 0)), "(0,0)"); assert_eq!(format!("{}", Value::new(2, 3)), "(2,3)"); assert_eq!(format!("{}", Value::new(-1, -2)), "(-1,-2)"); assert_eq!(format!("{}", Value::new(10, -5)), "(10,-5)"); } #[test] fn test_origin_detection() { assert!(Value::new(0, 0).is_origin()); assert!(!Value::new(1, 0).is_origin()); assert!(!Value::new(0, 1).is_origin()); assert!(!Value::new(-1, -1).is_origin()); let origin = Value::origin(); assert!(origin.is_origin()); } #[test] fn test_value_copy_semantics() { let v1 = Value::new(2, 3); let v2 = v1; // Copy, not move assert_eq!(v1, v2); // v1 is still usable assert_eq!(v1.outer, 2); } // ======================================================================== // MINIMUM DERIVATION PATH TESTS // ======================================================================== #[test] fn test_path_origin() { let path = get_minimum_derivation_path(Value::new(0, 0)); assert_eq!(path.len(), 1); assert_eq!(path[0], Value::new(0, 0)); } #[test] fn test_path_positive_outer() { // Test (3, 0) let path = get_minimum_derivation_path(Value::new(3, 0)); let expected = vec![ Value::new(0, 0), Value::new(1, 0), Value::new(2, 0), Value::new(3, 0), ]; assert_eq!(path, expected); assert_eq!(path.len(), 4); assert_eq!(path.first(), Some(&Value::new(0, 0))); assert_eq!(path.last(), Some(&Value::new(3, 0))); } #[test] fn test_path_negative_outer() { // Test (-3, 0) let path = get_minimum_derivation_path(Value::new(-3, 0)); let expected = vec![ Value::new(0, 0), Value::new(-1, 0), Value::new(-2, 0), Value::new(-3, 0), ]; assert_eq!(path, expected); assert_eq!(path.len(), 4); assert_eq!(path.first(), Some(&Value::new(0, 0))); assert_eq!(path.last(), Some(&Value::new(-3, 0))); } #[test] fn test_path_positive_inner() { // Test (0, 3) let path = get_minimum_derivation_path(Value::new(0, 3)); let expected = vec![ Value::new(0, 0), Value::new(0, 1), Value::new(0, 2), Value::new(0, 3), ]; assert_eq!(path, expected); assert_eq!(path.len(), 4); } #[test] fn test_path_negative_inner() { // Test (0, -3) let path = get_minimum_derivation_path(Value::new(0, -3)); let expected = vec![ Value::new(0, 0), Value::new(0, -1), Value::new(0, -2), Value::new(0, -3), ]; assert_eq!(path, expected); assert_eq!(path.len(), 4); } #[test] fn test_path_combined_positive() { // Test (2, 3) let path = get_minimum_derivation_path(Value::new(2, 3)); let expected = vec![ Value::new(0, 0), Value::new(1, 0), Value::new(2, 0), Value::new(2, 1), Value::new(2, 2), Value::new(2, 3), ]; assert_eq!(path, expected); assert_eq!(path.len(), 6); assert_eq!(path.first(), Some(&Value::new(0, 0))); assert_eq!(path.last(), Some(&Value::new(2, 3))); } #[test] fn test_path_combined_negative() { // Test (-2, -3) let path = get_minimum_derivation_path(Value::new(-2, -3)); let expected = vec![ Value::new(0, 0), Value::new(-1, 0), Value::new(-2, 0), Value::new(-2, -1), Value::new(-2, -2), Value::new(-2, -3), ]; assert_eq!(path, expected); assert_eq!(path.len(), 6); } #[test] fn test_path_mixed_signs() { // Test (2, -3) let path1 = get_minimum_derivation_path(Value::new(2, -3)); let expected1 = vec![ Value::new(0, 0), Value::new(1, 0), Value::new(2, 0), Value::new(2, -1), Value::new(2, -2), Value::new(2, -3), ]; assert_eq!(path1, expected1); // Test (-2, 3) let path2 = get_minimum_derivation_path(Value::new(-2, 3)); let expected2 = vec![ Value::new(0, 0), Value::new(-1, 0), Value::new(-2, 0), Value::new(-2, 1), Value::new(-2, 2), Value::new(-2, 3), ]; assert_eq!(path2, expected2); } #[test] fn test_path_length() { assert_eq!(path_length(Value::new(0, 0)), 1); assert_eq!(path_length(Value::new(3, 0)), 4); assert_eq!(path_length(Value::new(0, 3)), 4); assert_eq!(path_length(Value::new(2, 3)), 6); assert_eq!(path_length(Value::new(-2, -3)), 6); assert_eq!(path_length(Value::new(5, -7)), 13); } #[test] fn test_path_validity() { // Test that paths satisfy key properties let test_cases = vec![ Value::new(0, 0), Value::new(5, 0), Value::new(-5, 0), Value::new(0, 5), Value::new(0, -5), Value::new(3, 4), Value::new(-3, -4), Value::new(3, -4), Value::new(-3, 4), ]; for value in test_cases { let path = get_minimum_derivation_path(value); // Property 1: Path starts at origin assert_eq!( path.first(), Some(&Value::origin()), "Path must start at origin for {}", value ); // Property 2: Path ends at target value assert_eq!( path.last(), Some(&value), "Path must end at target value for {}", value ); // Property 3: Path length matches formula assert_eq!( path.len(), path_length(value), "Path length mismatch for {}", value ); // Property 4: Each step is valid (follows one of the 5 rules) assert_valid_steps(&path); } } fn assert_valid_steps(path: &[Value]) { for window in path.windows(2) { let v1 = window[0]; let v2 = window[1]; let o1 = v1.outer; let i1 = v1.inner; let o2 = v2.outer; let i2 = v2.inner; // Check that each transition follows exactly one rule let valid_transition = (o2 == o1 + 1 && i2 == i1) // Rule 2 || (o2 == o1 - 1 && i2 == i1) // Rule 3 || (o2 == o1 && i2 == i1 + 1) // Rule 4 || (o2 == o1 && i2 == i1 - 1); // Rule 5 assert!( valid_transition, "Invalid transition from {} to {}", v1, v2 ); } } #[test] fn test_path_immutability() { let value = Value::new(3, 4); let path1 = get_minimum_derivation_path(value); let path2 = get_minimum_derivation_path(value); // Both calls should produce identical results assert_eq!(path1, path2); // Original value should be unchanged assert_eq!(value.outer, 3); assert_eq!(value.inner, 4); } // ======================================================================== // PRETTY PRINT TESTS // ======================================================================== #[test] fn test_pretty_print_origin() { assert_eq!(Value::new(0, 0).pretty_print(), "0"); } #[test] fn test_pretty_print_zero_outer() { // When outer is 0, just print the inner value assert_eq!(Value::new(0, 1).pretty_print(), "1"); assert_eq!(Value::new(0, -1).pretty_print(), "-1"); assert_eq!(Value::new(0, 5).pretty_print(), "5"); assert_eq!(Value::new(0, -10).pretty_print(), "-10"); } #[test] fn test_pretty_print_positive_outer() { // Positive outer: nest in parentheses assert_eq!(Value::new(1, 0).pretty_print(), "(0)"); assert_eq!(Value::new(2, 0).pretty_print(), "((0))"); assert_eq!(Value::new(3, 0).pretty_print(), "(((0)))"); assert_eq!(Value::new(1, 3).pretty_print(), "(3)"); assert_eq!(Value::new(2, 1).pretty_print(), "((1))"); assert_eq!(Value::new(3, -2).pretty_print(), "(((-2)))"); } #[test] fn test_pretty_print_negative_outer() { // Negative outer: nest in square brackets assert_eq!(Value::new(-1, 0).pretty_print(), "[0]"); assert_eq!(Value::new(-2, 0).pretty_print(), "[[0]]"); assert_eq!(Value::new(-3, 0).pretty_print(), "[[[0]]]"); assert_eq!(Value::new(-1, 3).pretty_print(), "[3]"); assert_eq!(Value::new(-2, 1).pretty_print(), "[[1]]"); assert_eq!(Value::new(-3, 2).pretty_print(), "[[[2]]]"); assert_eq!(Value::new(-4, -5).pretty_print(), "[[[[-5]]]]"); } #[test] fn test_pretty_print_various_cases() { // Test a variety of combinations assert_eq!(Value::new(5, 7).pretty_print(), "(((((7)))))"); assert_eq!(Value::new(-5, 7).pretty_print(), "[[[[[7]]]]]"); assert_eq!(Value::new(4, -8).pretty_print(), "((((-8))))"); assert_eq!(Value::new(-4, -8).pretty_print(), "[[[[-8]]]]"); } #[test] fn test_pretty_print_large_values() { // Test with larger nesting levels assert_eq!(Value::new(10, 0).pretty_print(), "((((((((((0))))))))))"); assert_eq!(Value::new(-10, 0).pretty_print(), "[[[[[[[[[[0]]]]]]]]]]"); assert_eq!(Value::new(0, 100).pretty_print(), "100"); }