mathlex 0.4.1

Mathematical expression parser for LaTeX and plain text notation, producing a language-agnostic AST
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#![allow(clippy::approx_constant)]
//! Property-based tests using proptest to verify parser invariants and edge cases.
//!
//! This module uses proptest to generate arbitrary expressions and verify
//! important properties hold across a wide range of inputs.

use mathlex::ast::{BinaryOp, ExprKind, Expression, MathConstant, MathFloat, UnaryOp};
use mathlex::parser::parse;
use proptest::prelude::*;

/// Strategy to generate arbitrary expressions.
///
/// This strategy generates expressions with controlled depth to avoid
/// stack overflow and excessive generation time.
fn arb_expression() -> impl Strategy<Value = Expression> {
    let leaf = prop_oneof![
        // Integers in a reasonable range
        (-1000i64..1000).prop_map(Expression::integer),
        // Floats in a reasonable range (avoiding NaN and infinity for now)
        (-1000.0f64..1000.0)
            .prop_filter("No NaN", |f| !f.is_nan())
            .prop_map(|f| Expression::from(ExprKind::Float(MathFloat::from(f)))),
        // Single-letter variables
        "[a-z]".prop_map(Expression::variable),
        // Mathematical constants
        prop_oneof![
            Just(Expression::constant(MathConstant::Pi)),
            Just(Expression::constant(MathConstant::E)),
            Just(Expression::constant(MathConstant::I)),
        ],
    ];

    leaf.prop_recursive(
        4,  // max depth: 4 levels
        64, // max total nodes: 64
        10, // expected branch size: 10
        |inner| {
            prop_oneof![
                // Binary operations
                (arb_binary_op(), inner.clone(), inner.clone()).prop_map(|(op, left, right)| {
                    Expression::from(ExprKind::Binary {
                        op,
                        left: Box::new(left),
                        right: Box::new(right),
                    })
                }),
                // Unary operations
                (arb_unary_op(), inner.clone()).prop_map(|(op, operand)| {
                    Expression::from(ExprKind::Unary {
                        op,
                        operand: Box::new(operand),
                    })
                }),
                // Functions with 1-2 arguments
                (
                    arb_function_name(),
                    prop::collection::vec(inner.clone(), 1..=2)
                )
                    .prop_map(|(name, args)| Expression::from(ExprKind::Function { name, args }),),
                // Vectors with 1-3 elements
                prop::collection::vec(inner.clone(), 1..=3)
                    .prop_map(|elems| Expression::from(ExprKind::Vector(elems))),
            ]
        },
    )
}

/// Strategy to generate arbitrary binary operators.
fn arb_binary_op() -> impl Strategy<Value = BinaryOp> {
    prop_oneof![
        Just(BinaryOp::Add),
        Just(BinaryOp::Sub),
        Just(BinaryOp::Mul),
        Just(BinaryOp::Div),
        Just(BinaryOp::Pow),
        Just(BinaryOp::Mod),
    ]
}

/// Strategy to generate arbitrary unary operators.
fn arb_unary_op() -> impl Strategy<Value = UnaryOp> {
    prop_oneof![
        Just(UnaryOp::Neg),
        Just(UnaryOp::Pos),
        Just(UnaryOp::Factorial),
    ]
}

/// Strategy to generate arbitrary function names.
fn arb_function_name() -> impl Strategy<Value = String> {
    prop_oneof![
        Just("sin".to_string()),
        Just("cos".to_string()),
        Just("tan".to_string()),
        Just("log".to_string()),
        Just("exp".to_string()),
        Just("sqrt".to_string()),
        Just("abs".to_string()),
        Just("max".to_string()),
        Just("min".to_string()),
    ]
}

proptest! {
    /// Property: Clone equals original
    ///
    /// Verifies that cloning an expression produces an identical expression.
    #[test]
    fn prop_clone_equals_original(expr in arb_expression()) {
        let cloned = expr.clone();
        prop_assert_eq!(expr, cloned);
    }

    /// Property: Depth is at least 1
    ///
    /// Every expression must have a depth of at least 1, even leaf nodes.
    #[test]
    fn prop_depth_at_least_one(expr in arb_expression()) {
        prop_assert!(expr.depth() >= 1);
    }

    /// Property: Node count is at least 1
    ///
    /// Every expression must contain at least one node.
    #[test]
    fn prop_node_count_at_least_one(expr in arb_expression()) {
        prop_assert!(expr.node_count() >= 1);
    }

    /// Property: Depth never exceeds node count
    ///
    /// The depth (longest path) can never exceed the total number of nodes.
    #[test]
    fn prop_depth_le_node_count(expr in arb_expression()) {
        prop_assert!(expr.depth() <= expr.node_count());
    }

    /// Property: All variables found by find_variables appear in the expression
    ///
    /// This property verifies that find_variables doesn't return false positives.
    #[test]
    fn prop_find_variables_no_false_positives(expr in arb_expression()) {
        let vars = expr.find_variables();
        let expr_str = format!("{:?}", expr);

        for var in vars {
            // Each variable should appear in the debug representation
            prop_assert!(
                expr_str.contains(&format!("Variable(\"{}\")", var)),
                "Variable {} not found in expression debug output",
                var
            );
        }
    }

    /// Property: All functions found by find_functions appear in the expression
    ///
    /// This property verifies that find_functions doesn't return false positives.
    #[test]
    fn prop_find_functions_no_false_positives(expr in arb_expression()) {
        let funcs = expr.find_functions();
        let expr_str = format!("{:?}", expr);

        for func in funcs {
            // Each function should appear in the debug representation
            prop_assert!(
                expr_str.contains(&format!("name: \"{}\"", func)),
                "Function {} not found in expression debug output",
                func
            );
        }
    }

    /// Property: All constants found by find_constants appear in the expression
    ///
    /// This property verifies that find_constants doesn't return false positives.
    #[test]
    fn prop_find_constants_no_false_positives(expr in arb_expression()) {
        let consts = expr.find_constants();
        let expr_str = format!("{:?}", expr);

        for constant in consts {
            // Each constant should appear in the debug representation
            let const_str = format!("{:?}", constant);
            prop_assert!(
                expr_str.contains(&const_str),
                "Constant {} not found in expression debug output",
                const_str
            );
        }
    }

    /// Property: Display produces non-empty string
    ///
    /// Every expression should have a non-empty display representation.
    #[test]
    fn prop_display_non_empty(expr in arb_expression()) {
        let display_str = format!("{}", expr);
        prop_assert!(!display_str.is_empty());
    }

    /// Property: Display can be parsed back (for simple expressions)
    ///
    /// For expressions that don't use complex notation, the display output
    /// should be parseable back to an AST. Note: Not all expressions will
    /// round-trip exactly due to parser limitations and representation choices.
    #[test]
    fn prop_display_parseable(expr in arb_expression()) {
        let display_str = format!("{}", expr);

        // Attempt to parse the display string
        match parse(&display_str) {
            Ok(_parsed_expr) => {
                // Successfully parsed - this is good
                // Note: We don't assert equality because the parser may
                // produce a different but semantically equivalent AST
                // (e.g., different associativity, implicit operations made explicit)
            }
            Err(_) => {
                // Some complex expressions may not round-trip perfectly
                // This is acceptable for now
            }
        }
    }

    /// Property: Substitution preserves structure for non-matching variables
    ///
    /// Substituting a variable that doesn't appear in the expression
    /// should return an identical expression.
    #[test]
    fn prop_substitute_non_matching_identity(expr in arb_expression()) {
        let non_existent_var = "nonexistent_variable_xyz";
        let replacement = Expression::integer(999);

        let result = expr.substitute(non_existent_var, &replacement);
        prop_assert_eq!(expr, result);
    }

    /// Property: Double negation returns to similar depth
    ///
    /// Applying negation twice should increase depth by 2.
    #[test]
    fn prop_double_negation_depth(expr in arb_expression()) {
        let neg_once: Expression = ExprKind::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(expr.clone()),
        }.into();
        let neg_twice: Expression = ExprKind::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(neg_once.clone()),
        }.into();

        prop_assert_eq!(neg_once.depth(), expr.depth() + 1);
        prop_assert_eq!(neg_twice.depth(), expr.depth() + 2);
    }

    /// Property: Binary operation increases depth
    ///
    /// Creating a binary operation from two expressions should have depth
    /// equal to 1 + max(left.depth, right.depth).
    #[test]
    fn prop_binary_depth(
        left in arb_expression(),
        right in arb_expression(),
        op in arb_binary_op()
    ) {
        let binary: Expression = ExprKind::Binary {
            op,
            left: Box::new(left.clone()),
            right: Box::new(right.clone()),
        }.into();

        let expected_depth = 1 + left.depth().max(right.depth());
        prop_assert_eq!(binary.depth(), expected_depth);
    }

    /// Property: Binary operation node count
    ///
    /// Creating a binary operation should have node count equal to
    /// 1 + left.node_count + right.node_count.
    #[test]
    fn prop_binary_node_count(
        left in arb_expression(),
        right in arb_expression(),
        op in arb_binary_op()
    ) {
        let binary: Expression = ExprKind::Binary {
            op,
            left: Box::new(left.clone()),
            right: Box::new(right.clone()),
        }.into();

        let expected_count = 1 + left.node_count() + right.node_count();
        prop_assert_eq!(binary.node_count(), expected_count);
    }

    /// Property: Vector depth equals max element depth + 1
    ///
    /// A vector's depth should be 1 + maximum depth of its elements.
    #[test]
    fn prop_vector_depth(elements in prop::collection::vec(arb_expression(), 1..=5)) {
        let vector: Expression = ExprKind::Vector(elements.clone()).into();

        let max_element_depth = elements.iter().map(|e| e.depth()).max().unwrap_or(0);
        let expected_depth = if elements.is_empty() { 1 } else { 1 + max_element_depth };

        prop_assert_eq!(vector.depth(), expected_depth);
    }

    /// Property: Vector node count equals sum of element counts + 1
    ///
    /// A vector's node count should be 1 + sum of all element node counts.
    #[test]
    fn prop_vector_node_count(elements in prop::collection::vec(arb_expression(), 1..=5)) {
        let vector: Expression = ExprKind::Vector(elements.clone()).into();

        let element_count_sum: usize = elements.iter().map(|e| e.node_count()).sum();
        let expected_count = 1 + element_count_sum;

        prop_assert_eq!(vector.node_count(), expected_count);
    }

    /// Property: Hash consistency
    ///
    /// Equal expressions should hash to the same value (required for HashSet).
    #[test]
    fn prop_hash_consistency(expr in arb_expression()) {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let cloned = expr.clone();

        let mut hasher1 = DefaultHasher::new();
        expr.hash(&mut hasher1);
        let hash1 = hasher1.finish();

        let mut hasher2 = DefaultHasher::new();
        cloned.hash(&mut hasher2);
        let hash2 = hasher2.finish();

        prop_assert_eq!(hash1, hash2);
    }

    /// Property: Variable finding is consistent
    ///
    /// Running find_variables twice should return the same set.
    #[test]
    fn prop_find_variables_consistent(expr in arb_expression()) {
        let vars1 = expr.find_variables();
        let vars2 = expr.find_variables();
        prop_assert_eq!(vars1, vars2);
    }

    /// Property: Function finding is consistent
    ///
    /// Running find_functions twice should return the same set.
    #[test]
    fn prop_find_functions_consistent(expr in arb_expression()) {
        let funcs1 = expr.find_functions();
        let funcs2 = expr.find_functions();
        prop_assert_eq!(funcs1, funcs2);
    }

    /// Property: Constant finding is consistent
    ///
    /// Running find_constants twice should return the same set.
    #[test]
    fn prop_find_constants_consistent(expr in arb_expression()) {
        let consts1 = expr.find_constants();
        let consts2 = expr.find_constants();
        prop_assert_eq!(consts1, consts2);
    }

    /// Property: MathFloat equality is transitive
    ///
    /// If a == b and b == c, then a == c.
    #[test]
    fn prop_math_float_transitivity(
        a in -1000.0f64..1000.0,
        b in -1000.0f64..1000.0,
        c in -1000.0f64..1000.0
    ) {
        let fa = MathFloat::from(a);
        let fb = MathFloat::from(b);
        let fc = MathFloat::from(c);

        if fa == fb && fb == fc {
            prop_assert_eq!(fa, fc);
        }
    }

    /// Property: MathFloat equality is symmetric
    ///
    /// If a == b, then b == a.
    #[test]
    fn prop_math_float_symmetry(a in -1000.0f64..1000.0, b in -1000.0f64..1000.0) {
        let fa = MathFloat::from(a);
        let fb = MathFloat::from(b);

        if fa == fb {
            prop_assert_eq!(fb, fa);
        }
    }

    /// Property: MathFloat equality is reflexive
    ///
    /// a == a always.
    #[test]
    fn prop_math_float_reflexivity(a in -1000.0f64..1000.0) {
        let fa = MathFloat::from(a);
        prop_assert_eq!(fa, fa);
    }

    /// Property: MathFloat roundtrip
    ///
    /// Converting f64 -> MathFloat -> f64 should preserve the value.
    #[test]
    fn prop_math_float_roundtrip(f in -1000.0f64..1000.0) {
        let math_float = MathFloat::from(f);
        let roundtrip: f64 = math_float.into();

        // Use approximate equality due to floating-point precision
        prop_assert!((f - roundtrip).abs() < 1e-10);
    }
}

// Regular unit tests to verify specific edge cases

#[test]
fn test_empty_vector_depth() {
    let expr: Expression = ExprKind::Vector(vec![]).into();
    assert_eq!(expr.depth(), 1);
}

#[test]
fn test_empty_vector_node_count() {
    let expr: Expression = ExprKind::Vector(vec![]).into();
    assert_eq!(expr.node_count(), 1);
}

#[test]
fn test_single_variable_find_variables() {
    let expr = Expression::variable("x".to_string());
    let vars = expr.find_variables();
    assert_eq!(vars.len(), 1);
    assert!(vars.contains("x"));
}

#[test]
fn test_no_functions_in_leaf() {
    let expr = Expression::integer(42);
    assert_eq!(expr.find_functions().len(), 0);
}

#[test]
fn test_no_constants_in_integer() {
    let expr = Expression::integer(42);
    assert_eq!(expr.find_constants().len(), 0);
}

#[test]
fn test_pi_constant_found() {
    let expr = Expression::constant(MathConstant::Pi);
    let consts = expr.find_constants();
    assert_eq!(consts.len(), 1);
    assert!(consts.contains(&MathConstant::Pi));
}

#[test]
fn test_display_integer() {
    let expr = Expression::integer(42);
    assert_eq!(format!("{}", expr), "42");
}

#[test]
fn test_display_variable() {
    let expr = Expression::variable("x".to_string());
    assert_eq!(format!("{}", expr), "x");
}

#[test]
fn test_clone_integer() {
    let expr = Expression::integer(42);
    let cloned = expr.clone();
    assert_eq!(expr, cloned);
}

#[test]
fn test_hash_equal_expressions() {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let expr1 = Expression::integer(42);
    let expr2 = Expression::integer(42);

    let mut hasher1 = DefaultHasher::new();
    expr1.hash(&mut hasher1);
    let hash1 = hasher1.finish();

    let mut hasher2 = DefaultHasher::new();
    expr2.hash(&mut hasher2);
    let hash2 = hasher2.finish();

    assert_eq!(hash1, hash2);
}