mathypad 0.1.6

A smart TUI calculator that understands units and makes complex calculations simple.
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
//! Tests for expression parsing and evaluation

use super::*;
use crate::test_helpers::*;
use crate::units::Unit;

#[test]
fn test_basic_arithmetic() {
    // Basic operations
    assert_eq!(evaluate_test_expression("2 + 3"), Some("5".to_string()));
    assert_eq!(evaluate_test_expression("10 - 4"), Some("6".to_string()));
    assert_eq!(evaluate_test_expression("6 * 7"), Some("42".to_string()));
    assert_eq!(evaluate_test_expression("15 / 3"), Some("5".to_string()));

    // Order of operations
    assert_eq!(
        evaluate_test_expression("2 + 3 * 4"),
        Some("14".to_string())
    );
    assert_eq!(
        evaluate_test_expression("(2 + 3) * 4"),
        Some("20".to_string())
    );
    assert_eq!(
        evaluate_test_expression("10 - 2 * 3"),
        Some("4".to_string())
    );

    // Decimal numbers
    assert_eq!(evaluate_test_expression("1.5 + 2.5"), Some("4".to_string()));
    assert_eq!(
        evaluate_test_expression("3.14 * 2"),
        Some("6.28".to_string())
    );

    // Numbers with commas
    assert_eq!(
        evaluate_test_expression("1,000 + 500"),
        Some("1,500".to_string())
    );
    assert_eq!(
        evaluate_test_expression("1,234,567 / 1000"),
        Some("1,234.567".to_string())
    );
}

#[test]
fn test_inline_expressions() {
    // Test expressions within text
    assert_eq!(
        evaluate_test_expression("The result is 5 + 3"),
        Some("8".to_string())
    );
    assert_eq!(
        evaluate_test_expression("Cost: 100 * 12 dollars"),
        Some("1,200".to_string())
    );
    assert_eq!(
        evaluate_test_expression("Total (10 + 20) items"),
        Some("30".to_string())
    );
}

#[test]
fn test_complex_expressions() {
    // Complex arithmetic
    assert_eq!(
        evaluate_test_expression("(10 + 5) * 2 - 8 / 4"),
        Some("28".to_string())
    );
    assert_eq!(
        evaluate_test_expression("100 / (5 + 5) + 3 * 2"),
        Some("16".to_string())
    );

    // Large numbers with commas
    assert_eq!(
        evaluate_test_expression("1,000,000 + 500,000"),
        Some("1,500,000".to_string())
    );
    assert_eq!(
        evaluate_test_expression("2,500 * 1,000"),
        Some("2,500,000".to_string())
    );

    // Complex unit expressions
    assert_eq!(
        evaluate_test_expression("Transfer: 5 GiB/s * 10 minutes"),
        Some("3,000 GiB".to_string())
    );
}

#[test]
fn test_edge_cases() {
    // Division by zero
    println!("Testing 5 / 0: {:?}", evaluate_test_expression("5 / 0"));
    assert_eq!(evaluate_test_expression("5 / 0"), None);

    // Invalid expressions
    println!("Testing 5 +: {:?}", evaluate_test_expression("5 +"));
    assert_eq!(evaluate_test_expression("5 +"), None);
    assert_eq!(evaluate_test_expression("* 5"), None);
    assert_eq!(evaluate_test_expression("((5)"), None);

    // Empty or invalid input
    assert_eq!(evaluate_test_expression(""), None);
    assert_eq!(evaluate_test_expression("hello world"), None);

    // Incompatible unit operations
    assert_eq!(evaluate_test_expression("5 GiB + 10 seconds"), None);
    assert_eq!(evaluate_test_expression("1 hour - 500 MB"), None);
}

#[test]
fn test_precision() {
    // Test decimal precision
    assert_eq!(
        evaluate_test_expression("1.234 + 2.567"),
        Some("3.801".to_string())
    );
    assert_eq!(
        evaluate_test_expression("10.5 / 3"),
        Some("3.5".to_string())
    );

    // Test with units requiring precision
    let result = evaluate_with_unit_info("1.5 GiB to MiB");
    assert!(result.is_some());
    let unit_val = result.unwrap();
    assert!((unit_val.value - 1536.0).abs() < 0.001);
}

#[test]
fn test_whitespace_handling() {
    // Various whitespace formats
    assert_eq!(evaluate_test_expression("5+3"), Some("8".to_string()));
    assert_eq!(
        evaluate_test_expression("  5  +  3  "),
        Some("8".to_string())
    );
    assert_eq!(evaluate_test_expression("5 * 3"), Some("15".to_string()));

    // Units with whitespace
    let result = evaluate_with_unit_info("1   GiB   to   KiB");
    assert!(result.is_some());
    let unit_val = result.unwrap();
    assert!((unit_val.value - 1048576.0).abs() < 0.001);
}

#[test]
fn test_line_references() {
    // Test parsing line references
    assert_eq!(parse_line_reference("line1"), Some(0));
    assert_eq!(parse_line_reference("line5"), Some(4));
    assert_eq!(parse_line_reference("line123"), Some(122));
    assert_eq!(parse_line_reference("Line1"), Some(0)); // Case insensitive
    assert_eq!(parse_line_reference("LINE1"), Some(0)); // Case insensitive

    // Test invalid line references
    assert_eq!(parse_line_reference("line0"), None); // Line numbers start at 1
    assert_eq!(parse_line_reference("line"), None); // No number
    assert_eq!(parse_line_reference("lineabc"), None); // Invalid number
    assert_eq!(parse_line_reference("myline1"), None); // Doesn't start with "line"
    assert_eq!(parse_line_reference("1line"), None); // Doesn't start with "line"

    // Test line reference resolution with context
    let previous_results = vec![
        Some("10 GiB".to_string()),
        Some("5".to_string()),
        None,
        Some("1,024 MiB".to_string()),
    ];

    // Test valid line references
    assert_eq!(
        evaluate_expression_with_context("line1 + 4 GiB", &previous_results, 4),
        Some("14 GiB".to_string())
    );
    assert_eq!(
        evaluate_expression_with_context("line2 * 3", &previous_results, 4),
        Some("15".to_string())
    );
    assert_eq!(
        evaluate_expression_with_context("line4 to GiB", &previous_results, 4),
        Some("1 GiB".to_string())
    );

    // Test circular reference prevention
    assert_eq!(
        evaluate_expression_with_context("line1 + 2", &previous_results, 0),
        None
    ); // Can't reference self
    assert_eq!(
        evaluate_expression_with_context("line5 + 2", &previous_results, 4),
        None
    ); // Can't reference future lines

    // Test reference to line with no result
    assert_eq!(
        evaluate_expression_with_context("line3 + 5", &previous_results, 4),
        None
    ); // Line 3 has no result

    // Test complex expressions with line references
    assert_eq!(
        evaluate_expression_with_context("(line1 + line4) / 2", &previous_results, 4),
        Some("5,632 MiB".to_string())
    );
    assert_eq!(
        evaluate_expression_with_context("line1 * line2 to MiB", &previous_results, 4),
        Some("51,200 MiB".to_string())
    );
}

#[test]
fn test_line_reference_parsing_edge_cases() {
    // Test result string parsing
    assert!(parse_result_string("10 GiB").is_some());
    assert!(parse_result_string("1,024").is_some());
    assert!(parse_result_string("42").is_some());
    assert!(parse_result_string("3.14 MiB/s").is_some());

    // Test invalid result strings
    assert!(parse_result_string("").is_none());
    assert!(parse_result_string("invalid").is_none());
    assert!(parse_result_string("GiB 10").is_none()); // Wrong order

    // Test line reference in tokenizer
    let tokens = tokenize_with_units("line1 + 5 GiB").unwrap();
    assert!(matches!(tokens[0], Token::LineReference(0)));
    assert!(matches!(tokens[1], Token::Plus));
    assert!(matches!(tokens[2], Token::NumberWithUnit(5.0, Unit::GiB)));
}

#[test]
fn test_variable_assignments() {
    use std::collections::HashMap;
    
    // Test simple variable assignment
    let variables = HashMap::new();
    let previous_results = vec![];
    let (result, assignment) = evaluate_with_variables("servers = 40", &variables, &previous_results, 0);
    assert_eq!(result, Some("40".to_string()));
    assert_eq!(assignment, Some(("servers".to_string(), "40".to_string())));
    
    // Test variable assignment with units
    let (result, assignment) = evaluate_with_variables("ram = 1 TiB", &variables, &previous_results, 0);
    assert_eq!(result, Some("1 TiB".to_string()));
    assert_eq!(assignment, Some(("ram".to_string(), "1 TiB".to_string())));
    
    // Test variable assignment with expression
    let (result, assignment) = evaluate_with_variables("total = 10 + 20", &variables, &previous_results, 0);
    assert_eq!(result, Some("30".to_string()));
    assert_eq!(assignment, Some(("total".to_string(), "30".to_string())));
    
    // Test variable assignment with unit expression
    let (result, assignment) = evaluate_with_variables("storage = 2 GiB + 512 MiB", &variables, &previous_results, 0);
    assert_eq!(result, Some("2,560 MiB".to_string()));
    assert_eq!(assignment, Some(("storage".to_string(), "2,560 MiB".to_string())));
}

#[test]
fn test_variable_references() {
    use std::collections::HashMap;
    
    // Set up variables
    let mut variables = HashMap::new();
    variables.insert("servers".to_string(), "40".to_string());
    variables.insert("ram".to_string(), "1 TiB".to_string());
    variables.insert("speed".to_string(), "100 MB/s".to_string());
    
    let previous_results = vec![];
    
    // Test simple variable reference
    let (result, assignment) = evaluate_with_variables("servers", &variables, &previous_results, 0);
    assert_eq!(result, Some("40".to_string()));
    assert_eq!(assignment, None);
    
    // Test variable reference with unit
    let (result, assignment) = evaluate_with_variables("ram", &variables, &previous_results, 0);
    assert_eq!(result, Some("1 TiB".to_string()));
    assert_eq!(assignment, None);
    
    // Test variable arithmetic
    let (result, assignment) = evaluate_with_variables("servers * 2", &variables, &previous_results, 0);
    assert_eq!(result, Some("80".to_string()));
    assert_eq!(assignment, None);
    
    // Test variable with unit arithmetic
    let (result, assignment) = evaluate_with_variables("ram + 512 GiB", &variables, &previous_results, 0);
    assert_eq!(result, Some("1,536 GiB".to_string()));
    assert_eq!(assignment, None);
    
    // Test two variables together
    let (result, assignment) = evaluate_with_variables("servers * ram", &variables, &previous_results, 0);
    assert_eq!(result, Some("40 TiB".to_string()));
    assert_eq!(assignment, None);
}

#[test]
fn test_multiline_variable_scenario() {
    use std::collections::HashMap;
    
    // Simulate the multiline notebook scenario: servers = 40, ram = 1 TiB, servers * ram
    let mut variables = HashMap::new();
    let mut previous_results = vec![];
    
    // Line 1: servers = 40
    let (result1, assignment1) = evaluate_with_variables("servers = 40", &variables, &previous_results, 0);
    assert_eq!(result1, Some("40".to_string()));
    assert_eq!(assignment1, Some(("servers".to_string(), "40".to_string())));
    
    // Store the variable assignment
    if let Some((var_name, var_value)) = assignment1 {
        variables.insert(var_name, var_value);
    }
    previous_results.push(result1);
    
    // Line 2: ram = 1 TiB
    let (result2, assignment2) = evaluate_with_variables("ram = 1 TiB", &variables, &previous_results, 1);
    assert_eq!(result2, Some("1 TiB".to_string()));
    assert_eq!(assignment2, Some(("ram".to_string(), "1 TiB".to_string())));
    
    // Store the variable assignment
    if let Some((var_name, var_value)) = assignment2 {
        variables.insert(var_name, var_value);
    }
    previous_results.push(result2);
    
    // Line 3: servers * ram
    let (result3, assignment3) = evaluate_with_variables("servers * ram", &variables, &previous_results, 2);
    assert_eq!(result3, Some("40 TiB".to_string()));
    assert_eq!(assignment3, None); // No assignment, just evaluation
}

#[test]
fn test_variable_with_line_references() {
    use std::collections::HashMap;
    
    let mut variables = HashMap::new();
    variables.insert("multiplier".to_string(), "3".to_string());
    
    // Simulate previous line results
    let previous_results = vec![
        Some("10 GiB".to_string()),
        Some("5".to_string()),
    ];
    
    // Test variable with line reference
    let (result, assignment) = evaluate_with_variables("line1 * multiplier", &variables, &previous_results, 2);
    assert_eq!(result, Some("30 GiB".to_string()));
    assert_eq!(assignment, None);
    
    // Test assigning line reference to variable
    let (result, assignment) = evaluate_with_variables("backup = line1 + 5 GiB", &variables, &previous_results, 2);
    assert_eq!(result, Some("15 GiB".to_string()));
    assert_eq!(assignment, Some(("backup".to_string(), "15 GiB".to_string())));
}

#[test]
fn test_variable_conversions() {
    use std::collections::HashMap;
    
    let mut variables = HashMap::new();
    variables.insert("storage".to_string(), "1024 GiB".to_string());
    variables.insert("time".to_string(), "60 minutes".to_string());
    
    let previous_results = vec![];
    
    // Test variable conversion
    let (result, assignment) = evaluate_with_variables("storage to TB", &variables, &previous_results, 0);
    assert_eq!(result, Some("1.1 TB".to_string()));
    assert_eq!(assignment, None);
    
    // Test variable in complex conversion expression - this actually works now!
    let (result, assignment) = evaluate_with_variables("storage / time to GiB/s", &variables, &previous_results, 0);
    assert_eq!(result, Some("0.284 GiB/s".to_string())); // This complex operation now works!
    assert_eq!(assignment, None);
}

#[test]
fn test_variable_edge_cases() {
    use std::collections::HashMap;
    
    let variables = HashMap::new();
    let previous_results = vec![];
    
    // Test undefined variable
    let (result, assignment) = evaluate_with_variables("undefined_var + 5", &variables, &previous_results, 0);
    assert_eq!(result, None);
    assert_eq!(assignment, None);
    
    // Test variable name conflicts with units - now parses as [Unit, Assign, Number]
    // which doesn't match assignment pattern, so evaluates "GiB" as standalone unit
    let (result, assignment) = evaluate_with_variables("GiB = 5", &variables, &previous_results, 0);
    assert_eq!(result, Some("1 GiB".to_string())); // Evaluates "GiB" as standalone unit
    assert_eq!(assignment, None); // No variable assignment
    
    // Test variable name conflicts with keywords - this actually parses as [To, Assign, Number(10)]
    // which doesn't match variable assignment pattern but does parse the number 10
    let (result, assignment) = evaluate_with_variables("to = 10", &variables, &previous_results, 0);
    assert_eq!(result, Some("10".to_string())); // Parses the "10" part
    assert_eq!(assignment, None); // No variable assignment
    
    // Test variable name conflicts with line references - now parses as [LineReference, Assign, Number]
    // which doesn't match assignment pattern, so evaluates "20" from the expression
    let (result, assignment) = evaluate_with_variables("line1 = 20", &variables, &previous_results, 0);
    assert_eq!(result, Some("20".to_string())); // Evaluates "20" from the expression
    assert_eq!(assignment, None); // No variable assignment
}

#[test]
fn test_complex_variable_expressions() {
    use std::collections::HashMap;
    
    let mut variables = HashMap::new();
    variables.insert("servers".to_string(), "10".to_string());
    variables.insert("ram_per_server".to_string(), "32 GiB".to_string());
    variables.insert("cpu_cores".to_string(), "8".to_string());
    variables.insert("disk_size".to_string(), "1 TiB".to_string());
    
    let previous_results = vec![];
    
    // Test complex variable expression
    let (result, assignment) = evaluate_with_variables(
        "total_ram = servers * ram_per_server", 
        &variables, 
        &previous_results, 
        0
    );
    assert_eq!(result, Some("320 GiB".to_string()));
    assert_eq!(assignment, Some(("total_ram".to_string(), "320 GiB".to_string())));
    
    // Test expression with multiple variables and units
    let (result, assignment) = evaluate_with_variables(
        "(servers * disk_size) to GiB", 
        &variables, 
        &previous_results, 
        0
    );
    assert_eq!(result, Some("10,240 GiB".to_string()));
    assert_eq!(assignment, None);
    
    // Test complex arithmetic with variables
    let (result, assignment) = evaluate_with_variables(
        "servers * (ram_per_server + disk_size) to TiB", 
        &variables, 
        &previous_results, 
        0
    );
    assert_eq!(result, Some("10.312 TiB".to_string()));
    assert_eq!(assignment, None);
}

#[test]
fn test_user_multiline_scenario() {
    use std::collections::HashMap;
    
    // Test the specific user scenario: "memory = 40 GiB\ntime = 18 s\nmemory / time"
    let mut variables = HashMap::new();
    let mut previous_results = vec![];
    
    // Line 1: memory = 40 GiB
    let (result1, assignment1) = evaluate_with_variables("memory = 40 GiB", &variables, &previous_results, 0);
    assert_eq!(result1, Some("40 GiB".to_string()));
    assert_eq!(assignment1, Some(("memory".to_string(), "40 GiB".to_string())));
    
    // Store the variable assignment
    if let Some((var_name, var_value)) = assignment1 {
        variables.insert(var_name, var_value);
    }
    previous_results.push(result1);
    
    // Line 2: time = 18 s
    let (result2, assignment2) = evaluate_with_variables("time = 18 s", &variables, &previous_results, 1);
    assert_eq!(result2, Some("18 s".to_string()));
    assert_eq!(assignment2, Some(("time".to_string(), "18 s".to_string())));
    
    // Store the variable assignment
    if let Some((var_name, var_value)) = assignment2 {
        variables.insert(var_name, var_value);
    }
    previous_results.push(result2);
    
    // Line 3: memory / time
    let (result3, assignment3) = evaluate_with_variables("memory / time", &variables, &previous_results, 2);
    assert_eq!(result3, Some("2.222 GiB/s".to_string()));
    assert_eq!(assignment3, None); // No assignment, just evaluation
}