diff options
| -rw-r--r-- | morphology/examples/pretty_print_demo.rs | 36 | ||||
| -rw-r--r-- | morphology/src/value.rs | 37 | ||||
| -rw-r--r-- | morphology/tests/value.rs | 58 |
3 files changed, 131 insertions, 0 deletions
diff --git a/morphology/examples/pretty_print_demo.rs b/morphology/examples/pretty_print_demo.rs new file mode 100644 index 0000000..2760962 --- /dev/null +++ b/morphology/examples/pretty_print_demo.rs @@ -0,0 +1,36 @@ +//! Demonstration of the pretty_print function +//! +//! Compile and run: +//! cargo run --example pretty_print_demo + +use meta_affine::Value; + +fn main() { + println!("Pretty Print Demonstration\n"); + println!("{}", "=".repeat(50)); + + let examples = vec![ + (0, 0), + (0, 1), + (1, 0), + (-1, 0), + (1, 3), + (2, 1), + (-3, 2), + (5, 7), + (-5, 7), + (4, -8), + (-4, -8), + ]; + + for (outer, inner) in examples { + let value = Value::new(outer, inner); + println!( + "{:10} -> {}", + format!("{}", value), + value.pretty_print() + ); + } + + println!("{}", "=".repeat(50)); +} diff --git a/morphology/src/value.rs b/morphology/src/value.rs index 71fc2c6..51e9e40 100644 --- a/morphology/src/value.rs +++ b/morphology/src/value.rs @@ -68,6 +68,43 @@ impl Value { pub const fn as_tuple(&self) -> (i32, i32) { (self.outer, self.inner) } + + /// Returns a pretty-printed representation of the value. + /// + /// For a value (outer, inner): + /// - If outer is positive, inner is nested in outer pairs of parentheses + /// - If outer is negative, inner is nested in |outer| pairs of square brackets + /// - If outer is 0, returns just the inner value + /// + /// # Examples + /// + /// ``` + /// use meta_affine::Value; + /// assert_eq!(Value::new(0, 0).pretty_print(), "0"); + /// assert_eq!(Value::new(0, 1).pretty_print(), "1"); + /// assert_eq!(Value::new(1, 0).pretty_print(), "(0)"); + /// assert_eq!(Value::new(-1, 0).pretty_print(), "[0]"); + /// assert_eq!(Value::new(2, 1).pretty_print(), "((1))"); + /// assert_eq!(Value::new(-3, 2).pretty_print(), "[[[2]]]"); + /// ``` + pub fn pretty_print(&self) -> String { + let mut result = self.inner.to_string(); + + if self.outer > 0 { + // Positive outer: nest in parentheses + for _ in 0..self.outer { + result = format!("({})", result); + } + } else if self.outer < 0 { + // Negative outer: nest in square brackets + for _ in 0..self.outer.abs() { + result = format!("[{}]", result); + } + } + // If outer == 0, just return the inner value as-is + + result + } } impl fmt::Display for Value { diff --git a/morphology/tests/value.rs b/morphology/tests/value.rs index fcd4b76..10c21c5 100644 --- a/morphology/tests/value.rs +++ b/morphology/tests/value.rs @@ -302,3 +302,61 @@ fn test_path_immutability() { 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"); +} |